Skip to content

表示崩れを事前修正するMarkdown拡張を追加 #8

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 9 commits into from
Dec 16, 2024
Merged
4 changes: 1 addition & 3 deletions commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,16 @@ def extendMarkdown(self, md, md_globals):
pre = CommitPreprocessor(md)

md.registerExtension(self)
md.preprocessors.add('commit', pre, ">normalize_whitespace")
md.preprocessors.register(pre, 'commit', 25)


class CommitPreprocessor(Preprocessor):

def __init__(self, md):
Preprocessor.__init__(self, md)
self._markdown = md

def run(self, lines):
new_lines = []
self._markdown._meta_result = {}

for line in lines:
new_line = replace_commit_line(line)
Expand Down
75 changes: 75 additions & 0 deletions fix_display_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
"""
表示崩れを事前修正
=========================================

Markdownライブラリの以下の制限を回避:

- 箇条書きの前に空行が必要な制限を回避して、自動で空行を挟む
"""

import re
import datetime

from markdown.extensions import Extension
from markdown.preprocessors import Preprocessor

def is_item_line(line: str) -> bool:
stripped_line = line.strip()
m = re.match(r'^([0-9]+\.\s)', stripped_line)
if m:
return True

m = re.match(r'^([*+-]\s)', stripped_line)
if m:
return True
return False

def is_item_end_line(line: str) -> bool:
if len(line) == 0:
return True
if re.match(r'^#+ ', line):
return True
return False

class FixDisplayErrorExtension(Extension):

def extendMarkdown(self, md, md_globals):
pre = FixDisplayErrorPreprocessor(md)

md.registerExtension(self)
md.preprocessors.register(pre, 'fix_display_error', 28)


class FixDisplayErrorPreprocessor(Preprocessor):

def __init__(self, md):
Preprocessor.__init__(self, md)

def run(self, lines):
new_lines = []

prev_line: str | None = None
in_item: bool = False
for line in lines:
if prev_line == None:
prev_line = line
new_lines.append(line)
continue

if not is_item_line(prev_line) and not in_item and is_item_line(line):
new_lines.append("")

if not in_item and is_item_line(line):
in_item = True
if in_item and is_item_end_line(line):
in_item = False

prev_line = line
new_lines.append(line)

return new_lines


def makeExtension(**kwargs):
return FixDisplayErrorExtension(**kwargs)
3 changes: 2 additions & 1 deletion footer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def extendMarkdown(self, md, md_globals):
footer = FooterTreeprocessor()
footer.config = self.getConfigs()
md.registerExtension(self)
md.treeprocessors.add('footer', footer, '_begin')
#md.treeprocessors.add('footer', footer, '_begin')
md.treeprocessors.register(footer, 'footer', 50) # top priority (begin)


class FooterTreeprocessor(markdown.treeprocessors.Treeprocessor):
Expand Down
4 changes: 1 addition & 3 deletions mark.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,16 @@ def extendMarkdown(self, md, md_globals):
markpre = MarkPreprocessor(md)

md.registerExtension(self)
md.preprocessors.add('mark', markpre, ">normalize_whitespace")
md.preprocessors.register(markpre, 'mark', 25)


class MarkPreprocessor(Preprocessor):

def __init__(self, md):
Preprocessor.__init__(self, md)
self._markdown = md

def run(self, lines):
new_lines = []
self._markdown._meta_result = {}
pattern = re.compile("|".join(map(re.escape, MARK_DICT.keys())))

for line in lines:
Expand Down
2 changes: 1 addition & 1 deletion mathjax.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def extendMarkdown(self, md, md_globals):
mathjaxpre = MathJaxPreprocessor(md)

md.registerExtension(self)
md.preprocessors.add('mathjax', mathjaxpre, ">normalize_whitespace")
md.preprocessors.register(mathjaxpre, 'mathjax', 25)


class MathJaxPreprocessor(Preprocessor):
Expand Down
4 changes: 2 additions & 2 deletions meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def extendMarkdown(self, md, md_globals):
metapost = MetaPostprocessor(md)

md.registerExtension(self)
md.preprocessors.add('meta', metapre, ">normalize_whitespace")
md.postprocessors.add('meta', metapost, '_end')
md.preprocessors.register(metapre, 'meta', 25)
md.postprocessors.register(metapost, 'meta', 0) # bottom priority (end)


class MetaPreprocessor(Preprocessor):
Expand Down
2 changes: 1 addition & 1 deletion qualified_fenced_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def extendMarkdown(self, md, md_globals):
fenced_block = QualifiedFencedBlockPreprocessor(md, self.global_qualify_list)
md.registerExtension(self)

md.preprocessors.add('qualified_fenced_code', fenced_block, ">normalize_whitespace")
md.preprocessors.register(fenced_block, 'qualified_fenced_code', 29)


def _make_random_string():
Expand Down
4 changes: 1 addition & 3 deletions sponsor.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,16 @@ def extendMarkdown(self, md, md_globals):
pre = SponsorPreprocessor(md)

md.registerExtension(self)
md.preprocessors.add('sponsor', pre, ">normalize_whitespace")
md.preprocessors.register(pre, 'sponsor', 25)


class SponsorPreprocessor(Preprocessor):

def __init__(self, md):
Preprocessor.__init__(self, md)
self._markdown = md

def run(self, lines):
new_lines = []
self._markdown._meta_result = {}

jst = datetime.timezone(datetime.timedelta(hours=+9), 'JST')
now = datetime.datetime.now(jst)
Expand Down