Parse out code block params into a dictonary

This commit is contained in:
Oly 2018-10-24 14:15:20 +01:00
parent 82ec528406
commit 13d9f685d9
2 changed files with 31 additions and 4 deletions

View File

@ -3,14 +3,15 @@ from eorg.const import TOKENS, METADATA, ESCAPE
class Token:
__slots__ = ["token", "value"]
__slots__ = ["token", "value", "attrs"]
def __init__(self, token, value):
def __init__(self, token, value="", attrs=""):
self.token = token
self.value = value
self.attrs = attrs
def __repr__(self):
return f"Token(token={self.token}, value={self.value})"
return f'Token(token="{self.token}", value="{self.value}", attrs="{self.attrs}")'
class Document:
@ -82,6 +83,14 @@ class Document:
self.index.setdefault(value.token, []).append(len(self.doc))
self.doc.append(value)
def parse_attrs(text):
attrs = {}
value_list = text.split(':')
attrs['language'] = value_list.pop(0)
for row in value_list:
values = row.strip().split(' ')
attrs[values[0]] = values[1:]
return attrs
def parsebody(text, rx):
match = re.search(rx, text)
@ -92,6 +101,7 @@ def parsebody(text, rx):
def parseline(text):
print(text)
for key, (rx, block, s, e, count) in TOKENS.items():
match = re.search(rx, text)
if not match:
@ -104,7 +114,10 @@ def parseline(text):
block,
Token(token=match.group(0)[s:e], value=text[match.end() :]),
)
if key == "SRC_BEGIN":
return block, Token(token=key, attrs=parse_attrs(text[match.end():]))
return block, Token(token=key, value=text[match.end():])
text = text.strip()
if text == "":
return False, Token(token="BREAK", value=text)
@ -173,6 +186,7 @@ def parse_text(txt):
tokens[-1].value += char
return tokens
def parse(stream):
doc = Document()
block = False

13
tests/test_code_blocks.py Normal file
View File

@ -0,0 +1,13 @@
import os
import pytest
from io import StringIO
from eorg.parser import parse
CODE_BLOCK_EXAMPLE_01 = StringIO("""
#+BEGIN_SRC shell :results silent :tangle.env
elcato create --path=/tmp/myblog
#+END_SRC""")
def test_block_settings():
result = parse(CODE_BLOCK_EXAMPLE_01)
assert [i for i in result] == []