FB2 Backend to AsciiDoc
Background
My relations with the FictionBook 2 format were initially complicated: at first I did not recognize it, then I admitted it as the source material for the formation of my books, later, with the development of the format and related tools, it became the basis of my library.
Naturally, one of the first tasks was the problem of converting to this format. Of all the existing tools, not one suited me, and for ordinary works of art it was easier and faster for me to do in my usual text editor (jEdit or VIM).
Some minor difficulties for me were: a) a description of the document β this was solved by using a template; b) images - usually it was a cover and it could be added using the FB Editor (the first one); c) footnotes - they were not often met, little by little, and, in principle, knowing the format, they were added without much difficulty.
Some time ago, there was a converter from FB2 to PDF from KiR, in addition to being a wonderful tool for obtaining pdfs of good, almost publishing quality, it was an example of using the DocBook format, which I heard a lot about, but could not start and put all the necessary tools together.
DocBook, like FictionBook, is an XML-based format. And as for FictionBook, itβs not very convenient to edit it in its natural format, but, fortunately, there is such a utility as asciidoc , which allows you to create an appropriate document in a DocBook, html or other format from a text file with fairly simple markup.
Creating fb2 backend
As already mentioned, the current situation completely satisfied me: I do not read very often, and the process of preparing the book also brings me pleasure. But, when I wanted to transform not a fiction, but a popular science book with many illustrations and footnotes, the little difficulties b) and c) turned out to be quite significant. And the thought crept into my head to adapt asciidoc to the formation of books in the FictionBook format.
In a way, I succeeded. The resulting tool made it possible to issue the book βWhy We Don't Fall Through the Floorβ by James Gordon in just a day (not counting proofreading) with quite a lot of comfort and ease.
Positive qualities (IMHO):
- Text file as a source: ease of reading, lack of unnecessary markup, compactness, familiarity;
- Convenient addition of footnotes: the text of the footnotes is located at the place that he explains;
- Convenient addition of images: the file name is indicated, and the addition of the appropriate sections to the fb2-file falls on the backend.
Negative qualities:
- Asciidoc is designed to form a DobBook, which is richer in a set of tags: this, in particular, leads to the fact that sometimes asciidoc detects formatting that is not in FictionBook and creates the wrong fb2 file. In such situations, you need to know the asciidoc markup language and how to disable it;
- There is no direct support for poetry and other lyrics: I was not able to make such a setting, I think it is possible, but they are not often found with me, and you can do it by direct insertion (see the attached example);
- Images can only be in jpg and png formats : this is an arbitrary restriction and can be easily expanded;
- Images should be in the same place as the source text: it was easier;
- It is necessary to personally monitor that the sections are either with text or with other sections: DobBook does not have this limitation, and therefore there is no automatic control;
- Links to parts of the text are not implemented: at this point, DocBook and FictionBook are similar, so if there is a need, this can easily be organized, but so far there has been no need;
- The source text must be in utf-8 encoding;
- The names of the images in the text and on the disk must match up to case.
Approximate scheme of the fb2-backend
The application was thrown βhastilyβ, as an auxiliary, to achieve the goal: the collection of one book - this implies all the inflexibility that is inherent in it.
The entire application is compiled as a set of simple scripts, united by the txt2fb2.bat batch file .
So what happens after calling txt2fb2.bat SourceFile.txt:
- Asciidoc is called with the fb2 backend. Its result is something similar in structure to FictionBook 2, but requiring further refinement: adding images, removing footnotes at the end;
- Image preparation in progress. A companion file with the extension .b64 is created for all jpg and png images (this is actually an element
, which can be placed in the resulting fb2 file). It is at this stage that an arbitrary restriction arises on the formats of graphic files. - The finalization of FictionBook 2 is carried out by the xslt-script adjust_fb2.xsl . At this stage, the sections "Abstract" and "Document History" fall into the right places in the header. Footnotes are highlighted and placed at the end. Images from the files obtained in step 2 are added.
The resulting fb2 file must be checked with a validator.
Additional formatting
I recommend that you look at the asciidoc documentation to familiarize yourself with the valid markup, as well as take a look at the book that I am attaching as a sample.
For ease of conversion to fb2 format, some asciidoc markup extensions have been introduced, which I will describe here.
Document title
Many parameters are defined using the attributes of the document: An annotation is any text that is between the heading and the first section. The history of the document is described in a special section of the top level:
:genre: sf -- ΠΆΠ°Π½Ρ
:firstname: ΠΠΌΡ Π°Π²ΡΠΎΡΠ° ΠΊΠ½ΠΈΠ³ΠΈ
:middlename: ΠΡΡΠ΅ΡΡΠ²ΠΎ Π°Π²ΡΠΎΡΠ° ΠΊΠ½ΠΈΠ³ΠΈ
:lastname: Π€Π°ΠΌΠΈΠ»ΠΈΡ Π°Π²ΡΠΎΡΠ° ΠΊΠ½ΠΈΠ³ΠΈ
:nickname: ΠΡΠΎΠ·Π²ΠΈΡΠ΅ Π°Π²ΡΠΎΡΠ° ΠΊΠ½ΠΈΠ³ΠΈ
:date: 2009 -- Π΄Π°ΡΠ° ΠΈΠ·Π΄Π°Π½ΠΈΡ ΠΊΠ½ΠΈΠ³ΠΈ
:lang: ru -- ΡΠ·ΡΠΊ ΠΊΠ½ΠΈΠ³ΠΈ
:src-lang: en -- ΡΠ·ΡΠΊ ΠΎΡΠΈΠ³ΠΈΠ½Π°Π»Π°
:coverpage-image: Cover.png -- ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ ΠΎΠ±Π»ΠΎΠΆΠΊΠΈ
:sequence-name: Malazan Book of the Fallen -- ΠΠΌΡ ΡΠ΅ΡΠΈΠΈ
:sequence-number: 6 -- Π½ΠΎΠΌΠ΅Ρ Π² ΡΠ΅ΡΠΈΠΈ
:translator-firstname: ΠΠ²Π°Π½ -- ΠΈΠΌΡ ΠΏΠ΅ΡΠ΅Π²ΠΎΠ΄ΡΠΈΠΊΠ°
:translator-middlename: ΠΠ²Π°Π½ΠΎΠ²ΠΈΡ -- ΠΎΡΡΠ΅ΡΡΠ²ΠΎ ΠΏΠ΅ΡΠ΅Π²ΠΎΠ΄ΡΠΈΠΊΠ°
:translator-lastname: Π’ΠΎΠ»ΠΌΠΎΠ²ΠΈΡ -- ΡΠ°ΠΌΠΈΠ»ΠΈΡ ΠΏΠ΅ΡΠ΅Π²ΠΎΠ΄ΡΠΈΠΊΠ°
:document-firstname: Stas -- ΠΈΠΌΡ ΡΠΎΠ·Π΄Π°ΡΠ΅Π»Ρ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ°
:document-lastname: Bushuev -- ΡΠ°ΠΌΠΈΠ»ΠΈΡ ΡΠΎΠ·Π΄Π°ΡΠ΅Π»Ρ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ°
:document-nickname: Xitsa -- ΠΏΡΠΎΠ·Π²ΠΈΡΠ΅ ΡΠΎΠ·Π΄Π°ΡΠ΅Π»Ρ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ°
:document-date: 2009-05-07 -- Π΄Π°ΡΠ° ΡΠΎΠ·Π΄Π°Π½ΠΈΡ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ°
:document-id: Xitsa-7AD2-4B5F-92E5-892DEA3AE559 -- ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ°
:document-version: 1.01 -- Π²Π΅ΡΡΠΈΡ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ°
:program-used: FB Tools, sed, VIM, Far, asciidoc+fb2 backend -- ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½Π½ΡΠ΅ ΠΏΡΠΈ ΡΠΎΠ·Π΄Π°Π½ΠΈΠΈ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΡ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ
Document History
----------------
Footnotes
AsciiDoc supports two types of footnotes: footnote and footnoteref , for footnotes to be correctly generated using fb2-backend, only footnoteref is required and only with numbers (since it is used to form the footnote header, you could do the opposite by disabling footnoteref and organizing automatic footnote numbering, but I preferred to leave the possibility of re-linking).
Example: In asciidoc 8.4.5, for some reason, it was made so that the footnoteref macro before which there is no space and a significant character does not work, therefore, in the example I posted, asciidoc.conf is fixed and does not correspond to the original.
ΠΡΠ½ΠΎΠ²Π½ΠΎΠΉ ΡΠ΅ΠΊΡΡfootnoteref:[3,"Π’Π΅ΠΊΡΡ ΡΠ½ΠΎΡΠΊΠΈ, ΠΎΠ±ΡΠ°ΡΠΈΡΠ΅ \"Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅\", ΡΡΠΎ ΠΊΠ°Π²ΡΡΠΊΠΈ Π½Π΅ΠΎΠ±Ρ
ΠΎΠ΄ΠΈΠΌΠΎ ΡΠΊΡΠ°Π½ΠΈΡΠΎΠ²Π°ΡΡ"]
Π’Π΅ΠΊΡΡ Ρ ΠΏΠΎΠ²ΡΠΎΡΠ½ΠΎΠΉ ΡΡΡΠ»ΠΊΠΎΠΉ Π½Π° ΡΡ ΠΆΠ΅ ΡΠ½ΠΎΡΠΊΡfootnoteref:[3]
Additional macros
An additional author can be added using the author macro, unnecessary attributes can be omitted. For example: There is a subtitle tag in FictionBook , to which I did not find an analogue in DocBook, therefore I implemented it using a macro: Tag
author::[Π‘Π΅ΠΌΡΠ½ΠΎΠ²,Π‘Π΅ΡΠ³Π΅ΠΉ,ΠΠ½Π°ΡΠΎΠ»ΡΠ΅Π²ΠΈΡ,Nickname]
author::[Π‘Π΅ΠΌΡΠ½ΠΎΠ²,Π‘Π΅ΡΡΠΆΠ°]
subtitle::[* * *]
empty::[]
Quotes and Epigraphs
Both quotes and epigraphs are distinguished from the text by underlining. Attributes must either be in quotation marks together, or they must not contain commas.
Examples:
["quote","ΠΠ²ΡΠΎΡ ΡΠΈΡΠ°ΡΡ","ΠΏΡΠΎΠΈΠ·Π²Π΅Π΄Π΅Π½ΠΈΠ΅"]
____________________________________________________________
Π¦ΠΈΡΠ°ΡΠ° Ρ ΠΏΠΎΠ»Π½ΠΎΠΉ Π°ΡΡΠΈΠ±ΡΡΠΈΠΊΠΎΠΉ
____________________________________________________________
____________________________________________________________
ΠΡΠΎΡΡΠΎΠ΅ ΡΠΈΡΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ Π±Π΅Π· Π»ΠΈΡΠ½ΠΈΡ
ΠΈΠ·ΡΡΠΊΠΎΠ²
____________________________________________________________
[epigraph,Π. Π‘. ΠΡΡΠΊΠΈΠ½, Π― ΠΏΠΎΠΌΠ½Ρ...]
____________________________________________________________
++++++++++++++++++++++++++++++++++++++++
Π― ΠΏΠΎΠΌΠ½Ρ ΡΡΠ΄Π½ΠΎΠ΅ ΠΌΠ³Π½ΠΎΠ²Π΅Π½ΡΠ΅,
ΠΠ΅ΡΠ΅Π΄ΠΎ ΠΌΠ½ΠΎΠΉ ΡΠ²ΠΈΠ»Π°ΡΡ ΡΡ...
++++++++++++++++++++++++++++++++++++++++
ΠΡΠΎ ΠΏΡΠΈΠΌΠ΅Ρ ΡΠΎΠ³ΠΎ, ΠΊΠ°ΠΊ ΠΌΠΎΠΆΠ½ΠΎ Π²ΡΡΠ°Π²ΠΈΡΡ ΡΡΠΈΡ
ΠΈ Π² ΡΠ΅ΠΊΡΡ.
____________________________________________________________
How to try
I have assembled a separate application that does not require installation, you can get it here
. You can immediately try to run txt2fb2.bat materials.txt - this is a demo book that uses most of the asciidoc fb2 extensions.
The doc directory contains asciidoc documentation that you would like to read.
Linux users can take this file and modify it with a file:
- Install asciidoc 8.4.5 (and replace asciidoc.conf with mine (or take it from the trunk));
- Set python binding to libxml, or use xsltproc directly;
- Rewrite txt2fb2.bat in terms of the shell used.
Further development
The project is at the alpha stage and is laid out in the hope that someone else will be interested or useful. In this case, it will be possible to think about how to bring it in a decent form: with documentation, examples and modify it.