我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用pygments.lexers.TextLexer()。
def code_block(self, node, entering): '''Output Pygments if required else use default html5 output''' if self.use_pygments: self.cr() info_words = node.info.split() if node.info else [] if len(info_words) > 0 and len(info_words[0]) > 0: try: lexer = get_lexer_by_name(info_words[0]) except ValueError: # no lexer found - use the text one instead of an exception lexer = TextLexer() else: lexer = TextLexer() formatter = HtmlFormatter(**self.pygments_options) parsed = highlight(node.literal, lexer, formatter) self.lit(parsed) self.cr() else: super().code_block(node, entering)
def pygments_directive(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): try: lexer = get_lexer_by_name(arguments[0]) except ValueError: # no lexer found - use the text one instead of an exception lexer = TextLexer() # take an arbitrary option if more than one is given formatter = options and VARIANTS[options.keys()[0]] or DEFAULT parsed = highlight(u'\n'.join(content), lexer, formatter) parsed = '<div class="codeblock">%s</div>' % parsed return [nodes.raw('', parsed, format='html')]
def pygment_html_render(s, lexer=lexers.TextLexer): return highlight( s, lexer(), HtmlFormatter(linenos=True), )
def printheaders(step,*,file, id, http_part, colourful): """Prints out the heading of the step to the output file""" logging.debug("in printheaders() http_part=" + http_part) od = collections.OrderedDict(sorted(eval("step."+ http_part +".headers.items()"))) # sort the headers for key, val in od.items(): if colourful: #for now, does the same thing #print(highlight(key +": "+ val, lexers.TextLexer(stripnl=True), formatters.TerminalFormatter()), file=file) print(key +": "+val, file=file) else: print(key +": "+val, file=file) #TODO: @REFACTOR (60) refactor all these print* functions -- too much copy/paste!
def preformatted_emit(self, node): content = node.content lines = content.split("\n") if lines[0].startswith("#!code"): lexer_name = lines[0].split()[1] del lines[0] else: lexer_name = None content = "\n".join(lines) try: lexer = get_lexer_by_name(lexer_name) except ClassNotFound: lexer = TextLexer() return highlight(content, lexer, HtmlFormatter(cssclass="syntax")).strip()
def write_content(content_type, content, formatter, fh=sys.stdout): # nlines = len(content) content = dedent(''.join(content)) # ' ' to keep pygments from removing empty lines # split, merge by \n can introduce one additional line content = [' \n' if x == '' else x + '\n' for x in content.split('\n')][:nlines] # if content_type == 'COMMENT': fh.write(highlight(''.join(content), SoS_Lexer(), formatter)) elif content_type in ('REPORT', 'report'): fh.write(highlight(''.join(content), TextLexer(), formatter)) elif content_type == 'SECTION': fh.write(highlight(''.join(content), SoS_Lexer(), formatter)) elif content_type == 'DIRECTIVE': fh.write(highlight(''.join(content), SoS_Lexer(), formatter)) elif content_type == 'STATEMENT': fh.write(highlight(''.join(content), SoS_Lexer(), formatter)) elif content_type == 'ERROR': fh.write(highlight(''.join(content), SoS_Lexer(), formatter)) else: if content_type == 'run': content_type = 'bash' elif content_type == 'node': content_type = 'JavaScript' elif content_type == 'report': content_type = 'text' try: lexer = get_lexer_by_name(content_type) except Exception: try: lexer = guess_lexer(''.join(content)) except Exception: lexer = TextLexer() fh.write(highlight((''.join(content)), lexer, formatter))
def hilite(self): """ Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with optional line numbers. The output should then be styled with css to your liking. No styles are applied by default - only styling hooks (i.e.: <span class="k">). returns : A string of html. """ self.src = self.src.strip('\n') if self.lang is None: self._getLang() if pygments: try: lexer = get_lexer_by_name(self.lang) except ValueError: try: if self.guess_lang: lexer = guess_lexer(self.src) else: lexer = TextLexer() except ValueError: lexer = TextLexer() formatter = HtmlFormatter(linenos=self.linenums, cssclass=self.css_class, style=self.style, noclasses=self.noclasses) return highlight(self.src, lexer, formatter) else: # just escape and build markup usable by JS highlighting libs txt = self.src.replace('&', '&') txt = txt.replace('<', '<') txt = txt.replace('>', '>') txt = txt.replace('"', '"') classes = [] if self.lang: classes.append('language-%s' % self.lang) if self.linenums: classes.append('linenums') class_str = '' if classes: class_str = ' class="%s"' % ' '.join(classes) return '<pre class="%s"><code%s>%s</code></pre>\n'% \ (self.css_class, class_str, txt)
def hilite(self): """ Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with optional line numbers. The output should then be styled with css to your liking. No styles are applied by default - only styling hooks (i.e.: <span class="k">). returns : A string of html. """ self.src = self.src.strip('\n') if self.lang is None: self._parseHeader() if pygments: try: lexer = get_lexer_by_name(self.lang) except ValueError: try: if self.guess_lang: lexer = guess_lexer(self.src) else: lexer = TextLexer() except ValueError: lexer = TextLexer() formatter = HtmlFormatter(linenos=self.linenums, cssclass=self.css_class, style=self.style, noclasses=self.noclasses, hl_lines=self.hl_lines) return highlight(self.src, lexer, formatter) else: # just escape and build markup usable by JS highlighting libs txt = self.src.replace('&', '&') txt = txt.replace('<', '<') txt = txt.replace('>', '>') txt = txt.replace('"', '"') classes = [] if self.lang: classes.append('language-%s' % self.lang) if self.linenums: classes.append('linenums') class_str = '' if classes: class_str = ' class="%s"' % ' '.join(classes) return '<pre class="%s"><code%s>%s</code></pre>\n'% \ (self.css_class, class_str, txt)
def write_html_content(content_type, content, formatter, html): # dedent content but still keeps empty lines old_class = formatter.cssclass nlines = len(content) content = dedent('\n'.join(content)) # ' ' to keep pygments from removing empty lines # split, merge by \n can introduce one additional line content = [' \n' if x == '' else x + '\n' for x in content.split('\n')][:nlines] # if content_type == 'COMMENT': formatter.cssclass = 'source blob-code sos-comment' html.write('\n'.join(highlight(x, SoS_Lexer(), formatter) for x in content)) elif content_type in ('REPORT', 'report'): formatter.cssclass = 'source blob-code sos-report' html.write('{}\n'.format(highlight(''.join(content), TextLexer(), formatter))) elif content_type == 'SECTION': formatter.cssclass = 'source blob-code sos-header' html.write('{}\n'.format(highlight(''.join(content), SoS_Lexer(), formatter))) elif content_type == 'DIRECTIVE': formatter.cssclass = 'source blob-code sos-directive' html.write('{}\n'.format(highlight(''.join(content), SoS_Lexer(), formatter))) elif content_type == 'STATEMENT': formatter.cssclass = 'source blob-code sos-statement' html.write('{}\n'.format(highlight(''.join(content), SoS_Lexer(), formatter))) elif content_type == 'ERROR': formatter.cssclass = 'source blob-code sos-error ' html.write('{}\n'.format(highlight(''.join(content), SoS_Lexer(), formatter))) else: formatter.cssclass = 'source blob-code sos-script ' if content_type == 'run': content_type = 'bash' elif content_type == 'node': content_type = 'JavaScript' elif content_type == 'report': content_type = 'text' try: lexer = get_lexer_by_name(content_type) except Exception: try: lexer = guess_lexer(''.join(content)) except Exception: lexer = TextLexer() html.write('{}\n'.format(highlight((''.join(content)), lexer, formatter))) formatter.cssclass = old_class # # utility function #