Skip to content

Commit f400f01

Browse files
authored
Merge pull request #38 from krassowski/dunders-and-links
Fix multi-line links and incorrect dunder escapes in code
2 parents 4e8f011 + 9e22770 commit f400f01

File tree

5 files changed

+25
-9
lines changed

5 files changed

+25
-9
lines changed

.github/workflows/tests.yml

-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ jobs:
1212
matrix:
1313
os: [ubuntu-latest]
1414
python-version: [3.7, 3.8, 3.9, '3.10', '3.11']
15-
include:
16-
- os: ubuntu-20.04
17-
python-version: 3.6
1815
runs-on: ${{ matrix.os }}
1916
steps:
2017
- uses: actions/checkout@v2

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
On the fly conversion of Python docstrings to markdown
88

9-
- Python 3.6+
9+
- Python 3.6+ (tested on 3.7 up to 3.11)
1010
- can recognise reStructuredText and convert multiple of its features to Markdown
1111
- since v0.13 includes initial support for Google-formatted docstrings
1212

docstring_to_markdown/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from .plain import looks_like_plain_text, plain_text_to_markdown
44
from .rst import looks_like_rst, rst_to_markdown
55

6-
__version__ = "0.14"
6+
__version__ = "0.15"
77

88

99
class UnknownFormatError(Exception):

docstring_to_markdown/rst.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from abc import ABC, abstractmethod
22
from enum import IntEnum, auto
33
from types import SimpleNamespace
4-
from typing import Union, List, Dict
4+
from typing import Callable, Match, Union, List, Dict
55
import re
66

77

88
class Directive:
99
def __init__(
10-
self, pattern: str, replacement: str,
10+
self, pattern: str, replacement: Union[str, Callable[[Match], str]],
1111
name: Union[str, None] = None,
1212
flags: int = 0
1313
):
@@ -249,7 +249,7 @@ def inline_markdown(self):
249249
),
250250
Directive(
251251
pattern=r'`(?P<label>[^<`]+?)(\n?)<(?P<url>[^>`]+)>`_+',
252-
replacement=r'[\g<label>](\g<url>)'
252+
replacement=lambda m: '[' + m.group('label') + '](' + re.sub(r"\s+", "", m.group('url')) + ')'
253253
),
254254
Directive(
255255
pattern=r':mod:`(?P<label>[^`]+)`',
@@ -316,7 +316,7 @@ def inline_markdown(self):
316316

317317
ESCAPING_RULES: List[Directive] = [
318318
Directive(
319-
pattern=r'__(?P<text>\S+)__',
319+
pattern=r'(?<!`)__(?P<text>\S+)__(?!`)',
320320
replacement=r'\_\_\g<text>\_\_'
321321
)
322322
]

tests/test_rst.py

+19
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@
7878
"[this link](https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases)."
7979
)
8080

81+
RST_LINK_MULTILINE_EXAMPLE = """
82+
See
83+
`strftime documentation
84+
<https://docs.python.org/3/library/datetime.html
85+
#strftime-and-strptime-behavior>`_ for more.
86+
"""
87+
RST_LINK_MULTILINE_MARKDOWN = """
88+
See
89+
[strftime documentation](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior) for more.
90+
"""
91+
8192
RST_REF_EXAMPLE = """See :ref:`here <timeseries.offset_aliases>` for a list of frequency aliases."""
8293
RST_REF_MARKDOWN = """See here: `timeseries.offset_aliases` for a list of frequency aliases."""
8394

@@ -686,6 +697,10 @@ def foo():
686697
'rst': RST_LINK_EXAMPLE,
687698
'md': RST_LINK_EXAMPLE_MARKDOWN
688699
},
700+
'converts multi-line links': {
701+
'rst': RST_LINK_MULTILINE_EXAMPLE,
702+
'md': RST_LINK_MULTILINE_MARKDOWN
703+
},
689704
'changes highlight': {
690705
'rst': RST_HIGHLIGHTED_BLOCK,
691706
'md': RST_HIGHLIGHTED_BLOCK_MARKDOWN
@@ -764,6 +779,10 @@ def foo():
764779
'rst': '__init__',
765780
'md': r'\_\_init\_\_'
766781
},
782+
'does not escape dunders in code': {
783+
'rst': '`__init__`',
784+
'md': '`__init__`'
785+
},
767786
'converts bibliographic references': {
768787
'rst': REFERENCES,
769788
'md': REFERENCES_MARKDOWN

0 commit comments

Comments
 (0)