 Having started writing a topic about my perversions with TeX, I realized that I really lacked normal syntax highlighting. Googling in Habr and the environs was induced by a couple of editors who did not work for me and the description of the formatter formatted for pygments .
 Having started writing a topic about my perversions with TeX, I realized that I really lacked normal syntax highlighting. Googling in Habr and the environs was induced by a couple of editors who did not work for me and the description of the formatter formatted for pygments .-
<code class= "python" > <!-- , -->
#!/usr/bin/env python
import sys
</code> <!-- , -->
def preparse_text(text, linenos = False , style = None ):
"""Extract code blocks from raw text, render via pygments and return as unicode string"""
R = re . compile( ur'^\s*<code class="(?P<class>.*?)">\s*$(?P<code>.*?)^\s*</code>\s*$' , re . I | re . U | re . S | re . M)
out = []
prev = 0‌
ar = { 'linenos' : False }
if linenos:
ar[ 'linenos' ] = 'inline'
if style:
ar[ 'style' ] = style
for s in R . finditer(text):
fmt = OldHtmlFormatter( ** ar)
out . append(text[prev:s . start()])
lx = get_lexer_by_name(s . group( 'class' ))
if not lx:
lx = guess_lexer(s . group( 'code' ))
if lx:
s0 = s . group( 'code' )
s0 = s0 . replace( u' ' , u' \u00a0 ' ) #  
src = highlight(s0, lx, fmt) # .replace(u'\n', '<br/>\n') # for preview
else :
src = u'<code> %s </code>' % s . group( 'code' )
out . append(src)
prev = s . end()
del lx
del fmt
lx = None
out . append(text[prev:])
return u'' . join(out)
#
p = OptionParser(usage = 'usage: %prog [options] input_file' )
p . add_option( '-f' , '--file' , metavar = "FILE" , help = "Write output to FILE" )
p . add_option( '-s' , '--style' , metavar = "STYLE" ,help = "Use color STYLE for formatting" )
p . add_option( '--htm' , '--html' , action = "store_true" , help = "Add extra html headers in output" )
p . add_option( '--list-styles' , action = "store_true" , help = "Show list of supported styles" )
p . add_option( '--list-languages' , action = "store_true" , help = "Show list of supported languages" )
# , 'op', 'a[0]'
op,a = p . parse_args()
# - ,
if op . list_styles:
from pygments . styles import get_all_styles
print "Supported color styles:"
for s in get_all_styles():
print u" \t %s " % (s,)
sys . exit( 0‌ )
# - :)
if op . list_languages:
from pygments . lexers import get_all_lexers
print "Supported languages and aliases:"
ss = list (get_all_lexers())
ss . sort(key = lambda x:x[ 0‌ ] . lower())
for s in ss:
print s[ 0‌ ]
if s[ 1 ]:
print " \t " , ", " . join(s[ 1 ])
sys . exit( 0‌ )
# ,
if len (a) != 1 :
print "No input file specified!"
sys . exit( 1 )
srcfile = a[ 0‌ ]
dstfile = op . file
f = unicode ( open (srcfile, 'rb' ) . read(), 'utf-8' , 'replace' ) # , utf8 ^)
s = preparse_text(f, False , op . style) . encode( 'utf-8' ) #
if dstfile:
fn = open (dstfile, 'wb' )
else :
fn = sys . stdout
# HTML --
if op . htm:
fn . write( '<html><head><meta http-equiv="content-type" content="text/html; charset= \' utf8 \' "/></head><body> \n ' )
fn . write(s)
if op . htm:
fn . write( '</body></html> \n ' )
try :
close(fn) # stdout close,
except :
pass
Source: https://habr.com/ru/post/86781/
All Articles