Python Print Markdown



First and foremost, Python-Markdown is intended to be a python library moduleused by various projects to convert Markdown syntax into HTML.

The Basics¶

To use markdown as a module:

Nearly all Markdown applications support the basic syntax outlined in John Gruber’s original design document. There are minor variations and discrepancies between Markdown processors — those are noted inline wherever possible. To create a heading, add number signs (#) in front of a word or phrase. The number of number signs you. Split the file into yaml and markdown parts; Extract the meta-data from the YAML. Convert the markdown to an HTML fragment (the page content). Combine the meta-data and page content with the HTML template to create a complete HTML file. Fortunately, if we use the right Python libraries, each of these steps is very easy. Splitting the file.

The Details¶

Python-Markdown provides two public functions (markdown.markdownand markdown.markdownFromFile) both of which wrap thepublic class markdown.Markdown. If you’re processing onedocument at a time, these functions will serve your needs. However, if you needto process multiple documents, it may be advantageous to create a singleinstance of the markdown.Markdown class and pass multiple documents throughit. If you do use a single instance though, make sure to call the resetmethod appropriately (see below).

markdown.markdown(text [, **kwargs])¶

The following options are available on the markdown.markdown function:

text
Python Print Markdown

The source Unicode string. (required)

Important

Python-Markdown expects a Unicode string as input (some simple ASCII binary strings may work only bycoincidence) and returns output as a Unicode string. Do not pass binary strings to it! If your input isencoded, (e.g. as UTF-8), it is your responsibility to decode it. For example:

Markdown

If you want to write the output to disk, you must encode it yourself:

extensions

A list of extensions.

Python-Markdown provides an API for third parties towrite extensions to the parser adding their own additions or changes to thesyntax. A few commonly used extensions are shipped with the markdownlibrary. See the extension documentation for alist of available extensions.

The list of extensions may contain instances of extensions and/or stringsof extension names.

Note

The preferred method is to pass in an instance of an extension. Stringsshould only be used when it is impossible to import the Extension Classdirectly (from the command line or in a template).

When passing in extension instances, each class instance must be a subclassof markdown.extensions.Extension and any configuration options should bedefined when initiating the class instance rather than using theextension_configs keyword. For example:

If an extension name is provided as a string, the string must either be theregistered entry point of any installed extension or the importable pathusing Python’s dot notation.

See the documentation specific to an extension for the string name assignedto an extension as an entry point. Simply include the defined name asa string in the list of extensions. For example, if an extension has thename myext assigned to it and the extension is properly installed, thendo the following:

If an extension does not have a registered entry point, Python’s dotnotation may be used instead. The extension must be installed as aPython module on your PYTHONPATH. Generally, a class should be specified inthe name. The class must be at the end of the name and be separated by acolon from the module.

Therefore, if you were to import the class like this:

Then load the extension as follows:

If only one extension is defined within a module and the module includes amakeExtension function which returns an instance of the extension, thenthe class name is not necessary. For example, in that case one could doextensions=['path.to.module']. Check the documentation for a specificextension to determine if it supports this feature.

When loading an extension by name (as a string), you can only pass inconfiguration settings to the extension by using theextension_configs keyword.

See Also

See the documentation of the Extension API forassistance in creating extensions.

extension_configs

A dictionary of configuration settings for extensions.

Any configuration settings will only be passed to extensions loaded by name(as a string). When loading extensions as class instances, pass theconfiguration settings directly to the class when initializing it.

Note

The preferred method is to pass in an instance of an extension, whichdoes not require use of the extension_configs keyword at all.See the extensions keyword for details.

The dictionary of configuration settings must be in the following format:

When specifying the extension name, be sure to use the exact samestring as is used in the extensions keyword to load theextension. Otherwise, the configuration settings will not be applied tothe extension. In other words, you cannot use the entry point in onplace and Python dot notation in the other. While both may be valid fora given extension, they will not be recognized as being the sameextension by Markdown.

See the documentation specific to the extension you are using for help inspecifying configuration settings for that extension.

output_format:

Format of output.

Supported formats are:

  • 'xhtml': Outputs XHTML style tags. Default.
  • 'html5': Outputs HTML style tags.

The values can be in either lowercase or uppercase.

tab_length:

Length of tabs in the source. Default: 4

markdown.markdownFromFile (**kwargs)

With a few exceptions, markdown.markdownFromFile accepts the same options asmarkdown.markdown. It does not accept a text (or Unicode) string.Instead, it accepts the following required options:

input (required)

The source text file.

input may be set to one of three options:

  • a string which contains a path to a readable file on the file system,
  • a readable file-like object,
  • or None (default) which will read from stdin.
output

The target which output is written to.

output may be set to one of three options:

  • a string which contains a path to a writable file on the file system,
  • a writable file-like object,
  • or None (default) which will write to stdout.
encoding

The encoding of the source text file.

Defaults to 'utf-8'. The same encoding will always be used for input and output.The xmlcharrefreplace error handler is used when encoding the output.

Note

This is the only place that decoding and encoding of Unicodetakes place in Python-Markdown. If this rather naive solution does notmeet your specific needs, it is suggested that you write your own codeto handle your encoding/decoding needs.

markdown.Markdown([**kwargs])¶

The same options are available when initializing the markdown.Markdown classas on the markdown.markdown function, except that the class doesnot accept a source text string on initialization. Rather, the source textstring must be passed to one of two instance methods.

Warning

Instances of the markdown.Markdown class are only thread safe withinthe thread they were created in. A single instance should not be accessedfrom multiple threads.

Markdown.convert(source)¶

The source text must meet the same requirements as the textargument of the markdown.markdown function.

You should also use this method if you want to process multiple stringswithout creating a new instance of the class for each string.

Depending on which options and/or extensions are being used, the parser mayneed its state reset between each call to convert.

To make this easier, you can also chain calls to reset together:

Markdown.convertFile(**kwargs)¶

The arguments of this method are identical to the arguments of the samename on the markdown.markdownFromFile function (input,output, and encoding). As with theconvert method, this method should be used toprocess multiple files without creating a new instance of the class foreach document. State may need to be reset between each call toconvertFile as is the case with convert.

There are many markdown parsers in Python. Misaka was my favorite one. However, misaka is deprecated now, and the successor which is called hoedown still has issues to solve. That's why it is a was. But I still love it.

Here is a list of markdown parsers for Python in my knowledge:

  • Misaka: A python binding for Sundown. (CPython required)
  • Hoedown: A python binding for Hoedown, successor of Misaka.
  • Discount: A python binding for Discount. (CPython required)
  • cMarkdown: Markdown for Python, accelerated by C. (CPython required)
  • Markdown: A pure markdown parser, the very first implementation.
  • Markdown2: Another pure markdown parser.

And I've just released another pure markdown parser too, which is called mistune.

Misaka

Misaka was my favorite markdown parser. It is a python binding of Sundown, which means that it has all the features that Sundown provides.

It is super fast! Actually, it is the top one in my benchmarks. Since it is a binding of a C library, no wonder that it is this fast. If speed is what you want, you should try misaka, and as well as other bindings of a C library.

Python

But misaka is more than speed. It is the custom renderer feature that catches my heart. I am so fond of it, that's why I implement the custom renderer feature in my own markdown parser mistune.

A quick and very useful sample is code highlighting.

However, it is a binding of a C libary. It requires CPython, if you prefer PyPy, you have no access to it. Some App Engines have a limitation on compiling C libraries too, you can't use misaka in this case. And even if you are using CPython, it is still difficult to install it on a Windows OS.

Visual Studio's support for C is not optimal and most VS compilers are missing stdint.h, which is needed to compile Misaka.

Python Markdown To Html

If you are on a Windows, may god helps you. I don't care it a shit.

Markdown

Footnote feature is missing in Misaka. Maybe many of you don't need such a thing, in this case, misaka has nothing bad. It is stable, efficient, and has many GFM features.

The only trouble is Sundown is deprecated.1

Hoedown

Because the Sundown library is deprecated, here comes hoedown2, which is the fork of the original Sundown. It has a Python binding also called as hoedown.

Since Hoedown is the successor of Sundown, and python-hoedown is the successor of Misaka, all features that misaka has, python-hoedown has them too. But python-hoedown is more than that.

  1. It is PyPy compatible.
  2. It has footnote feature.

It looks promissing, and even misaka's author recommends it. I've tried it, but failed with one issue, a magic error that I can't do anything:

This isssue is not fixed yet. Once it does, hoedown would be a good choice for non-AE users.

Updated at Jun 23, 2014: you can use Hoep as the Python Binding.

cMarkdown & Discount

cMarkdown is much like Misaka, except that it is based on upskirt3 rather than sundown. The history is very interesting, sundown is a fork of upskirt, hoedown is a fork of sundown. And now, sundown is deprecated, upskirt is missing. The new markdown parser that vmg promised is still not available.

cMarkdown has all the disadvantages of Misaka, and it is a little slower than Misaka. This means you really should use misaka instead of cMarkdown.

Discount is a joke for me, I can't even install it successfully! There is not much to say. But I do know that Discount is slower than Sundown.

Markdown & Markdown2

Python-Markdown is the very first markdown parser in pure Python. It is good, except the documentation. However, I miss the renderer feature in misaka, which is not in Python-Markdown.

Python-Markdown is not that slow as I expected, since Python-Markdown2 calls itself as:

A fast and complete implementation of Markdown in Python.

But it is not true. Python-Markdown2 is much slower than Python-Markdown. I have no idea why it says itself fast. All features that 2 has, the older one has too.

The benchmark shows that Python-Markdown2 is almost twice slower than Python-Markdown. No wonder it is 2.

Mistune

Mistune is a new (just released) markdown parser. It is the fastest one in all pure Python implementations. Almost 4 times faster4 than Python-Markdown in pure Python environment, almost 5 times faster with Cython's help.

Python Print Markdown Program

I didn't expect it to be so fast when I wrote it. I know it would be a fast one, but I didn't know that it would be 4 times faster and even 5 times faster.

I have never thought of creating a Markdown parser my own. But it has been months since I reported the issue to Hoedown. The issue is still there, not solved a bit. Because it is a C binding, I am not able to do any help, the only thing I can do is waiting.

I don't use Python-Markdown or Python-Markdown2, because they have no renderer feature, and they are slow.

Python markdown to html

I have introduced renderer feature to marked, which is finally merged. And now I am trying to add the footnote feature. It occured to me that I can port marked to Python, since I know marked well, and I know it is the fastest in all pure JavaScript implementations. It would be fast in Python too, and it really does.

If you are looking for a fast, full featured5 and pure Python implementation, Mistune is a good choice. It also has renderer feature just like Misaka. You can always influnce the rendering results with custom renderers.

Additional Notes

I did a benchmark on my MacBook Air, view the results. You can run the benchmark script yourself: bench.py

Python Markdown File

Mistune can be compiled with Cython if you have Cython installed already.

The magic happens in the setup.py script. I'd like to introduce this part another time.

mistune is used by many great projects such as IPython, Rodeo and crossbar.

Markdown Python Code

*This post and all posts in markdown format on this site are rendered with mistune.*