From 0e90abbb63942fbd262260e06a3c6dea11fbc127 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Sun, 28 Aug 2022 14:08:54 +0200 Subject: [PATCH] feat: Support markup inside headline titles. --- org_rw/org_rw.py | 15 +++++++++++++-- tests/09-markup-on-headline.org | 10 ++++++++++ tests/test_org.py | 15 +++++++++++++++ tests/utils/assertions.py | 2 +- 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 tests/09-markup-on-headline.org diff --git a/org_rw/org_rw.py b/org_rw/org_rw.py index 81c638c..3ea3204 100644 --- a/org_rw/org_rw.py +++ b/org_rw/org_rw.py @@ -266,7 +266,9 @@ class Headline: self.priority_start = priority_start self.priority = priority self.title_start = title_start - self.title = title + self.title = parse_content_block( + [RawLine(linenum=start_line, line=title)] + ) self.state = state self.tags_start = tags_start self.shallow_tags = tags @@ -1146,9 +1148,18 @@ class Text: def __repr__(self): return "{{Text line: {}; content: {} }}".format(self.linenum, self.contents) + def get_text(self): + return token_list_to_plaintext(self.contents) + def get_raw(self): return token_list_to_raw(self.contents) +def token_list_to_plaintext(tok_list) -> str: + return "".join([ + chunk + for chunk in tok_list + if isinstance(chunk, str) + ]) def token_list_to_raw(tok_list): contents = [] @@ -1683,7 +1694,7 @@ class OrgDoc: if headline.state: state = headline.state + " " - yield "*" * headline.depth + headline.spacing + state + headline.title + tags + yield "*" * headline.depth + headline.spacing + state + token_list_to_raw(headline.title.contents) + tags planning = headline.get_planning_line() if planning is not None: diff --git a/tests/09-markup-on-headline.org b/tests/09-markup-on-headline.org new file mode 100644 index 0000000..53bc588 --- /dev/null +++ b/tests/09-markup-on-headline.org @@ -0,0 +1,10 @@ +#+TITLE: 09-Markup-on-headline +#+DESCRIPTION: Simple org file to test markup parsing on headlines +#+TODO: TODO(t) PAUSED(p) | DONE(d) + + +* Headline _with_ markup + :PROPERTIES: + :ID: 09-markup-on-headline-headline-with-markup-id + :CREATED: [2020-01-01 Wed 01:01] + :END: diff --git a/tests/test_org.py b/tests/test_org.py index a3e346c..0cd6136 100644 --- a/tests/test_org.py +++ b/tests/test_org.py @@ -619,3 +619,18 @@ class TestSerde(unittest.TestCase): *** Third headline """.strip(), ) + + + def test_markup_file_09(self): + with open(os.path.join(DIR, "09-markup-on-headline.org")) as f: + doc = load(f) + + hl = doc.getTopHeadlines()[0] + print(hl.title) + self.assertEqual(hl.title.contents, [ + 'Headline ', + MarkerToken(closing=False, tok_type=MarkerType.UNDERLINED_MODE), + 'with', + MarkerToken(closing=True, tok_type=MarkerType.UNDERLINED_MODE), + ' markup', + ]) diff --git a/tests/utils/assertions.py b/tests/utils/assertions.py index 8e4f2d8..59dc658 100644 --- a/tests/utils/assertions.py +++ b/tests/utils/assertions.py @@ -59,7 +59,7 @@ class HL: self.children = children def assert_matches(self, test_case: unittest.TestCase, doc): - test_case.assertEqual(self.title, doc.title) + test_case.assertEqual(self.title, get_raw(doc.title)) # Check properties if self.props is None: