Class: ODDB::ChapterParse::Writer

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

Instance Method Summary collapse

Constructor Details

#initializeWriter

Returns 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

#chapterObject



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

def chapter
	@chapter.clean!
	@chapter
end

#new_font(font) ⇒ Object



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

#new_fonthandler(fh) ⇒ Object



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

#new_tablehandler(th) ⇒ Object



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

#preformatted?(target) ⇒ Boolean

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

#send_flowing_data(data) ⇒ Object



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

#send_line_breakObject



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

#send_literal_data(data) ⇒ Object



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