Class: PDF::TechBook

Inherits:
Writer show all
Defined in:
lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb

Overview

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, < must be written as <; > must be written as >; and & must be written as &.

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).

Defined Under Namespace

Classes: TagTocDots, TagXref

Constant Summary collapse

LINE_DIRECTIVE_RE =

:nodoc:

%r{^\.([a-z]\w+)(?:$|\s+(.*)$)}io
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
}
HEADING_FORMAT_RE =

:nodoc:

%r{^([\d])<(.*)>([a-z\w]+)?$}o
LIST_ITEM_STYLES =
%w(bullet disc)

Constants inherited from Writer

Writer::ENCRYPT_OPTIONS, Writer::FONT_PATH, Writer::MATCH_TAG_DRAW_ONE_RE, Writer::MATCH_TAG_DRAW_PAIR_RE, Writer::MATCH_TAG_REPLACE_RE, Writer::PAGE_SIZES, Writer::PDF_VERSION_13, Writer::PDF_VERSION_14, Writer::PDF_VERSION_15, Writer::PDF_VERSION_16, Writer::TAGS, Writer::TAG_PARAM_RE, Writer::VERSION

Constants included from Transaction::Simple

Transaction::Simple::SKIP_TRANSACTION_VARS, Transaction::Simple::TRANSACTION_SIMPLE_VERSION

Constants included from Writer::Graphics

Writer::Graphics::KAPPA

Instance Attribute Summary collapse

Attributes inherited from Writer

#absolute_bottom_margin, #absolute_left_margin, #absolute_right_margin, #absolute_top_margin, #absolute_x_middle, #absolute_y_middle, #arc4, #bottom_margin, #catalog, #column_count, #column_gutter, #column_number, #column_width, #compressed, #current_base_font, #current_contents, #current_font, #current_page, #destinations, #encryption_key, #first_page, #font_families, #font_size, #info, #left_margin, #margin_height, #margin_width, #margin_x_middle, #margin_y_middle, #objects, #outlines, #page_height, #page_width, #pages, #pageset, #pointer, #procset, #right_margin, #top_margin, #version, #y

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Writer

#_post_transaction_rewind, #add_content, #add_destination, #add_info, #add_internal_link, #add_link, #add_object, #add_outline_item, #add_text, #add_text_wrap, #append_page, #bleed_box, #check_all_here, #close_object, #cm2pts, cm2pts, #columns?, #compressed?, #current_font!, #current_page_number, escape, #font_descender, #font_height, #in2pts, in2pts, #initialize, #insert_mode, #insert_page, #insert_position, #lines_remaining, #margins_cm, #margins_in, #margins_mm, #margins_pt, mm2pts, #mm2pts, #move_pointer, #new_page, #open_at, #open_here, #open_new_object, #open_object, #page_mode=, parse_fonts_conf, prepress, #prepress_center_mark, #prepress_clip_mark, #render, #reopen_object, #restore_state, #save_as, #save_state, #select_font, #size, #start_columns, #start_new_page, #start_page_numbering, #stop_columns, #stop_object, #stop_page_numbering, #text, #text_line_width, #text_width, #trim_box, #viewer_preferences, #which_page_number

Methods included from Transaction::Simple

__common_start, #abort_transaction, #commit_transaction, debug_io, debug_io=, debugging?, #rewind_transaction, start, start_named, #start_transaction, #transaction, #transaction_exclusions, #transaction_name, #transaction_open?

Methods included from Writer::Graphics

#add_image, #add_image_from_file, #circle_at, #close, #close_fill, #close_fill_stroke, #close_stroke, #curve, #curve_to, #ecurve, #ecurve_to, #ellipse2_at, #ellipse_at, #fill, #fill_color, #fill_color!, #fill_color?, #fill_stroke, #image, #line, #line_to, #move_to, #polygon, #rectangle, #rotate_axis, #rounded_rectangle, #scale_axis, #scurve, #scurve_to, #segment_at, #skew_axis, #star, #stroke, #stroke_color, #stroke_color!, #stroke_color?, #stroke_style, #stroke_style!, #stroke_style?, #text_render_style, #text_render_style!, #text_render_style?, #transform_matrix, #translate_axis

Constructor Details

This class inherits a constructor from PDF::Writer

Instance Attribute Details

#chapter_numberObject

Returns the value of attribute chapter_number.



231
232
233
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 231

def chapter_number
  @chapter_number
end

#table_of_contentsObject

Returns the value of attribute table_of_contents.



230
231
232
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 230

def table_of_contents
  @table_of_contents
end

#techbook_codefontObject

Returns the value of attribute techbook_codefont.



633
634
635
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 633

