def plain2markdown(text, preserve_multiple_spaces=False, has_html_entities=False): if not has_html_entities: # prevent &foo; and { from becoming HTML entities text = re_amp.sub('&', text) # avoid accidental 4-space indentations creating code blocks if preserve_multiple_spaces: text = text.replace('\t', ' ' * 4) text = re_preserve_spaces.sub(' ', text) else: text = re_leading_spaces.sub('', text) # use html2text for most of the escaping text = html2text.escape_md_section(text, snob=True) # prevent < and > from becoming tags text = re_angle_bracket_open.sub('<', text) text = re_angle_bracket_close.sub('>', text) return text
def plain2markdown(txt, preserve_multiple_spaces=False, has_html_entities=False): if not has_html_entities: # prevent &foo; and { from becoming HTML entities txt = re_amp.sub('&', txt) # avoid accidental 4-space indentations creating code blocks if preserve_multiple_spaces: txt = txt.replace('\t', ' ' * 4) txt = re_preserve_spaces.sub(' ', txt) else: txt = re_leading_spaces.sub('', txt) try: # try to use html2text for most of the escaping import html2text html2text.BODY_WIDTH = 0 txt = html2text.escape_md_section(txt, snob=True) except ImportError: # fall back to just escaping any MD-special chars txt = md_chars_matcher_all.sub(r"\\\1", txt) # prevent < and > from becoming tags txt = re_angle_bracket_open.sub('<', txt) txt = re_angle_bracket_close.sub('>', txt) return txt