def __init__(self, extensions=[], extension_configs={}, safe_mode=False): """ Creates a new Markdown instance. Keyword arguments: * extensions: A list of extensions. If they are of type string, the module mdx_name.py will be loaded. If they are a subclass of markdown.Extension, they will be used as-is. * extension-configs: Configuration setting for extensions. * safe_mode: Disallow raw html. One of "remove", "replace" or "escape". """ self.safeMode = safe_mode self.registeredExtensions = [] self.docType = "" self.stripTopLevelTags = True # Preprocessors self.preprocessors = odict.OrderedDict() self.preprocessors["html_block"] = \ preprocessors.HtmlBlockPreprocessor(self) self.preprocessors["reference"] = \ preprocessors.ReferencePreprocessor(self) # footnote preprocessor will be inserted with "<reference" # Block processors - ran by the parser self.parser = blockparser.BlockParser() self.parser.blockprocessors['empty'] = \ blockprocessors.EmptyBlockProcessor(self.parser) self.parser.blockprocessors['indent'] = \ blockprocessors.ListIndentProcessor(self.parser) self.parser.blockprocessors['code'] = \ blockprocessors.CodeBlockProcessor(self.parser) self.parser.blockprocessors['hashheader'] = \ blockprocessors.HashHeaderProcessor(self.parser) self.parser.blockprocessors['setextheader'] = \ blockprocessors.SetextHeaderProcessor(self.parser) self.parser.blockprocessors['hr'] = \ blockprocessors.HRProcessor(self.parser) self.parser.blockprocessors['olist'] = \ blockprocessors.OListProcessor(self.parser) self.parser.blockprocessors['ulist'] = \ blockprocessors.UListProcessor(self.parser) self.parser.blockprocessors['quote'] = \ blockprocessors.BlockQuoteProcessor(self.parser) self.parser.blockprocessors['paragraph'] = \ blockprocessors.ParagraphProcessor(self.parser) #self.prePatterns = [] # Inline patterns - Run on the tree self.inlinePatterns = odict.OrderedDict() self.inlinePatterns["backtick"] = \ inlinepatterns.BacktickPattern(inlinepatterns.BACKTICK_RE) self.inlinePatterns["escape"] = \ inlinepatterns.SimpleTextPattern(inlinepatterns.ESCAPE_RE) self.inlinePatterns["reference"] = \ inlinepatterns.ReferencePattern(inlinepatterns.REFERENCE_RE, self) self.inlinePatterns["link"] = \ inlinepatterns.LinkPattern(inlinepatterns.LINK_RE, self) self.inlinePatterns["image_link"] = \ inlinepatterns.ImagePattern(inlinepatterns.IMAGE_LINK_RE, self) self.inlinePatterns["image_reference"] = \ inlinepatterns.ImageReferencePattern(inlinepatterns.IMAGE_REFERENCE_RE, self) self.inlinePatterns["autolink"] = \ inlinepatterns.AutolinkPattern(inlinepatterns.AUTOLINK_RE, self) self.inlinePatterns["automail"] = \ inlinepatterns.AutomailPattern(inlinepatterns.AUTOMAIL_RE, self) self.inlinePatterns["linebreak2"] = \ inlinepatterns.SubstituteTagPattern(inlinepatterns.LINE_BREAK_2_RE, 'br') self.inlinePatterns["linebreak"] = \ inlinepatterns.SubstituteTagPattern(inlinepatterns.LINE_BREAK_RE, 'br') self.inlinePatterns["html"] = \ inlinepatterns.HtmlPattern(inlinepatterns.HTML_RE, self) self.inlinePatterns["entity"] = \ inlinepatterns.HtmlPattern(inlinepatterns.ENTITY_RE, self) self.inlinePatterns["not_strong"] = \ inlinepatterns.SimpleTextPattern(inlinepatterns.NOT_STRONG_RE) self.inlinePatterns["strong_em"] = \ inlinepatterns.DoubleTagPattern(inlinepatterns.STRONG_EM_RE, 'strong,em') self.inlinePatterns["strong"] = \ inlinepatterns.SimpleTagPattern(inlinepatterns.STRONG_RE, 'strong') self.inlinePatterns["emphasis"] = \ inlinepatterns.SimpleTagPattern(inlinepatterns.EMPHASIS_RE, 'em') self.inlinePatterns["emphasis2"] = \ inlinepatterns.SimpleTagPattern(inlinepatterns.EMPHASIS_2_RE, 'em') # The order of the handlers matters!!! # Tree processors - run once we have a basic parse. self.treeprocessors = odict.OrderedDict() self.treeprocessors["inline"] = treeprocessors.InlineProcessor(self) self.treeprocessors["prettify"] = \ treeprocessors.PrettifyTreeprocessor(self) # Postprocessors - finishing touches. self.postprocessors = odict.OrderedDict() self.postprocessors["raw_html"] = \ postprocessors.RawHtmlPostprocessor(self) self.postprocessors["amp_substitute"] = \ postprocessors.AndSubstitutePostprocessor() # footnote postprocessor will be inserted with ">amp_substitute" self.references = {} self.htmlStash = preprocessors.HtmlStash() self.registerExtensions(extensions=extensions, configs=extension_configs) self.reset()
def __init__(self, extensions=[], extension_configs={}, safe_mode=False, output_format=DEFAULT_OUTPUT_FORMAT): """ Creates a new Markdown instance. Keyword arguments: * extensions: A list of extensions. If they are of type string, the module mdx_name.py will be loaded. If they are a subclass of markdown.Extension, they will be used as-is. * extension-configs: Configuration setting for extensions. * safe_mode: Disallow raw html. One of "remove", "replace" or "escape". * output_format: Format of output. Supported formats are: * "xhtml1": Outputs XHTML 1.x. Default. * "xhtml": Outputs latest supported version of XHTML (currently XHTML 1.1). * "html4": Outputs HTML 4 * "html": Outputs latest supported version of HTML (currently HTML 4). Note that it is suggested that the more specific formats ("xhtml1" and "html4") be used as "xhtml" or "html" may change in the future if it makes sense at that time. """ self.safeMode = safe_mode self.registeredExtensions = [] self.docType = "" self.stripTopLevelTags = True # Preprocessors self.preprocessors = odict.OrderedDict() self.preprocessors["html_block"] = \ preprocessors.HtmlBlockPreprocessor(self) self.preprocessors["reference"] = \ preprocessors.ReferencePreprocessor(self) # footnote preprocessor will be inserted with "<reference" # Block processors - ran by the parser self.parser = blockparser.BlockParser() self.parser.blockprocessors['empty'] = \ blockprocessors.EmptyBlockProcessor(self.parser) self.parser.blockprocessors['indent'] = \ blockprocessors.ListIndentProcessor(self.parser) self.parser.blockprocessors['code'] = \ blockprocessors.CodeBlockProcessor(self.parser) self.parser.blockprocessors['hashheader'] = \ blockprocessors.HashHeaderProcessor(self.parser) self.parser.blockprocessors['setextheader'] = \ blockprocessors.SetextHeaderProcessor(self.parser) self.parser.blockprocessors['hr'] = \ blockprocessors.HRProcessor(self.parser) self.parser.blockprocessors['olist'] = \ blockprocessors.OListProcessor(self.parser) self.parser.blockprocessors['ulist'] = \ blockprocessors.UListProcessor(self.parser) self.parser.blockprocessors['quote'] = \ blockprocessors.BlockQuoteProcessor(self.parser) self.parser.blockprocessors['paragraph'] = \ blockprocessors.ParagraphProcessor(self.parser) #self.prePatterns = [] # Inline patterns - Run on the tree self.inlinePatterns = odict.OrderedDict() self.inlinePatterns["backtick"] = \ inlinepatterns.BacktickPattern(inlinepatterns.BACKTICK_RE) self.inlinePatterns["escape"] = \ inlinepatterns.SimpleTextPattern(inlinepatterns.ESCAPE_RE) self.inlinePatterns["reference"] = \ inlinepatterns.ReferencePattern(inlinepatterns.REFERENCE_RE, self) self.inlinePatterns["link"] = \ inlinepatterns.LinkPattern(inlinepatterns.LINK_RE, self) self.inlinePatterns["image_link"] = \ inlinepatterns.ImagePattern(inlinepatterns.IMAGE_LINK_RE, self) self.inlinePatterns["image_reference"] = \ inlinepatterns.ImageReferencePattern(inlinepatterns.IMAGE_REFERENCE_RE, self) self.inlinePatterns["autolink"] = \ inlinepatterns.AutolinkPattern(inlinepatterns.AUTOLINK_RE, self) self.inlinePatterns["automail"] = \ inlinepatterns.AutomailPattern(inlinepatterns.AUTOMAIL_RE, self) self.inlinePatterns["linebreak2"] = \ inlinepatterns.SubstituteTagPattern(inlinepatterns.LINE_BREAK_2_RE, 'br') self.inlinePatterns["linebreak"] = \ inlinepatterns.SubstituteTagPattern(inlinepatterns.LINE_BREAK_RE, 'br') self.inlinePatterns["html"] = \ inlinepatterns.HtmlPattern(inlinepatterns.HTML_RE, self) self.inlinePatterns["entity"] = \ inlinepatterns.HtmlPattern(inlinepatterns.ENTITY_RE, self) self.inlinePatterns["not_strong"] = \ inlinepatterns.SimpleTextPattern(inlinepatterns.NOT_STRONG_RE) self.inlinePatterns["strong_em"] = \ inlinepatterns.DoubleTagPattern(inlinepatterns.STRONG_EM_RE, 'strong,em') self.inlinePatterns["strong"] = \ inlinepatterns.SimpleTagPattern(inlinepatterns.STRONG_RE, 'strong') self.inlinePatterns["emphasis"] = \ inlinepatterns.SimpleTagPattern(inlinepatterns.EMPHASIS_RE, 'em') self.inlinePatterns["emphasis2"] = \ inlinepatterns.SimpleTagPattern(inlinepatterns.EMPHASIS_2_RE, 'em') # The order of the handlers matters!!! # Tree processors - run once we have a basic parse. self.treeprocessors = odict.OrderedDict() self.treeprocessors["inline"] = treeprocessors.InlineProcessor(self) self.treeprocessors["prettify"] = \ treeprocessors.PrettifyTreeprocessor(self) # Postprocessors - finishing touches. self.postprocessors = odict.OrderedDict() self.postprocessors["raw_html"] = \ postprocessors.RawHtmlPostprocessor(self) self.postprocessors["amp_substitute"] = \ postprocessors.AndSubstitutePostprocessor() # footnote postprocessor will be inserted with ">amp_substitute" # Map format keys to serializers self.output_formats = { 'html': html4.to_html_string, 'html4': html4.to_html_string, 'xhtml': etree.tostring, 'xhtml1': etree.tostring, } self.references = {} self.htmlStash = preprocessors.HtmlStash() self.registerExtensions(extensions=extensions, configs=extension_configs) self.set_output_format(output_format) self.reset()