Class: PDF::TechBook

Class | PDF::TechBook
In: | lib/pdf/techbook.rb
Parent: | PDF::Writer

# PDF::TechBook

The TechBook class is a markup language interpreter. This will read a file containing the "TechBook" markukp, described below, and create a PDF document from it. This is intended as a complete document language, but it does have a number of limitations.

The TechBook markup language and class are used to format the PDF::Writer manual, represented in the distrubtion by the file "manual.pwd".

The TechBook markup language is primarily stream-oriented with awareness of lines. That is to say that the document will be read and generated from beginning to end in the order of the markup stream.

## TechBook Markup

TechBook markup is relatively simple. The simplest markup is no markup at all (flowed paragraphs). This means that two lines separated by a single line separator will be treaed as part of the same paragraph and formatted appropriately by PDF::Writer. Paragaphs are terminated by empty lines, valid line markup directives, or valid headings.

Certain XML entitites will need to be escaped as they would in normal XML usage, that is, &lt; must be written as &amp;lt;; &gt; must be written as &amp;gt;; and &amp; must be written as &amp;amp;.

Comments, headings, and directives are line-oriented where the first mandatory character is in the first column of the document and take up the whole line. Styling and callback tags may appear anywhere in the text.

### Comments

Comments begin with the hash-mark (’#’) at the beginning of the line. Comment lines are ignored.

### Styling and Callback Tags

Within normal, preserved, or code text, or in headings, HTML-like markup may be used for bold (&lt;b&gt;) and italic (&lt;i&gt;) text. TechBook supports standard PDF::Writer callback tags (<c:alink>, <c:ilink>, <C:bullet/>, and <C:disc/>) and adds two new ones (<r:xref/>, <C:tocdots/>).

`&lt;r:xref/>`: | Creates an internal document link to the named cross-reference destination. Works with the heading format (see below). See tag_xref_replace for more information.
`&lt;C:tocdots/>`: | This is used internally to create and display a row of dots between a table of contents entry and the page number to which it refers. This is used internally by TechBook.

### Directives

Directives begin with a period (’.’) and are followed by a letter (‘a’..’z’) and then any combination of word characters (‘a’..’z’, ‘0’..’9’, and ‘_’). Directives are case-insensitive. A directive may have arguments; if there are arguments, they must follow the directive name after whitespace. After the arguments for a directive, if any, all other text is ignored and may be considered a comment.

#### `.newpage [force]`

The `.newpage` directive starts a new page. If multicolumn mode is on, a new column will be started if the current column is not the last column. If the optional argument `force` follows the `.newpage` directive, a new page will be started even if multicolumn mode is on.

      .newpage
      .newpage force

#### `.pre`, `.endpre`

The `.pre` and `.endpre` directives enclose a block of text with preserved newlines. This is similar to normal text, but the lines in the `.pre` block are not flowed together. This is useful for poetic forms or other text that must end when each line ends. `.pre` blocks may not be nested in any other formatting block. When an `.endpre` directive is encountered, the text format will be returned to normal (flowed text) mode.

      .pre
      The Way that can be told of is not the eternal Way;
      The name that can be named is not the eternal name.
      The Nameless is the origin of Heaven and Earth;
      The Named is the mother of all things.
      Therefore let there always be non-being,
        so we may see their subtlety,
      And let there always be being,
        so we may see their outcome.
      The two are the same,
      But after they are produced,
        they have different names.
      .endpre

#### `.code`, `.endcode`

The `.code` and `.endcode` directives enclose a block of text with preserved newlines. In addition, the font is changed from the normal techbook_textfont to techbook_codefont. The techbook_codefont is normally a fixed pitched font and defaults to Courier. At the end of the code block, the text state is restored to its prior state, which will either be `.pre` or normal.

      .code
      require 'pdf/writer'
      PDF::Writer.prepress # US Letter, portrait, 1.3, prepress
      .endcode

#### `.blist`, `.endblist`

These directives enclose a bulleted list block. Lists may be nested within other text states. If lists are nested, each list will be appropriately indented. Each line in the list block will be treated as a single list item with a bullet inserted in front using either the <C:bullet/> or <C:disc/> callbacks. Nested lists are successively indented. `.blist` directives accept one optional argument, the name of the type of bullet callback desired (e.g., ‘bullet’ for <C:bullet/> and ‘disc’ for <C:disc/>).

      .blist
      Item 1
      .blist disc
      Item 1.1
      .endblist
      .endblist

#### `.eval`, `.endeval`

With these directives, the block enclosed will collected and passed to Ruby‘s Kernel#eval. `.eval` blocks may be present within normal text, `.pre`, `.code`, and `.blist` blocks. No other block may be embedded within an `.eval` block.

      .eval
      puts "Hello"
      .endeval

#### `.columns`

Multi-column output is controlled with this directive, which accepts one or two parameters. The first parameter is mandatory and is either the number of columns (2 or more) or the word ‘off’ (turning off multi-column output). When starting multi-column output, a second parameter with the gutter size may be specified.

      .columns 3
      Column 1
      .newpage
      Column 2
      .newpage
      Column 3
      .columns off

#### `.toc`

This directive is used to tell TechBook to generate a table of contents after the first page (assumed to be a title page). If this is not present, then a table of contents will not be generated.

#### `.author`, `.title`, `.subject`, `.keywords`

Sets values in the PDF information object. The arguments — to the end of the line — are used to populate the values.

#### `.done`

Stops the processing of the document at this point.

### Headings

Headings begin with a number followed by the rest of the heading format. This format is "#<heading-text>" or "#<heading-text>xref_name". TechBook supports five levels of headings. Headings may include markup, but should not exceed a single line in size; those headings which have boxes as part of their layout are not currently configured to work with multiple lines of heading output. If an xref_name is specified, then the &lt;r:xref> tag can use this name to find the target for the heading. If xref_name is not specified, then the "name" associated with the heading is the index of the order of insertion. The xref_name is case sensitive.

      1<Chapter>xChapter
      2<Section>Section23
      3<Subsection>
      4<Subsection>
      5<Subsection>

#### Heading Level 1

First level headings are generally chapters. As such, the standard implementation of the heading level 1 method (__heading1), will be rendered as "chapter#. heading-text" in centered white on a black background, at 26 point (H1_STYLE). First level headings are added to the table of contents.

#### Heading Level 2

Second level headings are major sections in chapters. The headings are rendered by default as black on 80% grey, left-justified at 18 point (H2_STYLE). The text is unchanged (__heading2). Second level headings are added to the table of contents.

#### Heading Level 3, 4, and 5

The next three heading levels are used for varying sections within second level chapter sections. They are rendered by default in black on the background (there is no bar) at 18, 14, and 12 points, respectively (H3_STYLE, H4_STYLE, and H5_STYLE). Third level headings are bold-faced (__heading3); fourth level headings are italicised (__heading4), and fifth level headings are underlined (__heading5).

### Methods

__heading1 __heading2 __heading3 __heading4 __heading5 generate_table_of_contents? run techbook_directive_author techbook_directive_blist techbook_directive_code techbook_directive_columns techbook_directive_done techbook_directive_endblist techbook_directive_endcode techbook_directive_endeval techbook_directive_endpre techbook_directive_eval techbook_directive_keywords techbook_directive_newpage techbook_directive_pre techbook_directive_subject techbook_directive_title techbook_directive_toc techbook_parse techbook_text techbook_toc

### Classes and Modules

Class PDF::TechBook::TagTocDots
Class PDF::TechBook::TagXref

### Constants

H1_STYLE | = | { :background => Color::RGB::Black, :foreground => Color::RGB::White, :justification => :center, :font_size => 26, :bar => true
H2_STYLE | = | { :background => Color::RGB::Grey80, :foreground => Color::RGB::Black, :justification => :left, :font_size => 18, :bar => true
H3_STYLE | = | { :background => Color::RGB::White, :foreground => Color::RGB::Black, :justification => :left, :font_size => 18, :bar => false
H4_STYLE | = | { :background => Color::RGB::White, :foreground => Color::RGB::Black, :justification => :left, :font_size => 14, :bar => false
H5_STYLE | = | { :background => Color::RGB::White, :foreground => Color::RGB::Black, :justification => :left, :font_size => 12, :bar => false
LIST_ITEM_STYLES | = | %w(bullet disc)

### Attributes

chapter_number | [RW] |
table_of_contents | [RW] |
techbook_codefont | [RW] |
techbook_encoding | [RW] |
techbook_fontsize | [RW] |
techbook_source_dir | [RW] |
techbook_textfont | [RW] |
xref_table | [R] |

### Public Class methods

run(args)

### Public Instance methods

__heading1(heading)
__heading2(heading)
__heading3(heading)
__heading4(heading)
__heading5(heading)
generate_table_of_contents?()
techbook_directive_author(args)
techbook_directive_blist(args)
techbook_directive_code(args)

Code: .code

techbook_directive_columns(args)

Columns. .columns <number-of-columns>|off

techbook_directive_done(args)

Done. Stop parsing: .done

techbook_directive_endblist(args)
techbook_directive_endcode(args)

End Code: .endcode

techbook_directive_endeval(args)

End Eval: .endeval

techbook_directive_endpre(args)

End preserved newlines: .endpre

techbook_directive_eval(args)

Eval: .eval

techbook_directive_keywords(args)
techbook_directive_newpage(args)

Start a new page: .newpage

techbook_directive_pre(args)

Preserved newlines: .pre

techbook_directive_subject(args)
techbook_directive_title(args)
techbook_directive_toc(args)
techbook_parse(document, progress = nil)
techbook_text(line)
techbook_toc(progress = nil)

[Validate]