From e019265aa124457b991c4cb5096d3295c6b16065 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Wed, 21 Dec 2022 01:49:53 +0000 Subject: [PATCH] org_tools: support escaping percents in templates via \% also add a regression test addresses https://github.com/karlicoss/grasp/issues/35 https://github.com/karlicoss/grasp/pull/43 Thanks @Chickensoupwithrice for spotting and fix attempt --- server/org_tools.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/server/org_tools.py b/server/org_tools.py index 7dc4756..ac9332d 100644 --- a/server/org_tools.py +++ b/server/org_tools.py @@ -66,7 +66,9 @@ Additionally, supports :selection, :comment and :tags expansions. You can look at `test_templates` for some specific examples. """ - py_template = re.sub(r'%[:]?([?\w]+)', r'{\1}', org_template) + py_template = re.sub(r'([^\\])%[:]?([?\w]+)', r'\1{\2}', org_template) + # replace all escaped % with regular %, safe to do after substitution + py_template = py_template.replace(r'\%', '%') NOW = datetime.now() if _now is None else _now org_date = date2org(NOW) @@ -116,6 +118,7 @@ def test_templates() -> None: comment = 'fafewfewf' tags = ['aba', 'caba'] + vimzettel_template = r'zettel \%title [%:description] testing' # https://orgmode.org/guide/Capture-templates.html org_templates = [ DEFAULT_TEMPLATE, @@ -135,6 +138,7 @@ def test_templates() -> None: %:selection #+END_QUOTE """, + vimzettel_template, ] # check they aren't crashing for org_template in org_templates: @@ -170,3 +174,13 @@ Comment: fafewfewf '''.lstrip() assert res == expected + + res = as_org( + url, + title, + selection, + comment, + tags, + org_template=vimzettel_template, + ) + assert res == 'zettel %title [hello] testing'