def apply_syntax_highlighting(html: str) -> str: """Get all code blocks with defined info string in class and highlight them.""" soup = BeautifulSoup(html, features="html5lib") # Get all code blocks and for every code block that has info string code_blocks = soup.find_all("code", class_=re.compile("^language-")) for code_block in code_blocks: language = code_block["class"][0].replace("language-", "") try: lexer = get_lexer_by_name(language) # If target language is PHP, override default lexer construction # and set startinline to True, so even code that is not enclosed # inside <?php ... ?> will get highlighted. if isinstance(lexer, PhpLexer): lexer = PhpLexer(startinline=True) except ClassNotFound: continue highlighted = highlight( code_block.text, lexer, CodeHtmlFormatter(classprefix="syntax-") ) html = html.replace(str(code_block), highlighted, 1) return html
def highlight(code, language, _preview=False, _linenos=True): """Highlight a given code to HTML.""" if not _preview: if language == 'diff': return highlight_diff(code) elif language == 'creole': return format_creole(code) elif language == 'csv': return format_csv(code) elif language == 'gcc-messages': return format_compiler_messages(parse_gcc_messages(code), 'gcc') elif language == 'javac-messages': return format_compiler_messages(parse_javac_messages(code), 'javac') if language == 'multi': return highlight_multifile(code) elif language == 'php': lexer = PhpLexer(startinline=True) else: try: lexer = get_lexer_by_name(language) except ClassNotFound: lexer = TextLexer() style = get_style(name_only=True) formatter = HtmlFormatter(linenos=_linenos, cssclass='syntax', style=style) return u'<div class="code">%s</div>' % \ pygments.highlight(code, lexer, formatter)
def highlightString(src): try: if self.currentExt == 'php': from pygments.lexers import PhpLexer return highlight(src, PhpLexer(), HtmlFormatter()) elif self.currentExt == 'py': from pygments.lexers import PythonLexer return highlight(src, PythonLexer(), HtmlFormatter()) elif self.currentExt == 'rb': from pygments.lexers import RubyLexer return highlight(src, RubyLexer(), HtmlFormatter()) elif self.currentExt == 'pl': from pygments.lexers import PerlLexer return highlight(src, PerlLexer(), HtmlFormatter()) elif self.currentExt == 'java': from pygments.lexers import JavaLexer return highlight(src, JavaLexer(), HtmlFormatter()) elif self.currentExt == 'cs': from pygments.lexers import CSharpLexer return highlight(src, CSharpLexer(), HtmlFormatter()) else: from pygments.lexers import JavascriptLexer return highlight(src, JavascriptLexer(), HtmlFormatter()) except: return "File could not be highlighted"
class PygmentsPreviewer(Previewer): # All supported MIME types MIMETYPES = ('text/css', 'text/x-python', 'text/x-ruby-script', 'text/x-java-source', 'text/x-c', 'application/javascript', 'text/x-c', 'text/x-fortran', 'text/x-csharp', 'text/php', 'text/x-php') # Python's mimetypes lib and Pygments do not quite agree on some MIME types CUSTOM_LEXERS = { 'text/x-c': CppLexer(), 'text/x-java-source': JavaLexer(), 'text/x-ruby-script': RubyLexer(), 'text/php': PhpLexer() } @classmethod def can_preview(cls, attachment_file): return attachment_file.content_type in cls.MIMETYPES @classmethod def generate_content(cls, attachment): mime_type = attachment.file.content_type lexer = cls.CUSTOM_LEXERS.get(mime_type) if lexer is None: lexer = get_lexer_for_mimetype(mime_type) with attachment.file.open() as f: html_formatter = HtmlFormatter(style='tango', linenos='inline', prestyles='mono') html_code = highlight(f.read(), lexer, html_formatter) css_code = html_formatter.get_style_defs('.highlight') return render_template('previewer_code:pygments_preview.html', attachment=attachment, html_code=html_code, css_code=css_code)
def _pygmentize(self, lang_type, caller): lexer = None formatter = HtmlFormatter(linenos=False) content = caller() if lang_type is None: lexer = guess_lexer(content) elif lang_type.upper() == "PHP": lexer = PhpLexer(startinline=True) else: lexer = get_lexer_by_name(lang_type) return Markup(highlight(content, lexer, formatter)).unescape()
def highlight_code(code, language, _preview=False, _linenos=True): """ Syntax highlights a code file """ if language == "php": lexer = PhpLexer(startinline=True) elif language == "guess": lexer = guess_lexer(code) else: try: lexer = get_lexer_by_name(language) except ClassNotFound: lexer = TextLexer() formatter = HtmlFormatter(linenos=_linenos, cssclass="syntax", style="xcode") return f'<div class="syntax">{pygments.highlight(code, lexer, formatter)}</div>'
class PhpTest(unittest.TestCase): def setUp(self): self.lexer = PhpLexer() def testStringEscapingRun(self): fragment = '<?php $x="{\\""; ?>\n' tokens = [ (Token.Comment.Preproc, '<?php'), (Token.Text, ' '), (Token.Name.Variable, '$x'), (Token.Operator, '='), (Token.Literal.String.Double, '"'), (Token.Literal.String.Double, '{'), (Token.Literal.String.Escape, '\\"'), (Token.Literal.String.Double, '"'), (Token.Punctuation, ';'), (Token.Text, ' '), (Token.Comment.Preproc, '?>'), (Token.Other, '\n'), ] self.assertEqual(tokens, list(self.lexer.get_tokens(fragment)))
def _format_sql(sql, data, format='html'): popts = {} if data.get('remove_comments'): popts['strip_comments'] = True if data.get('keyword_case', 'undefined') not in ('undefined', ''): popts['keyword_case'] = data.get('keyword_case') if data.get('identifier_case', 'undefined') not in ('undefined', ''): popts['identifier_case'] = data.get('identifier_case') if data.get('n_indents', None) is not None: val = data.get('n_indents') try: popts['indent_width'] = max(1, min(1000, int(val))) popts['reindent'] = True except (ValueError, TypeError): pass if (not 'indent_width' in popts and data.get('reindent', '').lower() in ('1', 'true', 't')): popts['indent_width'] = 2 popts['reindent'] = True if data.get('output_format', None) is not None: popts['output_format'] = data.get('output_format') logging.debug('Format: %s, POPTS: %r', format, popts) logging.debug(sql) sql = sqlparse.format(sql, **popts) if format in ('html', 'json'): if data.get('highlight', False): if popts['output_format'] == 'python': lexer = PythonLexer() elif popts['output_format'] == 'php': lexer = PhpLexer() else: lexer = SqlLexer() sql = highlight(sql, lexer, HtmlFormatter()) else: sql = ('<textarea class="resizable" ' 'style="height: 350px; margin-top: 1em;">%s</textarea>' % sql) return sql
def lexer(): yield PhpLexer()
# If true, show page references after internal links. #latex_show_pagerefs = False # If true, show URL addresses after external links. #latex_show_urls = False # Additional stuff for the LaTeX preamble. #latex_preamble = '' # Documents to append as an appendix to all manuals. #latex_appendices = [] # If false, no module index is generated. #latex_domain_indices = True # -- Options for manual page output -------------------------------------------- # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [('index', 'mailgunrestapi', u'Mailgun REST API Documentation', [u'Mailgun Inc'], 1)] # Normally, the Pygments PHP lexer requires PHP code to be between '<?php' '?>' # delimiters. The hack below allows highlighting for PHP code which is not # between those tags. from sphinx.highlighting import lexers from pygments.lexers import PhpLexer, TextLexer lexers['php'] = PhpLexer(startinline=True) lexers['url'] = TextLexer()
#!/usr/bin/env python # -*- coding: utf-8 -*- import os from codecs import open from pygments import highlight from pygments.lexers import PhpLexer from pygments.formatters import HtmlFormatter lexer=PhpLexer() formatter=HtmlFormatter() sourceExamplesDir='../src/examples/' for fileName in os.listdir(sourceExamplesDir): if fileName != 'launcher.php': code=open(sourceExamplesDir+fileName,'rb','utf-8').read() highlighted=highlight(code,lexer,formatter) open('source/_templates/sphinxdoc/examples/'+fileName.replace('.php','.html'),'wb','utf-8' ).write(highlighted)
if (closing_char) and def_started: def_started = False # for cases such as array(); - value is (); if len(svalue) > 1: yield token.Text, svalue[:-1] yield token.Text, "\n" if def_started: yield ttype, value if __name__ == "__main__": import os from pygments import highlight from pygments.lexers import PhpLexer from pygments.formatters import NullFormatter lex = PhpLexer(startinline=True) lex.add_filter(PHPAPIFilter()) for (path, dirs, files) in os.walk('~/repos/git/symfony/src'): for fname in files: f = os.path.join(path, fname) if f.endswith(".php"): code = open(f, 'r').read() print "---------- start %s ----------" % f print highlight(code, lex, NullFormatter()) print "---------- end %s ----------" % f
# If there isn't only 2 args something weird is going on expecting = 2 if (len(sys.argv) != expecting + 1): exit(128) # Get the code language = (sys.argv[1]).lower() filename = sys.argv[2] f = open(filename, 'rb') code = f.read() f.close() # PHP if language == 'php': from pygments.lexers import PhpLexer lexer = PhpLexer(startinline=True) # GUESS elif language == 'guess': from pygments.lexers import guess_lexer lexer = guess_lexer(code) # GET BY NAME else: from pygments.lexers import get_lexer_by_name lexer = get_lexer_by_name(language) # OUTPUT formatter = HtmlFormatter(linenos=False, nowrap=True) highlighted = highlight(code, lexer, formatter) print highlighted
# coding: utf-8 # https://forum.omz-software.com/topic/2521/rich-text-clipboard from objc_util import * from pygments import highlight from pygments.lexers import PhpLexer from pygments.formatters import RtfFormatter code = '<?php echo "hello world"; ?>' rtf = highlight(code, PhpLexer(), RtfFormatter()) fp = open('highlight.rtf', 'w+') fp.write(rtf) data = NSData.dataWithContentsOfFile_('highlight.rtf') c = ObjCClass('UIPasteboard') pasteBoard = c.generalPasteboard() pasteBoard.setValue_forPasteboardType_(data, 'public.rtf') #============================== > pasteBoard.pasteboardTypes() <__NSCFArray: ( "public.rtf" )> #============================== with open(...) as fp:
def setUp(self): self.lexer = PhpLexer()
# https://forum.omz-software.com/topic/2521/rich-text-clipboard/12 from objc_util import * from pygments import highlight from pygments.lexers import PhpLexer from pygments.formatters import RtfFormatter p = ObjCClass('UIPasteboard').generalPasteboard() code = b'<?php echo "hello world"; ?>' rtf = highlight(code, PhpLexer(), RtfFormatter()) rtf_data = ns(bytes(rtf, 'ascii')) p.setData_forPasteboardType_(rtf_data, 'public.RTF')
def pygment_content(rts): return highlight( rts, PhpLexer(), HtmlFormatter(style='monokai', cssclass="source", full=True))