Fix highlighting and don't strip spaces and tabs

This commit is contained in:
Oliver Marks 2018-10-27 22:51:57 +01:00
parent 9089acfdaa
commit ed02e1bea3
4 changed files with 8 additions and 49 deletions

View File

@ -6,8 +6,12 @@ from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
def src(doc, code, cls=''):
lexer = get_lexer_by_name(code.attrs.get('language', 'shell'))
return highlight(code.value, lexer, HtmlFormatter())
try:
lexer = get_lexer_by_name(code.attrs.get('language', 'shell'))
except pygments.util.ClassNotFound as e:
lexer = get_lexer_by_name(code.attrs.get('language', 'text'))
return highlight(code.value, lexer, HtmlFormatter(linenos=True))
def img(doc, item, cls=''):
caption = doc.previous('CAPTION')

View File

@ -1,41 +0,0 @@
from io import StringIO
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
code = 'print "Hello World"'
def src(code):
lexer = get_lexer_by_name('lisp')
return highlight(code, lexer, HtmlFormatter())
builddoc ={
# "TITLE": "h1",
# "EMAIL": "h1",
# "AUTHOR": "h1",
"HEADER1": "h2",
"HEADER2": "h3",
"HEADER3": "h4",
"BREAK": "br",
"TEXT": "p",
"SRC_BEGIN": src,
# "COMMENT": "pre",
}
def generate(doc):
response = StringIO()
for item in doc:
print(item)
tag = builddoc.get(item.token)
if not tag:
continue
if callable(tag):
response.write(tag(item.value))
continue
else:
response.write('<%s>%s<%s/>\n' % (tag, item.value, tag))
response.seek(0)
return response

View File

@ -99,9 +99,7 @@ def parsebody(text, rx):
else:
return rx, text + "\n"
def parseline(text):
print(text)
for key, (rx, block, s, e, count) in TOKENS.items():
match = re.search(rx, text)
if not match:
@ -191,7 +189,7 @@ def parse(stream):
doc = Document()
block = False
for line in stream:
line = line.strip()
line = line.strip('\n')
if block is not False:
result = parsebody(line, block)
if result:
@ -207,7 +205,5 @@ def parse(stream):
doc.append(result[1])
for item in doc.filter('TEXT'):
#print('@@@@@@@@@@@@@@@@@')
#print(item.value)
item.value = parse_text(item.value)
return doc

View File

@ -1,2 +1,2 @@
__version__ = 0.3
__version__ = 0.4