VIM + \ latex + tth + Python + metaweblog API blog writing strategy

Source: Internet
Author: User

VIM + \ latex + tth + Python + metaweblog API blog writing strategy

VIM + latex + tth + Python + metaweblog API write blog introduction mdyang Chapter 1
Preparations

First, install the latex environment. This article uses Ubuntu + Tex live and the editor uses vim. If you are familiar with other editors such as Emacs, you can also.

 

With the latex environment, you can useLatex,PdflatexAnd other commands to compile the latex source code to generate the corresponding DVI or PDF file. As we need to write a blog post and HTML documents, we need to convert latex into HTML tools. We recommend tth, a tool that compiles latex into HTML.

 
1.1 tth: texto HTML Conversion Tool

The simplest use of tth is:

 
tth input.tex
 

When you run this commandInput. TexCompileInput.htmlNote that tth ignores the CJK macro package, so\ Usepackage {CJK (utf8 )},\ Begin {CJK}And\ End {CJK}And so on. Although the CJK macro package is ignored, but tth can still correctly process Chinese, because the HTML file generated by tth is UTF-8 encoding, so you only need to ensure that the input latex source code is also UTF-8 encoding. When you open the generated. HtmlThe Chinese characters may be garbled during the file, because the HTML file generated by tth is missing<Head>One of<Meta>Tags:

 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
 

This line tells the browser that the page encoding is a UTF-8, the lack of this line of browser may choose the wrong set of display page, for example, in the Chinese version of Windows system may use GBK encoding to display the page, resulting in Chinese garbled characters. If you manually set the page encoding to a UTF-8 in the browser's viewing settings, you will find that the text in it is correctly displayed. So this is just a small problem. You just need to insert the above in the HTML file.<Meta>Label.

1.2 pyblog: Python Implementation of metaweblog API

Metaweblog is a set of RFC standards, metaweblog API is a set of XML-RPC-based Remote Call of the API, you can use these APIs for blog operations, including publishing logs, modify logs, classification management, etc. Mainstream programming languages have XML-RPC library. In python, this library isXmlrpclib. However, before implementing the python-based metaweblog API, I searched on Google Code in line with the principle of avoiding repeated inventions. I found a python library of metaweblog API: pyblog. This library encapsulates some common functions of the metaweblog API and also supports the WordPress API (the superset of the metaweblog API ). In the United States, the library seems to have some errors. In my Windows Server 2008 r2 + Python 2.7.2 amd64, an error is reported. After research, we found that 91st lines of code had errors:

 
r = getattr(self.server, methodname)(args)
 

It should be:

 
r = getattr(self.server, methodname)(*args)
 

Another weakness for me is that the pyblog library does not provide support for the HTTP proxy server.XMLRPCAdded methods supported by the HTTP Proxy Server to improve the pyblog code. Note thatUrllibtransportClass constructor lacks the following sentence:

 
self._use_datetime = True
 

I added this sentence because the original log containsUrllibtransportWhen the class code is applied to pyblog, an error is reported, sayingUrllibtransportClass does not_ Use_datetimeThis attribute. I guess this attribute should be a boolean type based on the name, so I added the above sentence. The code works correctly after being added.

 

Now the two most important problems have been solved:

 
  • Convert latex code to HTML (tth)

     
  • Use the metaweblog API to publish HTML code logs to a blog (using pyblog)
 

The next step is to connect the two steps. Considering that pyblog is written in Python, use python as the glue language to connect the two steps.

Chapter 2
Pyposter: Python script for connecting latex and metaweblog APIs
2.1 design objectives

Compared with the WYSIWYG editor, the biggest inconvenience of Using latex to write blogs is that what you see is what you get. For a written blog, if you want to check the draft, you need to compile it and view it in a browser. Therefore, using latex for blog writing is mainly divided into the following stages:

 
  1. Write latex source code

     
  2. Compile the latex source code as HTML and preview it in the browser. If there is no problem, go to the next step. Otherwise, return 1. Modify
  3. Send a written blog to a blog
 

Based on the above considerations, I have designed two features for pyposter:BuildAndPost. WhereBuildCompile latex source code into HTML and start an HTTP server to facilitate preview,PostUse the metaweblog API to publish the HTML source code to the blog. The following sections detail the functions.

 
2.2 Build: Latex source code compilation and blog Preview

Pyposter is a Python script whose file name isPyposter. This script hasBuildAndPostTwo functions. This section introducesBuildFunction.

 

For latex source code compilation, you only need to call tth. In pythonOS. System ()Run the command.

 

After compilation, You need to preview the content. First, you need to add the missing content to the HTML generated by tth.<Meta>Label to declare the file encoding, so that the display code page of the browser can be displayed normally without manual modification. This can be achieved through simple text processing, seePyposterInBuild ().

 

As for previewing, Python has a built-in simple HTTP server.Simplehttpserver, You can run the following command to start:

 
python -m SimpleHTTPServer
 

The default homepage is the file. Otherwise, the file list is displayed.

 

After compilation and preview are completed, you needPyposterPerform some processing so that it can be used anywhere in the system. You can execute the pyposter Python script at any location through the following three settings:

 
  • InPyposterAdd the first line#! /Usr/bin/Python(Tell the system that the file is executed by Python)

     
  • Is a filePyposterAdd execution permission (Chmod + x pyposter)
  • Add the path of pyposter to the Environment Variable$ Path 
 

After the settings are complete, you can use the following command to compile and preview a blog post written using latex:

 
pyposter build
 

The above command uses tth to compileMain. Tex, GenerateMain.html, Row-by-row ScanMain.htmlTo add the following encoding information:

 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
 

Modified HTML code is outputIndex.htmlThen an HTTP server is started in the current directory and accessed in the browserHttp: // host: 8000To Preview (note:HostReplace it with a specific host name or IP address ). Press Ctrl + C to finish previewing.Pyposter buildProcess. If modification is required, return Vim for modification, and compile and preview after modification. If the article is completed, proceed to the next step.

2.3 Post: Publish a blog

A small problem to be solved when posting a blog post is that the metaweblog API is called.New_postYou must specify the blog title separately. Therefore, you need to extract the article title from the HTML file. You only need<Title>The text in the label can be extracted. Simple text processing is not described here. For more information, seePyposterOfPost ()Function.

 

The implementation of the publish operation is much simpler. You can directly call the encapsulated metaweblog API in pyblog. I made some minor changes to the pyblog Code so that it can use the HTTP proxy server.

 

Run the following commandIndex.htmlPublished as BLOG:

 
pyposter post
Chapter 3
Summary

I have put pyposter on Google Code: pyposter at Google Code. Modify the configuration fileConf. pyPyposter can be applied to other blogs.

 

Pyposter can now:

 
  • Compile the latex source code with one command and preview the HTML (throughPyposter build)

     
  • A command is used to publish an HTML blog post to a blogPyposter post)
 

Pyposter has basic functions. However, many functions need to be improved:

 
  • Although you can obtain all categories correctly (through pyblog'sMetaweblog. get_categories ()), But still cannot add category tags for blog posts

     
  • Unable to manage category tags (add or delete category tags)
  • You cannot set the entryname of a blog.
  • Not considering the situation where a blog post contains images
  • Editing of published blog posts is not supported currently)
 

These are functions that will be improved later. In addition, due to the current application scenarios, pyposter can only be used for the metaweblog API of the blog Park. Other blog systems have not yet been verified. In addition to the metaweblog API, pyblog also implements the WordPress API, and its functions need to be verified.

 

If you are interested in the trial, please correct me.

 

File translated from
Tex
By
Tth,
Version 4.01.
On 15 May 2012.

Related Article

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.