Parse out code block params into a dictonary
This commit is contained in:
parent
82ec528406
commit
13d9f685d9
|
@ -3,14 +3,15 @@ from eorg.const import TOKENS, METADATA, ESCAPE
|
||||||
|
|
||||||
|
|
||||||
class Token:
|
class Token:
|
||||||
__slots__ = ["token", "value"]
|
__slots__ = ["token", "value", "attrs"]
|
||||||
|
|
||||||
def __init__(self, token, value):
|
def __init__(self, token, value="", attrs=""):
|
||||||
self.token = token
|
self.token = token
|
||||||
self.value = value
|
self.value = value
|
||||||
|
self.attrs = attrs
|
||||||
|
|
||||||
def __repr__(self):
|
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:
|
class Document:
|
||||||
|
@ -82,6 +83,14 @@ class Document:
|
||||||
self.index.setdefault(value.token, []).append(len(self.doc))
|
self.index.setdefault(value.token, []).append(len(self.doc))
|
||||||
self.doc.append(value)
|
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):
|
def parsebody(text, rx):
|
||||||
match = re.search(rx, text)
|
match = re.search(rx, text)
|
||||||
|
@ -92,6 +101,7 @@ def parsebody(text, rx):
|
||||||
|
|
||||||
|
|
||||||
def parseline(text):
|
def parseline(text):
|
||||||
|
print(text)
|
||||||
for key, (rx, block, s, e, count) in TOKENS.items():
|
for key, (rx, block, s, e, count) in TOKENS.items():
|
||||||
match = re.search(rx, text)
|
match = re.search(rx, text)
|
||||||
if not match:
|
if not match:
|
||||||
|
@ -104,7 +114,10 @@ def parseline(text):
|
||||||
block,
|
block,
|
||||||
Token(token=match.group(0)[s:e], value=text[match.end() :]),
|
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():])
|
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)
|
||||||
|
@ -173,6 +186,7 @@ def parse_text(txt):
|
||||||
tokens[-1].value += char
|
tokens[-1].value += char
|
||||||
return tokens
|
return tokens
|
||||||
|
|
||||||
|
|
||||||
def parse(stream):
|
def parse(stream):
|
||||||
doc = Document()
|
doc = Document()
|
||||||
block = False
|
block = False
|
||||||
|
|
|
@ -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] == []
|
Loading…
Reference in New Issue