|
1 | | -import re |
| 1 | +import enum |
2 | 2 | import os |
| 3 | +import re |
3 | 4 | import shlex |
4 | 5 | import textwrap |
5 | 6 | from dataclasses import dataclass |
6 | 7 | from typing import List |
7 | 8 |
|
| 9 | +import mkdocs |
8 | 10 | from mkdocs.plugins import BasePlugin |
9 | | -from codeinclude.resolver import select |
| 11 | + |
10 | 12 | from codeinclude.languages import get_lang_class |
| 13 | +from codeinclude.resolver import select |
11 | 14 |
|
12 | 15 | RE_START = r"""(?x) |
13 | 16 | ^ |
|
35 | 38 | """ |
36 | 39 |
|
37 | 40 |
|
38 | | -class CodeIncludePlugin(BasePlugin): |
39 | | - def on_page_markdown(self, markdown, page, config, site_navigation=None, **kwargs): |
40 | | - "Provide a hook for defining functions from an external module" |
41 | | - |
42 | | - blocks = find_code_include_blocks(markdown) |
43 | | - substitutes = get_substitutes(blocks, page) |
44 | | - return substitute(markdown, substitutes) |
45 | | - |
46 | | - |
47 | 41 | @dataclass |
48 | 42 | class CodeIncludeBlock(object): |
49 | 43 | first_line_index: int |
50 | 44 | last_line_index: int |
51 | 45 | content: str |
52 | 46 |
|
53 | 47 |
|
54 | | -def find_code_include_blocks(markdown: str) -> List[CodeIncludeBlock]: |
55 | | - ci_blocks = list() |
56 | | - first = -1 |
57 | | - in_block = False |
58 | | - lines = markdown.splitlines() |
59 | | - for index, line in enumerate(lines): |
60 | | - if re.match(RE_START, lines[index]): |
61 | | - if in_block: |
62 | | - raise ValueError( |
63 | | - f"Found two consecutive code-include starts: at lines {first} and {index}" |
64 | | - ) |
65 | | - first = index |
66 | | - in_block = True |
67 | | - elif re.match(RE_END, lines[index]): |
68 | | - if not in_block: |
69 | | - raise ValueError( |
70 | | - f"Found code-include end without preceding start at line {index}" |
71 | | - ) |
72 | | - last = index |
73 | | - content = "\n".join(lines[first : last + 1]) |
74 | | - ci_blocks.append(CodeIncludeBlock(first, last, content)) |
75 | | - in_block = False |
76 | | - return ci_blocks |
77 | | - |
78 | | - |
79 | 48 | @dataclass |
80 | 49 | class Replacement(object): |
81 | 50 | first_line_index: int |
82 | 51 | last_line_index: int |
83 | 52 | content: str |
84 | 53 |
|
85 | 54 |
|
86 | | -def get_substitutes(blocks: List[CodeIncludeBlock], page) -> List[Replacement]: |
87 | | - replacements = list() |
88 | | - for ci_block in blocks: |
89 | | - replacement_content = "" |
90 | | - for snippet_match in re.finditer(RE_SNIPPET, ci_block.content): |
91 | | - title = snippet_match.group("title") |
92 | | - filename = snippet_match.group("filename") |
93 | | - indent = snippet_match.group("leading_space") |
94 | | - raw_params = snippet_match.group("params") |
95 | | - |
96 | | - if raw_params: |
97 | | - params = dict(token.split(":") for token in shlex.split(raw_params)) |
98 | | - lines = params.get("lines", "") |
99 | | - block = params.get("block", "") |
100 | | - inside_block = params.get("inside_block", "") |
101 | | - else: |
102 | | - lines = "" |
103 | | - block = "" |
104 | | - inside_block = "" |
| 55 | +class CodeIncludePlugin(BasePlugin): |
105 | 56 |
|
106 | | - code_block = get_substitute( |
107 | | - page, title, filename, lines, block, inside_block |
108 | | - ) |
109 | | - # re-indent |
110 | | - code_block = re.sub("^", indent, code_block, flags=re.MULTILINE) |
| 57 | + config_scheme = ( |
| 58 | + ( |
| 59 | + "title_mode", |
| 60 | + mkdocs.config.config_options.Choice( |
| 61 | + choices=["none", "legacy_pymdownx.superfences", "pymdownx.tabbed"], |
| 62 | + default="pymdownx.tabbed", |
| 63 | + ), |
| 64 | + ), |
| 65 | + ) |
111 | 66 |
|
112 | | - replacement_content += code_block |
113 | | - replacements.append( |
114 | | - Replacement( |
115 | | - ci_block.first_line_index, ci_block.last_line_index, replacement_content |
| 67 | + def on_page_markdown(self, markdown, page, config, site_navigation=None, **kwargs): |
| 68 | + """Provide a hook for defining functions from an external module""" |
| 69 | + |
| 70 | + blocks = self.find_code_include_blocks(markdown) |
| 71 | + substitutes = self.get_substitutes(blocks, page) |
| 72 | + return self.substitute(markdown, substitutes) |
| 73 | + |
| 74 | + def find_code_include_blocks(self, markdown: str) -> List[CodeIncludeBlock]: |
| 75 | + ci_blocks = list() |
| 76 | + first = -1 |
| 77 | + in_block = False |
| 78 | + lines = markdown.splitlines() |
| 79 | + for index, line in enumerate(lines): |
| 80 | + if re.match(RE_START, lines[index]): |
| 81 | + if in_block: |
| 82 | + raise ValueError( |
| 83 | + f"Found two consecutive code-include starts: at lines {first} and {index}" |
| 84 | + ) |
| 85 | + first = index |
| 86 | + in_block = True |
| 87 | + elif re.match(RE_END, lines[index]): |
| 88 | + if not in_block: |
| 89 | + raise ValueError( |
| 90 | + f"Found code-include end without preceding start at line {index}" |
| 91 | + ) |
| 92 | + last = index |
| 93 | + content = "\n".join(lines[first : last + 1]) |
| 94 | + ci_blocks.append(CodeIncludeBlock(first, last, content)) |
| 95 | + in_block = False |
| 96 | + return ci_blocks |
| 97 | + |
| 98 | + def get_substitutes( |
| 99 | + self, blocks: List[CodeIncludeBlock], page |
| 100 | + ) -> List[Replacement]: |
| 101 | + replacements = list() |
| 102 | + for ci_block in blocks: |
| 103 | + replacement_content = "" |
| 104 | + for snippet_match in re.finditer(RE_SNIPPET, ci_block.content): |
| 105 | + title = snippet_match.group("title") |
| 106 | + filename = snippet_match.group("filename") |
| 107 | + indent = snippet_match.group("leading_space") |
| 108 | + raw_params = snippet_match.group("params") |
| 109 | + |
| 110 | + if raw_params: |
| 111 | + params = dict(token.split(":") for token in shlex.split(raw_params)) |
| 112 | + lines = params.get("lines", "") |
| 113 | + block = params.get("block", "") |
| 114 | + inside_block = params.get("inside_block", "") |
| 115 | + else: |
| 116 | + lines = "" |
| 117 | + block = "" |
| 118 | + inside_block = "" |
| 119 | + |
| 120 | + code_block = self.get_substitute( |
| 121 | + page, title, filename, lines, block, inside_block |
| 122 | + ) |
| 123 | + # re-indent |
| 124 | + code_block = re.sub("^", indent, code_block, flags=re.MULTILINE) |
| 125 | + |
| 126 | + replacement_content += code_block |
| 127 | + replacements.append( |
| 128 | + Replacement( |
| 129 | + ci_block.first_line_index, |
| 130 | + ci_block.last_line_index, |
| 131 | + replacement_content, |
| 132 | + ) |
116 | 133 | ) |
| 134 | + return replacements |
| 135 | + |
| 136 | + def get_substitute(self, page, title, filename, lines, block, inside_block): |
| 137 | + # Compute the fence header |
| 138 | + lang_code = get_lang_class(filename) |
| 139 | + header = lang_code |
| 140 | + title = title.strip() |
| 141 | + |
| 142 | + # Select the code content |
| 143 | + page_parent_dir = os.path.dirname(page.file.abs_src_path) |
| 144 | + import_path = os.path.join(page_parent_dir, filename) |
| 145 | + # Always use UTF-8, as it is the recommended default for source file encodings. |
| 146 | + with open(import_path, encoding="UTF-8") as f: |
| 147 | + content = f.read() |
| 148 | + |
| 149 | + selected_content = select( |
| 150 | + content, lines=lines, block=block, inside_block=inside_block |
117 | 151 | ) |
118 | | - return replacements |
119 | | - |
120 | | - |
121 | | -def get_substitute(page, title, filename, lines, block, inside_block): |
122 | | - # Compute the fence header |
123 | | - lang_code = get_lang_class(filename) |
124 | | - header = lang_code |
125 | | - title = title.strip() |
126 | | - if len(title) > 0: |
127 | | - header += f' tab="{title}"' |
128 | | - |
129 | | - # Select the code content |
130 | | - page_parent_dir = os.path.dirname(page.file.abs_src_path) |
131 | | - import_path = os.path.join(page_parent_dir, filename) |
132 | | - # Always use UTF-8, as it is the recommended default for source file encodings. |
133 | | - with open(import_path, encoding="UTF-8") as f: |
134 | | - content = f.read() |
135 | | - |
136 | | - selected_content = select( |
137 | | - content, lines=lines, block=block, inside_block=inside_block |
138 | | - ) |
139 | 152 |
|
140 | | - dedented = textwrap.dedent(selected_content) |
| 153 | + dedented = textwrap.dedent(selected_content) |
141 | 154 |
|
142 | | - return f""" |
| 155 | + if self.config.get("title_mode") == "pymdownx.tabbed" and len(title) > 0: |
| 156 | + return f""" |
| 157 | +=== "{title}" |
143 | 158 | ```{header} |
144 | 159 | {dedented} |
145 | 160 | ``` |
146 | 161 |
|
147 | 162 | """ |
| 163 | + elif ( |
| 164 | + self.config.get("title_mode") == "legacy_pymdownx.superfences" |
| 165 | + and len(title) > 0 |
| 166 | + ): |
| 167 | + return f""" |
| 168 | +```{header} tab="{title}" |
| 169 | +{dedented} |
| 170 | +``` |
148 | 171 |
|
149 | | - |
150 | | -def substitute(markdown: str, substitutes: List[Replacement]) -> str: |
151 | | - substitutes_by_first_line = dict() |
152 | | - # Index substitutes by the first line |
153 | | - for s in substitutes: |
154 | | - substitutes_by_first_line[s.first_line_index] = s |
155 | | - |
156 | | - # Perform substitutions |
157 | | - result = "" |
158 | | - index = 0 |
159 | | - lines = markdown.splitlines() |
160 | | - while index < len(lines): |
161 | | - if index in substitutes_by_first_line.keys(): |
162 | | - # Replace the codeinclude fragment starting at this line |
163 | | - substitute = substitutes_by_first_line[index] |
164 | | - result += substitute.content |
165 | | - index = substitute.last_line_index |
| 172 | +""" |
166 | 173 | else: |
167 | | - # Keep the input line |
168 | | - result += lines[index] + "\n" |
169 | | - index += 1 |
170 | | - return result |
| 174 | + return f""" |
| 175 | +```{header} |
| 176 | +{dedented} |
| 177 | +``` |
| 178 | +
|
| 179 | +""" |
| 180 | + |
| 181 | + def substitute(self, markdown: str, substitutes: List[Replacement]) -> str: |
| 182 | + substitutes_by_first_line = dict() |
| 183 | + # Index substitutes by the first line |
| 184 | + for s in substitutes: |
| 185 | + substitutes_by_first_line[s.first_line_index] = s |
| 186 | + |
| 187 | + # Perform substitutions |
| 188 | + result = "" |
| 189 | + index = 0 |
| 190 | + lines = markdown.splitlines() |
| 191 | + while index < len(lines): |
| 192 | + if index in substitutes_by_first_line.keys(): |
| 193 | + # Replace the codeinclude fragment starting at this line |
| 194 | + substitute = substitutes_by_first_line[index] |
| 195 | + result += substitute.content |
| 196 | + index = substitute.last_line_index |
| 197 | + else: |
| 198 | + # Keep the input line |
| 199 | + result += lines[index] + "\n" |
| 200 | + index += 1 |
| 201 | + return result |
0 commit comments