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']
|
METADATA = ['TITLE', 'AUTHOR', 'EMAIL', 'DESCRIPTION', 'KEYWORDS']
|
||||||
t_META = r"^[#]\+(" + '|'.join(METADATA) +")\:"
|
t_META = r"^[#]\+(" + '|'.join(METADATA) +")\:"
|
||||||
|
t_BLANK_LINE = '^\s*$'
|
||||||
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_EXAMPLE_BEGIN = r"^\#\+BEGIN_EXAMPLE"
|
||||||
|
t_EXAMPLE_END = r"^\#\+END_EXAMPLE"
|
||||||
t_SRC_BEGIN = r"^\#\+BEGIN_SRC\s+"
|
t_SRC_BEGIN = r"^\#\+BEGIN_SRC\s+"
|
||||||
t_SRC_END = r"^\#\+END_SRC"
|
t_SRC_END = r"^\#\+END_SRC"
|
||||||
|
t_RESULTS_START = r"^\#\+RESULTS:"
|
||||||
|
t_RESULTS_END = r"^\:..*"
|
||||||
|
|
||||||
t_HEADER = r"^\*+"
|
t_HEADER = r"^\*+"
|
||||||
|
|
||||||
|
|
||||||
# Start regex, End regex, skip start, skip end, count matches
|
# Start regex, End regex, skip start, skip end, count matches
|
||||||
TOKENS = {
|
TOKENS = {
|
||||||
"META": (t_META, False, 2, -1, False),
|
"META": (t_META, False, 2, -1, False),
|
||||||
"COMMENT": (t_COMMENT_BEGIN, t_COMMENT_END, 2, None, 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),
|
"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),
|
"HEADER": (t_HEADER, False, 1, None, True),
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,8 +15,10 @@ builddoc ={
|
||||||
"BREAK": "br",
|
"BREAK": "br",
|
||||||
"TEXT": "p",
|
"TEXT": "p",
|
||||||
"SRC_BEGIN": src,
|
"SRC_BEGIN": src,
|
||||||
|
"EXAMPLE": 'pre',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def html(doc):
|
def html(doc):
|
||||||
response = StringIO()
|
response = StringIO()
|
||||||
for item in doc:
|
for item in doc:
|
||||||
|
|
|
@ -26,11 +26,16 @@ class Document:
|
||||||
if not idx:
|
if not idx:
|
||||||
if default is not None:
|
if default is not None:
|
||||||
return default
|
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:
|
if len(idx) == 1:
|
||||||
return self.doc[idx[0]].value
|
return self.doc[idx[0]].value
|
||||||
return [self.doc[v].value for v in idx]
|
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):
|
def update(self, value):
|
||||||
self.doc[-1].value += value
|
self.doc[-1].value += value
|
||||||
|
|
||||||
|
@ -69,16 +74,15 @@ def parseline(text):
|
||||||
if count is True:
|
if count is True:
|
||||||
key += str(level)
|
key += str(level)
|
||||||
if key == "META":
|
if key == "META":
|
||||||
# return block, [match.group(0)[s:e], text[match.end():]]
|
|
||||||
return (
|
return (
|
||||||
block,
|
block,
|
||||||
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() :])
|
||||||
text = text.strip()
|
text = text.strip()
|
||||||
if text == '':
|
if text == "":
|
||||||
return False, Token(token='BREAK', value=text)
|
return False, Token(token="BREAK", value=text)
|
||||||
return '^\s*$', Token(token='TEXT', value=text + ' ')
|
return False, Token(token="TEXT", value=text + " ")
|
||||||
|
|
||||||
|
|
||||||
def parse(stream):
|
def parse(stream):
|
||||||
|
@ -95,5 +99,8 @@ def parse(stream):
|
||||||
result = parseline(line)
|
result = parseline(line)
|
||||||
if result:
|
if result:
|
||||||
block = result[0]
|
block = result[0]
|
||||||
|
if doc.token() == "TEXT" and result[1].token == "TEXT":
|
||||||
|
doc.update(result[1].value)
|
||||||
|
continue
|
||||||
doc.append(result[1])
|
doc.append(result[1])
|
||||||
return doc
|
return doc
|
||||||
|
|
|
@ -6,5 +6,6 @@ from eorg.parser import parse
|
||||||
with open(os.path.abspath("../../tests/fixtures/test.org"), "r") as fp:
|
with open(os.path.abspath("../../tests/fixtures/test.org"), "r") as fp:
|
||||||
doc = parse(fp)
|
doc = parse(fp)
|
||||||
print(doc.title)
|
print(doc.title)
|
||||||
for item in doc.body():
|
print(doc.keywords)
|
||||||
|
for item in doc:
|
||||||
print(item)
|
print(item)
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#+TITLE: Emacs org-mode examples
|
#+TITLE: Emacs org-mode examples
|
||||||
#+AUTHOR: Eric H. Neilsen, Jr.
|
#+AUTHOR: Eric H. Neilsen, Jr.
|
||||||
#+EMAIL: neilsen@fnal.gov
|
#+EMAIL: neilsen@fnal.gov
|
||||||
|
#+DATE: jkkj
|
||||||
|
#+KEYWORDS: emacs, orgmode, tests
|
||||||
#+DESCRIPTION: Test DESCRIPTION
|
#+DESCRIPTION: Test DESCRIPTION
|
||||||
#+KEYWORDS: key1, key2
|
#+KEYWORDS: key1, key2
|
||||||
|
|
||||||
|
|
|
@ -6,4 +6,12 @@ from eorg.parser import parse
|
||||||
def test_basic():
|
def test_basic():
|
||||||
with open(os.path.abspath("./tests/fixtures/test.org"), "r") as fp:
|
with open(os.path.abspath("./tests/fixtures/test.org"), "r") as fp:
|
||||||
doc = parse(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