feat: Support markup inside headline titles.

This commit is contained in:
Sergio Martínez Portela 2022-08-28 14:08:54 +02:00
parent baaa7cbb86
commit 0e90abbb63
4 changed files with 39 additions and 3 deletions

View File

@ -266,7 +266,9 @@ class Headline:
self.priority_start = priority_start self.priority_start = priority_start
self.priority = priority self.priority = priority
self.title_start = title_start self.title_start = title_start
self.title = title self.title = parse_content_block(
[RawLine(linenum=start_line, line=title)]
)
self.state = state self.state = state
self.tags_start = tags_start self.tags_start = tags_start
self.shallow_tags = tags self.shallow_tags = tags
@ -1146,9 +1148,18 @@ class Text:
def __repr__(self): def __repr__(self):
return "{{Text line: {}; content: {} }}".format(self.linenum, self.contents) return "{{Text line: {}; content: {} }}".format(self.linenum, self.contents)
def get_text(self):
return token_list_to_plaintext(self.contents)
def get_raw(self): def get_raw(self):
return token_list_to_raw(self.contents) 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): def token_list_to_raw(tok_list):
contents = [] contents = []
@ -1683,7 +1694,7 @@ class OrgDoc:
if headline.state: if headline.state:
state = 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() planning = headline.get_planning_line()
if planning is not None: if planning is not None:

View File

@ -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:

View File

@ -619,3 +619,18 @@ class TestSerde(unittest.TestCase):
*** Third headline *** Third headline
""".strip(), """.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',
])

View File

@ -59,7 +59,7 @@ class HL:
self.children = children self.children = children
def assert_matches(self, test_case: unittest.TestCase, doc): 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 # Check properties
if self.props is None: if self.props is None: