From 7e944bcb3d0f837bf7fb8ce4ead5acdcc8257163 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Tue, 3 Aug 2021 23:05:15 +0200 Subject: [PATCH] Add support for markdown and link parsing on lists. --- org_rw/org_rw.py | 10 +++++++++- tests/test_org.py | 23 +++++++++++++++-------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/org_rw/org_rw.py b/org_rw/org_rw.py index e26ee01..4c180c2 100644 --- a/org_rw/org_rw.py +++ b/org_rw/org_rw.py @@ -99,6 +99,8 @@ def get_tokens(value): return value.contents if isinstance(value, RawLine): return [value.line] + if isinstance(value, list): + return value raise Exception("Unknown how to get tokens from: {}".format(value)) @@ -361,6 +363,10 @@ class Headline: for content in self.contents: yield from get_links_from_content(content) + for lst in self.getLists(): + for item in lst: + yield from get_links_from_content(item.content) + def get_lines_between(self, start, end): for line in self.contents: if start <= line.linenum < end: @@ -1551,7 +1557,9 @@ class OrgDocReader: match.group("checkbox_value"), match.group("tag_indentation"), match.group("tag"), - match.group("content"), + parse_content_block( + [RawLine(linenum=linenum, line=match.group("content"))] + ).contents, ) if len(self.headline_hierarchy) == 0: diff --git a/tests/test_org.py b/tests/test_org.py index 2a09c7c..15d68cd 100644 --- a/tests/test_org.py +++ b/tests/test_org.py @@ -4,7 +4,7 @@ import unittest from datetime import date from datetime import datetime as DT -from org_rw import Timestamp, dumps, load, loads +from org_rw import MarkerToken, MarkerType, Timestamp, dumps, load, loads from utils.assertions import (BOLD, CODE, HL, ITALIC, SPAN, STRIKE, UNDERLINED, VERBATIM, WEB_LINK, Doc, Tokens) @@ -459,13 +459,20 @@ class TestSerde(unittest.TestCase): # ... lists = hl.getLists() self.assertEqual(len(lists), 3) - self.assertEqual(lists[0][0].content, " This is a simple list.") + self.assertEqual(lists[0][0].content, [" This is a simple list."]) self.assertEqual(lists[0][0].bullet, "-") self.assertEqual( - lists[0][1].content, " This list has multiple elements, with _markup_." + lists[0][1].content, + [ + " This list has multiple elements, with ", + MarkerToken(closing=False, tok_type=MarkerType.UNDERLINED_MODE), + "markup", + MarkerToken(closing=True, tok_type=MarkerType.UNDERLINED_MODE), + ".", + ], ) - self.assertEqual(lists[1][0].content, " This is a simple list.") + self.assertEqual(lists[1][0].content, [" This is a simple list."]) self.assertEqual(lists[1][0].bullet, "+") hl2 = doc.getTopHeadlines()[1] @@ -473,19 +480,19 @@ class TestSerde(unittest.TestCase): lists2 = hl2.getLists() self.assertEqual(len(lists2), 2) - self.assertEqual(lists2[0][0].content, " First element") + self.assertEqual(lists2[0][0].content, [" First element"]) self.assertEqual(lists2[0][0].counter, "1") self.assertEqual(lists2[0][0].counter_sep, ".") - self.assertEqual(lists2[0][1].content, " Second element") + self.assertEqual(lists2[0][1].content, [" Second element"]) self.assertEqual(lists2[0][1].counter, "2") self.assertEqual(lists2[0][1].counter_sep, ".") - self.assertEqual(lists2[1][0].content, " First element") + self.assertEqual(lists2[1][0].content, [" First element"]) self.assertEqual(lists2[1][0].counter, "1") self.assertEqual(lists2[1][0].counter_sep, ")") - self.assertEqual(lists2[1][1].content, " Second element") + self.assertEqual(lists2[1][1].content, [" Second element"]) self.assertEqual(lists2[1][1].counter, "2") self.assertEqual(lists2[1][1].counter_sep, ")")