notroff 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ class Filter
2
+ def process(paragraphs)
3
+ paragraphs.find_all {|p| included?(p)}
4
+ end
5
+
6
+ def included?(paragraph)
7
+ true
8
+ end
9
+ end
10
+
11
+ class IncludedFilter < Filter
12
+ def included?(paragraph)
13
+ paragraph[:included]
14
+ end
15
+ end
16
+
17
+ class RegularExpressionExcludeFilter
18
+ < Filter
19
+ def initialize(exclude_re)
20
+ @exclude_re = exclude_re
21
+ end
22
+
23
+ def included?(paragraph)
24
+ @exclude_re !~ paragraph
25
+ end
26
+
27
+ end
@@ -13,3 +13,14 @@ class IncludedFilter < Filter
13
13
  paragraph[:included]
14
14
  end
15
15
  end
16
+
17
+ class RegularExpressionExcludeFilter < Filter
18
+ def initialize(exclude_re)
19
+ @exclude_re = exclude_re
20
+ end
21
+
22
+ def included?(paragraph)
23
+ @exclude_re !~ paragraph
24
+ end
25
+
26
+ end
@@ -36,7 +36,9 @@ end
36
36
  class OdtFormatter < Formatter
37
37
  def initialize(input, output)
38
38
  super()
39
+ prepend_processor RegularExpressionExcludeFilter.new(/^--.*$/)
39
40
  prepend_processor FileReader.new(input)
41
+
40
42
  add_processor BodyTypeRefiner.new
41
43
  add_processor CodeTypeRefiner.new
42
44
  add_processor CodeTypeRefiner.new(:listing, :first_listing, :middle_listing, :end_listing)
@@ -41,7 +41,7 @@ class OdtRenderer < Processor
41
41
  new_element = format( paragraph )
42
42
  elements << new_element if new_element
43
43
  end
44
- elements
44
+ {:body => elements}
45
45
  end
46
46
 
47
47
  def format( para )
@@ -9,17 +9,23 @@ class TemplateExpander < Processor
9
9
  def initialize
10
10
  end
11
11
 
12
- def process( elements )
13
- puts "reading file: #{TEMPLATE}"
12
+ def process( content_map )
14
13
  doc = REXML::Document.new(File.new(TEMPLATE))
14
+ insert_body(doc, content_map[:body])
15
+ doc.to_s
16
+ end
17
+
18
+ def insert_body(doc, body_elements)
19
+ puts "reading file: #{TEMPLATE}"
20
+
15
21
  text_element = REXML::XPath.first(doc, PATH)
16
22
  n = text_element.elements.size
17
23
  puts "deleting #{n} elements"
18
24
  n.times {|i| text_element.delete_element(1)}
19
- puts "ELs: #{elements.size}"
20
- puts "ELs: #{elements}"
21
- elements.each {|el| text_element.add_element(el)}
22
- puts "OUtput: #{doc.to_s}"
23
- doc.to_s
25
+ puts "ELs: #{body_elements.size}"
26
+ puts "ELs: #{body_elements}"
27
+ body_elements.each {|el| text_element.add_element(el)}
28
+
29
+ doc
24
30
  end
25
31
  end
@@ -44,7 +44,7 @@ class OdtParser
44
44
  ParagraphStyle.new('HA', nil, :title, true),
45
45
  ParagraphStyle.new('HB', nil, :subtitle, true),
46
46
  ParagraphStyle.new('HC', nil, :sec, true),
47
- ParagraphStyle.new('HD', nil, :subsec, true),
47
+ ParagraphStyle.new('TH', nil, :theading, true),
48
48
  ParagraphStyle.new('LH', nil, :ltitle, true),
49
49
  ParagraphStyle.new('LC', nil, :listing, false),
50
50
  ParagraphStyle.new('LC2', nil, :listing, false),
@@ -53,6 +53,7 @@ class OdtParser
53
53
  ParagraphStyle.new('BL1', nil, :bullet, true),
54
54
  ParagraphStyle.new('BL', nil, :bullet, true),
55
55
  ParagraphStyle.new('BX', nil, :bullet, true),
56
+ ParagraphStyle.new('BLPara', nil, :bullet, true),
56
57
  ParagraphStyle.new('Quotation_20_Attribution', nil, :attribution, true),
57
58
  ParagraphStyle.new('CDTX', nil, :code, false)
58
59
  ]
@@ -92,7 +93,7 @@ class OdtParser
92
93
  styles.each do |s|
93
94
  attrs = s.attributes
94
95
  style = ParagraphStyle.new(attrs['style:name'])
95
- style.parent = lookup_para_style(attrs['parent-style-name'])
96
+ style.parent = find_or_create_para_style(attrs['parent-style-name'])
96
97
  @para_styles[style.name] = style
97
98
  end
98
99
  end
@@ -112,6 +113,17 @@ class OdtParser
112
113
  s
113
114
  end
114
115
 
116
+ def find_or_create_para_style(name)
117
+ # return lookup_para_style(name)
118
+ s = @para_styles[name]
119
+ unless s
120
+ STDERR.puts "Warning: no paragraph style named #{name}"
121
+ s = ParagraphStyle.new(name, nil, :body, false)
122
+ @para_styles[s.name] = s
123
+ s
124
+ end
125
+ end
126
+
115
127
  def lookup_text_style(name)
116
128
  name = "Default" if name.nil? or name.empty?
117
129
  s = @text_styles[name]
@@ -119,11 +131,21 @@ class OdtParser
119
131
  s
120
132
  end
121
133
 
134
+ def find_or_create_text_style(name)
135
+ s = @text_styles[name]
136
+ unless s
137
+ STDERR.puts "Warning: no character style named #{name}"
138
+ s = TextStyle.new(name)
139
+ @text_styles[s.name] = s
140
+ end
141
+ s
142
+ end
143
+
122
144
  def parse_paragraph(p)
123
145
  attrs = p.attributes
124
146
  # puts "Parsing paragraph, attrs #{attrs}"
125
147
  # puts "==> style-name: [[#{attrs['text:style-name']}]]"
126
- style = lookup_para_style(attrs['text:style-name'])
148
+ style = find_or_create_para_style(attrs['text:style-name'])
127
149
 
128
150
  para = Paragraph.new(style)
129
151
  para.contents = parse_contents(para, p)
@@ -132,7 +154,7 @@ class OdtParser
132
154
 
133
155
  def parse_span(el)
134
156
  attrs = el.attributes
135
- style = lookup_text_style(attrs['text:style-name'])
157
+ style = find_or_create_text_style(attrs['text:style-name'])
136
158
  indent = attrs['text:c'] ? attrs['text:c'].to_i : 0
137
159
  span = Span.new(style)
138
160
  span.indent = indent
@@ -87,7 +87,7 @@ end
87
87
 
88
88
  class Paragraph < Container
89
89
  def render(w)
90
- style.render(w)
90
+ style.render(w) if style
91
91
  super(w)
92
92
  w.end_paragraph
93
93
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notroff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -33,6 +33,7 @@ files:
33
33
  - spec/text_spec.rb
34
34
  - spec/type_assigner_spec.rb
35
35
  - spec/with_commands.nr
36
+ - lib/notroff/#filter.rb#
36
37
  - lib/notroff/#io.rb#
37
38
  - lib/notroff/code_scrubber.rb
38
39
  - lib/notroff/command_processor.rb