[PYTHON] How to easily convert format from Markdown

I was asked to create a product manual with a mechanism similar to a simplified version of O'Reilly Atlas. I wanted to use Asciidoctor to create the manual, so I decided to write it in AsciiDoc format, but it was easier to get used to Markdown format, so I had to convert from Markdown format to AsciiDoc format. It was. I thought I could afford to use pandoc, but I couldn't convert as I intended.

What I looked up

If you use pandoc, you should study Haskell and fix it, but I don't have time, so I wondered if I could make a conversion program using Python's Markdown parser library.

The table is as follows.

Evaluation item Python-Markdown Misaka Mistune
URL https://github.com/waylan/Python-Markdown https://github.com/FSX/misaka https://github.com/lepture/mistune
license BSD MIT BSD
Implementation python python + C python
Corresponding version Python 2.7, 3.3+ and PyPy Python 2.7, 3.2+ and PyPy 2.6 Python 2.6+, Python 3.3+ and PyPy
Output custom ×
Remarks Depends on Hoedown

Python-Markdown is a path because you can't customize the output. Misaka seems to be dead, but it's a pass because it depends on Hoedown.

I decided to use Mistune by the elimination method, so I will implement it using this.

Easy to use

Installation

It seems to work with Python 2.6 or higher, so I confirmed the operation with 2.7.11.

pip install mistune

It's easy!

Convert from Markdown to HTML

We have prepared the following Markdown format files and sources for HTML conversion. It is almost the same as what is written in the official Basic Usage.

test.md


#Sample documentation
##Chapter XX
###Section XXX

convert.py


import mistune

with open('test.md', 'r') as test_file:
    markdown = mistune.Markdown()
    print markdown(test_file.read())

And run it.

python convert.py

Then, the output result is as follows.

output


<h1>Sample documentation</h1>
<h2>Chapter XX</h2>
<h3>Section XXX</h3>

Since it is partially applied, it seems good to prepare a template separately from the HTML definition etc. and make it as if rewriting only the contents of the Body.

Convert from Markdown to free format

Now the main subject.

It seems that mistune should inherit the Renderer class and override the item (method) you want to output. This time, I want to replace the heading tag with section in AsciiDoc format, so I made it as follows.

custom_render.py


import mistune

class CustomRenderer(mistune.Renderer):
    def header(self, text, level, raw=None):
        section = '=' * level
        return '{0} {1}\n'.format(section, text)

if __name__ == '__main__':
    custom_renderer = CustomRenderer()
    with open('test.md', 'r') as test_file:
        markdown = mistune.Markdown(renderer=custom_renderer)
        print markdown(test_file.read())

I will do it.

python custom_render.py

The result is as follows.

output


=Sample documentation
==Chapter XX
===Section XXX

I got the results I expected!

After that, if you override the method from the required syntax and arrange the output format, you can convert the format.

Recommended Posts

How to easily convert format from Markdown
How to convert from .mgz to .nii.gz
Linux script to convert Markdown files from JupyterLab format to Qiita format
How to convert DateTimeField format in Django
Convert from Markdown to HTML in Python
[Tensorflowjs_converter] How to convert Tensorflow model to Tensorflow.js format
How to convert Json file to CSV format or EXCEL format
[Caffe] Convert mean file from binary proto format to npy format
Convert markdown to PDF in Python
How to convert 0.5 to 1056964608 in one shot
Convert from pdf to txt 2 [pyocr]
How to launch Explorer from WSL
How to convert Tensorflow model to Lite
How to access wikipedia from python
Convert PIL format images read from form with Django to base64 format
[Python] How to convert db file to csv
How to create a clone from Github
Convert xml format data to txt format data (yolov3)
[Python] How to convert a 2D list to a 1D list
How to convert csv to tsv in CLI
How to update Google Sheets from Python
How to convert Python to an exe file
Convert matplotlib graphs to emf file format
Convert from PDF to CSV with pdfplumber
[TF] How to use Tensorboard from Keras
How to utilize multi-core from multiple languages
Convert from katakana to vowel kana [python]
Convert PDF attached to email to text format
How to access RDS from Lambda (python)
How to operate Linux from the console
How to collect face images relatively easily
Convert Python date types to RFC822 format
How to create a repository from media
How to access the Datastore from the outside
How to easily operate IOT home appliances from Siri by API hacking
How to convert m4a acquired by iTunes to wav
How to open a web browser from python
Convert to HSV
How to create a function object from a string
[Python] How to change the date format (display format)
Study from Python Hour7: How to use classes
[Python] Convert from DICOM to PNG or CSV
How to get results from id in Celery
[Python] How to read data from CIFAR-10 and CIFAR-100
Easily convert Jupyter Notebooks to blogs with fastpages
How to generate a Python object from JSON
Sum from 1 to 10
How to call Cloud API from GCP Cloud Functions
[Introduction to Python] How to handle JSON format data
How to operate Linux from the outside Procedure
How to handle Linux commands well from Python
How to convert SVG to PDF and PNG [Python]
How to extract coefficients from a fractional formula
How to measure line speed from the terminal
[Python] How to easily drop a child process started by multiprocess from another process
How to Git from GCP's Jupyter Lab to GSR
Convert json format data to txt (using yolo)
How to convert (32,32,3) to 4D tensor (1,32,32,1) with ndarray type
Learn how to inflate images from TensorFlow code
Convert binary packages for windows to wheel format
Convert strings to character-by-character list format with python