notroff 0.2.11 → 0.2.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,18 @@
1
+ class FileProcessor
2
+ def initialize(path)
3
+ @path = path
4
+ end
5
+ end
6
+
7
+ class FileReader < FileProcessor
8
+ def process(ignored)
9
+ paras = File.readlines(@path)
10
+ paras.map! {|p| p.rstrip}
11
+ end
12
+ end
13
+
14
+ class FileWriter < FileProcessor
15
+ def process(output)
16
+ File.open(@path, 'w') {|f| f.write(output)}
17
+ end
18
+ end
@@ -1,3 +1,13 @@
1
+ class SkipFilter
2
+ def initialize(re1)
3
+ @re = re
4
+ end
5
+
6
+ def process(paras)
7
+ paras.find_all {|p| p != re}
8
+ end
9
+ end
10
+
1
11
  class BetweenFilter
2
12
  def initialize(re1, re2=re1)
3
13
  @re1 = re1
@@ -83,41 +93,81 @@ class EmbeddedRubyProcessor
83
93
  def process_command(ruby_expression)
84
94
  Logger.log "Ruby expression: #{ruby_expression}"
85
95
  paras = eval(ruby_expression, binding)
86
- paras = [paras] unless paras.kind_of?(Array)
87
- paras.map! {|p| Text.new(p, :type => :code)}
96
+ paras.keep_if {|p| p[:included]}
88
97
  end
89
98
 
90
- def embed(*filters, &block)
91
- paras = block.call.map {|line| line.rstrip}
92
- Logger.log "EMBED: #{paras}"
93
- unless filters.empty?
94
- filters.each {|f| paras = f.process(paras)}
95
- paras = paras.find_all {|p| p[:included]}
99
+ def embed(type, inc_all=true, lines)
100
+ lines.map! do |line|
101
+ result = Text.new(line.rstrip, :type => type)
102
+ result[:original_text] = line.rstrip
103
+ result[:included] = inc_all
104
+ result
96
105
  end
97
- paras
106
+ lines
98
107
  end
99
108
 
100
- def inc(path, *filters)
101
- embed(*filters) {File.readlines(path)}
109
+ def skip(re, paras)
110
+ paras.each do |para|
111
+ para[:included] = false if (para =~ re)
112
+ end
102
113
  end
103
114
 
104
- def run(shell_command, *filters)
105
- embed(*filters) {File.popen(shell_command).readlines}
115
+ def inc(path, inc_all=true, type=:code)
116
+ embed(type, inc_all, File.readlines(path))
106
117
  end
107
118
 
108
- def ex(ruby_command, *filters)
119
+ def run(shell_command, type=:code)
120
+ embed(type, File.popen(shell_command).readlines)
121
+ end
122
+
123
+ def ex(ruby_command, type=:code)
109
124
  puts "ruby_command: #{ruby_command}"
110
- embed(*filters) {eval(ruby_command).to_s.split("\n")}
125
+ embed(type, eval(ruby_command).to_s.split("\n"))
111
126
  end
112
127
 
