def _aHelp(self, parts): # {{aHelp|HID|VISIBILITY|CONTENT}} if len(parts) == 4: return """<ahelp hid="{HID}" visibility="{VISIBILITY}">{CONTENT}</ahelp>""".format( HID=h.escape_html(parts[1]), VISIBILITY=("visible" if parts[2] == "visible" else "hidden"), CONTENT=h.escape_html(parts[3])) return self._write_template_error(parts)
def link(self, content, link, title=""): title = h.escape_html(title) if title is None: title = "" if not re.match("(http://|https://|ftp://|vnd.sun.star.help://)", link): link = re.sub("^(./|../)", "", link) link = re.sub(".md$", ".xhp", link) link = "{ID}/{LINK}".format(ID=h.escape_html(self.identifier), LINK=link) return """<link href="{HREF}" name="{NAME}">{CONTENT}</link>""".format( HREF=link, NAME=title, CONTENT=content)
def block_code(self, text, lang): if not lang: if isinstance(text, unicode): text = text.encode('utf-8') return '<pre><code>%s</code></pre>' % h.escape_html(text.strip()) if self.use_pygments: lexer = get_lexer_by_name(lang, stripall=True) formatter = HtmlFormatter() return highlight(text, lexer, formatter) return '<pre class="language-%s"><code>%s</code></pre>' % ( lang, h.escape_html(text.strip()))
def block_code(self, text, lang): if not lang: if isinstance(text, unicode): text = text.encode('utf-8') return '<pre><code>%s</code></pre>' % h.escape_html(text.strip()) if self.use_pygments: lexer = get_lexer_by_name(lang, stripall=True) formatter = HtmlFormatter() return highlight(text, lexer, formatter) return '<pre class="language-%s"><code>%s</code></pre>' % ( lang, h.escape_html(text.strip()) )
def block_code(self, text, lang): if not lang: return '\n<pre><code>%s</code></pre>\n' % \ h.escape_html(text.strip()) if str(lang).strip() == 'latex-block': md_str = latex2markdown.LaTeX2Markdown(text).to_markdown() return md_str try: lexer = get_lexer_by_name(lang, stripall=True) formatter = HtmlFormatter() return highlight(text, lexer, formatter) except pygments.util.ClassNotFound: return '\n<pre><code>%s</code></pre>\n' % \ h.escape_html(text.strip())
def block_code(self, text, lang): if not lang: if isinstance(text, unicode): text = text.encode('utf-8') return '<pre><code>%s</code></pre>' % h.escape_html(text.strip()) if self.use_pygments: try: # if the language can not be found, it will raise lexer = get_lexer_by_name(lang.lower(), stripall=True) formatter = HtmlFormatter() return highlight(text, lexer, formatter) except: pass return '<pre class="language-%s"><code>%s</code></pre>' % ( lang, h.escape_html(text.strip()))
def blockcode(self, text, lang): if not lang: return '\n<pre><code>{}</code></pre>\n'.format(houdini.escape_html(text.strip())) lexer = get_lexer_by_name(lang) formatter = HtmlFormatter() return highlight(text, lexer, formatter)
def _Variable(self, parts): # {{Variable|ID|VISIBILITY|CONTENT}} if len(parts) == 4: return """<variable id="{ID}" visibility="{VISIBILITY}">{CONTENT}</variable>""".format( ID=self._generate_id("var"), VISIBILITY=("visible" if parts[2] == "visible" else "hidden"), CONTENT=h.escape_html(parts[3])) return self._write_template_error(parts)
def block_code(self, text, lang): if not lang: return '\n<pre><code>%s</code></pre>\n' % \ h.escape_html(text.strip()) lexer = get_lexer_by_name(lang, stripall=True) formatter = CodeHtmlFormatter() return highlight(text, lexer, formatter)
def block_code(self, text, lang): if not lang: if isinstance(text, unicode): text = text.encode('utf-8') return '<pre><code>%s</code></pre>' % h.escape_html(text.strip()) if hasattr(self, 'use_pygments') and self.use_pygments: try: # if the language can not be found, it will raise lexer = get_lexer_by_name(lang.lower(), stripall=True) formatter = HtmlFormatter() return highlight(text, lexer, formatter) except: pass return '<pre class="language-%s"><code>%s</code></pre>' % ( lang, h.escape_html(text.strip()) )
def image(self, link, title="", alt=""): # todo resolve image location and width/height? if link.startswith(self.base_addr): link = link[len(self.base_addr):] link = "{ID}/{LINK}".format(ID=h.escape_html(self.identifier), LINK=link) return """<image id="{ID}" src="{SRC}">{CONTENT}</image>""".format( ID=self._generate_id("img"), SRC=link, CONTENT=title)
def block_code(self, text, lang): try: lexer = get_lexer_by_name(lang, stripall=True) except ClassNotFound: return '\n<pre><code>%s</code></pre>\n' % h.escape_html(text.strip()) formatter = HtmlFormatter() return highlight(text, lexer, formatter)
def block_code(self, text, lang): if not lang: return '\n<pre><code>%s</code></pre>\n' % \ houdini.escape_html(text.strip()) lexer = get_lexer_by_name(lang, stripall=True) formatter = HtmlFormatter() return highlight(text, lexer, formatter)
def _code_no_lexer(self, text): # encode to utf8 string text = text.encode(charset).strip() return (""" <div class="highlight"> <pre><code>%s</code></pre> </div> """ % houdini.escape_html(text))
def _Tip(self, parts): # {{Tip|CONTENT}}, {{Caution|CONTENT}} or {{Warning|CONTENT}} if len(parts) == 2: role = parts[0].lower() if role == "caution": role = "warning" return self.paragraph(h.escape_html(parts[1]), role=role) return self._write_template_error(parts)
def blockcode(self, text, lang): if not lang: return '\n<pre><code>{}</code></pre>\n'.format( h.escape_html(text.strip())) lexer = get_lexer_by_name(lang, stripall=True) formatter = HtmlFormatter() return highlight(m.smartypants(text), lexer, formatter)
def block_code(self, text, lang): try: lexer = get_lexer_by_name(lang, stripall=True) except ClassNotFound: text = escape_html(text.strip()) return '\n<pre><code>%s</code></pre>\n' % text else: formatter = HtmlFormatter() return highlight(text, lexer, formatter)
def block_code(self, text, lang): if not lang: return ('\n<pre><code>%s</code></pre>\n' % h.escape_html(text.strip())) elif lang == 'blockdiag': return generatoBlockdiag(text) lexer = get_lexer_by_name(lang, stripall=True) formatter = HtmlFormatter() return highlight(text, lexer, formatter)
def txt_reader(path): post = html_reader(path) content = post.content.encode("utf-8") content = escape_html(content) content = content.replace( "\n", "<br />" ) post.content = to_unicode(content) return post
def blockcode(self, text, lang): """Return HTML for block code""" if not lang: return '\n<pre><code>{}</code></pre>\n'.format( h.escape_html(text.strip())) lexer = get_lexer_by_name(lang, stripall=True) formatter = HtmlFormatter() return highlight(text, lexer, formatter)
def _code_no_lexer(self, text): # encode to utf8 string text = text.encode(charset).strip() return( """ <div class="highlight"> <pre><code>%s</code></pre> </div> """ % houdini.escape_html(text) )
def blockcode(self, text, lang): try: lexer = get_lexer_by_name(lang, stripall=False) except ClassNotFound: lexer = None if lexer: formatter = HtmlFormatter(linenos=True) return highlight(text, lexer, formatter) # default return '\n<pre><code>{}</code></pre>\n'.format(houdini.escape_html(text.strip()))
def blockcode(self, text, lang): try: lexer = get_lexer_by_name(lang, stripall=True) except ClassNotFound: lexer = None if lexer: formatter = HtmlFormatter(noclasses=True) return highlight(text, lexer, formatter) # default return '\n<pre><code>{}</code></pre>\n'.format( h.escape_html(text.strip()))
def doc_header(self, inline_render): # todo meta/history return """<?xml version="1.0" encoding="UTF-8"?> <helpdocument version="1.0"> <meta> <topic id="topic_" indexer="include"> <title id="tit" xml-lang="{LANG}"></title> <filename>{FILENAME}</filename> </topic> </meta> <body>\n""".format( LANG=self.lang, CREATED="", EDITED="", FILENAME=h.escape_html(self._get_file_name()))
def parse_catsup_meta(lines, path=None): meta = read_base_meta(path) if lines[0][0] == "#": meta.title = escape_html(lines.pop(0)[1:].strip()) for line in lines: if not line: continue if ":" not in line: not_valid(path) name, value = line.split(':', 1) name = name.strip().lstrip('-').strip().lower() meta[name] = value.strip() return meta
def _Bookmark(self, parts): # {{Bookmark|BRANCH|COMPONENTS|VALUES}} # {{Bookmark|BRANCH|VALUES}} if len(parts) == 3: parts.insert(2, "") if len(parts) > 3: id = self._generate_id("bk") if parts[2]: id = id + "_".join([p.strip().lower() for p in parts[2].split(",")]) values = parts[3].split("||") if len(parts) > 3 else [] return """<bookmark branch="{BRANCH}" id="{ID}" lang="{LANG}">{VALUES}</bookmark>""".format( BRANCH=h.escape_html(parts[1]), ID=id, LANG=self.lang, VALUES="\n".join(["<bookmark_value xml-lang=\"{LANG}\">".format(LANG=self.lang) + h.escape_html(v) + "</bookmark_value>" for v in values])) return self._write_template_error(parts)
def block_code(self, text, lang): if not lang: text = text.encode(charset).strip() return ( """\n<div class="highlight"> <pre><code>%s</code></pre> </div>\n""" % h.escape_html(text) ) lexer = get_lexer_by_name(lang, stripall=True) formatter = HtmlFormatter() return highlight(text, lexer, formatter)
def parse_catsup_meta(lines, path=None): meta = ObjectDict() title_line = lines.pop(0) if title_line[0] != "#": not_valid(path) meta.title = escape_html(title_line[1:].strip()) for line in lines: if not line: continue if ":" not in line: not_valid(path) name, value = line.split(':', 1) name = name.strip().lstrip('-').strip().lower() meta[name] = value.strip() return meta
def blockcode(self, text, lang): cssclass = 'code-highlighted' try: lexer = get_lexer_by_name(lang, stripall=True) except ClassNotFound: lexer = None if lexer: # Fixme: it would be better to have a single css file created, # which is possible! but right now in this early phase that's # a distraction formatter = HtmlFormatter(noclasses=True, cssclass=cssclass) return highlight(text, lexer, formatter) # no lexer return '\n<div class="{1}"><pre>{0}</pre></div>\n'.format( houdini.escape_html(text.strip()), cssclass)
def block_code(self, text, lang): output = '' if not lang: return '\n<pre><code>%s</code></pre>\n' % \ h.escape_html(text.strip()) try: lexer = get_lexer_by_name(lang, stripall=True) except: output += '<b>Language "' + lang + '" not supported</b>' lexer = get_lexer_by_name(text, stripall=True) output += highlight(text, lexer, formatter) return output
def block_code(self, text, lang): # 使用run_prettify.js自动识别及高亮 content = h.escape_html(text.strip()) style = ' class="prettyprint linenums"' if lang is not None: if lang.lower() == 'plain': style = '' elif lang.lower() == 'code': style = ' class="prettyprint linenums"' elif lang.lower() == 'flow': content = text.strip() style = ' class="ns-flow-chart"' elif lang.lower() == 'seq': content = text.strip() style = ' class="ns-seq-chart"' else: style = '' return '\n<pre><code%s>%s</code></pre>\n' % (style, content)
def _code_no_lexer(self, text): text = text.encode('utf8') return '<pre><code>{}</code></pre>'.format(escape_html(text))
def test_output_preserves_unicodeness(): string = u'héllo < hëllo' escaped = houdini.escape_html(string) assert_equals(escaped, u'héllo < hëllo')
def test_doesnt_escape_stuff_if_it_doesnt_need_to(): string = u'hello' escaped = houdini.escape_html(string) assert string is escaped
def block_code(self, text, lang): text = h.escape_html(text.encode('utf-8'), 1).decode('utf-8') lang = ' data-lang="{0}"'.format(lang) if lang else '' return '<pre><code{0}>{1}</code></pre>'.format(lang, text)
def autolink(self, link, is_email): url = h.escape_html(link) return """<link href="{URL}">{URL}</link>""".format(URL=url)
def math(self, text, displaymode): # not supported return h.escape_html(text)
def normal_text(self, text): return h.escape_html(text.strip())
def test_escapes_stuff_if_necessary(): string = u"<>&;\"" escaped = houdini.escape_html(string) assert_equals(escaped, u'<>&;"')
def codespan(self, text): return h.escape_html(text)
def blockcode(self, text, lang=""): return self.paragraph("<br />".join(h.escape_html(text).split("\n")), role="code")