Tags

, , , , , , , , ,

When I did my first book, I decided to use XML for the book contents. Considering my background in academia , I might have chosen LaTeX instead.

But I wanted the possibility to generate different output formats. Specifically, I wanted to generate html and pdf.

And I wanted to learn XML.

I chose PSGML and Emacs as my editing tools. I found this combination to be really powerful, and eventually (after a lot of practice) I could write almost as fast as when writing in a Word processor, like MS Word.

In early 2015, I found that my PSGML and Emacs combination did not work as good as before.

I did some trouble-shooting, but without luck.

I realized that it was perhaps time for something newer. I searched for alternatives, and after a while I found nxml. This turned out to be an interesting alternative, but I had to do some porting work, in order to get started.

I translated my old DTD files to RelaxNG schemas.

I added code to my .emacs file, as

(require 'nxml-mode)
(add-to-list 'auto-mode-alist '("\\.xml$" . nxml-mode))

(eval-after-load 'rng-loc
  '(add-to-list 'rng-schema-locating-files "~/rng/schemas.xml"))

The file schemas.xml is a reference to the actual schemas. As an example, taken from my own schemas.xml file, there is a reference to a file called chapter.rnc, as

  <documentElement prefix="" localName="chapter" typeId="chapter"/>
  <typeId id="chapter" uri="chapter.rnc"/>

Inside chapter.rnc we find e.g.

    element chapter_intro {
       element para_list {
          element para { external "para_elements.rnc"}*}},

which defines an element called chapter_intro, with an element called para_list inside, which in turn is a list of para elements.

The para element itself is defined in the file para_elements.rnc. As an example of contents from this file, we have the element item_list, which is a list of item elements (which in turn can contain text). The element item_list is part of a mixed element, and its Relax NG definition is

     element item_list {
         element item { text }+ } |

Using Relax NG schemas in this way, I found the actual editing to be fairly straightforward. It turns out that Emacs highlights (in red) parts of the XML file that do not comply with the schema. This is very helpful when doing the editing, and it is a feature that was not there when I used the PSGML and Emacs combination.

In PSGML, I used quite a few editing commands (which had to be learned, and practiced, to get up to speed). When using Relax NG schemas, I use only the command for completion.

The command for completion is set to Alt-Tab as default. As we know, Alt-Tab has another standard meaning! It changes focus to another program. It turns out, however, that Alt-Tab can be used anyway.

On Mac, I could use Alt-Tab as is, since the key that corresponds to Alt in Alt-Tab is the command key on Mac!

On Linux, I could use Esc Tab (the Escape key followed by Tab, i.e. the two keys are pressed in sequence, and the Escape key must be released before pressing the Tab key). This is not so surprising, since this is the alternative (old) way to use Alt-commands in Emacs.

In Emacs, Alt is often referred to as the Meta key. A Meta key combination can be obtained by pressing Alt and another key while holding down Alt, or alternatively, by pressing and releasing the Escape key before pressing the next key (Tab in this case).

The net result was that I could continue writing books in XML.

You can see some examples of generated files, as

  • Into Computers, a book about creating your own computer, in html
  • Into Programming, a book about programming, in pdf (Python) and pdf (C)
  • Into Embedded, a book about embedded systems, in epub
Advertisements