markdown
Python Markdown.
Python Markdown converts Markdown to HTML and can be used as a library or called from the command line.
Basic usage as a module:
import markdown
html = markdown.markdown(your_text_string)
See https://pythonhosted.org/Markdown/ for more information and instructions on how to extend the functionality of Python Markdown. Read that before you try modifying this file.
Authors and License
Started by Manfred Stienstra. Continued and maintained by Yuri Takhteyev, Waylan Limberg and Artem Yunusov.
Contact: markdown@freewisdom.org
Copyright 2007-2013 The Python Markdown Project (v. 1.7 and later) Copyright 200? Django Software Foundation (OrderedDict implementation) Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b) Copyright 2004 Manfred Stienstra (the original version)
License: BSD (see LICENSE for details).
Attributes
logger = logging.getLogger('MARKDOWN')
module-attribute
Classes
Markdown(*args, **kwargs)
Bases: object
Convert Markdown to HTML.
Creates a new Markdown instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args |
Any
|
positional args |
()
|
**kwargs |
Any
|
keyword args |
{}
|
Other Parameters:
Name | Type | Description |
---|---|---|
extensions |
list[Extension | str]
|
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 |
dict[str, Any]
|
Configuration settings for extensions. |
output_format |
str
|
Format of output. Supported formats are: * "xhtml1": Outputs XHTML 1.x. Default. * "xhtml5": Outputs XHTML style tags of HTML 5 * "xhtml": Outputs latest supported version of XHTML (currently XHTML 1.1). * "html4": Outputs HTML 4 * "html5": Outputs HTML style tags of HTML 5 * "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. |
safe_mode |
str
|
Deprecated! Disallow raw html. One of "remove", "replace" or "escape". |
html_replacement_text |
str
|
Deprecated! Text used when safe_mode is set to "replace". |
tab_length |
int
|
Length of tabs in the source. Default: 4 |
enable_attributes |
bool
|
Enable the conversion of attributes. Default: True |
smart_emphasis |
bool
|
Treat |
lazy_ol |
bool
|
Ignore number of first item of ordered lists. Default: True |
Source code in pyrevitlib/pyrevit/coreutils/markdown/__init__.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
|
Attributes
doc_tag = 'div'
class-attribute
instance-attribute
option_defaults = {'html_replacement_text': '[HTML_REMOVED]', 'tab_length': 4, 'enable_attributes': True, 'smart_emphasis': True, 'lazy_ol': True}
class-attribute
instance-attribute
output_formats = {'html': to_html_string, 'html4': to_html_string, 'html5': to_html_string, 'xhtml': to_xhtml_string, 'xhtml1': to_xhtml_string, 'xhtml5': to_xhtml_string}
class-attribute
instance-attribute
safeMode = kwargs.get('safe_mode', False)
instance-attribute
enable_attributes = False
instance-attribute
ESCAPED_CHARS = ['\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!']
instance-attribute
registeredExtensions = []
instance-attribute
docType = ''
instance-attribute
stripTopLevelTags = True
instance-attribute
references = {}
instance-attribute
htmlStash = util.HtmlStash()
instance-attribute
Functions
build_parser()
Build the parser from the various parts.
Source code in pyrevitlib/pyrevit/coreutils/markdown/__init__.py
registerExtensions(extensions, configs)
Register extensions with this instance of Markdown.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
extensions |
list[Extension]
|
extensions strings or objects. See the docstring on Markdown. |
required |
configs |
dict[str, Any]
|
A dictionary mapping module names to config options. |
required |
Source code in pyrevitlib/pyrevit/coreutils/markdown/__init__.py
build_extension(ext_name, configs)
Build extension by name, then return the module.
The extension name may contain arguments as part of the string in the following format: "extname(key1=value1,key2=value2)"
Source code in pyrevitlib/pyrevit/coreutils/markdown/__init__.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
|
registerExtension(extension)
reset()
Resets all state variables so that we can start with a new text.
Source code in pyrevitlib/pyrevit/coreutils/markdown/__init__.py
set_output_format(format)
Set the output format for the class instance.
Source code in pyrevitlib/pyrevit/coreutils/markdown/__init__.py
convert(source)
Convert markdown to serialized XHTML or HTML.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source |
str
|
Source text as a Unicode string. |
required |
Markdown processing takes place in five steps:
- A bunch of "preprocessors" munge the input text.
- BlockParser() parses the high-level structural elements of the pre-processed text into an ElementTree.
- A bunch of "treeprocessors" are run against the ElementTree. One such treeprocessor runs InlinePatterns against the ElementTree, detecting inline markup.
- Some post-processors are run against the text after the ElementTree has been serialized into text.
- The output is written to a string.
Source code in pyrevitlib/pyrevit/coreutils/markdown/__init__.py
convertFile(input=None, output=None, encoding=None)
Converts a Markdown file and returns the HTML as a Unicode string.
Decodes the file using the provided encoding (defaults to utf-8), passes the file content to markdown, and outputs the html to either the provided stream or the file with provided name, using the same encoding as the source file. The 'xmlcharrefreplace' error handler is used when encoding the output.
Note: This is the only place that decoding and encoding of Unicode takes place in Python-Markdown. (All other code is Unicode-in / Unicode-out.)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input |
str | None
|
File object or path. Reads from stdin if |
None
|
output |
str | None
|
File object or path. Writes to stdout if |
None
|
encoding |
str
|
Encoding of input and output files. Defaults to utf-8. |
None
|
Source code in pyrevitlib/pyrevit/coreutils/markdown/__init__.py
Functions
markdown(text, *args, **kwargs)
Convert a Markdown string to HTML and return HTML as a Unicode string.
This is a shortcut function for Markdown
class to cover the most
basic use case. It initializes an instance of Markdown, loads the
necessary extensions and runs the parser on the given text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
Markdown formatted text as Unicode or ASCII string. |
required |
*args |
Any
|
Any arguments accepted by the Markdown class. |
()
|
**kwargs |
Any
|
Any arguments accepted by the Markdown class. |
{}
|
Returns:
Type | Description |
---|---|
str
|
An HTML document as a string. |
Source code in pyrevitlib/pyrevit/coreutils/markdown/__init__.py
markdownFromFile(*args, **kwargs)
Read markdown code from a file and write it to a file or a stream.
This is a shortcut function which initializes an instance of Markdown, and calls the convertFile method rather than convert.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args |
Any
|
Any arguments accepted by the Markdown class. input (str): a file name or readable object. output (str): a file name or writable object. encoding (str): Encoding of input and output. |
()
|
**kwargs |
Any
|
Any arguments accepted by the Markdown class. |
{}
|