def techbook_codefont
  @techbook_codefont
end

#techbook_encodingObject

Returns the value of attribute techbook_encoding.



635
636
637
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 635

def techbook_encoding
  @techbook_encoding
end

#techbook_fontsizeObject

Returns the value of attribute techbook_fontsize.



636
637
638
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 636

def techbook_fontsize
  @techbook_fontsize
end

#techbook_source_dirObject

Returns the value of attribute techbook_source_dir.



791
792
793
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 791

def techbook_source_dir
  @techbook_source_dir
end

#techbook_textfontObject

Returns the value of attribute techbook_textfont.



634
635
636
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 634

def techbook_textfont
  @techbook_textfont
end

#xref_tableObject (readonly)

Returns the value of attribute xref_table.



354
355
356
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 354

def xref_table
  @xref_table
end

Class Method Details

.run(args) ⇒ Object



793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 793

def self.run(args)
  config = OpenStruct.new
  config.regen      = false
  config.cache      = true
  config.compressed = false

  opts = OptionParser.new do |opt|
    opt.banner    = PDF::Writer::Lang[:techbook_usage_banner] % [ File.basename($0) ]
    PDF::Writer::Lang[:techbook_usage_banner_1].each do |ll|
      opt.separator "  #{ll}"
    end
    opt.on('-f', '--force-regen', *PDF::Writer::Lang[:techbook_help_force_regen]) { config.regen = true }
    opt.on('-n', '--no-cache', *PDF::Writer::Lang[:techbook_help_no_cache]) { config.cache = false }
    opt.on('-z', '--compress', *PDF::Writer::Lang[:techbook_help_compress]) { config.compressed = true }
    opt.on_tail ""
    opt.on_tail("--help", *PDF::Writer::Lang[:techbook_help_help]) { $stderr << opt; exit(0) }
  end
  opts.parse!(args)

  config.document = args[0]

  unless config.document
    config.document = "manual.pwd"
    unless File.exist?(config.document)
      dirn = File.dirname(__FILE__)
      config.document = File.join(dirn, File.basename(config.document))
      unless File.exist?(config.document)
        dirn = File.join(dirn, "..")
        config.document = File.join(dirn, File.basename(config.document))
        unless File.exist?(config.document)
          dirn = File.join(dirn, "..")
          config.document = File.join(dirn,
                                      File.basename(config.document))
          unless File.exist?(config.document)
            $stderr.puts PDF::Writer::Lang[:techbook_cannot_find_document]
            exit(1)
          end
        end
      end
    end

    $stderr.puts PDF::Writer::Lang[:techbook_using_default_doc] % config.document
  end

  dirn = File.dirname(config.document)
  extn = File.extname(config.document)
  base = File.basename(config.document, extn)

  files = {
    :document => config.document,
    :cache    => "#{base}._mc",
    :pdf      => "#{base}.pdf"
  }

  unless config.regen
    if File.exist?(files[:cache])
      _tm_doc = File.mtime(config.document)
      _tm_prg = File.mtime(__FILE__)
      _tm_cch = File.mtime(files[:cache])
      
        # If the cached file is newer than either the document or the
        # class program, then regenerate.
      if (_tm_doc < _tm_cch) and (_tm_prg < _tm_cch)
        $stderr.puts PDF::Writer::Lang[:techbook_using_cached_doc] % File.basename(files[:cache])
        pdf = File.open(files[:cache], "rb") { |cf| Marshal.load(cf.read) }
        pdf.save_as(files[:pdf])
        File.open(files[:pdf], "wb") { |pf| pf.write pdf.render }
        exit(0)
      else
        $stderr.puts PDF::Writer::Lang[:techbook_regenerating]
      end
    end
  else
    $stderr.puts PDF::Writer::Lang[:techbook_ignoring_cache] if File.exist?(files[:cache])
  end

    # Create the manual object.
  pdf = PDF::TechBook.new
  pdf.compressed = config.compressed
  pdf.techbook_source_dir = File.expand_path(dirn)

  document = open(files[:document]) { |io| io.read.split($/) }
  progress = ProgressBar.new(base.capitalize, document.size)
  pdf.techbook_parse(document, progress)
  progress.finish

  if pdf.generate_table_of_contents?
    progress = ProgressBar.new("TOC", pdf.table_of_contents.size)
    pdf.techbook_toc(progress)
    progress.finish
  end

  if config.cache
    File.open(files[:cache], "wb") { |f| f.write Marshal.dump(pdf) }
  end

  pdf.save_as(files[:pdf])
end

Instance Method Details

#__heading1(heading) ⇒ Object



433
434
435
436
437
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 433

