|
22 | 22 | from .codehilite import CodeHilite, CodeHiliteExtension, parse_hl_lines |
23 | 23 | from .attr_list import get_attrs, AttrListExtension |
24 | 24 | from ..util import parseBoolValue |
| 25 | +from ..serializers import _escape_attrib_html |
25 | 26 | import re |
26 | 27 |
|
27 | 28 |
|
@@ -120,30 +121,24 @@ def run(self, lines): |
120 | 121 | else: |
121 | 122 | id_attr = lang_attr = class_attr = kv_pairs = '' |
122 | 123 | if lang: |
123 | | - lang_attr = ' class="{}{}"'.format(self.config.get('lang_prefix', 'language-'), lang) |
| 124 | + prefix = self.config.get('lang_prefix', 'language-') |
| 125 | + lang_attr = f' class="{prefix}{_escape_attrib_html(lang)}"' |
124 | 126 | if classes: |
125 | | - class_attr = ' class="{}"'.format(' '.join(classes)) |
| 127 | + class_attr = f' class="{_escape_attrib_html(" ".join(classes))}"' |
126 | 128 | if id: |
127 | | - id_attr = ' id="{}"'.format(id) |
| 129 | + id_attr = f' id="{_escape_attrib_html(id)}"' |
128 | 130 | if self.use_attr_list and config and not config.get('use_pygments', False): |
129 | 131 | # Only assign key/value pairs to code element if attr_list ext is enabled, key/value pairs |
130 | 132 | # were defined on the code block, and the `use_pygments` key was not set to True. The |
131 | 133 | # `use_pygments` key could be either set to False or not defined. It is omitted from output. |
132 | | - kv_pairs = ' ' + ' '.join( |
133 | | - '{k}="{v}"'.format(k=k, v=v) for k, v in config.items() if k != 'use_pygments' |
| 134 | + kv_pairs = ''.join( |
| 135 | + f' {k}="{_escape_attrib_html(v)}"' for k, v in config.items() if k != 'use_pygments' |
134 | 136 | ) |
135 | | - code = '<pre{id}{cls}><code{lang}{kv}>{code}</code></pre>'.format( |
136 | | - id=id_attr, |
137 | | - cls=class_attr, |
138 | | - lang=lang_attr, |
139 | | - kv=kv_pairs, |
140 | | - code=self._escape(m.group('code')) |
141 | | - ) |
| 137 | + code = self._escape(m.group('code')) |
| 138 | + code = f'<pre{id_attr}{class_attr}><code{lang_attr}{kv_pairs}>{code}</code></pre>' |
142 | 139 |
|
143 | 140 | placeholder = self.md.htmlStash.store(code) |
144 | | - text = '{}\n{}\n{}'.format(text[:m.start()], |
145 | | - placeholder, |
146 | | - text[m.end():]) |
| 141 | + text = f'{text[:m.start()]}\n{placeholder}\n{text[m.end():]}' |
147 | 142 | else: |
148 | 143 | break |
149 | 144 | return text.split("\n") |
|
0 commit comments