Markdown file exported as HTML applet

Source: Internet
Author: User
Tags sublime text

Markdown file exported as HTML applet why do

Recently put some learning experience down, summed up into Markdown file, unknowingly already have 12 articles.
The Sublime Text MarkDown Preview Plugin is capable of converting MarkDown syntax to HTML and provides three ways to preview It: browser preview, Save as an HTML file, and output to a new tab in Sublime.

But one drawback is that I have 12 markdown files that I want to convert to HTML files, which need to be repeated 12 times 打开文件->Crtl+P调出命令面板->输入MarkDown Preview->选择Save To HTML->选择保存路径->确定 .

I have a whim, sublime is based on python, why not use Markdown preview api, write a Python script for batch Conversion. In this way, whether it is 12 or 120, you can easily complete the conversion with a single command, avoiding duplication of Work.

How do you do that?

I found a folder named after Sublime's preferences->browse Packages python-markdwon , which seems to be used to support markdown Conversions.
Read the __init__.py file and learn that the package can convert Markdown to Html. It provides two apis:

    • html = markdown.markdown(your_text_string)

    • html = markdown.markdownFromFile(file_name)

So easy to use, that's it!

After testing, markdown.markdownFromFile() This function is poor for Chinese support: if the file content contains Chinese characters, the converted HTML string appears strangely Garbled. It seems that you can provide additional parameters to specify the encoding format, but I decided not to steal this lazy (well, actually is too lazy to study the source code).

The design idea is simple:

  • Read All. MD files within the specified folder:

    for x in os.listdir(‘./input‘):         if os.path.splitext(x)[1]==‘.md‘:            ...
  • For Each. MD file, its contents are a string

    with open(‘/path/file_name.md‘, ‘r‘) as f:        md = f.read()
  • using markdown.markdown () to html-formatted pairs of strings

    html = markdown.markdown(md)
  • Writes an HTML format string to an. HTML file

    with open(‘/path/file_name.html‘, ‘w‘) as f:       f.write(html)

of course, the problem of path, character encoding and so on is also considered. This is not listed here, detailed code can be see BELOW.

How to use

I have created such a file structure:

 . |---markdown | |---input |   |---1.md |   |---2.md | |---output |   |---1.html |   |---2.html | |---run.py | |---empty_output.py
  • markdown文件夹is directly copied from the sublime plug-in directory python-markdown .

  • input文件夹The markdown file is placed in front of the conversion, only the English file name is currently Supported.

  • output文件夹The converted HTML file is automatically produced, with the same name as the corresponding markdown File.

  • run.pyIs the script file that is executed to convert the markdown to Html.

  • empty_output.pyUsed to empty the contents of the output folder without deleting the Folder.

The steps to run are simple:

    • markdown files into the input folder

    • command line execution python run.py

    • go to the output folder to find the converted file

Why only English filenames are supported? I also want to solve this problem, search some blog, in MacOS Test Chinese file name all Ok. But it over face not under Windows. Or my Python daoxing is too shallow, simply support the English file Name.

What you've done.

Put run.py empty_output.py the code here AND. The most core conversion algorithm, python-markdown, can be found on github.

The source code for the entire program can be obtained here.

run.py
    ImportOsImportMarkdownImportCodecsImportSYS Reload (sys) sys.setdefaultencoding (' Utf-8 ') Input_dir ='./input 'Ouput_dir ='./output 'Input_file_type ='. MD 'Ouput_file_type ='. html '        Print ' \ n '     forFull_input_file_nameinchOs.listdir (input_dir):ifOs.path.splitext (full_input_file_name) [1]==input_file_type:Print ' converting '+ Full_input_file_name +' ... 'file_name = Os.path.splitext (full_input_file_name) [0] Full_input_file_name = Input_dir +'/'+ Full_input_file_name Full_ouput_file_name = Ouput_dir +'/'+ file_name + ouput_file_type withCodecs.open (full_input_file_name,' R ') asIfile:in_file_content = ifile.read () ou_file_content = Markdown.markdown (in_file_content) withCodecs.open (full_ouput_file_name,' W ',' GBK ') asOfile:ofile.write (ou_file_content)Print ' \nall done! '
empty_output.py
    ImportOsImportMarkdownImportCodecsImportSYS Reload (sys) sys.setdefaultencoding (' Utf-8 ') Ouput_dir ='./output '        Print ' \ n '     forfile_nameinchOs.listdir (ouput_dir):Print ' Deleting '+ file_name +' ... 'Full_file_name = Ouput_dir +'/'+ file_name Os.remove (full_file_name)Print ' \nall done! '

Markdown file exported as HTML applet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.