Emacs for beginners: Preparation of articles for Habr in Emacs
Introduction
We have talked a lot about the power of Emacs. Let's solve some practical problem. For example, we will learn how to prepare texts for Habr’s articles with a minimum of manual work.
Let's estimate what we need:
- Convert text to HTML
- Spellchecker
- Text syntax highlighting for programs in different languages
Getting down.
Convert text to HTML: muse mode
The most traditional UNIX way. The bottom line is that we write the article in text form, observing some conventions, quite simple, such simple markup rules, and then process this text with a parser that turns our text into an HTML file, which we insert into the Habr article editing form.
Installation
On Debian / Ubuntu, a package with this mode is called muse-el . So:
apt-get muse-el
In .emacs add:
(require 'muse) (require 'muse-html)
Now you can enable muse mode:
Mx muse-mode
Markup
The markup when working with muse is very simple, for example, if you start a line with an asterisk, or several, when broadcasting, muse will turn this line into a heading with a level equal to the number of stars. For example, like this:
* First level heading ** Second level heading
In order to make a paragraph, you need to leave an empty line in the
text. In order to highlight a word, you need to frame it with one, two
or three asterisks:
* highlighted word * ** stronger ** *** even stronger *** _ underlined_ = monospace =
the highlighted word is even stronger underlined.
моноспейс
You can make tables:
Double trait || Separates header fields Single trait | Separates table body fields Another row | table bodies Triple bar || Separates footer fields
Double trait | Separates header fields |
---|---|
Single trait | Separates table body fields |
Another row | table bodies |
Spellchecker: flyspell
On Ubuntu 9.04, flyspell is included with emacs. So that:
Mx flyspell-mode
and spelling is checked on the fly.
Formatting program sources: muse + python
There is a built-in translation of the source code of the programs in HTML using the htmlizer.el package, but unfortunately such HTML is not shown in Habré. Have to be perverted.
The code2html utility translates the source code into HTML well, but its output needs to be slightly corrected so that the program text looks good in Habré. Namely, insert & nobsp; instead of leading spaces
To do this, we write a small function in Python. Then in the text of the article we insert the tag of the python interpreter invocation, the program text and the call to our function, Something like this:
import srcform src = "" " i = 1 while i <10: print i i + = 1 "" " srcform.src_form ('code2html -lpython -H -t4', src)
Work result:
i = 1
while i <10:
print i
i + = 1
Convert to HTML
Mx muse-publish-this-file Publish with style: html
Problems
When working with muse I had to face some problems.
1. In muse-mode in faces, a font helv is specified for displaying headers. But there are no Russian characters in it. If we customize faces, muse, when turned on, still overwrites the settings for faces. I had to make a wrapper function in which, after setting the muse-mode, faces are reconfigured again.
2. Habr does not display a single digit 0 (zero), so colored.
In the comments, alexott suggested a solution to problem 1. It is necessary to replace the font 'helv' with some other in the face 'variable-pitch', for example 'fixed-misc' or 'arial'.
If anyone does not know, then this is done the easiest way. We type the command:
Mx describe-face variable-pitch
In the received description of face, click on the link '(customize this face)'. In the customization buffer that appears, change the value in the 'Font family' field from 'helv' to what you need, for example, to 'arial'. Now click on the button 'Save for Future Sessions'. Face change commands will be appended to the end of your .emacs file.
application
The script text for calling the utility.
import subprocess
def src_form (command, in_src):
in_str = in_src
proc = subprocess.Popen ([command],
shell = True,
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,)
stdout_value = proc.communicate (in_str) [0 ]
lines_in = stdout_value.splitlines ()
lines_out = []
print "
for line in lines_in:
l_out =" "
l_out = line.lstrip ()
spaces = len (line) - len (l_out)
for j in xrange (spaces):
l_out =" "+ l_out
print l_out
print "