In the world of e-books and digital reading, having content in the right format can make all the difference in your reading experience. The FB2 (FictionBook) format has become popular among e-book enthusiasts due to its XML-based structure that preserves formatting and metadata effectively. This article introduces a powerful yet simple bash script called txt2fb2.sh that converts plain text files to the FB2 format on Linux systems.

What is FB2 Format?

FB2 (FictionBook) is an XML-based e-book format. Unlike formats such as PDF that focus on preserving the visual appearance of a document, FB2 is designed to preserve the structure of a book, including chapters, paragraphs, footnotes, and other elements. Its XML foundation makes it highly portable and allows for consistent rendering across different e-reading devices and applications.

Introducing txt2fb2.sh

The txt2fb2.sh script is a lightweight command-line utility that transforms plain text files into properly structured FB2 documents. It handles the complexities of XML generation, character escaping, and metadata embedding, offering a straightforward way to create FB2 files from text content.

Key Features

  • Comprehensive Metadata Support: Add author name, book title, and creation date
  • Cover Image Integration: Embed a cover image for your e-book
  • Proper Text Formatting: Converts text lines to paragraphs and preserves empty lines
  • XML Safety: Automatically escapes special characters to ensure valid XML
  • Unique Identification: Generates a UUID for each book
  • Base64 Image Encoding: Embeds cover images directly in the FB2 file

How to Use txt2fb2.sh

Using the script is straightforward. After saving it and making it executable, you can run it with the following syntax:

./txt2fb2.sh -a "AUTHOR NAME" -t "BOOK TITLE" -i "/path/to/image.jpg" book.txt

Parameters Explained

  • -a: Author's name (required)
  • -t: Book title (required)
  • -i: Path to cover image (optional)
  • The final parameter is the input text file path

The script processes the text file and creates an FB2 file with the same name but different extension in the same directory.

How It Works

The script follows several steps to convert your text file:

  1. Parameter Processing: Collects author name, title, and cover image path
  2. Metadata Generation: Creates proper FB2 metadata including UUID and timestamps
  3. Content Processing:
    • Reads the input file
    • Escapes XML special characters
    • Transforms each line into a proper paragraph tag
    • Converts empty lines to FB2's <empty-line/> tags
  4. Structure Creation: Builds the complete FB2 XML structure
  5. Image Processing: If a cover image is provided, encodes it as base64 and embeds it
  6. Output Generation: Writes the complete FB2 document to the output file

Technical Highlights

Author Name Parsing

The script intelligently splits the author name into first name and last name:

<first-name>$(echo "$AUTHOR" | cut -d' ' -f1)</first-name>
<last-name>$(echo "$AUTHOR" | cut -d' ' -f2-)</last-name>

XML Character Escaping

To ensure valid XML, the script escapes special characters:

escape_xml() {
  echo "$1" | sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/"/\&quot;/g; s/'\''/\&apos;/g'
}

Base64 Image Encoding

For embedding cover images, the script uses base64 encoding:

encode_image() {
  if [ -f "$1" ]; then
    echo "<binary id=\"cover.jpg\" content-type=\"image/jpeg\">"
    base64 "$1" | tr -d '\n'
    echo "</binary>"
  fi
}

Use Cases

  • Converting Public Domain Texts: Transform plain text classics into well-formatted e-books
  • Self-Publishing: Convert your draft manuscripts into e-reader-friendly formats
  • Documentation: Turn plain text documentation into structured, readable e-books
  • Batch Processing: Use in combination with other scripts to process multiple files

Extending the Script

The basic script can be enhanced in several ways:

  • Chapter Detection: Add logic to detect and mark chapters
  • Table of Contents Generation: Create a navigable table of contents
  • Multiple Image Support: Allow for embedding multiple images throughout the book
  • Style Options: Add parameters for text styling and formatting preferences
  • Metadata Expansion: Include more detailed metadata like genres, annotations, etc.

Conclusion

The txt2fb2.sh script offers a simple yet powerful way to convert plain text files to the FB2 format. Whether you're an e-book enthusiast looking to format your collection or an author preparing your work for digital distribution, this script provides a valuable tool in your workflow. By automating the conversion process, it eliminates the tedious manual work of creating properly structured FB2 files, allowing you to focus on content creation and enjoyment.

Source https://gist.github.com/onesixromcom/168043a6eb1c04142a55a7de0c4cb819

With its combination of simplicity and functionality, txt2fb2.sh exemplifies the power of bash scripting for practical file format conversions on Linux systems. Give it a try and transform your plain text files into richly formatted e-books today!