-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from elkirby/fenced-code-blocks-835
Fenced code blocks 835
- Loading branch information
Showing
12 changed files
with
369 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ Contributors | |
|
||
* Dave MacNamara <[email protected]> | ||
* Justin Michalicek <[email protected]> | ||
* Erin Kirby <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
__author__ = """Jason Emerick""" | ||
__email__ = '[email protected]' | ||
__version__ = '0.2.7' | ||
__version__ = '0.3.1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
scoped_code_tabs | ||
---------------------------------- | ||
docdown.scoped_code_tabs Markdown extension module | ||
""" | ||
import re | ||
|
||
from markdown.preprocessors import Preprocessor | ||
from markdown_fenced_code_tabs import CodeTabsExtension | ||
|
||
|
||
class ScopedCodeTabsPreprocessor(Preprocessor): | ||
RE_FENCE_START = r'^ *\|\~\s*$' # start line, e.g., ` |~ ` | ||
RE_FENCE_END = r'^\s*\~\|\s*$' # last non-blank line, e.g, '~|\n \n\n' | ||
|
||
def __init__(self, md, code_tabs_preprocessor): | ||
self.code_tabs_preprocessor = code_tabs_preprocessor | ||
super(ScopedCodeTabsPreprocessor, self).__init__(md) | ||
|
||
def run(self, lines): | ||
new_lines = [] | ||
fenced_code_tab = [] | ||
starting_line = None | ||
in_tab = False | ||
|
||
for line in lines: | ||
if re.search(self.RE_FENCE_START, line): | ||
# Start block pattern, save line in case of no end fence | ||
in_tab = True | ||
starting_line = line | ||
elif re.search(self.RE_FENCE_END, line): | ||
# End of code block, run through fenced code tabs pre-processor and reset code tab list | ||
new_lines += self.code_tabs_preprocessor.run(fenced_code_tab) | ||
fenced_code_tab = [] | ||
in_tab = False | ||
elif in_tab: | ||
# Still in tab -- append to tab list | ||
fenced_code_tab.append(line) | ||
else: | ||
# Not in a fenced code tab, and not starting/ending one -- pass as usual | ||
new_lines.append(line) | ||
|
||
# Non-terminated code tab block, append matching starting fence and remaining lines without processing | ||
if fenced_code_tab: | ||
new_lines += [starting_line] + fenced_code_tab | ||
return new_lines | ||
|
||
|
||
class ScopedCodeTabExtension(CodeTabsExtension): | ||
|
||
def __init__(self, **kwargs): | ||
""" | ||
A Markdown extension that serves to scope where Fenced Code Tabs are rendered by way of |~ ... ~| fences. | ||
Example: | ||
## A set of code tabs in Python and Java | ||
|~ | ||
```python | ||
def main(): | ||
print("This would be passed through markdown_fenced_code_tabs") | ||
``` | ||
```java | ||
public static void main(String[] args) { | ||
System.out.println("This would be passed through markdown_fenced_code_tabs"); | ||
} | ||
``` | ||
~| | ||
## A regular, non-tabbed code block in Bash | ||
```bash | ||
codeblockinfo() { | ||
echo("This would NOT be passed through markdown_fenced_code tabs"); | ||
} | ||
``` | ||
""" | ||
super(ScopedCodeTabExtension, self).__init__(**kwargs) | ||
|
||
def extendMarkdown(self, md, md_globals): | ||
super(ScopedCodeTabExtension, self).extendMarkdown(md, md_globals) | ||
md.registerExtension(self) | ||
|
||
md.preprocessors.add('scoped_code_tabs', | ||
ScopedCodeTabsPreprocessor(md, | ||
code_tabs_preprocessor=md.preprocessors['fenced_code_block']), | ||
">normalize_whitespace") | ||
del md.preprocessors['fenced_code_block'] | ||
|
||
|
||
def makeExtension(*args, **kwargs): | ||
return ScopedCodeTabExtension(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
###################### | ||
Scoped Code Tabs | ||
###################### | ||
|
||
Scoped Code Tabs allows for the explicit annotation of when and where to tabulate a set of code blocks versus rendering them | ||
separately. | ||
|
||
A scoped code tab is delimited by an opening ``|~`` fence and closing ``~|`` fence. The code blocks within the fences | ||
are defined as typical code blocks, using backticks, with the opening backtick fence specifying the code language contained | ||
within the block. | ||
|
||
The configuration for rendering the tabs is as directly defined by the `markdown_fenced_code_tabs`_ extension. | ||
|
||
|
||
============= | ||
Dependencies | ||
============= | ||
The ``docdown.scoped_code_tabs`` extension requires the third-party extension `markdown_fenced_code_tabs`_ in order to process | ||
the tabulated fenced code blocks. | ||
|
||
============== | ||
Configuration | ||
============== | ||
|
||
single_block_as_tab | ||
Whether a single code block should still be rendered as a code tab. Default: ``False`` | ||
active_class | ||
The CSS class to apply to the active tab. Default: ``active`` | ||
template | ||
Which template to use to render code tabs. One of: [``default``, ``bootstrap3``, ``bootstrap4``]. Default: ``default`` | ||
Please see the *-template.html files in the `markdown_fenced_code_tabs`_ extension. | ||
|
||
======= | ||
Usage | ||
======= | ||
In documents | ||
------------- | ||
|
||
.. code-block:: md | ||
### Hello World Examples | ||
|~ | ||
```bash | ||
helloWorld() { | ||
greeting=${1:-World} | ||
echo(`Hello ${greeting}`) | ||
} | ||
``` | ||
~| | ||
```python | ||
def hello_world(greeting: str = "World") -> None: | ||
print(f"Hello {greeting}") | ||
``` | ||
Python | ||
-------------- | ||
|
||
.. code-block:: python | ||
config = { | ||
'docdown.scoped_code_tabs': { | ||
'single_block_as_tab': True, | ||
'template': 'bootstrap4', | ||
'active_class': 'tab-active' | ||
} | ||
} | ||
text = """\ | ||
### Hello World Examples | ||
|~ | ||
```bash | ||
helloWorld() { | ||
greeting=${1:-World} | ||
echo(`Hello ${greeting}`) | ||
} | ||
``` | ||
~| | ||
```python | ||
def hello_world(greeting: str = "World") -> None: | ||
print(f"Hello {greeting}") | ||
``` | ||
""" | ||
html = markdown.markdown( | ||
text, | ||
extensions=['docdown.scoped_code_tabs'], | ||
extension_configs=config, | ||
output_format='html5') | ||
======= | ||
Output | ||
======= | ||
Note the extra classes and divs for tabulation around the ``|~`` ``~|`` code block. | ||
|
||
.. code-block:: html | ||
|
||
<h3>Hello World Examples</h3> | ||
<p> <div class=md-fenced-code-tabs id=tab-tab-group-0><ul class="nav nav-tabs"><li class=nav-item><a class="nav-link tab-active" href=#tab-group-0-0_bash-panel role=tab id=tab-group-0-0_bash-tab data-toggle=tab data-lang=bash aria-controls=tab-group-0-0_bash-panel aria-selected=true>Bash</a></li></ul><div class=tab-content><div id=tab-group-0-0_bash-panel class="tab-pane show tab-active" role=tabpanel aria-labelledby=tab-group-0-0_bash-tab><pre><code class=bash>helloWorld() { | ||
greeting=${1:-World} | ||
echo(`Hello ${greeting}`) | ||
} | ||
</code></pre></div></div></div></p> | ||
<p><code>python | ||
def hello_world(greeting: str = "World") -> None: | ||
print(f"Hello {greeting}")</code></p> | ||
|
||
.. _`markdown_fenced_code_tabs`: https://github.com/yacir/markdown-fenced-code-tabs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[bumpversion] | ||
current_version = 0.2.7 | ||
current_version = 0.3.1 | ||
commit = True | ||
tag = True | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.