@@ -60,7 +60,6 @@ def implementation(self, el, text, convert_as_inline):
6060 return implementation
6161
6262
63-
6463def _todict (obj ):
6564 return dict ((k , getattr (obj , k )) for k in dir (obj ) if not k .startswith ('_' ))
6665
@@ -141,12 +140,21 @@ def is_nested_node(el):
141140
142141 def process_text (self , el ):
143142 text = six .text_type (el )
143+
144+ # dont remove any whitespace when handling pre or code in pre
145+ if (el .parent .name == 'pre'
146+ or (el .parent .name == 'code' and el .parent .parent .name == 'pre' )):
147+ return escape (text or '' )
148+
149+ cleaned_text = escape (whitespace_re .sub (' ' , text or '' ))
150+
144151 # remove trailing whitespaces if any of the following condition is true:
145152 # - current text node is the last node in li
146153 # - current text node is followed by an embedded list
147154 if el .parent .name == 'li' and (not el .next_sibling or el .next_sibling .name in ['ul' , 'ol' ]):
148- return escape (all_whitespace_re .sub (' ' , text or '' )).rstrip ()
149- return escape (whitespace_re .sub (' ' , text or '' ))
155+ return cleaned_text .rstrip ()
156+
157+ return cleaned_text
150158
151159 def __getattr__ (self , attr ):
152160 # Handle headings
@@ -214,7 +222,11 @@ def convert_br(self, el, text, convert_as_inline):
214222 else :
215223 return ' \n '
216224
217- convert_code = abstract_inline_conversion (lambda self : '`' )
225+ def convert_code (self , el , text , convert_as_inline ):
226+ if el .parent .name == 'pre' :
227+ return text
228+ converter = abstract_inline_conversion (lambda self : '`' )
229+ return converter (self , el , text , convert_as_inline )
218230
219231 convert_del = abstract_inline_conversion (lambda self : '~~' )
220232
@@ -296,6 +308,11 @@ def convert_p(self, el, text, convert_as_inline):
296308 return text
297309 return '%s\n \n ' % text if text else ''
298310
311+ def convert_pre (self , el , text , convert_as_inline ):
312+ if not text :
313+ return ''
314+ return '\n ```\n %s\n ```\n ' % text
315+
299316 convert_s = convert_del
300317
301318 convert_strong = convert_b
0 commit comments