def __heading1(heading)
  @chapter_number ||= 0
  @chapter_number = @chapter_number.succ
  "#{chapter_number}. #{heading}"
end

#__heading2(heading) ⇒ Object



438
439
440
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 438

def __heading2(heading)
  heading
end

#__heading3(heading) ⇒ Object



441
442
443
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 441

def __heading3(heading)
  "<b>#{heading}</b>"
end

#__heading4(heading) ⇒ Object



444
445
446
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 444

def __heading4(heading)
  "<i>#{heading}</i>"
end

#__heading5(heading) ⇒ Object



447
448
449
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 447

def __heading5(heading)
  "<c:uline>#{heading}</c:uline>"
end

#generate_table_of_contents?Boolean

Returns:

  • (Boolean)


787
788
789
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 787

def generate_table_of_contents?
  @gen_toc
end

#techbook_directive_author(args) ⇒ Object



746
747
748
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 746

def techbook_directive_author(args)
  info.author = args
end

#techbook_directive_blist(args) ⇒ Object



764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 764

def techbook_directive_blist(args)
  __render_paragraph
  sm = /^(\w+).*$/o.match(args)
  style = sm.captures[0] if sm
  style = "bullet" unless LIST_ITEM_STYLES.include?(style)

  @blist_factor = @left_margin * 0.10 if @blist_info.empty?

  info = {
    :left_margin  => @left_margin,
    :style        => style
  }
  @blist_info << info
  @left_margin += @blist_factor

  @techbook_lastmode, @techbook_mode = @techbook_mode, :blist if :blist != @techbook_mode
end

#techbook_directive_code(args) ⇒ Object

Code: .code



661
662
663
664
665
666
667
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 661

def techbook_directive_code(args)
  __render_paragraph
  select_font @techbook_codefont, @techbook_encoding
  @techbook_lastmode, @techbook_mode = @techbook_mode, :code
  @techbook_textopt  = { :justification => :left, :left => 20, :right => 20 }
  @techbook_fontsize = 10
end

#techbook_directive_columns(args) ⇒ Object

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



719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 719

def techbook_directive_columns(args)
  av = /^(\d+|off)(?: (\d+))?(?: .*)?$/o.match(args)
  unless av
    $stderr.puts PDF::Writer::Lang[:techbook_bad_columns_directive] % args
    raise ArgumentError
  end
  cols = av.captures[0]

    # Flush the paragraph cache.
  __render_paragraph

  if cols == "off" or cols.to_i < 2
    stop_columns
  else
    if av.captures[1]
      start_columns(cols.to_i, av.captures[1].to_i)
    else
      start_columns(cols.to_i)
    end
  end
end

#techbook_directive_done(args) ⇒ Object

Done. Stop parsing: .done



709
710
711
712
713
714
715
716
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 709

def techbook_directive_done(args)
  unless @techbook_code.empty?
    $stderr.puts PDF::Writer::Lang[:techbook_code_not_empty]
    $stderr.puts @techbook_code
  end
  __render_paragraph
  :break
end

#techbook_directive_endblist(args) ⇒ Object



782
783
784
785
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 782

def techbook_directive_endblist(args)
  self.left_margin = @blist_info.pop[:left_margin]
  @techbook_lastmode, @techbook_mode = @techbook_mode, @techbook_lastmode if @blist_info.empty?
end

#techbook_directive_endcode(args) ⇒ Object

End Code: .endcode



670
671
672
673
674
675
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 670

def techbook_directive_endcode(args)
  select_font @techbook_textfont, @techbook_encoding
  @techbook_lastmode, @techbook_mode = @techbook_mode, @techbook_lastmode
  @techbook_textopt  = { :justification => :full }
  @techbook_fontsize = 12
end

#techbook_directive_endeval(args) ⇒ Object

End Eval: .endeval



684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 684

def techbook_directive_endeval(args)
  save_state

  thread = Thread.new do
    begin
      @techbook_code.untaint
      pdf = self
      eval @techbook_code
    rescue Exception => ex
      err = PDF::Writer::Lang[:techbook_eval_exception]
      $stderr.puts err % [ @techbook_line__, ex, ex.backtrace.join("\n") ]
      raise ex
    end
  end
  thread.abort_on_exception = true
  thread.join

  restore_state
  select_font @techbook_textfont, @techbook_encoding

  @techbook_code = ""
  @techbook_mode, @techbook_lastmode = @techbook_lastmode, @techbook_mode
end

#techbook_directive_endpre(args) ⇒ Object

End preserved newlines: .endpre



656
657
658
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 656

def techbook_directive_endpre(args)
  @techbook_mode = :normal
end

#techbook_directive_eval(args) ⇒ Object

