|
90 | 90 | on Extras. |
91 | 91 | * wiki-tables: Google Code Wiki-style tables. See |
92 | 92 | <http://code.google.com/p/support/wiki/WikiSyntax#Tables>. |
| 93 | +* wavedrom: Support for generating Wavedrom digital timing diagrams |
93 | 94 | * xml: Passes one-liner processing instructions and namespaced XML tags. |
94 | 95 | """ |
95 | 96 |
|
@@ -347,6 +348,9 @@ def convert(self, text): |
347 | 348 |
|
348 | 349 | text = self.preprocess(text) |
349 | 350 |
|
| 351 | + if 'wavedrom' in self.extras: |
| 352 | + text = self._do_wavedrom_blocks(text) |
| 353 | + |
350 | 354 | if "fenced-code-blocks" in self.extras and not self.safe_mode: |
351 | 355 | text = self._do_fenced_code_blocks(text) |
352 | 356 |
|
@@ -1011,6 +1015,9 @@ def _run_block_gamut(self, text): |
1011 | 1015 | if 'admonitions' in self.extras: |
1012 | 1016 | text = self._do_admonitions(text) |
1013 | 1017 |
|
| 1018 | + if 'wavedrom' in self.extras: |
| 1019 | + text = self._do_wavedrom_blocks(text) |
| 1020 | + |
1014 | 1021 | if "fenced-code-blocks" in self.extras: |
1015 | 1022 | text = self._do_fenced_code_blocks(text) |
1016 | 1023 |
|
@@ -2084,6 +2091,42 @@ def _encode_code(self, text): |
2084 | 2091 | self._code_table[text] = hashed |
2085 | 2092 | return hashed |
2086 | 2093 |
|
| 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 | + |
2087 | 2130 | _admonitions = r'admonition|attention|caution|danger|error|hint|important|note|tip|warning' |
2088 | 2131 | _admonitions_re = re.compile(r''' |
2089 | 2132 | ^(\ *)\.\.\ (%s)::\ * # $1 leading indent, $2 the admonition |
|
0 commit comments