📜 ⬆️ ⬇️

Emacs for beginners: Preparing articles for Habr in Emacs

Introduction


We've talked a lot about the power of Emacs. Let's solve some practical problem. For example, let's learn how to prepare articles for Habr with a minimum of manual work.

Let's estimate what we need:


')
Getting started.

Convert text to HTML: muse mode


The most traditional UNIX-way. The bottom line is that we write an article in text form, following some conventions, quite simple, such simplest rules of markup, and then we process this text with a parser that turns our text into an HTML file, which we insert into the Habr article editing form.

Installation


In 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 the muse mode:

 Mx muse-mode


Markup


The markup for working with muse is very simple, for example, if you start a line with an asterisk or so many, when translated, muse will turn this line into a heading with a level equal to the number of asterisks. For example:

 * Heading 1
 ** Heading 2


To make a paragraph, you need to leave an empty line in
text. To select a word, you need to frame it with one, two
or three asterisks:

 * selected word * ** stronger ** *** even stronger *** _stressed_ = monospace =


highlighted word is stronger even more stressed

You can create tables:

   Double line ||  Splits header fields
   Single line |  Splits table body fields
   Another row |  table bodies
   Triple line ||  Splits footer fields


Double traitSplits header fields
Single lineSplits table body fields
Another rowtable bodies


Spellchecker: flyspell


In Ubuntu 9.04, flyspell is included with emacs. So that:

 Mx flyspell-mode


and spelling is checked on the fly.

Formatting source programs: muse + python


There is a built-in translation of source texts of programs in HTML using the htmlizer.el package, but unfortunately such HTML is not shown in Habré. We'll have to pervert.

Well translates the source code into HTML code2html utility, but its output needs to be slightly corrected so that the program text looks good in Habré. Namely, insert instead of the leading spaces & nobsp;

To do this, we write a small function in Python. Then in the text of the article we insert the python interpreter call tag, the program text and the call to our function, like this:

 <python markup = "">
 import srcform
 src = "" "
 i = 1
 while i <10:
     print i
     i + = 1
 "" "
 srcform.src_form ('code2html -lpython -H -t4', src)
 </ python>


Result of work:

i = 1
while i < 10 :
print i
i + = 1

HTML conversion


 Mx muse-publish-this-file
 Publish with style: html


Problems


When working with muse had to meet with some problems.

1. In the muse-mode in faces, the helv font is indicated to display the headers. But there are no Russian characters in it. If we customize faces, muse, when turned on, still rewrites the settings for faces. I had to make a wrapper function in which, after setting the mode, the muse-mode again reconfigures the faces.
2. Habr does not display a single digit 0 (zero), so colored.

In the comments, alexott suggested solving problem 1. It is necessary in face 'variable-pitch' to replace the font 'helv' with something else, for example 'fixed-misc' or 'arial'.

If anyone does not know, then this is done the easiest way. We type the command:

 Mx write-face variable-pitch


In the received description face click on the link '(customize this face)'. In the customization buffer that appears, we change the value in the 'Font family' field from 'helv' to what you need, for example, to 'arial'. Now click on the 'Save for Future Sessions' button. The face change commands will be added to the end of your .emacs file.

application


The text of the script to call 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 "<literal>"
for line in lines_in :
l_out = ""
l_out = line . lstrip ( )
spaces = len ( line ) - len ( l_out )
for j in xrange ( spaces ) :
l_out = "& nbsp;" + l_out
print l_out
print "</ literal>"

Source: https://habr.com/ru/post/60215/


All Articles