rclpython: renamed rclpython.py. Use rclexecm. Only colorize for preview, not indexing

This commit is contained in:
Jean-Francois Dockes 2020-11-04 10:32:18 +01:00
parent 93951a3509
commit f50a4e54b1
2 changed files with 43 additions and 33 deletions

View File

@ -708,7 +708,7 @@ filters/rclppt.py \
filters/rclps \ filters/rclps \
filters/rclpst.py \ filters/rclpst.py \
filters/rclpurple \ filters/rclpurple \
filters/rclpython \ filters/rclpython.py \
filters/rclrar \ filters/rclrar \
filters/rclrtf.py \ filters/rclrtf.py \
filters/rclscribus \ filters/rclscribus \

View File

@ -1,13 +1,9 @@
#!/usr/bin/env python3 #!/usr/bin/python3
# -*- coding: iso-8859-1 -*-
"""
MoinMoin - Python source parser and colorizer
"""
# Rclpython is verbatim "colorize.py" from: # Rclpython is based on "colorize.py" from:
# http://chrisarndt.de/en/software/python/colorize.html # http://chrisarndt.de/en/software/python/colorize.html
# #
# Based on the code from Jürgen Herman, the following changes where made: # Based on the code from Jürgen Herman, the following changes where made:
# #
# Mike Brown <http://skew.org/~mike/>: # Mike Brown <http://skew.org/~mike/>:
# - make script callable as a CGI and a Apache handler for .py files. # - make script callable as a CGI and a Apache handler for .py files.
@ -22,25 +18,21 @@
# - parse script encoding and allow output in any encoding by using unicode # - parse script encoding and allow output in any encoding by using unicode
# as intermediate # as intermediate
from __future__ import print_function
__version__ = '0.3' __version__ = '0.3'
__date__ = '2005-07-04' __date__ = '2005-07-04'
__license__ = 'GPL' __license__ = 'GPL'
__author__ = 'Jürgen Hermann, Mike Brown, Christopher Arndt'
__author__ = 'Jürgen Hermann, Mike Brown, Christopher Arndt'
# Imports # Imports
import string, sys import rclexecm
from rclbasehandler import RclBaseHandler
import string, sys, os
import html import html
import io import io
import keyword, token, tokenize import keyword, token, tokenize
def makebytes(data):
if type(data) == type(u''):
return data.encode("UTF-8")
return data
############################################################################# #############################################################################
### Python Source Parser (does Hilighting) ### Python Source Parser (does Hilighting)
############################################################################# #############################################################################
@ -140,7 +132,7 @@ class Parser:
# parse the source and write it # parse the source and write it
self.pos = 0 self.pos = 0
text = io.BytesIO(self.raw) text = io.BytesIO(self.raw)
self.out.write(makebytes(self.stylesheet)) self.out.write(rclexecm.makebytes(self.stylesheet))
self.out.write(b'<pre class="code">\n') self.out.write(b'<pre class="code">\n')
try: try:
for a,b,c,d,e in tokenize.tokenize(text.readline): for a,b,c,d,e in tokenize.tokenize(text.readline):
@ -195,8 +187,8 @@ class Parser:
css_class = _css_classes.get(toktype, 'text') css_class = _css_classes.get(toktype, 'text')
# send text # send text
self.out.write(makebytes('<span class="%s">' % (css_class,))) self.out.write(rclexecm.makebytes('<span class="%s">' % (css_class,)))
self.out.write(makebytes(html.escape(toktext))) self.out.write(rclexecm.makebytes(html.escape(toktext)))
self.out.write(b'</span>') self.out.write(b'</span>')
@ -230,23 +222,41 @@ def colorize_file(file=None, outstream=sys.stdout, standalone=True):
source = sourcefile.read() source = sourcefile.read()
if standalone: if standalone:
outstream.write(makebytes(_HTML_HEADER % {'title': filename})) outstream.write(rclexecm.makebytes(_HTML_HEADER % {'title': filename}))
Parser(source, out=outstream).format() Parser(source, out=outstream).format()
if standalone: if standalone:
outstream.write(makebytes(_HTML_FOOTER)) outstream.write(rclexecm.makebytes(_HTML_FOOTER))
if file: if file:
sourcefile.close() sourcefile.close()
if __name__ == "__main__":
import os def _htmlwrapplain(txt, title=b"", charset=b"utf-8"):
out = sys.stdout.buffer return \
if os.environ.get('PATH_TRANSLATED'): b'<html>\n<head>\n<title>' + \
filepath = os.environ.get('PATH_TRANSLATED') title + \
print('Content-Type: text/html; charset="iso-8859-1"\n') b'</title>\n' + \
colorize_file(filepath, out) b'<meta http-equiv="Content-Type" content="text/html; charset=' + \
elif len(sys.argv) > 1: charset + \
filepath = sys.argv[1] b'">\n' + \
colorize_file(filepath, out) b'<body>\n<pre>\n' + \
else: rclexecm.htmlescape(txt) + \
colorize_file() b'</pre>\n</body>\n</html>\n'
class PythonDump(RclBaseHandler):
def __init__(self, em):
super(PythonDump, self).__init__(em)
def html_text(self, fn):
preview = os.environ.get("RECOLL_FILTER_FORPREVIEW", "no")
if preview == "yes":
out = io.BytesIO()
colorize_file(fn, out)
return out.getvalue()
else:
return _htmlwrapplain(open(fn, 'rb').read())
if __name__ == '__main__':
proto = rclexecm.RclExecM()
extract = PythonDump(proto)
rclexecm.main(proto, extract)