From cfbab91524b7f7718c264b52fcda565bb6d1ae61 Mon Sep 17 00:00:00 2001 From: Jerome Dumonteil Date: Sat, 30 Nov 2024 20:35:37 +0100 Subject: [PATCH] markdown: fix successive bold tags --- odfdo/mixin_md.py | 16 +++++++++++++--- tests/test_markdown.py | 20 ++++++++++---------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/odfdo/mixin_md.py b/odfdo/mixin_md.py index 39a7ddc..cdce858 100644 --- a/odfdo/mixin_md.py +++ b/odfdo/mixin_md.py @@ -21,12 +21,17 @@ """ from __future__ import annotations +import re from copy import deepcopy from itertools import chain from typing import Any, Callable, NamedTuple MD_GLOBAL = {} +RE_STAR6 = re.compile(r"(? None: def _strip_left_spaces(text: str) -> str: - return text.lstrip(" ") + return RE_STAR4.sub("", RE_STAR6.sub("", RE_UND2.sub("", text.lstrip(" ")))) def _md_swap_spaces(word: str) -> SplitSpace: @@ -95,11 +100,16 @@ def _md_escape(text: str | None) -> str: if not text: return "" return ( - text.replace(" ", " ") # non break space is no understood as char + text.replace(" ", r" ") # non break space is no understood as char .replace("#", r"\#") .replace(r"\*", "*") .replace("*", r"\*") + .replace(r"\_", r"_") + .replace("_", r"\_") + .replace("-", r"\-") + .replace(r"\`", "`") .replace("`", r"\`") + .replace(r"\~", "~") .replace("~", r"\~") .replace("|", r"\|") ) @@ -110,7 +120,7 @@ def _as_italic(text: str | None) -> str: if not text.strip(): return text word = _md_swap_spaces(text) - return f"{word.start}*{word.word}*{word.end}" + return f"{word.start}_{word.word}_{word.end}" def _as_bold(text: str | None) -> str: diff --git a/tests/test_markdown.py b/tests/test_markdown.py index 5a1ae9d..852226e 100755 --- a/tests/test_markdown.py +++ b/tests/test_markdown.py @@ -275,7 +275,7 @@ def test_md_list_text(document_list): Some text - une liste accentuée - - un sous-élément + - un sous\\-élément 1. une liste numérotée 2. et de deux ! @@ -354,7 +354,7 @@ def test_user_fields_text(document_uf): md = document_uf.to_markdown() expected = dedent( """\ - A document with user-field declarations. + A document with user\\-field declarations. Paris @@ -400,7 +400,7 @@ def test_md_case1_text(document_case1): # FAIL This is a paragraph with a named style. - Some list : + Some list : - item1 - item2 @@ -436,7 +436,7 @@ def test_md_case2_text(document_case2): # FAIL """\ # odfdo Test Case2 - Some style `here` and *there* + Some style `here` and _there_ Two spaces at start @@ -450,11 +450,11 @@ def test_md_case2_text(document_case2): # FAIL This is a **bold** **paragraph** with a named style. - Here **bolded space** at *begin*. + Here **bolded space** at _begin_. - Some *special* ***case*** *to* check if ~~barred~~ + Some _special_ ***case*** _to_ check if ~~barred~~ - Some list : + Some list : - item1 - **item2** @@ -473,7 +473,7 @@ def test_md_case2_text(document_case2): # FAIL 3. c - - *item6* + - _item6_ - [x] done 1. u @@ -483,7 +483,7 @@ def test_md_case2_text(document_case2): # FAIL - [ ] undone - - last paragraph *with italic* + last paragraph _with italic_ \\#\\#\\#\\# To see **\\*\\*stars\\*\\*** or \\*\\*any\\*\\* @@ -546,7 +546,7 @@ def test_md_case3_text(document_case3): # FAIL ``` another one, - with *thing* and other things + with _thing_ and other things in it tabs in same place spaces