Handle extra elements and group text together.
This commit is contained in:
parent
9ffd78ca08
commit
45779c6e3d
|
@ -1,19 +1,23 @@
|
|||
t_BLANK_LINE = '^\s*$'
|
||||
METADATA = ['TITLE', 'AUTHOR', 'EMAIL', 'DESCRIPTION', 'KEYWORDS']
|
||||
t_META = r"^[#]\+(" + '|'.join(METADATA) +")\:"
|
||||
t_BLANK_LINE = '^\s*$'
|
||||
t_COMMENT_BEGIN = r"^\#\+BEGIN_COMMENT"
|
||||
t_COMMENT_END = r"^\#\+END_COMMENT"
|
||||
|
||||
t_EXAMPLE_BEGIN = r"^\#\+BEGIN_EXAMPLE"
|
||||
t_EXAMPLE_END = r"^\#\+END_EXAMPLE"
|
||||
t_SRC_BEGIN = r"^\#\+BEGIN_SRC\s+"
|
||||
t_SRC_END = r"^\#\+END_SRC"
|
||||
t_RESULTS_START = r"^\#\+RESULTS:"
|
||||
t_RESULTS_END = r"^\:..*"
|
||||
|
||||
t_HEADER = r"^\*+"
|
||||
|
||||
|
||||
# Start regex, End regex, skip start, skip end, count matches
|
||||
TOKENS = {
|
||||
"META": (t_META, False, 2, -1, False),
|
||||
"COMMENT": (t_COMMENT_BEGIN, t_COMMENT_END, 2, None, False),
|
||||
"EXAMPLE": (t_EXAMPLE_BEGIN, t_EXAMPLE_END, 2, None, False),
|
||||
"SRC_BEGIN": (t_SRC_BEGIN, t_SRC_END, 2, None, False),
|
||||
"RESULTS": (t_SRC_BEGIN, t_SRC_END, 2, None, False),
|
||||
"HEADER": (t_HEADER, False, 1, None, True),
|
||||
}
|
||||
|
|
|
@ -15,8 +15,10 @@ builddoc ={
|
|||
"BREAK": "br",
|
||||
"TEXT": "p",
|
||||
"SRC_BEGIN": src,
|
||||
"EXAMPLE": 'pre',
|
||||
}
|
||||
|
||||
|
||||
def html(doc):
|
||||
response = StringIO()
|
||||
for item in doc:
|
||||
|
|
|
@ -26,11 +26,16 @@ class Document:
|
|||
if not idx:
|
||||
if default is not None:
|
||||
return default
|
||||
raise ValueError(f'Attribute of {name} does not exist in document')
|
||||
raise ValueError(f"Attribute of {name} does not exist in document")
|
||||
if len(idx) == 1:
|
||||
return self.doc[idx[0]].value
|
||||
return [self.doc[v].value for v in idx]
|
||||
|
||||
def token(self):
|
||||
if self.doc:
|
||||
return self.doc[-1].token
|
||||
return ""
|
||||
|
||||
def update(self, value):
|
||||
self.doc[-1].value += value
|
||||
|
||||
|
@ -69,16 +74,15 @@ def parseline(text):
|
|||
if count is True:
|
||||
key += str(level)
|
||||
if key == "META":
|
||||
# return block, [match.group(0)[s:e], text[match.end():]]
|
||||
return (
|
||||
block,
|
||||
Token(token=match.group(0)[s:e], value=text[match.end() :]),
|
||||
)
|
||||
return block, Token(token=key, value=text[match.end() :])
|
||||
text = text.strip()
|
||||
if text == '':
|
||||
return False, Token(token='BREAK', value=text)
|
||||
return '^\s*$', Token(token='TEXT', value=text + ' ')
|
||||
if text == "":
|
||||
return False, Token(token="BREAK", value=text)
|
||||
return False, Token(token="TEXT", value=text + " ")
|
||||
|
||||
|
||||
def parse(stream):
|
||||
|
@ -95,5 +99,8 @@ def parse(stream):
|
|||
result = parseline(line)
|
||||
if result:
|
||||
block = result[0]
|
||||
if doc.token() == "TEXT" and result[1].token == "TEXT":
|
||||
doc.update(result[1].value)
|
||||
continue
|
||||
doc.append(result[1])
|
||||
return doc
|
||||
|
|
|
@ -6,5 +6,6 @@ from eorg.parser import parse
|
|||
with open(os.path.abspath("../../tests/fixtures/test.org"), "r") as fp:
|
||||
doc = parse(fp)
|
||||
print(doc.title)
|
||||
for item in doc.body():
|
||||
print(doc.keywords)
|
||||
for item in doc:
|
||||
print(item)
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#+TITLE: Emacs org-mode examples
|
||||
#+AUTHOR: Eric H. Neilsen, Jr.
|
||||
#+EMAIL: neilsen@fnal.gov
|
||||
#+DATE: jkkj
|
||||
#+KEYWORDS: emacs, orgmode, tests
|
||||
#+DESCRIPTION: Test DESCRIPTION
|
||||
#+KEYWORDS: key1, key2
|
||||
|
||||
|
|
|
@ -6,4 +6,12 @@ from eorg.parser import parse
|
|||
def test_basic():
|
||||
with open(os.path.abspath("./tests/fixtures/test.org"), "r") as fp:
|
||||
doc = parse(fp)
|
||||
assert len(doc) == 14
|
||||
assert doc.title != ''
|
||||
assert doc.author != ''
|
||||
assert len(doc) == 20
|
||||
|
||||
|
||||
def test_body():
|
||||
with open(os.path.abspath("./tests/fixtures/test.org"), "r") as fp:
|
||||
doc = parse(fp)
|
||||
assert len([i for i in doc.body()]) > 0
|
||||
|
|
Loading…
Reference in New Issue