forked from kenkeiras/org-rw
Add simple support for nested markup.
This commit is contained in:
parent
5b886e5e24
commit
f6de69fd90
6 changed files with 356 additions and 121 deletions
|
@ -19,3 +19,18 @@
|
|||
This is a _underlined phrase_.
|
||||
|
||||
This is a ~code phrase~.
|
||||
|
||||
This is a nested *bold =verbatim /italic +strike _underlined ~code .~ ._ .+ ./ .= .*
|
||||
|
||||
This is a _ non-underlined phrase because an incorrectly placed content _.
|
||||
|
||||
This is a _ non-underlined phrase because an incorrectly placed content beginning_.
|
||||
|
||||
This is a _non-underlined phrase because an incorrectly placed content end _.
|
||||
|
||||
This is a _non-underlined phrase because the lack of an end.
|
||||
|
||||
|
||||
This is a _non-underlined phrase because an empty line between beginning and
|
||||
|
||||
end._
|
||||
|
|
13
tests/03-links.org
Normal file
13
tests/03-links.org
Normal file
|
@ -0,0 +1,13 @@
|
|||
#+TITLE: 03-Links
|
||||
#+DESCRIPTION: Simple org file to test links
|
||||
#+TODO: TODO(t) PAUSED(p) | DONE(d)
|
||||
|
||||
|
||||
* First level
|
||||
:PROPERTIES:
|
||||
:ID: 03-markup-first-level-id
|
||||
:CREATED: [2020-01-01 Wed 01:01]
|
||||
:END:
|
||||
This is a [[https://codigoparallevar.com][web link]].
|
||||
|
||||
This is an /italized [[https://codigoparallevar.com][web link]]/.
|
|
@ -5,7 +5,7 @@ from datetime import datetime as DT
|
|||
|
||||
from org_dom import dumps, load, loads
|
||||
from utils.dom_assertions import (BOLD, CODE, HL, ITALIC, SPAN, STRIKE,
|
||||
UNDERLINED, VERBATIM, Dom)
|
||||
UNDERLINED, VERBATIM, WEB_LINK, Dom,)
|
||||
|
||||
DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
|
@ -47,6 +47,7 @@ class TestSerde(unittest.TestCase):
|
|||
self.assertEqual(dumps(doc), orig)
|
||||
|
||||
def test_markup_file_02(self):
|
||||
self.maxDiff = 1024
|
||||
with open(os.path.join(DIR, '02-markup.org')) as f:
|
||||
doc = load(f)
|
||||
|
||||
|
@ -60,22 +61,73 @@ class TestSerde(unittest.TestCase):
|
|||
],
|
||||
content=[
|
||||
SPAN(" This is a ", BOLD("bold phrase"),
|
||||
"."),
|
||||
SPAN(""),
|
||||
".\n"),
|
||||
SPAN("\n"),
|
||||
SPAN(" This is a ",
|
||||
VERBATIM("verbatim phrase"), "."),
|
||||
SPAN(""),
|
||||
VERBATIM("verbatim phrase"), ".\n"),
|
||||
SPAN("\n"),
|
||||
SPAN(" This is a ", ITALIC("italic phrase"),
|
||||
"."),
|
||||
SPAN(""),
|
||||
".\n"),
|
||||
SPAN("\n"),
|
||||
SPAN(" This is a ",
|
||||
STRIKE("strike-through phrase"), "."),
|
||||
SPAN(""),
|
||||
STRIKE("strike-through phrase"), ".\n"),
|
||||
SPAN("\n"),
|
||||
SPAN(" This is a ",
|
||||
UNDERLINED("underlined phrase"), "."),
|
||||
SPAN(""),
|
||||
UNDERLINED("underlined phrase"), ".\n"),
|
||||
SPAN("\n"),
|
||||
SPAN(" This is a ", CODE("code phrase"),
|
||||
"."),
|
||||
".\n"),
|
||||
|
||||
SPAN("\n"),
|
||||
SPAN(" This is a nested ", BOLD(["bold ", VERBATIM(["verbatim ", ITALIC(["italic ", STRIKE(["strike ", UNDERLINED(["underlined ", CODE("code ."), " ."]), " ."]), " ."]), " ."]), " ."])),
|
||||
SPAN("\n"),
|
||||
|
||||
# SPAN(""),
|
||||
# # TODO: THIS IS INTERLEAVED, not nested
|
||||
# In ORG: This is a interleaved *bold =verbatim /italic +strike _underlined ~code .* .= ./ .+ ._ .~
|
||||
# SPAN(" This is a nested ", BOLD(["bold ", VERBATIM(["verbatim ", ITALIC(["italic ", STRIKE(["strike ", UNDERLINED(["underlined ", CODE("code ."), " ."]), " ."]), " ."]), " ."]), " ."])),
|
||||
# SPAN(""),
|
||||
|
||||
SPAN("\n"),
|
||||
SPAN(" This is a _ non-underlined phrase because an incorrectly placed content _.\n"),
|
||||
SPAN("\n"),
|
||||
|
||||
SPAN(" This is a _ non-underlined phrase because an incorrectly placed content beginning_.\n"),
|
||||
SPAN("\n"),
|
||||
|
||||
SPAN(""),
|
||||
SPAN(" This is a _non-underlined phrase because an incorrectly placed content end _.\n"),
|
||||
SPAN("\n"),
|
||||
|
||||
SPAN(""),
|
||||
SPAN(" This is a _non-underlined phrase because the lack of an end.\n"),
|
||||
SPAN("\n"),
|
||||
|
||||
SPAN("\n"),
|
||||
SPAN(" This is a _non-underlined phrase because an empty line between beginning and\n"),
|
||||
SPAN("\n"),
|
||||
|
||||
SPAN(""),
|
||||
SPAN(" end._\n"),
|
||||
])))
|
||||
|
||||
ex.assert_matches(self, doc)
|
||||
|
||||
# def test_links_file_03(self):
|
||||
# with open(os.path.join(DIR, '03-links.org')) as f:
|
||||
# doc = load(f)
|
||||
|
||||
# ex = Dom(props=[('TITLE', '03-Links'),
|
||||
# ('DESCRIPTION', 'Simple org file to test links'),
|
||||
# ('TODO', 'TODO(t) PAUSED(p) | DONE(d)')],
|
||||
# children=(HL('First level',
|
||||
# props=[
|
||||
# ('ID', '03-markup-first-level-id'),
|
||||
# ('CREATED', DT(2020, 1, 1, 1, 1)),
|
||||
# ],
|
||||
# content=[
|
||||
# SPAN(" This is a ", WEB_LINK("web link", "https://codigoparallevar.com"),
|
||||
# "."),
|
||||
# ])))
|
||||
|
||||
# ex.assert_matches(self, doc)
|
||||
|
|
|
@ -9,6 +9,15 @@ def timestamp_to_datetime(ts):
|
|||
return datetime(ts.year, ts.month, ts.day, ts.hour, ts.minute)
|
||||
|
||||
|
||||
def get_raw(doc):
|
||||
if isinstance(doc, str):
|
||||
return doc
|
||||
elif isinstance(doc, list):
|
||||
return ''.join([get_raw(e) for e in doc])
|
||||
else:
|
||||
return doc.get_raw()
|
||||
|
||||
|
||||
class Dom:
|
||||
def __init__(self, *, props=None, children=None):
|
||||
self.props = props
|
||||
|
@ -65,15 +74,7 @@ class HL:
|
|||
test_case.assertEqual(
|
||||
timestamp_to_datetime(doc_props[i].value), prop[1])
|
||||
|
||||
if isinstance(self.content, str):
|
||||
test_case.assertEqual(get_raw_contents(doc), self.content)
|
||||
else:
|
||||
if len(doc.contents) != len(self.content):
|
||||
print("Contents:", doc.contents)
|
||||
print("Expected:", self.content)
|
||||
test_case.assertEqual(len(doc.contents), len(self.content))
|
||||
for i, content in enumerate(self.content):
|
||||
content.assert_matches(test_case, doc.contents[i])
|
||||
test_case.assertEqual(get_raw_contents(doc), self.get_raw())
|
||||
|
||||
# Check children
|
||||
if self.children is None:
|
||||
|
@ -86,18 +87,21 @@ class HL:
|
|||
for i, children in enumerate(self.children):
|
||||
children.assert_matches(test_case, doc_headlines[i])
|
||||
|
||||
def get_raw(self):
|
||||
return ''.join(map(get_raw, self.content))
|
||||
|
||||
|
||||
class SPAN:
|
||||
def __init__(self, *kwargs):
|
||||
self.contents = kwargs
|
||||
|
||||
def to_raw(self):
|
||||
def get_raw(self):
|
||||
chunks = []
|
||||
for section in self.contents:
|
||||
if isinstance(section, str):
|
||||
chunks.append(section)
|
||||
else:
|
||||
chunks.append(section.to_raw())
|
||||
chunks.append(section.get_raw())
|
||||
|
||||
return ''.join(chunks)
|
||||
|
||||
|
@ -116,8 +120,8 @@ class BOLD:
|
|||
def __init__(self, text):
|
||||
self.text = text
|
||||
|
||||
def to_raw(self):
|
||||
return '*{}*'.format(self.text)
|
||||
def get_raw(self):
|
||||
return '*{}*'.format(get_raw(self.text))
|
||||
|
||||
def assertEqual(self, test_case, other):
|
||||
test_case.assertTrue(isinstance(other, Bold))
|
||||
|
@ -128,8 +132,8 @@ class CODE:
|
|||
def __init__(self, text):
|
||||
self.text = text
|
||||
|
||||
def to_raw(self):
|
||||
return '~{}~'.format(self.text)
|
||||
def get_raw(self):
|
||||
return '~{}~'.format(get_raw(self.text))
|
||||
|
||||
def assertEqual(self, test_case, other):
|
||||
test_case.assertTrue(isinstance(other, Code))
|
||||
|
@ -139,8 +143,8 @@ class ITALIC:
|
|||
def __init__(self, text):
|
||||
self.text = text
|
||||
|
||||
def to_raw(self):
|
||||
return '/{}/'.format(self.text)
|
||||
def get_raw(self):
|
||||
return '/{}/'.format(get_raw(self.text))
|
||||
|
||||
def assertEqual(self, test_case, other):
|
||||
test_case.assertTrue(isinstance(other, Italic))
|
||||
|
@ -150,8 +154,8 @@ class STRIKE:
|
|||
def __init__(self, text):
|
||||
self.text = text
|
||||
|
||||
def to_raw(self):
|
||||
return '+{}+'.format(self.text)
|
||||
def get_raw(self):
|
||||
return '+{}+'.format(get_raw(self.text))
|
||||
|
||||
def assertEqual(self, test_case, other):
|
||||
test_case.assertTrue(isinstance(other, Strike))
|
||||
|
@ -162,8 +166,8 @@ class UNDERLINED:
|
|||
def __init__(self, text):
|
||||
self.text = text
|
||||
|
||||
def to_raw(self):
|
||||
return '_{}_'.format(self.text)
|
||||
def get_raw(self):
|
||||
return '_{}_'.format(get_raw(self.text))
|
||||
|
||||
def assertEqual(self, test_case, other):
|
||||
test_case.assertTrue(isinstance(other, Underlined))
|
||||
|
@ -173,9 +177,22 @@ class VERBATIM:
|
|||
def __init__(self, text):
|
||||
self.text = text
|
||||
|
||||
def to_raw(self):
|
||||
return '={}='.format(self.text)
|
||||
def get_raw(self):
|
||||
return '={}='.format(get_raw(self.text))
|
||||
|
||||
def assertEqual(self, test_case, other):
|
||||
test_case.assertTrue(isinstance(other, Verbatim))
|
||||
test_case.assertEqual(self.text, other.contents)
|
||||
|
||||
class WEB_LINK:
|
||||
def __init__(self, text, link):
|
||||
self.text = text
|
||||
self.link = link
|
||||
|
||||
def get_raw(self):
|
||||
return '[[{}][{}]]'.format(self.link, self.text)
|
||||
|
||||
def assertEqual(self, test_case, other):
|
||||
test_case.assertTrue(isinstance(other, WebLink))
|
||||
test_case.assertEqual(self.text, other.contents)
|
||||
test_case.assertEqual(self.link, other.link)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue