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.
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.
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
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.
markdown-server provides routing in the form http: // host / [file_name]
for Markdown files located in resources / markdown / [file_name]
.
The converted files will be placed in the resources / html
directory. CSS is also included in the generated HTML file, so distribution is easy.
Environment variables such as host name and port number are grouped in ʻenv.py`. You can change it as appropriate.
env.py
ms_port = '8009'
ms_host = 'localhost'
env.py
css_name = 'github.css'
markdown_type = 'gfm'
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)
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.