Skip to content
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

update markdown-it-py extension #92

Merged
merged 1 commit into from
Feb 28, 2024
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies = [
"dynaconf",
"httpx",
"markdownify", # convert pre-formatted HTML from Legifrance to markdown for skeletons
"markdown-it-py[plugins]",
"markdown-it-py[plugins]>=3.0.0",
"mdformat>=0.7.16",
"more-itertools",
"typer[all]>=0.9.0",
Expand Down
30 changes: 13 additions & 17 deletions src/catleg/markdown_it/heading_extension.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
""" Atex heading (#, ##, ...) """
from __future__ import annotations

import logging

from itertools import islice, repeat

from markdown_it import MarkdownIt
from markdown_it.common.utils import isSpace

from markdown_it.common.utils import isStrSpace
from markdown_it.rules_block.state_block import StateBlock

"""
Expand Down Expand Up @@ -39,45 +40,40 @@
SOFTWARE.
"""

""" Atex heading (#, ##, ...) """

LOGGER = logging.getLogger(__name__)
HEADING_LIMIT = 20


def more_heading(state: StateBlock, startLine: int, endLine: int, silent: bool):
def more_heading(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool:
LOGGER.debug("entering heading: %s, %s, %s, %s", state, startLine, endLine, silent)

pos = state.bMarks[startLine] + state.tShift[startLine]
maximum = state.eMarks[startLine]

# if it's indented more than 3 spaces, it should be a code block
if state.sCount[startLine] - state.blkIndent >= 4:
if state.is_code_block(startLine):
return False

ch: int | None = state.srcCharCode[pos]
ch: str | None = state.src[pos]

# /* # */
if ch != 0x23 or pos >= maximum:
if ch != "#" or pos >= maximum:
return False

# count heading level
level = 1
pos += 1
try:
ch = state.srcCharCode[pos]
ch = state.src[pos]
except IndexError:
ch = None
# /* # */
while ch == 0x23 and pos < maximum and level <= HEADING_LIMIT:
while ch == "#" and pos < maximum and level <= HEADING_LIMIT:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks better :)

level += 1
pos += 1
try:
ch = state.srcCharCode[pos]
ch = state.src[pos]
except IndexError:
ch = None

if level > HEADING_LIMIT or (pos < maximum and not isSpace(ch)):
if level > HEADING_LIMIT or (pos < maximum and not isStrSpace(ch)):
return False

if silent:
Expand All @@ -86,8 +82,8 @@ def more_heading(state: StateBlock, startLine: int, endLine: int, silent: bool):
# Let's cut tails like ' ### ' from the end of string

maximum = state.skipSpacesBack(maximum, pos)
tmp = state.skipCharsBack(maximum, 0x23, pos) # #
if tmp > pos and isSpace(state.srcCharCode[tmp - 1]):
tmp = state.skipCharsStrBack(maximum, "#", pos)
if tmp > pos and isStrSpace(state.src[tmp - 1]):
maximum = tmp

state.line = startLine + 1
Expand Down