Class: ODDB::ChapterParse::Writer

Inherits:
NullWriter show all
Defined in:
ext/chapterparse/src/writer.rb

Instance Method Summary (collapse)

Methods inherited from NullWriter

#new_linkhandler, #send_image, #send_meta

Constructor Details

- (Writer) initialize

A new instance of Writer



10
11
12
13
14
# File 'ext/chapterparse/src/writer.rb', line 10

def initialize
  @chapter = Text::Chapter.new
  @section = @chapter.next_section
  @target = @section.next_paragraph
end

Instance Method Details

- (Object) chapter



15
16
17
18
# File 'ext/chapterparse/src/writer.rb', line 15

def chapter
  @chapter.clean!
  @chapter
end

- (Object) new_font(font)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'ext/chapterparse/src/writer.rb', line 19

def new_font(font)
  return if @table
  case font
  when [nil, 1, nil, nil]
    if(@target.is_a?(Text::Paragraph) \
      && !(@target.empty? || @target.preformatted?))
      @target.set_format(:italic)
    else
      @section = @chapter.next_section
      @subheading = @target = @section.subheading
    end
  when [nil, nil, nil, 1]
    unless(preformatted?(@target))
      @target = @section.next_paragraph
      @target.preformatted!
    end
  else
    if(@target.is_a?(Text::Paragraph))
      unless(@target.preformatted?)
        @target.set_format()
      end
    else
      @target = @section.next_paragraph
    end
  end
end

- (Object) new_fonthandler(fh)



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'ext/chapterparse/src/writer.rb', line 45

def new_fonthandler(fh)
  if(@target.is_a?(Text::Paragraph))
    if(fh && fh.attribute('face') == 'Symbol')
      @target.augment_format(:symbol)
    else
      @target.reduce_format(:symbol)
    end
    if(fh && (align = fh.attribute('vertical-align')))
      @target.augment_format(align.to_sym)
    else
      @target.reduce_format(:superscript)
      @target.reduce_format(:subscript)
    end
  end
end

- (Object) new_tablehandler(th)



60
61
62
63
64
65
66
67
68
69
70
# File 'ext/chapterparse/src/writer.rb', line 60

def new_tablehandler(th)
  if(@table)
    paragraph = @section.next_paragraph
    paragraph.preformatted!
    paragraph << @table.to_s
  end
  @table = @target = th
  if(th.nil?)
    @target = @section.next_paragraph
  end
end

- (Boolean) preformatted?(target)

Returns:

  • (Boolean)


71
72
73
74
# File 'ext/chapterparse/src/writer.rb', line 71

def preformatted?(target)
  target.is_a?(Text::Paragraph) \
    && target.preformatted?
end

- (Object) send_flowing_data(data)



89
90
91
92
93
94
# File 'ext/chapterparse/src/writer.rb', line 89

def send_flowing_data(data)
  if(preformatted?(@target))
    @target = @section.next_paragraph
  end
  @target << data.gsub(/\302\240/u, " ")
end

- (Object) send_line_break



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'ext/chapterparse/src/writer.rb', line 75

def send_line_break
  if(!@table && (@target.empty? && @subheading) \
    || @target == @section.subheading)
    @subheading << "\n"
    @subheading = nil
  end
  if(@table)
    @table.next_line
  elsif(preformatted?(@target))
    @target << "\n"
  else
    @target = @section.next_paragraph
  end
end

- (Object) send_literal_data(data)



95
96
97
# File 'ext/chapterparse/src/writer.rb', line 95

def send_literal_data(data)
  @target << data.gsub(/\302\240/u, " ").gsub(/\r\n?/u, "\n")
end