Skip to content

Commit

Permalink
Merge branch 'master' into task-list-gfm-compliancy
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell authored Dec 5, 2022
2 parents edec998 + 277229c commit f36de50
Show file tree
Hide file tree
Showing 35 changed files with 702 additions and 140 deletions.
21 changes: 11 additions & 10 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ jobs:

runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: [pypy3, 3.6, 3.7, 3.8, 3.9]
python-version: ['pypy-3.7', '3.7', '3.8', '3.9', '3.10']

steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -64,13 +65,13 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Build package
python-version: "3.8"
- name: install flit
run: |
pip install build
python -m build
- name: Publish
uses: pypa/gh-action-pypi-publish@v1.1.0
with:
user: __token__
password: ${{ secrets.PYPI_KEY }}
pip install flit~=3.4
- name: Build and publish
run: |
flit publish
env:
FLIT_USERNAME: __token__
FLIT_PASSWORD: ${{ secrets.PYPI_KEY }}
28 changes: 7 additions & 21 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,31 @@ exclude: >
repos:

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.3.0
rev: v4.3.0
hooks:
- id: check-json
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: https://github.com/mgedmin/check-manifest
rev: "0.46"
hooks:
- id: check-manifest
args: [--no-build-isolation]
additional_dependencies: [setuptools>=46.4.0]

# this is not used for now,
# since it converts mdit-py-plugins to mdit_py_plugins and removes comments
# - repo: https://github.com/asottile/setup-cfg-fmt
# rev: v1.17.0
# hooks:
# - id: setup-cfg-fmt

- repo: https://github.com/timothycrosley/isort
rev: 5.8.0
rev: 5.10.1
hooks:
- id: isort

- repo: https://github.com/psf/black
rev: 20.8b1
rev: 22.8.0
hooks:
- id: black

- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.1
- repo: https://github.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
additional_dependencies: [flake8-bugbear==21.3.1]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.812
rev: v0.971
hooks:
- id: mypy
additional_dependencies: [markdown-it-py~=1.0]
additional_dependencies: [markdown-it-py~=2.0]
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log

## 0.3.1 - 2022-09-27

- ⬆️ UPGRADE: Drop Python 3.6, support Python 3.10
- 🐛 FIX: Parsing when newline is between footnote ID and first paragraph
- 🐛 FIX: Anchor ids in separate renders no longer affect each other.
- ✨ NEW: Add `attrs_plugin`
- 🔧 MAINTAIN: Use flit PEP 621 package build

## 0.3.0 - 2021-12-03

- ⬆️ UPGRADE: Compatible with markdown-it-py `v2`.
Expand Down
17 changes: 0 additions & 17 deletions MANIFEST.in

This file was deleted.

2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ coverage:
status:
project:
default:
target: 93%
target: 92%
threshold: 0.2%
patch:
default:
Expand Down
6 changes: 6 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ html_string = md.render("some *Markdown*")
.. autofunction:: mdit_py_plugins.container.container_plugin
```

## Inline Attributes

```{eval-rst}
.. autofunction:: mdit_py_plugins.attrs.attrs_plugin
```

## Math

```{eval-rst}
Expand Down
2 changes: 1 addition & 1 deletion mdit_py_plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.3.0"
__version__ = "0.3.1"
3 changes: 1 addition & 2 deletions mdit_py_plugins/anchors/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ def _make_anchors_func(
permalinkBefore: bool,
permalinkSpace: bool,
):
slugs: Set[str] = set()

def _anchor_func(state: StateCore):
slugs: Set[str] = set()
for (idx, token) in enumerate(state.tokens):
if token.type != "heading_open":
continue
Expand Down
1 change: 1 addition & 0 deletions mdit_py_plugins/attrs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .index import attrs_plugin # noqa: F401
118 changes: 118 additions & 0 deletions mdit_py_plugins/attrs/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
from typing import List, Optional

from markdown_it import MarkdownIt
from markdown_it.rules_inline import StateInline
from markdown_it.token import Token

from .parse import ParseError, parse


def attrs_plugin(
md: MarkdownIt,
*,
after=("image", "code_inline", "link_close", "span_close"),
spans=True,
):
"""Parse inline attributes that immediately follow certain inline elements::
![alt](https://image.com){#id .a b=c}
This syntax is inspired by
`Djot spans
<https://htmlpreview.github.io/?https://github.com/jgm/djot/blob/master/doc/syntax.html#inline-attributes>`_.
Inside the curly braces, the following syntax is possible:
- `.foo` specifies foo as a class.
Multiple classes may be given in this way; they will be combined.
- `#foo` specifies foo as an identifier.
An element may have only one identifier;
if multiple identifiers are given, the last one is used.
- `key="value"` or `key=value` specifies a key-value attribute.
Quotes are not needed when the value consists entirely of
ASCII alphanumeric characters or `_` or `:` or `-`.
Backslash escapes may be used inside quoted values.
- `%` begins a comment, which ends with the next `%` or the end of the attribute (`}`).
Multiple attribute blocks are merged.
:param md: The MarkdownIt instance to modify.
:param after: The names of inline elements after which attributes may be specified.
This plugin does not support attributes after emphasis, strikethrough or text elements,
which all require post-parse processing.
:param spans: If True, also parse attributes after spans of text, encapsulated by `[]`.
Note Markdown link references take precedence over this syntax.
"""

def _attr_rule(state: StateInline, silent: bool):
if state.pending or not state.tokens:
return False
token = state.tokens[-1]
if token.type not in after:
return False
try:
new_pos, attrs = parse(state.src[state.pos :])
except ParseError:
return False
token_index = _find_opening(state.tokens, len(state.tokens) - 1)
if token_index is None:
return False
state.pos += new_pos + 1
if not silent:
attr_token = state.tokens[token_index]
if "class" in attrs and "class" in token.attrs:
attrs["class"] = f"{attr_token.attrs['class']} {attrs['class']}"
attr_token.attrs.update(attrs)
return True

if spans:
md.inline.ruler.after("link", "span", _span_rule)
md.inline.ruler.push("attr", _attr_rule)


def _find_opening(tokens: List[Token], index: int) -> Optional[int]:
"""Find the opening token index, if the token is closing."""
if tokens[index].nesting != -1:
return index
level = 0
while index >= 0:
level += tokens[index].nesting
if level == 0:
return index
index -= 1
return None


def _span_rule(state: StateInline, silent: bool):
if state.srcCharCode[state.pos] != 0x5B: # /* [ */
return False

maximum = state.posMax
labelStart = state.pos + 1
labelEnd = state.md.helpers.parseLinkLabel(state, state.pos, False)

# parser failed to find ']', so it's not a valid span
if labelEnd < 0:
return False

pos = labelEnd + 1

try:
new_pos, attrs = parse(state.src[pos:])
except ParseError:
return False

pos += new_pos + 1

if not silent:
state.pos = labelStart
state.posMax = labelEnd
token = state.push("span_open", "span", 1)
token.attrs = attrs
state.md.inline.tokenize(state)
token = state.push("span_close", "span", -1)

state.pos = pos
state.posMax = maximum
return True
Loading

0 comments on commit f36de50

Please sign in to comment.