Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
- Improve test coverage
- Cleanup some API code
  • Loading branch information
frnmst committed Apr 14, 2024
1 parent 89e2d60 commit 5c7ddab
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 141 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: 'v4.5.0'
rev: 'v4.6.0'
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
Expand Down Expand Up @@ -49,7 +49,7 @@ repos:
additional_dependencies: ['flake8-docstrings']

- repo: https://github.com/PyCQA/bandit
rev: '1.7.7'
rev: '1.7.8'
hooks:
- id: bandit
args: ['-c', 'pyproject.toml', '--level', 'LOW']
Expand All @@ -61,7 +61,7 @@ repos:
- id: isort

- repo: https://codeberg.org/frnmst/md-toc
rev: '8.2.3'
rev: '9.0.0'
hooks:
- id: md-toc
args: ['-p', 'cmark', '-l6']
Expand Down
87 changes: 37 additions & 50 deletions md_toc/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def write_strings_on_files_between_markers(
:rtype: bool
:raises: an fpyutils exception or a built-in exception.
"""
if not len(filenames) == len(strings):
if len(filenames) != len(strings):
raise ValueError

file_id: int = 0
Expand Down Expand Up @@ -264,7 +264,7 @@ def build_toc(
# Help the developers: override the list_marker in case
# this function is called with the default unordered list marker,
# for example like this:
# print(md_toc.build_toc('test.md', ordered=True))
# print(md_toc.api.build_toc('test.md', ordered=True))
# This avoids an AssertionError later on.
if (ordered and list_marker
== md_parser[parser]['list']['unordered']['default_marker']):
Expand All @@ -282,10 +282,17 @@ def build_toc(
loop: bool = True
line_counter = 1
while loop:
if line_counter > skip_lines or f.readline() == '':
loop = False
line_counter += 1
try:
if line_counter > skip_lines or f.readline() == '':
loop = False
line_counter += 1
except UnicodeDecodeError:
return ''.join([
'<!--stop reading ', filename,
': probably a binary file-->'
])

# Read next line after possible skips.
try:
line = f.readline()
except UnicodeDecodeError:
Expand All @@ -294,9 +301,9 @@ def build_toc(

indentation_log: dict[types.IndentationLogElement] = init_indentation_log(
parser, list_marker)
is_within_code_fence = False
is_within_code_fence: bool = False
code_fence = None
is_document_end = False
is_document_end: bool = False
while line:
# Document ending detection.
#
Expand Down Expand Up @@ -532,9 +539,7 @@ def increase_index_ordered_list(
:raises: GithubOverflowOrderedListMarker or a built-in exception.
"""
# header_type_prev might be 0 while header_type_curr can't.
if not header_type_prev >= 0:
raise ValueError
if not header_type_curr >= 1:
if not (header_type_prev >= 0 or header_type_curr >= 1):
raise ValueError

# Base cases for a new table of contents or a new index type.
Expand Down Expand Up @@ -624,29 +629,24 @@ def compute_toc_line_indentation_spaces(
.. warning:: In case of ordered TOCs you must explicitly pass one of the
supported ordered list markers.
"""
if not header_type_curr >= 1:
raise ValueError
if not header_type_prev >= 0:
raise ValueError
if parser in ['github', 'cmark', 'gitlab', 'commonmarker', 'goldmark']:
if not len(indentation_log
) == md_parser['github']['header']['max_levels']:
if (header_type_curr >= 1 and header_type_prev >= 0 and index >= 1):
if (parser
in ['github', 'cmark', 'gitlab', 'commonmarker', 'goldmark']
and len(indentation_log) !=
md_parser[parser]['header']['max_levels']):
raise ValueError
if not index >= 1:
raise ValueError

if parser in [
'github', 'cmark', 'gitlab', 'commonmarker', 'goldmark',
'redcarpet'
]:
if ordered:
if list_marker not in md_parser[parser]['list']['ordered'][
'closing_markers']:
if (parser in [
'github', 'cmark', 'gitlab', 'commonmarker', 'goldmark',
'redcarpet'
]):
if ordered and list_marker not in md_parser[parser]['list'][
'ordered']['closing_markers']:
raise ValueError
else:
if list_marker not in md_parser[parser]['list']['unordered'][
'bullet_markers']:
elif not ordered and list_marker not in md_parser[parser]['list'][
'unordered']['bullet_markers']:
raise ValueError
else:
raise ValueError

if parser in ['github', 'cmark', 'gitlab', 'commonmarker', 'goldmark']:
index_length: int
Expand Down Expand Up @@ -799,8 +799,7 @@ def build_toc_line(
if not no_of_indentation_spaces >= 0:
raise ValueError

indentation: str = no_of_indentation_spaces * ' '
return ''.join([indentation, toc_line_no_indent])
return ''.join([no_of_indentation_spaces * ' ', toc_line_no_indent])


def remove_html_tags(line: str, parser: str = 'github') -> str:
Expand Down Expand Up @@ -850,10 +849,7 @@ def remove_emphasis(line: str, parser: str = 'github') -> str:
>>> md_toc.api.remove_emphasis('__my string__ *is this* one')
'my string is this one'
"""
if parser in [
'github', 'cmark', 'gitlab', 'commonmarker', 'goldmark',
'redcarpet'
]:
if parser in ['github', 'cmark', 'gitlab', 'commonmarker', 'goldmark']:
mem = None
refmap = references_h._cmarkCmarkReferenceMap()

Expand Down Expand Up @@ -1064,8 +1060,6 @@ def build_anchor_link(

return header_text_trimmed_middle_stage_str

return None


def replace_and_split_newlines(line: str) -> list[str]:
r"""Replace all the newline characters with line feeds and separate the components.
Expand Down Expand Up @@ -1434,8 +1428,6 @@ def is_valid_code_fence_indent(line: str, parser: str = 'github') -> bool:
# TODO.
return False

return False


def is_opening_code_fence(line: str, parser: str = 'github') -> str | None:
r"""Determine if the given line is possibly the opening of a fenced code block.
Expand Down Expand Up @@ -1510,8 +1502,6 @@ def is_opening_code_fence(line: str, parser: str = 'github') -> str | None:
# TODO.
return None

return None


def is_closing_code_fence(
line: str,
Expand Down Expand Up @@ -1551,20 +1541,19 @@ def is_closing_code_fence(
False
"""
if parser in ['github', 'cmark', 'gitlab', 'commonmarker', 'goldmark']:
markers = md_parser['github']['code_fence']['marker']
marker_min_length = md_parser['github']['code_fence'][
'min_marker_characters']

if not is_valid_code_fence_indent(line):
return False

# Remove opening fence indentation after it is known to be valid.
fence = fence.lstrip(' ')
# Check if fence uses valid characters.
if not fence.startswith((markers['backtick'], markers['tilde'])):
if not fence.startswith(
(md_parser[parser]['code_fence']['marker']['backtick'],
md_parser[parser]['code_fence']['marker']['tilde'])):
return False

if len(fence) < marker_min_length:
if len(fence
) < md_parser[parser]['code_fence']['min_marker_characters']:
return False

# Additional security.
Expand Down Expand Up @@ -1618,8 +1607,6 @@ def is_closing_code_fence(
# TODO.
return False

return False


if __name__ == '__main__':
pass
Loading

0 comments on commit 5c7ddab

Please sign in to comment.