Start of simple html generator

This commit is contained in:
Oly 2018-10-17 14:32:17 +01:00
parent 4ded0c9ef6
commit 9ffd78ca08
5 changed files with 89 additions and 3 deletions

View File

@ -1,11 +1,11 @@
t_BLANK_LINE = '^\s*$'
METADATA = ['TITLE', 'AUTHOR', 'EMAIL', 'DESCRIPTION', 'KEYWORDS'] METADATA = ['TITLE', 'AUTHOR', 'EMAIL', 'DESCRIPTION', 'KEYWORDS']
t_META = r"^[#]\+(" + '|'.join(METADATA) +")\:" t_META = r"^[#]\+(" + '|'.join(METADATA) +")\:"
t_COMMENT_BEGIN = r"^\#\+BEGIN_COMMENT" t_COMMENT_BEGIN = r"^\#\+BEGIN_COMMENT"
t_COMMENT_END = r"^\#\+END_COMMENT" t_COMMENT_END = r"^\#\+END_COMMENT"
t_SRC_BEGIN = r"^\#\+BEGIN_SRC\s+" t_SRC_BEGIN = r"^\#\+BEGIN_SRC\s+"
t_SRC_END = r"[#]\+END_SRC\s+$" t_SRC_END = r"^\#\+END_SRC"
t_HEADER = r"^\*+" t_HEADER = r"^\*+"

32
eorg/generate.py Normal file
View File

@ -0,0 +1,32 @@
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
def src(code):
lexer = get_lexer_by_name('lisp')
return highlight(code, lexer, HtmlFormatter())
builddoc ={
"HEADER1": "h2",
"HEADER2": "h3",
"HEADER3": "h4",
"BREAK": "br",
"TEXT": "p",
"SRC_BEGIN": src,
}
def html(doc):
response = StringIO()
for item in doc:
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

41
eorg/html.py Normal file
View File

@ -0,0 +1,41 @@
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

@ -75,7 +75,10 @@ def parseline(text):
Token(token=match.group(0)[s:e], value=text[match.end() :]), Token(token=match.group(0)[s:e], value=text[match.end() :]),
) )
return block, Token(token=key, value=text[match.end() :]) return block, Token(token=key, value=text[match.end() :])
return False, Token(token='TEXT', value=text) text = text.strip()
if text == '':
return False, Token(token='BREAK', value=text)
return '^\s*$', Token(token='TEXT', value=text + ' ')
def parse(stream): def parse(stream):

View File

@ -0,0 +1,10 @@
import os
from eorg.parser import parse
from eorg.html import generate
doc=[]
with open(os.path.abspath('../../tests/fixtures/test.org'), 'r') as fp:
doc = parse(fp)
print('#################')
print(generate(doc).read())