Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d6651a4

Browse files
Merge pull request trentm#477 from Crozzers/feature-wavedrom-support
Feature wavedrom support
2 parents 3f61134 + b84d367 commit d6651a4

File tree

12 files changed

+482
-8
lines changed

12 files changed

+482
-8
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## python-markdown2 2.4.6 (not yet released)
44

5-
(nothing yet)
5+
- [pull #477] Feature wavedrom support
66

77

88
## python-markdown2 2.4.5

lib/markdown2.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
on Extras.
9191
* wiki-tables: Google Code Wiki-style tables. See
9292
<http://code.google.com/p/support/wiki/WikiSyntax#Tables>.
93+
* wavedrom: Support for generating Wavedrom digital timing diagrams
9394
* xml: Passes one-liner processing instructions and namespaced XML tags.
9495
"""
9596

@@ -347,6 +348,9 @@ def convert(self, text):
347348

348349
text = self.preprocess(text)
349350

351+
if 'wavedrom' in self.extras:
352+
text = self._do_wavedrom_blocks(text)
353+
350354
if "fenced-code-blocks" in self.extras and not self.safe_mode:
351355
text = self._do_fenced_code_blocks(text)
352356

@@ -1011,6 +1015,9 @@ def _run_block_gamut(self, text):
10111015
if 'admonitions' in self.extras:
10121016
text = self._do_admonitions(text)
10131017

1018+
if 'wavedrom' in self.extras:
1019+
text = self._do_wavedrom_blocks(text)
1020+
10141021
if "fenced-code-blocks" in self.extras:
10151022
text = self._do_fenced_code_blocks(text)
10161023

@@ -2084,6 +2091,42 @@ def _encode_code(self, text):
20842091
self._code_table[text] = hashed
20852092
return hashed
20862093

2094+
def _wavedrom_block_sub(self, match):
2095+
# if this isn't a wavedrom diagram block, exit now
2096+
if match.group(2) != 'wavedrom':
2097+
return match.string[match.start():match.end()]
2098+
2099+
# dedent the block for processing
2100+
lead_indent, waves = self._uniform_outdent(match.group(3))
2101+
# default tags to wrap the wavedrom block in
2102+
open_tag, close_tag = '<script type="WaveDrom">\n', '</script>'
2103+
2104+
# check if the user would prefer to have the SVG embedded directly
2105+
if not isinstance(self.extras['wavedrom'], dict):
2106+
embed_svg = True
2107+
else:
2108+
# default behaviour is to embed SVGs
2109+
embed_svg = self.extras['wavedrom'].get('prefer_embed_svg', True)
2110+
2111+
if embed_svg:
2112+
try:
2113+
import wavedrom
2114+
waves = wavedrom.render(waves).tostring()
2115+
open_tag, close_tag = '<div>', '\n</div>'
2116+
except ImportError:
2117+
pass
2118+
2119+
# hash SVG to prevent <> chars being messed with
2120+
self._escape_table[waves] = _hash_text(waves)
2121+
2122+
return self._uniform_indent(
2123+
'\n%s%s%s\n' % (open_tag, self._escape_table[waves], close_tag),
2124+
lead_indent, include_empty_lines=True
2125+
)
2126+
2127+
def _do_wavedrom_blocks(self, text):
2128+
return self._fenced_code_block_re.sub(self._wavedrom_block_sub, text)
2129+
20872130
_admonitions = r'admonition|attention|caution|danger|error|hint|important|note|tip|warning'
20882131
_admonitions_re = re.compile(r'''
20892132
^(\ *)\.\.\ (%s)::\ * # $1 leading indent, $2 the admonition

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"""
3232

3333
extras_require = {
34-
"code_syntax_highlighting": ["pygments>=2.7.3"]
34+
"code_syntax_highlighting": ["pygments>=2.7.3"],
35+
"wavedrom": ["wavedrom; python_version>='3.7'"]
3536
}
3637
# nested listcomp to combine all optional extras into convenient "all" option
3738
extras_require["all"] = [i for v in tuple(extras_require.values()) for i in v]

test/test.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
"""The markdown2 test suite entry point."""
66

7+
import importlib
78
import os
89
from os.path import join, abspath, dirname
910
import sys
1011
import logging
11-
1212
import testlib
1313

1414
log = logging.getLogger("test")
@@ -37,11 +37,12 @@ def setup():
3737

3838
setup()
3939
default_tags = []
40-
try:
41-
import pygments # noqa
42-
except ImportError:
43-
log.warning("skipping pygments tests ('pygments' module not found)")
44-
default_tags.append("-pygments")
40+
for extra_lib in ('pygments', 'wavedrom'):
41+
try:
42+
importlib.import_module(extra_lib)
43+
except ImportError:
44+
log.warning("skipping %s tests ('%s' module not found)" % (extra_lib, extra_lib))
45+
default_tags.append("-%s" % extra_lib)
4546

4647
retval = testlib.harness(testdir_from_ns=testdir_from_ns,
4748
default_tags=default_tags)

test/tm-cases/wavedrom.html

Lines changed: 348 additions & 0 deletions
Large diffs are not rendered by default.

test/tm-cases/wavedrom.opts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"extras": ["wavedrom"]}

test/tm-cases/wavedrom.tags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extra wavedrom

test/tm-cases/wavedrom.text

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Here is a basic wavedrom diagram:
2+
```wavedrom
3+
{ "signal": [{ "name": "Alfa", "wave": "01.zx=ud.23.456789" }] }
4+
```
5+
6+
And here is a more complex one, inside of a list
7+
8+
* More complex diagram
9+
* Inside of nested list
10+
```wavedrom
11+
{ signal: [
12+
{ name: "pclk", wave: 'p.......' },
13+
{ name: "Pclk", wave: 'P.......' },
14+
{ name: "nclk", wave: 'n.......' },
15+
{ name: "Nclk", wave: 'N.......' },
16+
{},
17+
{ name: 'clk0', wave: 'phnlPHNL' },
18+
{ name: 'clk1', wave: 'xhlhLHl.' },
19+
{ name: 'clk2', wave: 'hpHplnLn' },
20+
{ name: 'clk3', wave: 'nhNhplPl' },
21+
{ name: 'clk4', wave: 'xlh.L.Hx' },
22+
]}
23+
```
24+
* Making sure not to break indentation
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<p>Here is a basic wavedrom diagram:</p>
2+
3+
<script type="WaveDrom">
4+
{ "signal": [{ "name": "Alfa", "wave": "01.zx=ud.23.456789" }] }
5+
</script>
6+
7+
<p>And here is a more complex one, inside of a list</p>
8+
9+
<ul>
10+
<li>More complex diagram
11+
<ul>
12+
<li>Inside of nested list <br />
13+
<script type="WaveDrom">
14+
{ signal: [
15+
{ name: "pclk", wave: 'p.......' },
16+
{ name: "Pclk", wave: 'P.......' },
17+
{ name: "nclk", wave: 'n.......' },
18+
{ name: "Nclk", wave: 'N.......' },
19+
{},
20+
{ name: 'clk0', wave: 'phnlPHNL' },
21+
{ name: 'clk1', wave: 'xhlhLHl.' },
22+
{ name: 'clk2', wave: 'hpHplnLn' },
23+
{ name: 'clk3', wave: 'nhNhplPl' },
24+
{ name: 'clk4', wave: 'xlh.L.Hx' },
25+
]}
26+
</script></li>
27+
<li>Making sure not to break indentation</li>
28+
</ul></li>
29+
</ul>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"extras": {"wavedrom": {"prefer_embed_svg": False}}}

0 commit comments

Comments
 (0)