Support updating ranges with contents with different size.

This commit is contained in:
Sergio Martínez Portela 2020-12-20 12:14:58 +01:00
parent ebecc5feca
commit 83710a4fc1
5 changed files with 89 additions and 37 deletions

View file

@ -11,3 +11,7 @@
This is a [[https://codigoparallevar.com/1][web link]].
This is a /italized [[https://codigoparallevar.com/2][web link]]/.
This is a link with no description to [[* First level]].
This is [[id:03-markup-first-level-id][a link to a section by id]].

View file

@ -200,12 +200,19 @@ class TestSerde(unittest.TestCase):
doc = load(f)
links = list(doc.get_links())
self.assertEqual(len(links), 2)
self.assertEqual(len(links), 4)
self.assertEqual(links[0].value, "https://codigoparallevar.com/1")
self.assertEqual(links[0].description, "web link")
self.assertEqual(links[1].value, "https://codigoparallevar.com/2")
self.assertEqual(links[1].description, "web link")
self.assertEqual(links[2].value, "* First level")
self.assertEqual(links[2].description, None)
self.assertEqual(links[3].value, "id:03-markup-first-level-id")
self.assertEqual(links[3].description, "a link to a section by id")
ex = Dom(
props=[
("TITLE", "03-Links"),
@ -238,17 +245,34 @@ class TestSerde(unittest.TestCase):
),
".\n",
),
SPAN("\n"),
SPAN(
" This is a link with no description to ",
WEB_LINK(None, "* First level"),
".\n",
),
SPAN("\n"),
SPAN(
" This is ",
WEB_LINK(
"a link to a section by id",
"id:03-markup-first-level-id",
),
".\n",
),
],
)
),
)
ex.assert_matches(self, doc)
def test_update_links_file_03(self):
with open(os.path.join(DIR, "03-links.org")) as f:
doc = load(f)
links = list(doc.get_links())
self.assertEqual(len(links), 2)
self.assertEqual(len(links), 4)
self.assertEqual(links[0].value, "https://codigoparallevar.com/1")
self.assertEqual(links[0].description, "web link")
links[0].value = "https://codigoparallevar.com/1-updated"
@ -259,6 +283,16 @@ class TestSerde(unittest.TestCase):
links[1].value = "https://codigoparallevar.com/2-updated"
links[1].description = "web link #2 with update"
self.assertEqual(links[2].value, "* First level")
self.assertEqual(links[2].description, None)
links[2].value = "* Non-existent level"
links[2].description = "a description now"
self.assertEqual(links[3].value, "id:03-markup-first-level-id")
self.assertEqual(links[3].description, "a link to a section by id")
links[3].value = "id:03-markup-non-existent-level-id"
links[3].description = None
ex = Dom(
props=[
("TITLE", "03-Links"),
@ -295,6 +329,21 @@ class TestSerde(unittest.TestCase):
),
".\n",
),
SPAN("\n"),
SPAN(
" This is a link with no description to ",
WEB_LINK("a description now", "* Non-existent level"),
".\n",
),
SPAN("\n"),
SPAN(
" This is ",
WEB_LINK(
None,
"id:03-markup-non-existent-level-id",
),
".\n",
),
],
)
),

View file

@ -2,17 +2,8 @@ import collections
import unittest
from datetime import datetime
from org_dom import (
Bold,
Code,
Italic,
Line,
Strike,
Text,
Underlined,
Verbatim,
get_raw_contents,
)
from org_dom import (Bold, Code, Italic, Line, Strike, Text, Underlined,
Verbatim, get_raw_contents)
def timestamp_to_datetime(ts):
@ -209,7 +200,10 @@ class WEB_LINK:
self.link = link
def get_raw(self):
return "[[{}][{}]]".format(self.link, self.text)
if self.text:
return "[[{}][{}]]".format(self.link, self.text)
else:
return "[[{}]]".format(self.link)
def assertEqual(self, test_case, other):
test_case.assertTrue(isinstance(other, WebLink))