|
25 | 25 | UNDERSCORE = '_' |
26 | 26 |
|
27 | 27 |
|
28 | | -def escape(text, escape_underscores): |
29 | | - if not text: |
30 | | - return '' |
31 | | - if escape_underscores: |
32 | | - return text.replace('_', r'\_') |
33 | | - return text |
34 | | - |
35 | | - |
36 | 28 | def chomp(text): |
37 | 29 | """ |
38 | 30 | If the text in an inline tag like b, a, or em contains a leading or trailing |
@@ -71,10 +63,13 @@ class DefaultOptions: |
71 | 63 | autolinks = True |
72 | 64 | bullets = '*+-' # An iterable of bullet types. |
73 | 65 | code_language = '' |
| 66 | + code_language_callback = None |
74 | 67 | convert = None |
75 | 68 | default_title = False |
| 69 | + escape_asterisks = True |
76 | 70 | escape_underscores = True |
77 | 71 | heading_style = UNDERLINED |
| 72 | + keep_inline_images_in = [] |
78 | 73 | newline_style = SPACES |
79 | 74 | strip = None |
80 | 75 | strong_em_symbol = ASTERISK |
@@ -161,7 +156,7 @@ def process_text(self, el): |
161 | 156 | text = whitespace_re.sub(' ', text) |
162 | 157 |
|
163 | 158 | if el.parent.name != 'code': |
164 | | - text = escape(text, self.options['escape_underscores']) |
| 159 | + text = self.escape(text) |
165 | 160 |
|
166 | 161 | # remove trailing whitespaces if any of the following condition is true: |
167 | 162 | # - current text node is the last node in li |
@@ -199,6 +194,15 @@ def should_convert_tag(self, tag): |
199 | 194 | else: |
200 | 195 | return True |
201 | 196 |
|
| 197 | + def escape(self, text): |
| 198 | + if not text: |
| 199 | + return '' |
| 200 | + if self.options['escape_asterisks']: |
| 201 | + text = text.replace('*', r'\*') |
| 202 | + if self.options['escape_underscores']: |
| 203 | + text = text.replace('_', r'\_') |
| 204 | + return text |
| 205 | + |
202 | 206 | def indent(self, text, level): |
203 | 207 | return line_beginning_re.sub('\t' * level, text) if text else '' |
204 | 208 |
|
@@ -278,7 +282,8 @@ def convert_img(self, el, text, convert_as_inline): |
278 | 282 | src = el.attrs.get('src', None) or '' |
279 | 283 | title = el.attrs.get('title', None) or '' |
280 | 284 | title_part = ' "%s"' % title.replace('"', r'\"') if title else '' |
281 | | - if convert_as_inline: |
| 285 | + if (convert_as_inline |
| 286 | + and el.parent.name not in self.options['keep_inline_images_in']): |
282 | 287 | return alt |
283 | 288 |
|
284 | 289 | return '' % (alt, src, title_part) |
@@ -331,7 +336,12 @@ def convert_p(self, el, text, convert_as_inline): |
331 | 336 | def convert_pre(self, el, text, convert_as_inline): |
332 | 337 | if not text: |
333 | 338 | return '' |
334 | | - return '\n```%s\n%s\n```\n' % (self.options['code_language'], text) |
| 339 | + code_language = self.options['code_language'] |
| 340 | + |
| 341 | + if self.options['code_language_callback']: |
| 342 | + code_language = self.options['code_language_callback'](el) or code_language |
| 343 | + |
| 344 | + return '\n```%s\n%s\n```\n' % (code_language, text) |
335 | 345 |
|
336 | 346 | convert_s = convert_del |
337 | 347 |
|
|
0 commit comments