1. Project Introduction
Hzwz-markdown is a Python-based tool for converting Markdown files into styled public article HTML.
Warehouse address: github.com/coder-pig/h…
Simple point is: with a style template, run a direct generation with style HTML, paste to the public number editor can be directly sent.
2. Project Background
I used to use Markdown to write technical papers and then post them on a public account, not directly, but typesetted.
Typesetting can be described as physical work, especially in the case of tables, one by one manual copy and paste, often need to queue for more than half an hour.
Typesetting is arranged according to a unified style, so it is repetitive work, and I am in urgent need of a tool to improve my typesetting efficiency.
There are many excellent Markdown tools on the Internet, such as Markdown Conversion Tool, Md2All, MPEditor, Mdnice, etc.
At the beginning of the use is still ok, the back found that the style is too single, a bunch of public number is also in use, the distinction is not high, some support custom, but to change their OWN CSS, there is a certain threshold, no front-end experience of the user may be difficult to start. Most of my users probably want the same thing:
I don’t know color matching, I don’t know CSS, I just think other people’s articles look good, I want to use it in my own articles, that’s all.
Therefore, I came up with the idea of writing a tool by myself. I hope it will:
- β Style flexible: STYLE I can’t write, I will steal ah, see like the public style directly stolen, who else?
- (2) Simple and easy to use: users through as simple as possible configuration, one button to complete the conversion, once and for all.
- β’ Pure & customizable: Open source code, no illicit, pure transparent, Python can also be customized.
3. Practice process
Technology selection and basic principle
The main reason for developing in Python is that the author is better at Python than other languages (such as JavaScript) that might be easier to implement.
The core of the tool is simple:
Parsing Markdown files β rendering elements of a specific type β generating HTML files
This is followed by a step-by-step refinement of these three steps:
Parse the Markdown file
To parse Markdown and distinguish between different types of elements, use wheel β mistune:
- One of the fastest pure Python Markdown fast parsers yet, inspired by marked.
- Official warehouse: github.com/lepture/mis…
- The official document: mistune. Readthedocs. IO/en/latest /
β‘ Render elements of a specific type
That is, different types of elements are converted into HTML with inline style, and string stitching is directly used, which is redundant and poor in reuse. The concept of template is introduced here, and the style of each element is saved into a separate HTML template, which is rendered with time-passing parameters. Wheels β Jinja2 template library is used here.
β’ Generate HTML files
Stitching together the HTML code after all the elements have been rendered, you can also do some DIY, such as adding a diversion style at the top and bottom of the article, and finally exporting it to an HTML file.
β‘ Specific implementation details
Write a Markdown conversion tool in Python to create a Markdown conversion tool for each element type.
Here are two styles that I have customized and are using to show you the effect:
1) Python series
β‘ Kotlin series: blowout, head cover picture + text /CoderPig, learn from Google developers public style, title color according to red yellow switch, hyperlink exquisite style, bottom drainage end+ picture ~
4. Summarize and think
In fact, the development of a lot of pit, such as newline \n was eaten by wechat, code highlighting how to solve, but fortunately also liver out.
There are still some minor bugs in the process of self-use, such as the logic of paragraph-wrapping, strange Spaces in references, etc. There are also incomplete functions, and Latex formulas are not supported.
π€‘ there will be time to gradually improve, welcome to try out the Star, raise issues and suggestions, thank you, life is too short, I use Pythonπ!
This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign