|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +scoped_code_tabs |
| 5 | +---------------------------------- |
| 6 | +
|
| 7 | +docdown.scoped_code_tabs Markdown extension module |
| 8 | +""" |
| 9 | +import re |
| 10 | + |
| 11 | +from markdown.preprocessors import Preprocessor |
| 12 | +from markdown_fenced_code_tabs import CodeTabsExtension |
| 13 | + |
| 14 | + |
| 15 | +class ScopedCodeTabsPreprocessor(Preprocessor): |
| 16 | + RE_FENCE_START = r'^ *\|\~\s*$' # start line, e.g., ` |~ ` |
| 17 | + RE_FENCE_END = r'^\s*\~\|\s*$' # last non-blank line, e.g, '~|\n \n\n' |
| 18 | + |
| 19 | + def __init__(self, md, code_tabs_preprocessor): |
| 20 | + self.code_tabs_preprocessor = code_tabs_preprocessor |
| 21 | + super(ScopedCodeTabsPreprocessor, self).__init__(md) |
| 22 | + |
| 23 | + def run(self, lines): |
| 24 | + new_lines = [] |
| 25 | + fenced_code_tab = [] |
| 26 | + starting_line = None |
| 27 | + in_tab = False |
| 28 | + |
| 29 | + for line in lines: |
| 30 | + if re.search(self.RE_FENCE_START, line): |
| 31 | + # Start block pattern, save line in case of no end fence |
| 32 | + in_tab = True |
| 33 | + starting_line = line |
| 34 | + elif re.search(self.RE_FENCE_END, line): |
| 35 | + # End of code block, run through fenced code tabs pre-processor and reset code tab list |
| 36 | + new_lines += self.code_tabs_preprocessor.run(fenced_code_tab) |
| 37 | + fenced_code_tab = [] |
| 38 | + in_tab = False |
| 39 | + elif in_tab: |
| 40 | + # Still in tab -- append to tab list |
| 41 | + fenced_code_tab.append(line) |
| 42 | + else: |
| 43 | + # Not in a fenced code tab, and not starting/ending one -- pass as usual |
| 44 | + new_lines.append(line) |
| 45 | + |
| 46 | + # Non-terminated code tab block, append matching starting fence and remaining lines without processing |
| 47 | + if fenced_code_tab: |
| 48 | + new_lines += [starting_line] + fenced_code_tab |
| 49 | + return new_lines |
| 50 | + |
| 51 | + |
| 52 | +class ScopedCodeTabExtension(CodeTabsExtension): |
| 53 | + |
| 54 | + def __init__(self, **kwargs): |
| 55 | + """ |
| 56 | + A Markdown extension that serves to scope where Fenced Code Tabs are rendered by way of |~ ... ~| fences. |
| 57 | +
|
| 58 | + Example: |
| 59 | +
|
| 60 | + ## A set of code tabs in Python and Java |
| 61 | + |~ |
| 62 | + ```python |
| 63 | + def main(): |
| 64 | + print("This would be passed through markdown_fenced_code_tabs") |
| 65 | + ``` |
| 66 | +
|
| 67 | + ```java |
| 68 | + public static void main(String[] args) { |
| 69 | + System.out.println("This would be passed through markdown_fenced_code_tabs"); |
| 70 | + } |
| 71 | + ``` |
| 72 | + ~| |
| 73 | +
|
| 74 | + ## A regular, non-tabbed code block in Bash |
| 75 | + ```bash |
| 76 | + codeblockinfo() { |
| 77 | + echo("This would NOT be passed through markdown_fenced_code tabs"); |
| 78 | + } |
| 79 | + ``` |
| 80 | + """ |
| 81 | + super(ScopedCodeTabExtension, self).__init__(**kwargs) |
| 82 | + |
| 83 | + def extendMarkdown(self, md, md_globals): |
| 84 | + super(ScopedCodeTabExtension, self).extendMarkdown(md, md_globals) |
| 85 | + md.registerExtension(self) |
| 86 | + |
| 87 | + md.preprocessors.add('scoped_code_tabs', |
| 88 | + ScopedCodeTabsPreprocessor(md, |
| 89 | + code_tabs_preprocessor=md.preprocessors['fenced_code_block']), |
| 90 | + ">normalize_whitespace") |
| 91 | + del md.preprocessors['fenced_code_block'] |
| 92 | + |
| 93 | + |
| 94 | +def makeExtension(*args, **kwargs): |
| 95 | + return ScopedCodeTabExtension(*args, **kwargs) |
0 commit comments