I made a web application in Python that converts Markdown to HTML

First Qiita ... everyone ...

I will talk about creating a web application called Markdown-> HTML conversion ** markdown-server **. Source code: https://github.com/ohbarye/markdown-server

What is markdown-server? It is a simple web application that converts Markdown files to HTML and responds in text / html format. The default Markdown Engine is Github flavored Markdown.

Motivation

I write personal notes such as procedure manuals and TODO at the company in Markdown (Addition: It is a story of the workplace where I worked until 2015), but since it is a company that loves Excel, when it comes to showing it to others, "Markdown" What? ”Starts from that point. So I wondered if there was a good way to show or distribute it to people who don't have a Markdown editor.

How to use / To move for the time being

You need a python runtime environment. Development / operation check is done in 2.7.9. It's not required, but it's quick if you can git clone.

The server will start with just the following command.

$ git clone https://github.com/ohbarye/markdown-server
$ cd markdown-server
$ pip install -r packages_requirements.txt
$ python start_server.py

When the server starts, access the following address and check the conversion result of the sample Markdown file.

$ open http://localhost:8009/sample.md

Example

A Markdown file like this

# Convert Markdown to HTML with Python

 HTMLize with Github flavored Markdown.

## Library used

 |No.|Name|Description|
 |:-|:-|:-|
 |1|markdown|Markdown ->HTML conversion library|
 |2|pygments|For syntax highlighting|
 |3|bottle|Web application framework|

## Code syntax highlighting

\```python
import markdown as md

class MarkdownConverter(object):

    def convert(self,file_name):
        code = md.markdown(self.read_md(file_name), extensions=['gfm'])
        return self.write_html(file_name,code)

    def read_md(self,file_name):
        md_file = codecs.open(markdown_root + file_name,encoding=ms_encoding,mode='r')
        return md_file.read()

\```

It will be drawn like this.

http:localhost/8009/sample.md

Features / Expansion method

env.py


ms_port        = '8009'
ms_host        = 'localhost'

env.py


css_name       = 'github.css'
markdown_type  = 'gfm'

About the technology used

Dependent libraries

It mainly depends on the following libraries. See the GitHub repository (https://github.com/ohbarye/markdown-server/blob/master/packages_requirements.txt) for the entire library, including the libraries they depend on.

No. Name Description
1 markdown Markdown ->HTML conversion library
2 pygments For syntax highlighting
3 bottle Web application framework

markdown

Markdown conversion library. Most of the functions of this server are the functions of this server. https://pypi.python.org/pypi/Markdown Qiita also introduces how to use it in this article. http://qiita.com/kimihiro_n/items/982c6fc0b3c7cf226799

Basically, you can convert with just this.

import markdown as md
md.markdown("# markdown text")

If you want to change the markup format, specify ʻextensions`. Below is an example of Github Flavored Markdown.

import markdown as md
md.markdown("# markdown text", extensions=['gfm'])

However, the above code alone does not convert well with GFM. I was particularly addicted to this time, but it is said that the behavior of markdown conversion changes depending on whether or not pygments described below is installed. (Seriously)

pygments

Python syntax highlighter. http://pygments.org/ https://showa-yojyo.github.io/note/python-pygments.html

The relationship between the movement of markdown and pygments was summarized in the following article.

Syntax highlighting is not working. However, when I included Markdown + py-gfm in MoinMoin in my last entry, it had syntax highlighting. why?

I was curious about this and read various sources, but the answer is that MoinMoin depends on pygments. The Markdown library behaves differently before and after installing pygments. http://tototoshi.hatenablog.com/entry/2014/05/17/020241

As you can see from the source, this time I haven't ʻimport pygments` anywhere. I just installed it for GFM only.

bottle

Web application framework library. http://bottlepy.org/docs/dev/index.html

Speaking of Python web application FW, I knew only ** Django **, but there are many others.

