From 45779c6e3dc892b017bf92fa39bc8288c3ee6662 Mon Sep 17 00:00:00 2001 From: Oliver Marks Date: Wed, 17 Oct 2018 20:11:23 +0100 Subject: [PATCH] Handle extra elements and group text together. --- eorg/const.py | 10 +++++++--- eorg/generate.py | 2 ++ eorg/parser.py | 17 ++++++++++++----- examples/raw/output.py | 3 ++- tests/fixtures/test.org | 2 ++ tests/test_documents.py | 10 +++++++++- 6 files changed, 34 insertions(+), 10 deletions(-) diff --git a/eorg/const.py b/eorg/const.py index 55fa5a5..074e768 100755 --- a/eorg/const.py +++ b/eorg/const.py @@ -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), } diff --git a/eorg/generate.py b/eorg/generate.py index d141c5d..988c2c6 100644 --- a/eorg/generate.py +++ b/eorg/generate.py @@ -15,8 +15,10 @@ builddoc ={ "BREAK": "br", "TEXT": "p", "SRC_BEGIN": src, + "EXAMPLE": 'pre', } + def html(doc): response = StringIO() for item in doc: diff --git a/eorg/parser.py b/eorg/parser.py index 97d9d6c..af69927 100644 --- a/eorg/parser.py +++ b/eorg/parser.py @@ -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 diff --git a/examples/raw/output.py b/examples/raw/output.py index 2920e29..7522d8b 100644 --- a/examples/raw/output.py +++ b/examples/raw/output.py @@ -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) diff --git a/tests/fixtures/test.org b/tests/fixtures/test.org index afd5110..748f23d 100755 --- a/tests/fixtures/test.org +++ b/tests/fixtures/test.org @@ -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 diff --git a/tests/test_documents.py b/tests/test_documents.py index ccfb6ff..1a458af 100755 --- a/tests/test_documents.py +++ b/tests/test_documents.py @@ -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