113
- def matches(re1, re2=re1)
114
- BetweenFilter.new(re1, re2)
128
+ def stdinc(path, re1=/##A/, re2=/##Z/, type=:code)
129
+ skip(/##X/, between(re1, re2, inc(path, false, type)))
115
130
  end
116
131
 
117
- def method(name, include_body=true)
118
- MethodFilter.new(name, include_body)
132
+ def between(re1, re2, paras)
133
+ state = :before_first
134
+ paras.each do |para|
135
+ if state == :before_first and re1 =~ para
136
+ state = :after_first
137
+ break if para =~ re2
138
+ elsif state == :after_first
139
+ break if para =~ re2
140
+ para[:included] = true
141
+ end
142
+ end
143
+ paras
144
+ end
145
+
146
+ def definition(def_re, include_body, paras)
147
+ state = :before_def
148
+ end_re = nil
149
+ paras.each do |para|
150
+ Logger.log para, (def_re =~ para)
151
+ if state == :before_def and def_re =~ para
152
+ para[:included] = true
153
+ end_re = Regexp.new( "^#{' ' * para.string.indent_depth}end" )
154
+ state = :after_def
155
+ elsif state == :after_def and end_re =~ para.string
156
+ para[:included] = true
157
+ break
158
+ elsif state == :after_def and include_body
159
+ para[:included] = true
160
+ end
161
+ end
162
+ paras
163
+ end
164
+
165
+
166
+ def meth(method_name, include_body, paras)
167
+ definition(/^ *def +#{method_name}(\(|$| )/, include_body, paras)
119
168
  end
120
169
 
170
+ =begin
121
171
  def clazz(name, include_body=true)
122
172
  ClassFilter.new(name, include_body)
123
173
  end
@@ -125,6 +175,7 @@ class EmbeddedRubyProcessor
125
175
  def mod(name, include_body=true)
126
176
  ModuleFilter.new(name, include_body)
127
177
  end
178
+ =end
128
179
 
129
180
  def indent(delta_indent, paragraphs)
130
181
  paragraphs.map do |p|
@@ -37,9 +37,13 @@ class OdtFormatter < Formatter
37
37
  def initialize(input, output)
38
38
  super()
39
39
  prepend_processor FileReader.new(input)
40
+ add_processor BodyTypeRefiner.new
40
41
  add_processor CodeTypeRefiner.new
42
+ add_processor CodeTypeRefiner.new(:listing, :first_listing, :middle_listing, :end_listing)
41
43
  add_processor Grouper.new(:bullet)
42
44
  add_processor Grouper.new(:list)
45
+ add_processor Grouper.new(:quote)
46
+ add_processor Grouper.new(:attribution)
43
47
  add_processor OdtRenderer.new
44
48
  add_processor TemplateExpander.new
45
49
  add_processor OdtReplacer.new(output)
@@ -9,16 +9,24 @@ class OdtRenderer < Processor
9
9
  LONG_DASH_CODE = 0xe1.chr + 0x80.chr + 0x93.chr
10
10
 
11
11
  PARAGRAPH_STYLES = {
12
- :body => 'BodyNoIndent',
13
- :title => 'HB',
12
+ :body => 'FT',
13
+ :body2 => 'IT',
14
+ :title => 'HA',
15
+ :subtitle => 'HB',
14
16
  :section => 'HC',
15
17
  :sec => 'HC',
18
+ :subsec => 'HD',
16
19
  :first_code => 'CDT1',
17
20
  :middle_code => 'CDT',
18
21
  :end_code => 'CDTX',
19
22
  :author => 'AU',
20
23
  :quote => 'Quotation',
24
+ :attribution => 'Quotation Attribution',
21
25
  :single_code => 'C1',
26
+ :ltitle => 'LH',
27
+ :first_listing => 'LC',
28
+ :middle_listing => 'LC2',
29
+ :end_listing => 'LX',
22
30
  :pn => 'PN',
23
31
  :pt => 'PT',
24
32
  :cn => 'HA',
@@ -45,19 +53,30 @@ class OdtRenderer < Processor
45
53
 
46
54
  result = nil
47
55
 
48
- if [ :author, :section, :sec, :title, :pn, :pt, :chapter ].include?( type )
49
- result = new_text_element( type )
50
- result.add_text( text.string )
51
- elsif [:body, :quote].include?(type)
56
+
57
+ if [:body, :body2].include?(type)
58
+ Logger.log("Rendering bodyish para of type ", type )
52
59
  result = new_text_element( type )
53
60
  add_body_text( text, result )
61
+
62
+ elsif quote_type?(para)
63
+ result = new_quote_element(para[:kid_type], para[:kids])
64
+
54
65
  elsif list_type?(para)
55
66
  result = new_list_element(para[:kids])
67
+
56
68
  elsif bullet_type?(para)
57
69
  result = new_bullet_element(para[:kids])
70
+
58
71
  elsif code_type?(type)
59
72
  result = new_text_element( type )
60
73
  add_code_text( text, result )
74
+
75
+ elsif PARAGRAPH_STYLES[type]
76
+ Logger.log("Rendering simple para of type ", type )
77
+ result = new_text_element( type )
78
+ result.add_text( text.string )
79
+
61
80
  else
62
81
  raise "Dont know what to do with type [#{type}]"
63
82
  end
@@ -68,6 +87,10 @@ class OdtRenderer < Processor
68
87
  para[:type] == :group
69
88
  end
70
89
 
90
+ def quote_type?(para)
91
+ group?(para) and [:quote, :attribution].include?(para[:kid_type])
92
+ end
93
+
71
94
  def list_type?(para)
72
95
  group?(para) and para[:kid_type] == :list
73
96
  end
@@ -77,21 +100,21 @@ class OdtRenderer < Processor
77
100
  end
78
101
 
79
102
  def code_type?( type )
80
- [ :first_code, :middle_code, :end_code, :single_code ].include?(type)
103
+ [ :first_code, :middle_code, :end_code, :single_code,
104
+ :listing, :first_listing, :middle_listing, :end_listing ].include?(type)
81
105
  end
82
106
 
83
- # <text:list xml:id="list899615401" text:style-name="L2">
84
- # <text:list-item>
85
- # <text:p text:style-name="P7">first</text:p>
86
- # </text:list-item>
87
- # <text:list-item>
88
- # <text:p text:style-name="P3">second</text:p>
89
- # </text:list-item>
90
- # <text:list-item>
91
- # <text:p text:style-name="P4">third</text:p>
92
- # </text:list-item>
93
- # </text:list>
94
-
107
+ def new_quote_element(type, items)
108
+ p = new_text_element(type)
109
+ items.each_with_index do |item, i|
110
+ p.add( Element.new('text:line-break')) unless i == 0
111
+ el = Element.new('text:span')
112
+ add_body_text(item.string, el)
113
+ p.add(el)
114
+ end
115
+ puts p.to_s
116
+ p
117
+ end
95
118
 
96
119
  def new_list_element(items)
97
120
  list = Element.new('text:list')
Binary file
@@ -6,8 +6,11 @@ class TypeAssigner
6
6
 
7
7
  paragraphs.each do |paragraph|
8
8
  type = paragraph[:type]
9
- if (type == :body) or (type == :code) or (type == :quote)
9
+ if (type == :body) or (type == :code) or (type == :listing)
10
10
  current_type = type
11
+ elsif type == :quote
12
+ paragraph[:type] = :quote
13
+ processed_paragraphs << paragraph
11
14
  elsif type == :c1 || type == :code1
12
15
  paragraph[:type] = :code
13
16
  processed_paragraphs << paragraph
@@ -28,22 +28,45 @@ class TypeRefiner
28
28
  end
29
29
 
30
30
  class CodeTypeRefiner < TypeRefiner
31
+ def initialize(base_type=:code, first_type=:first_code, middle_type=:middle_code,
32
+ end_type=:end_code, single_type=middle_type)
33
+ @base_type = base_type
34
+ @first_type = first_type
35
+ @middle_type = middle_type
36
+ @end_type = end_type
37
+ @single_type = single_type
38
+ end
39
+
31
40
  def type_for( previous_type, type, next_type )
32
41
  Logger.log "code type for [#{previous_type}] [#{type}] [#{next_type}]"
33
- if type != :code
42
+ if type != @base_type
34
43
  new_type = type
35
44
 
36
- elsif previous_type == :code and next_type == :code
37
- new_type = :middle_code
45
+ elsif previous_type ==@base_type and next_type == @base_type
46
+ new_type = @middle_type
38
47
 
39
- elsif previous_type == :code
40
- new_type = :end_code
48
+ elsif previous_type == @base_type
49
+ new_type = @end_type
41
50
 
42
- elsif next_type == :code
43
- new_type = :first_code
51
+ elsif next_type == @base_type
52
+ new_type = @first_type
44
53
 
45
54
  else
46
- new_type = :single_code
55
+ new_type = @single_type
56
+ end
57
+
58
+ Logger.log("new type: #{new_type}")
59
+ new_type
60
+ end
61
+ end
62
+
63
+ class BodyTypeRefiner < TypeRefiner
64
+ def type_for( previous_type, type, next_type )
65
+ Logger.log "body type for [#{previous_type}] [#{type}] [#{next_type}]"
66
+
67
+ new_type = type
68
+ if (type == :body) and (previous_type == :body)
69
+ new_type = :body2
47
70
  end
48
71
 
49
72
  Logger.log("new type: #{new_type}")
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.2.11
4
+ version: 0.2.13
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -32,6 +32,7 @@ files:
32
32
  - spec/text_spec.rb
33
33
  - spec/type_assigner_spec.rb
34
34
  - spec/with_commands.nr
35
+ - lib/notroff/#io.rb#
35
36
  - lib/notroff/code_scrubber.rb
36
37
  - lib/notroff/command_processor.rb
37
38
  - lib/notroff/composite_processor.rb