Eval: .eval



678
679
680
681
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 678

def techbook_directive_eval(args)
  __render_paragraph
  @techbook_lastmode, @techbook_mode = @techbook_mode, :eval
end

#techbook_directive_keywords(args) ⇒ Object



758
759
760
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 758

def techbook_directive_keywords(args)
  info.keywords = args
end

#techbook_directive_newpage(args) ⇒ Object

Start a new page: .newpage



639
640
641
642
643
644
645
646
647
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 639

def techbook_directive_newpage(args)
  __render_paragraph

  if args =~ /^force/
    start_new_page true
  else
    start_new_page
  end
end

#techbook_directive_pre(args) ⇒ Object

Preserved newlines: .pre



650
651
652
653
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 650

def techbook_directive_pre(args)
  __render_paragraph
  @techbook_mode = :preserved
end

#techbook_directive_subject(args) ⇒ Object



754
755
756
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 754

def techbook_directive_subject(args)
  info.subject  = args
end

#techbook_directive_title(args) ⇒ Object



750
751
752
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 750

def techbook_directive_title(args)
  info.title  = args
end

#techbook_directive_toc(args) ⇒ Object



741
742
743
744
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 741

def techbook_directive_toc(args)
  @toc_title  = args unless args.empty?
  @gen_toc    = true
end

#techbook_parse(document, progress = nil) ⇒ Object



517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 517

def techbook_parse(document, progress = nil)
  @table_of_contents = []

  @toc_title          = "Table of Contents"
  @gen_toc            = false
  @techbook_code      = ""
  @techbook_para      = ""
  @techbook_fontsize  = 12
  @techbook_textopt   = { :justification => :full }
  @techbook_lastmode  = @techbook_mode = :normal

  @techbook_textfont  = "Times-Roman"
  @techbook_codefont  = "Courier"

  @blist_info         = []

  @techbook_line__    = 0

  __build_xref_table(document)

  document.each do |line|
  begin
    progress.inc if progress
    @techbook_line__ += 1

    next if line =~ %r{^#}o

    directive, args = techbook_find_directive(line)
    if directive
        # Just try to call the method/directive. It will be far more
        # common to *find* the method than not to.
      res = __send__("techbook_directive_#{directive}", args) rescue nil
      break if :break == res 
      next
    end

    case @techbook_mode
    when :eval
      @techbook_code << line << "\n"
      next
    when :code
      techbook_text(line)
      next
    when :blist
      line = "<C:#{@blist_info[-1][:style]}/>#{line}"
      techbook_text(line)
      next
    end

    next if techbook_heading(line)

    if :preserved == @techbook_mode
      techbook_text(line)
      next
    end

    line.chomp!

    if line.empty?
      __render_paragraph
      techbook_text("\n")
    else
      @techbook_para << " " unless @techbook_para.empty?
      @techbook_para << line
    end
  rescue Exception => ex
    $stderr.puts PDF::Writer::Lang[:techbook_exception] % [ ex, @techbook_line ]
    raise
  end
  end
end

#techbook_text(line) ⇒ Object



892
893
894
895
896
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 892

def techbook_text(line)
  opt = @techbook_textopt.dup
  opt[:font_size] = @techbook_fontsize
  text(line, opt)
end

#techbook_toc(progress = nil) ⇒ Object



589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# File 'lib/gems/pdf-writer-1.1.8/lib/pdf/techbook.rb', line 589

def techbook_toc(progress = nil)
  insert_mode :on
  insert_position :after
  insert_page 1
  start_new_page

  style = H1_STYLE
  save_state

  if style[:bar]
    fill_color    style[:background]
    fh = font_height(style[:font_size]) * 1.01
    fd = font_descender(style[:font_size]) * 1.01
    x = absolute_left_margin
    w = absolute_right_margin - absolute_left_margin
    rectangle(x, y - fh + fd, w, fh).fill
  end

  fill_color  style[:foreground]
  text(@toc_title, :font_size => style[:font_size],
       :justification => style[:justification])

  restore_state

  self.y += font_descender(style[:font_size])#* 0.5

  right = absolute_right_margin

    # TODO -- implement tocdots as a replace tag and a single drawing tag.
  @table_of_contents.each do |entry|
    progress.inc if progress

    info =  "<c:ilink dest='#{entry[:xref]}'>#{entry[:title]}</c:ilink>"
    info << "<C:tocdots level='#{entry[:level]}' page='#{entry[:page]}' xref='#{entry[:xref]}'/>"

    case entry[:level]
    when 1
      text info, :font_size => 16, :absolute_right => right
    when 2
      text info, :font_size => 12, :left => 50, :absolute_right => right
    end
  end
end