This time, I chose ** bottle ** with the highest priority on "lightness". The code below is pulled from the official as it is, but it is a tremendous lightweight feeling that Hello world can be done in 5 lines.

from bottle import route, run, template

@route('/hello/<name>')
def index(name):
    return template('<b>Hello {{name}}</b>!', name=name)

run(host='localhost', port=8080)

This time, I don't even use the template function because I only respond to the converted HTML. The method for responding to static files is static_file.

start_server.py


return static_file(html_file_name, root=ms_root)

Impressions-> Python is good

I usually use Java as the main language, and I rarely write scripting languages, so it was refreshing. Package management with ** pip ** / Development environment management with ** pyenv ** / Development without IDE (** Atom ** this time).

By the way, my Python history is about ** codecademy **, which I did all the Python courses in a week. I thought it was a good language with no quirks, easy to write, and no strange addiction to writing simple scripts.

I hope the number of Markdown users will increase in the company as well.

Recommended Posts

I made a web application in Python that converts Markdown to HTML
Steps to develop a web application in Python
I made a web application that converts photos into Van Gogh's style
I made a payroll program in Python!
Convert from Markdown to HTML in Python
I made a WEB application with Django
I wrote a script to extract a web page link in Python
I made a library that adds docstring to a Python stub file.
I made a toolsver that spits out OS, Python, modules and tool versions to Markdown
I made a program to collect images in tweets that I liked on twitter with Python
I made a plugin to generate Markdown table from csv in Vim
A memo that I wrote a quicksort in Python
[Python] I made a decorator that doesn't seem to have any use.
I made a GUI application with Python + PyQt5
I want to create a window in Python
I made a Discord bot in Python that translates when it reacts
I made a garbled generator that encodes favorite sentences from UTF-8 to Shift-JIS (cp932) in Python
I tried to develop a Formatter that outputs Python logs in JSON
I tried to discriminate a 6-digit number with a number discrimination application made with python
I made a script in python to convert .md files to Scrapbox format
[IOS] I made a widget that displays Qiita trends in Pythonista3. [Python]
I made a program to check the size of a file in Python
I made a Caesar cryptographic program in Python.
I want to make a web application using React and Python flask
What I was addicted to when creating a web application in a windows environment
I made a Line Bot that uses Python to retrieve unread Gmail emails!
I want to create a priority queue that can be updated in Python (2.7)
[ES Lab] I tried to develop a WEB application with Python and Flask ②
I made a familiar function that can be used in statistics with Python
I want to create a web application that uses League of Legends data ①
I made a module in C language to filter images loaded by Python
I searched for the skills needed to become a web engineer in Python
In Python, I made a LINE Bot that sends pollen information from location information.
A story that I was addicted to when I made SFTP communication with python
I want to easily implement a timeout in python
I made a prime number generation program in Python
(Python) Try to develop a web application using Django
I want to write in Python! (2) Let's write a test
I made a VM that runs OpenCV for Python
I made a script to put a snippet in README.md
I tried to implement a pseudo pachislot in Python
I made a Python module to translate comment outs
I want to randomly sample a file in Python
I want to work with a robot in python.
[Python] A convenient library that converts kanji to hiragana
I made a prime number generation program in Python 2
I made a command to markdown the table clipboard
I made a python library to do rolling rank
I made a python text
[Python + Bottle] I tried to release a web service that visualizes Twitter's positioned tweets.
I made a web application that maps IT event information with Vue and Flask
I can't sleep until I build a server !! (Introduction to Python server made in one day)
I made a simple typing game with tkinter in Python
I tried to implement a one-dimensional cellular automaton in Python
I made a package to filter time series with python
I made a quick feed reader using feedparser in Python
I made a simple book application with python + Flask ~ Introduction ~
I made a command to generate a table comment in Django
I tried "a program that removes duplicate statements in Python"
I tried "How to get a method decorated in Python"
I made a puzzle game (like) with Tkinter in Python