Fix highlighting and don't strip spaces and tabs
This commit is contained in:
parent
9089acfdaa
commit
ed02e1bea3
|
@ -6,8 +6,12 @@ from pygments.lexers import get_lexer_by_name
|
||||||
from pygments.formatters import HtmlFormatter
|
from pygments.formatters import HtmlFormatter
|
||||||
|
|
||||||
def src(doc, code, cls=''):
|
def src(doc, code, cls=''):
|
||||||
lexer = get_lexer_by_name(code.attrs.get('language', 'shell'))
|
try:
|
||||||
return highlight(code.value, lexer, HtmlFormatter())
|
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=''):
|
def img(doc, item, cls=''):
|
||||||
caption = doc.previous('CAPTION')
|
caption = doc.previous('CAPTION')
|
||||||
|
|
41
eorg/html.py
41
eorg/html.py
|
@ -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
|
|
|
@ -99,9 +99,7 @@ def parsebody(text, rx):
|
||||||
else:
|
else:
|
||||||
return rx, text + "\n"
|
return rx, text + "\n"
|
||||||
|
|
||||||
|
|
||||||
def parseline(text):
|
def parseline(text):
|
||||||
print(text)
|
|
||||||
for key, (rx, block, s, e, count) in TOKENS.items():
|
for key, (rx, block, s, e, count) in TOKENS.items():
|
||||||
match = re.search(rx, text)
|
match = re.search(rx, text)
|
||||||
if not match:
|
if not match:
|
||||||
|
@ -191,7 +189,7 @@ def parse(stream):
|
||||||
doc = Document()
|
doc = Document()
|
||||||
block = False
|
block = False
|
||||||
for line in stream:
|
for line in stream:
|
||||||
line = line.strip()
|
line = line.strip('\n')
|
||||||
if block is not False:
|
if block is not False:
|
||||||
result = parsebody(line, block)
|
result = parsebody(line, block)
|
||||||
if result:
|
if result:
|
||||||
|
@ -207,7 +205,5 @@ def parse(stream):
|
||||||
doc.append(result[1])
|
doc.append(result[1])
|
||||||
|
|
||||||
for item in doc.filter('TEXT'):
|
for item in doc.filter('TEXT'):
|
||||||
#print('@@@@@@@@@@@@@@@@@')
|
|
||||||
#print(item.value)
|
|
||||||
item.value = parse_text(item.value)
|
item.value = parse_text(item.value)
|
||||||
return doc
|
return doc
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
__version__ = 0.3
|
__version__ = 0.4
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue