Skip to content

Commit

Permalink
Merge pull request #38 from krassowski/dunders-and-links
Browse files Browse the repository at this point in the history
Fix multi-line links and incorrect dunder escapes in code
  • Loading branch information
krassowski committed Feb 21, 2024
2 parents 4e8f011 + 9e22770 commit f400f01
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ jobs:
matrix:
os: [ubuntu-latest]
python-version: [3.7, 3.8, 3.9, '3.10', '3.11']
include:
- os: ubuntu-20.04
python-version: 3.6
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

On the fly conversion of Python docstrings to markdown

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

Expand Down
2 changes: 1 addition & 1 deletion docstring_to_markdown/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .plain import looks_like_plain_text, plain_text_to_markdown
from .rst import looks_like_rst, rst_to_markdown

__version__ = "0.14"
__version__ = "0.15"


class UnknownFormatError(Exception):
Expand Down
8 changes: 4 additions & 4 deletions docstring_to_markdown/rst.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from abc import ABC, abstractmethod
from enum import IntEnum, auto
from types import SimpleNamespace
from typing import Union, List, Dict
from typing import Callable, Match, Union, List, Dict
import re


class Directive:
def __init__(
self, pattern: str, replacement: str,
self, pattern: str, replacement: Union[str, Callable[[Match], str]],
name: Union[str, None] = None,
flags: int = 0
):
Expand Down Expand Up @@ -249,7 +249,7 @@ def inline_markdown(self):
),
Directive(
pattern=r'`(?P<label>[^<`]+?)(\n?)<(?P<url>[^>`]+)>`_+',
replacement=r'[\g<label>](\g<url>)'
replacement=lambda m: '[' + m.group('label') + '](' + re.sub(r"\s+", "", m.group('url')) + ')'
),
Directive(
pattern=r':mod:`(?P<label>[^`]+)`',
Expand Down Expand Up @@ -316,7 +316,7 @@ def inline_markdown(self):

ESCAPING_RULES: List[Directive] = [
Directive(
pattern=r'__(?P<text>\S+)__',
pattern=r'(?<!`)__(?P<text>\S+)__(?!`)',
replacement=r'\_\_\g<text>\_\_'
)
]
Expand Down
19 changes: 19 additions & 0 deletions tests/test_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@
"[this link](https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases)."
)

RST_LINK_MULTILINE_EXAMPLE = """
See
`strftime documentation
<https://docs.python.org/3/library/datetime.html
#strftime-and-strptime-behavior>`_ for more.
"""
RST_LINK_MULTILINE_MARKDOWN = """
See
[strftime documentation](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior) for more.
"""

RST_REF_EXAMPLE = """See :ref:`here <timeseries.offset_aliases>` for a list of frequency aliases."""
RST_REF_MARKDOWN = """See here: `timeseries.offset_aliases` for a list of frequency aliases."""

Expand Down Expand Up @@ -686,6 +697,10 @@ def foo():
'rst': RST_LINK_EXAMPLE,
'md': RST_LINK_EXAMPLE_MARKDOWN
},
'converts multi-line links': {
'rst': RST_LINK_MULTILINE_EXAMPLE,
'md': RST_LINK_MULTILINE_MARKDOWN
},
'changes highlight': {
'rst': RST_HIGHLIGHTED_BLOCK,
'md': RST_HIGHLIGHTED_BLOCK_MARKDOWN
Expand Down Expand Up @@ -764,6 +779,10 @@ def foo():
'rst': '__init__',
'md': r'\_\_init\_\_'
},
'does not escape dunders in code': {
'rst': '`__init__`',
'md': '`__init__`'
},
'converts bibliographic references': {
'rst': REFERENCES,
'md': REFERENCES_MARKDOWN
Expand Down

0 comments on commit f400f01

Please sign in to comment.