From 9ef2a8ece619da854891dbab5d0f652ab18850a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Fri, 6 May 2022 15:54:14 +0200 Subject: [PATCH] Handle links in dom's text. --- org_rw/org_rw.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/org_rw/org_rw.py b/org_rw/org_rw.py index cf94c02..ab62c35 100644 --- a/org_rw/org_rw.py +++ b/org_rw/org_rw.py @@ -323,7 +323,40 @@ class Headline: print("Parent: {}\nValue: {}".format(current_node, line)) assert type(current_node) in NON_FINISHED_GROUPS current_node = None - tree.append(dom.Text(line)) + contents = [] + + in_link = False + in_description = False + link_value = [] + link_description = [] + + for tok in line.contents: + if isinstance(tok, LinkToken): + if tok.tok_type == LinkTokenType.OPEN_LINK: + in_link = True + open_link_token = tok + elif tok.tok_type == LinkTokenType.OPEN_DESCRIPTION: + in_description = True + elif tok.tok_type == LinkTokenType.CLOSE: + rng = RangeInRaw(content, open_link_token, tok) + contents.append(Link( + "".join(link_value), + "".join(link_description) if in_description else None, + rng, + )) + elif isinstance(tok, str) and in_link: + if in_description: + link_description.append(tok) + else: + link_value.append(tok) + else: + contents.append(tok) + tree.append(dom.Text(contents)) + in_link = False + in_description = False + link_value = [] + link_description = [] + elif isinstance(line, ListItem): if current_node is None: