Skip to content

Commit

Permalink
markdown: fix successive bold tags
Browse files Browse the repository at this point in the history
  • Loading branch information
jdum committed Nov 30, 2024
1 parent 0fc0a57 commit cfbab91
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
16 changes: 13 additions & 3 deletions odfdo/mixin_md.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"(?<!\\)(\*{6})")
RE_STAR4 = re.compile(r"(?<!\\)(\*{4})")
RE_UND2 = re.compile(r"(?<!\\)(_{2})")


class LIStyle(NamedTuple):
name: str
Expand Down Expand Up @@ -74,7 +79,7 @@ def _release_list_counter(level: int) -> 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:
Expand All @@ -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"\|")
)
Expand All @@ -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:
Expand Down
20 changes: 10 additions & 10 deletions tests/test_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 !
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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**
Expand All @@ -473,7 +473,7 @@ def test_md_case2_text(document_case2): # FAIL
3. c
- *item6*
- _item6_
- [x] done
1. u
Expand All @@ -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\\*\\*
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cfbab91

Please sign in to comment.