Usually in the table of content of a book, each line is for a chapter or section and ended with its page number, for example,

1 first chapter 10
1.1 first section 11
1.1.1 first subsection 12
1.2 second section 13
2 second chapter 14 
2.1 first section 16
2.2 second section 13

The format of bookmark of djvu files for the above example is

 ("1 first chapter" "#10" 
 ("1.1 first section" "#11" 
 ("1.1.1 first subsection" "#12" ))
 ("1.2 second section" "#13" ))
 ("2 second chapter" "#14" 
 ("2.1 first section" "#16" )
 ("2.2 second section" "#13" ))

where the main points are the paring of left and right parenthesis for tree-like organization of sections and chapters, double quote for each bookmark item and each page number is preceded by a #. The separation between lines and indention are just for convenience to manually view and edit the bookmark text file, and don't matter at all.

My questions are:

  1. I was wondering if there are some convenient ways to convert a plain text file of table of content into djvu bookmark format, which can reduce manual handling as much as possible? This may be achieved by text processing applications, or programming under some programming language, or combination of both.
  2. As Suggested with a related reply by Dennis Williamson,

    What you're doing is starting to look a little bit like XML. Perhaps you should use the real thing and use proper tools to manipulate it.

    a quick google turns up djvuxml. XML files can be easily manipulated using the appropriate Python or Perl module or shell utilities such as xmlstarlet.

    I wonder if djvu bookmark format is similar to XML format (I admit I am not familiar with XML yet, but willing to learn it if it can help here)? Can XML tools be used to convert a text file of table of content into djvu bookmark format?


Here's an awk script that attempts to produce decent indentation. It assumes the input is well-formed (e.g. the line before 3.2.2 must be 3.1 or 3.1.something, not 3).

#! /usr/bin/awk -f
BEGIN { printf "(bookmarks"; depth = 1; }
    level = split($1, s, ".");
    while (level < depth) {--depth; printf ")";}
    print "";
    depth = level + 1;
    gsub(/[\\"]/, "\\&");
    page = $NF;
    sub(/ +[^ ]*$/,"");
    for (i in s) printf " ";
    printf "(\"%s\" \"#%s\"", $0, page;
END { while (depth-- > 0) printf ")"; print ""; }

The DJVU syntax has nothing to do with XML; these are s-expressions.

  • Thanks! I was wondering if there are some text editor/plugins that can do the following: after I select multiple consecutive lines, it can put these lines between a pair of parentheses? I.e., it can help in interactively writing S-expressions.
    – Tim
    Jul 20 '11 at 19:59
  • 1
    @Tim Yes, Emacs. Actually I don't understand exactly what you want to do, but 1. Emacs is a correct answer to pretty much any question that starts with “is there a text editor that can”, and 2. Emacs is partly written in Lisp, so its authors have put quite a bit of attention on making editing Lisp easy. Jul 20 '11 at 20:36
  • Thanks! What I want to do are: (1) put several lines into a pair of parenthesis, by highlighting them and then running some command; (2) display the tree structure represented by S-expressions more clearly by, for example, indentation. I just tried Paredit mode of Emacs, but I can't find the functionalities. Am I missing something?
    – Tim
    Jul 20 '11 at 20:48
  • @Tim For Lisp syntax, use Lisp mode. To insert parentheses around each line, replace the regexp ^ by ( and $ by ). Not sure offhand what to suggest as a sexp pretty-printer (Emacs's default isn't that pretty) but there are surely good ones around. Jul 20 '11 at 21:30

Any programming language will be able to parse your input example correctly.

Choose a programming language and then parse the input deliminating the input first by "." and " " at first and " " second.

I would use Perl, but whatever language the developer is most familiar with would work fine.

Keep in mind that automatic solutions will only work if the input follows a strict syntax. And unless this is a known standard there aren't going to be any premade tools convert this into XML or djvu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.