Skip to content

Complete support for RST admonitions #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 76 additions & 6 deletions docstring_to_markdown/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,75 @@ def __init__(
]


class Admonition:
def __init__(self, name: str, label: str, icon: str = ''):
self.name = name
self.label = label
self.icon = icon

@property
def block_markdown(self):
return f'{self.icon} **{self.label}**'

@property
def inline_markdown(self):
return self.block_markdown + ':'


ADMONITIONS = [
Admonition(
name='caution',
label='Caution',
icon='⚠️ '
),
Admonition(
name='attention',
label='Attention',
icon='⚠️ '
),
Admonition(
name='danger',
label='Danger',
icon='⚠️ '
),
Admonition(
name='hint',
label='Hint',
icon='🛈'
),
Admonition(
name='important',
label='Important',
icon='⚠️ '
),
Admonition(
name='note',
label='Note',
icon='🛈'
),
Admonition(
name='tip',
label='Tip',
icon='🛈'
),
Admonition(
name='warning',
label='Warning',
icon='⚠️ '
)
]


ADMONITION_DIRECTIVES: List[Directive] = [
# https://docutils.sourceforge.io/docs/ref/rst/directives.html#admonitions
Directive(
pattern=rf'\.\. {admonition.name}::',
replacement=admonition.inline_markdown
)
for admonition in ADMONITIONS
]


RST_DIRECTIVES: List[Directive] = [
Directive(
pattern=r'\.\. versionchanged:: (?P<version>\S+)(?P<end>$|\n)',
Expand All @@ -169,10 +238,7 @@ def __init__(
pattern=r'\.\. deprecated:: (?P<version>\S+)(?P<end>$|\n)',
replacement=r'*Deprecated since \g<version>*\g<end>'
),
Directive(
pattern=r'\.\. warning::',
replacement=r'**Warning**:'
),
*ADMONITION_DIRECTIVES,
Directive(
pattern=r'\.\. seealso::(?P<short_form>.*)(?P<end>$|\n)',
replacement=r'*See also*\g<short_form>\g<end>'
Expand Down Expand Up @@ -604,13 +670,17 @@ def initiate_parsing(self, line: str, current_language: str):

class NoteBlockParser(IndentedBlockParser):
enclosure = '\n---'
directives = {'.. note::', '.. warning::'}
directives = {
f'.. {admonition.name}::': admonition
for admonition in ADMONITIONS
}

def can_parse(self, line: str):
return line.strip() in self.directives

def initiate_parsing(self, line: str, current_language: str):
self._start_block('\n**Note**\n' if 'note' in line else '\n**Warning**\n')
admonition = self.directives[line.strip()]
self._start_block(f'\n{admonition.block_markdown}\n')
return IBlockBeginning(remainder='')


Expand Down
10 changes: 5 additions & 5 deletions tests/test_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def func(): pass


---
**Note**
🛈 **Note**

The `chararray` class exists for backwards compatibility with
Numarray, it is not recommended for new development.
Expand Down Expand Up @@ -399,7 +399,7 @@ def func(): pass


---
**Warning**
⚠️ **Warning**

Loading pickled data received from untrusted sources can be
unsafe.
Expand All @@ -421,7 +421,7 @@ def func(): pass
LINE_WARNING_MARKDOWN = """
Create a view into the array with the given shape and strides.

**Warning**: This function has to be used with extreme care, see notes.
⚠️ **Warning**: This function has to be used with extreme care, see notes.

Parameters
"""
Expand Down Expand Up @@ -462,7 +462,7 @@ def func(): pass
"""

SIMPLE_TABLE_MARKDOWN = """
**Warning**: This is not a standard simple table
⚠️ **Warning**: This is not a standard simple table

| Character | Meaning |
| --------- | --------------------------------------------------------------- |
Expand All @@ -483,7 +483,7 @@ def func(): pass
"""

SIMPLE_TABLE_2_MARKDOWN = """
**Warning**: This is a standard simple table
⚠️ **Warning**: This is a standard simple table

| A | B | A and B |
| ----- | ----- | ------- |
Expand Down