Skip to content

Commit bfabeff

Browse files
authored
Merge pull request #101 from take-cheeze/merge-md2html
markdown_to_htmlをsubtree merge
2 parents ef214f1 + 4002b5c commit bfabeff

14 files changed

Lines changed: 1683 additions & 4 deletions

.gitmodules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
[submodule "markdown_to_html"]
2-
path = markdown_to_html
3-
url = https://github.com/cpprefjp/markdown_to_html.git
41
[submodule "crsearch"]
52
path = crsearch
63
url = git@github.com:cpprefjp/crsearch.git

markdown_to_html

Lines changed: 0 additions & 1 deletion
This file was deleted.

markdown_to_html/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pyc

markdown_to_html/__init__.py

Whitespace-only changes.

markdown_to_html/commit.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
コミット構文
4+
=========================================
5+
6+
コミットIDをリンクに変換する
7+
[commit REPOSITORY_NAME, commit-id0, commit-id-2...]
8+
9+
>>> text = "[commit REPOSITORY_NAME, 1234567, abcdefg]"
10+
>>> md = markdown.Markdown(['commit'])
11+
>>> print md.convert(text)
12+
<a href="https://github.com/REPOSITORY_NAME/commit/1234567">1234567</a> <a href="https://github.com/REPOSITORY_NAME/commit/abcdefg">abcdefg</a>
13+
"""
14+
15+
import re
16+
17+
from markdown.extensions import Extension
18+
from markdown.preprocessors import Preprocessor
19+
20+
21+
def replace_commit_line(line: str) -> str:
22+
new_line: str = line
23+
for m in re.finditer(r'\[commit (.*?)\]', line.strip()):
24+
c = m[1].split(", ")
25+
repo = c[0]
26+
links: list[str] = []
27+
for id in c[1:]:
28+
id = id.strip()
29+
if len(id) == 0:
30+
continue
31+
links.append("<a href=\"https://github.com/{0}/commit/{1}\">{1}</a>".format(repo, id))
32+
commits: str = " ".join(links)
33+
new_line = new_line.replace(m[0], commits)
34+
return new_line
35+
36+
class CommitExtension(Extension):
37+
38+
def extendMarkdown(self, md):
39+
pre = CommitPreprocessor(md)
40+
41+
md.registerExtension(self)
42+
md.preprocessors.register(pre, 'commit', 25)
43+
44+
45+
class CommitPreprocessor(Preprocessor):
46+
47+
def __init__(self, md):
48+
Preprocessor.__init__(self, md)
49+
50+
def run(self, lines):
51+
new_lines = []
52+
53+
for line in lines:
54+
new_line = replace_commit_line(line)
55+
new_lines.append(new_line)
56+
57+
return new_lines
58+
59+
60+
def makeExtension(**kwargs):
61+
return CommitExtension(**kwargs)

0 commit comments

Comments
 (0)