notroff 0.3.2 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,6 +12,9 @@ while true
12
12
  elsif ARGV.first == '-h'
13
13
  formatter_class = HtmlFormatter
14
14
  ARGV.shift
15
+ elsif ARGV.first == '-s'
16
+ formatter_class = SpecFormatter
17
+ ARGV.shift
15
18
  elsif ARGV.first == '-d'
16
19
  formatter_class = DocbookFormatter
17
20
  ARGV.shift
@@ -17,6 +17,7 @@ require "notroff/html_renderer"
17
17
  require "notroff/docbook_renderer"
18
18
  require "notroff/odt_renderer"
19
19
  require "notroff/odt_replacer"
20
+ require "notroff/spec_renderer"
20
21
  require "notroff/template_expander"
21
22
  require "notroff/type_refiner"
22
23
  require "notroff/filter"
@@ -81,7 +81,6 @@ class EmbeddedRubyProcessor
81
81
  if p[:type] == :x
82
82
  Logger.log p
83
83
  results = process_command(p.string)
84
- puts "**** results: #{results}"
85
84
  new_paragraphs << results if results
86
85
  else
87
86
  new_paragraphs << p
@@ -103,6 +102,7 @@ class EmbeddedRubyProcessor
103
102
  result[:included] = inc_all
104
103
  result
105
104
  end
105
+ Logger.warn("Zero lines included!!") if lines.empty?
106
106
  lines
107
107
  end
108
108
 
@@ -121,7 +121,6 @@ class EmbeddedRubyProcessor
121
121
  end
122
122
 
123
123
  def ex(ruby_command, type=:code)
124
- puts "ruby_command: #{ruby_command}"
125
124
  embed(type, eval(ruby_command).to_s.split("\n"))
126
125
  end
127
126
 
@@ -136,10 +135,15 @@ class EmbeddedRubyProcessor
136
135
  state = :after_first
137
136
  break if para =~ re2
138
137
  elsif state == :after_first
139
- break if para =~ re2
138
+ if para =~ re2
139
+ state = :after_second
140
+ break
141
+ end
140
142
  para[:included] = true
141
143
  end
142
144
  end
145
+ raise "Couldnt find first pattern" if state == :before_first
146
+ raise "Couldnt find second pattern" unless state == :after_second
143
147
  paras
144
148
  end
145
149
 
@@ -29,6 +29,8 @@ class DocbookFormatter < Formatter
29
29
  add_processor Grouper.new(:bullet)
30
30
  add_processor Grouper.new(:list)
31
31
  add_processor DocbookRenderer.new
32
+ add_processor OdtReplacer.new(output)
33
+ add_processor OdtReplacer.new(output)
32
34
  add_processor FileWriter.new(output)
33
35
  end
34
36
  end
@@ -49,7 +51,25 @@ class OdtFormatter < Formatter
49
51
  add_processor OdtRenderer.new
50
52
  add_processor TemplateExpander.new
51
53
  add_processor OdtReplacer.new(output)
54
+ end
55
+ end
56
+
57
+ class SpecFormatter < Formatter
58
+ def initialize(input, output)
59
+ super()
60
+ prepend_processor RegularExpressionExcludeFilter.new(/^--.*$/)
61
+ prepend_processor FileReader.new(input)
62
+ add_processor BodyTypeRefiner.new
63
+ add_processor CodeTypeRefiner.new
64
+ add_processor CodeTypeRefiner.new(:listing, :first_listing, :middle_listing, :end_listing)
65
+ add_processor Grouper.new(:bullet)
66
+ add_processor Grouper.new(:list)
67
+ add_processor Grouper.new(:quote)
68
+ add_processor Grouper.new(:attribution)
69
+ add_processor SpecRenderer.new
70
+ add_processor FileWriter.new(output)
52
71
  end
72
+
53
73
  end
54
74
 
55
75
 
@@ -10,4 +10,9 @@ module Logger
10
10
  return unless @verbose
11
11
  puts args.join(' ')
12
12
  end
13
+
14
+ def self.warn(*args)
15
+ STDERR.puts "WARNING: #{args.join(' ')} ******"
16
+ log(args)
17
+ end
13
18
  end
@@ -31,7 +31,8 @@ class OdtRenderer < Processor
31
31
  :pt => 'PT',
32
32
  :cn => 'HA',
33
33
  :chapter => 'HA',
34
- :ct => 'HB' }
34
+ :ct => 'HB',
35
+ :fc => 'FC'}
35
36
 
36
37
  @@footnote_number = 1
37
38
 
@@ -112,7 +113,6 @@ class OdtRenderer < Processor
112
113
  add_body_text(item.string, el)
113
114
  p.add(el)
114
115
  end
115
- puts p.to_s
116
116
  p
117
117
  end
118
118
 
@@ -120,7 +120,6 @@ class OdtRenderer < Processor
120
120
  list = Element.new('text:list')
121
121
  list.attributes['text:style-name'] = 'L2'
122
122
  items.each_with_index do |item, i|
123
- puts "*** adding item: #{item}"
124
123
  list_item_element = Element.new('text:list-item')
125
124
  text_element = Element.new('text:p')
126
125
  text_element.attributes['text:style-name'] = numbered_item_style_for(items, i)
@@ -128,7 +127,6 @@ class OdtRenderer < Processor
128
127
  list_item_element.add(text_element)
129
128
  list.add(list_item_element)
130
129
  end
131
- puts list.to_s
132
130
  list
133
131
  end
134
132
 
@@ -142,7 +140,6 @@ class OdtRenderer < Processor
142
140
  list = Element.new('text:list')
143
141
  list.attributes['text:style-name'] = 'L1'
144
142
  items.each_with_index do |item, i|
145
- puts "*** adding item: #{item}"
146
143
  list_item_element = Element.new('text:list-item')
147
144
  text_element = Element.new('text:p')
148
145
  text_element.attributes['text:style-name'] = bullet_item_style_for(items, i)
@@ -150,7 +147,6 @@ class OdtRenderer < Processor
150
147
  list_item_element.add(text_element)
151
148
  list.add(list_item_element)
152
149
  end
153
- puts list.to_s
154
150
  list
155
151
  end
156
152
 
Binary file
@@ -0,0 +1,53 @@
1
+ require 'rexml/document'
2
+ require 'pp'
3
+
4
+
5
+ class SpecRenderer < Processor
6
+ include Tokenize
7
+ include REXML
8
+
9
+ @@footnote_number = 1
10
+
11
+ def initialize
12
+ @last_code = false
13
+ end
14
+
15
+ def process( paragraphs )
16
+ elements = []
17
+ paragraphs.each do |paragraph|
18
+ new_element = format( paragraph )
19
+ elements << new_element if new_element
20
+ end
21
+ "require 'utils/utils'\n\ndescribe 'foo' do\n\n#{elements.join}\nend\n"
22
+ end
23
+
24
+ def format( para )
25
+ Logger.log "Format: #{para.inspect}"
26
+ type = para[:type]
27
+ text = para
28
+
29
+ this_code = code_type?(type)
30
+
31
+
32
+ if @last_code and this_code
33
+ ret = "#{para}\n"
34
+
35
+ elsif @last_code
36
+ ret = "##FOO_B\n end\n"
37
+
38
+ elsif this_code
39
+ ret = "\n it 'should foo' do\n##FOO_A\n#{para}\n"
40
+
41
+ else
42
+ ret = nil
43
+ end
44
+
45
+ @last_code = this_code
46
+ ret
47
+ end
48
+
49
+ def code_type?( type )
50
+ [ :first_code, :middle_code, :end_code, :single_code,
51
+ :listing, :first_listing, :middle_listing, :end_listing ].include?(type)
52
+ end
53
+ end
@@ -14,18 +14,12 @@ class TemplateExpander < Processor
14
14
  insert_body(doc, content_map[:body])
15
15
  doc.to_s
16
16
  end
17
-
18
- def insert_body(doc, body_elements)
19
- puts "reading file: #{TEMPLATE}"
20
17
 
18
+ def insert_body(doc, body_elements)
21
19
  text_element = REXML::XPath.first(doc, PATH)
22
20
  n = text_element.elements.size
23
- puts "deleting #{n} elements"
24
21
  n.times {|i| text_element.delete_element(1)}
25
- puts "ELs: #{body_elements.size}"
26
- puts "ELs: #{body_elements}"
27
22
  body_elements.each {|el| text_element.add_element(el)}
28
-
29
23
  doc
30
24
  end
31
25
  end
@@ -2,7 +2,9 @@ class NRWriter
2
2
  attr_reader :para_style
3
3
 
4
4
  def initialize
5
- @para_style = :body
5
+ @bold_depth = 0
6
+ @italic_depth = 0
7
+ @code_depth = 0
6
8
  @text_style = :normal
7
9
  @single_line = false
8
10
  end
@@ -21,7 +23,7 @@ class NRWriter
21
23
  end
22
24
 
23
25
  def switch_text_style(new_style)
24
- debug " <<switching to new style #{new_style}>> "
26
+ log " <<switching to new style #{new_style}>> "
25
27
  return if @text_style == new_style
26
28
  if @text_style != :normal
27
29
  toggle_text_style(@text_style)
@@ -32,12 +34,6 @@ class NRWriter
32
34
  @text_style = new_style
33
35
  end
34
36
 
35
- def toggle_text_style(style)
36
- toggle_bold if style == :bold
37
- toggle_italic if style == :italic
38
- toggle_code if style == :code
39
- end
40
-
41
37
  def end_paragraph
42
38
  puts
43
39
  if @single_line
@@ -49,22 +45,68 @@ class NRWriter
49
45
  end
50
46
 
51
47
  def indent(n)
52
- print (' ' * n)
53
-
48
+ log "indent #{n}"
49
+ print (' ' * n)
50
+ end
51
+
52
+ def start_bold
53
+ print "!!" if @bold_depth == 0
54
+ @bold_depth += 1
54
55
  end
55
- def toggle_bold
56
- print "!!"
56
+
57
+ def end_bold
58
+ print "!!" if @bold_depth == 1
59
+ @bold_depth -= 1
60
+ end
61
+
62
+ def start_italic
63
+ print "~~" if @italic_depth == 0
64
+ @italic_depth += 1
57
65
  end
58
66
 
59
- def toggle_italic
60
- print "~~"
67
+ def end_italic
68
+ print "~~ " if @italic_depth == 1
69
+ @italic_depth -= 1
61
70
  end
62
71
 
63
- def toggle_code
64
- print '@@'
72
+ def start_code
73
+ print "@@" if @code_depth == 0
74
+ @code_depth += 1
75
+ end
76
+
77
+ def end_code
78
+ print "@@ " if @code_depth == 1
79
+ @code_depth -= 1
80
+ end
81
+
82
+ def special_style?
83
+ return true unless @para_style == :body
84
+ @code_depth > 0 or @italic_depth > 0 or @bold_depth > 0
85
+ end
86
+
87
+ def split_text(t)
88
+ return t if t.length < 80
89
+ words = t.split(' ')
65
90
  end
66
91
 
67
92
  def text(t)
68
- print t
93
+ log "==>Text #{t}"
94
+ if special_style?
95
+ print t
96
+ else
97
+ array = t.split(' ')
98
+ line_len = 0
99
+ array.each do |word|
100
+ print word
101
+ line_len += word.length
102
+ if line_len > 70
103
+ print "\n"
104
+ line_len = 0
105
+ else
106
+ print " "
107
+ line_len += 1
108
+ end
109
+ end
110
+ end
69
111
  end
70
112
  end
@@ -13,9 +13,21 @@ def debug(*args)
13
13
  puts args.join(' ')
14
14
  end
15
15
 
16
- class OdtParser
16
+ def additional_styles(styles)
17
+ log "noop additional paragraph styles function"
18
+ end
19
+
20
+ def additional_text_styles(styles)
21
+ log "noop additional text styles function"
22
+ end
17
23
 
24
+ class OdtParser
18
25
  def initialize(odt_path)
26
+ if File.exist?("yesroff.rc")
27
+ log "loading notroff.rb"
28
+ load 'yesroff.rc'
29
+ end
30
+
19
31
  log "Reading #{odt_path}..."
20
32
  Zip::ZipFile.open(odt_path ) do |zipfile|
21
33
  zipfile.file.open("content.xml") do |content|
@@ -34,68 +46,64 @@ class OdtParser
34
46
  cd1 = TextStyle.new("CD1")
35
47
  cd1.code = true
36
48
 
37
- styles = [
38
- cd1,
39
- TextStyle.new("Default"),
40
- TextStyle.new("C1"),
41
- TextStyle.new("C1_20_HD"),
42
- TextStyle.new("FN"),
43
- TextStyle.new("Base_20_Font"),
44
- TextStyle.new("Chapter_20_Word")
45
- ]
46
- hash = {}
47
- styles.each {|s| hash[s.name] = s}
49
+ hash = StyleHash.new
50
+ hash.add_style cd1
51
+ hash.add_style TextStyle.new("Default")
52
+ hash.add_style TextStyle.new("C1")
53
+ hash.add_style TextStyle.new("C1_20_HD")
54
+ hash.add_style TextStyle.new("FN")
55
+ hash.add_style TextStyle.new("Base_20_Font")
56
+ hash.add_style TextStyle.new("Chapter_20_Word")
57
+ additional_text_styles(hash)
48
58
  hash
49
59
  end
50
60
 
61
+
51
62
  def default_para_styles
52
- styles = [
53
- ParagraphStyle.new('FT', nil, :body, false),
54
- ParagraphStyle.new('IT', nil, :body, false),
55
- ParagraphStyle.new('Quotation', nil, :quote, true),
56
-
57
- ParagraphStyle.new('CDT1', nil, :code, false),
58
- ParagraphStyle.new('CDT', nil, :code, false),
59
- ParagraphStyle.new('CDTX', nil, :code, false),
60
- ParagraphStyle.new('C1', nil, :c1, true),
61
- ParagraphStyle.new('C2', nil, :c1, true),
62
- ParagraphStyle.new('TB', nil, :c1, true),
63
- ParagraphStyle.new('Free_20_Form', nil, :c1, true),
64
-
65
-
66
- ParagraphStyle.new('NLC1', nil, :code, false),
67
- ParagraphStyle.new('NLC', nil, :code, false),
68
- ParagraphStyle.new('NLCX', nil, :code, false),
69
- ParagraphStyle.new('NLPara', nil, :code, false),
70
-
71
- ParagraphStyle.new('TX', nil, :code, false),
72
-
73
- ParagraphStyle.new('HA', nil, :title, true),
74
- ParagraphStyle.new('HB', nil, :subtitle, true),
75
- ParagraphStyle.new('HC', nil, :sec, true),
76
- ParagraphStyle.new('HD', nil, :subsec, true),
77
- ParagraphStyle.new('TH', nil, :theading, true),
78
- ParagraphStyle.new('LH', nil, :ltitle, true),
79
- ParagraphStyle.new('LC', nil, :listing, false),
80
- ParagraphStyle.new('LC2', nil, :listing, false),
81
- ParagraphStyle.new('LX', nil, :listing, false),
82
-
83
- ParagraphStyle.new('BL1', nil, :bullet, true),
84
- ParagraphStyle.new('BL', nil, :bullet, true),
85
- ParagraphStyle.new('BX', nil, :bullet, true),
86
-
87
- ParagraphStyle.new('NL1', nil, :list, true),
88
- ParagraphStyle.new('NL', nil, :list, true),
89
- ParagraphStyle.new('NX', nil, :list, true),
90
-
91
- ParagraphStyle.new('BLPara', nil, :bullet, true),
92
- ParagraphStyle.new('Quotation_20_Attribution', nil, :attribution, true)
93
-
94
- ]
95
-
96
- hash = {}
97
- styles.each {|s| hash[s.name] = s}
98
- hash
63
+ styles = StyleHash.new
64
+ styles.add_style ParagraphStyle.new('FT', nil, :body, false)
65
+ styles.add_style ParagraphStyle.new('IT', nil, :body, false)
66
+ styles.add_style ParagraphStyle.new('Quotation', nil, :quote, true)
67
+
68
+ styles.add_style ParagraphStyle.new('CDT1', nil, :code, false)
69
+ styles.add_style ParagraphStyle.new('CDT', nil, :code, false)
70
+ styles.add_style ParagraphStyle.new('CDTX', nil, :code, false)
71
+ styles.add_style ParagraphStyle.new('C1', nil, :c1, true)
72
+ styles.add_style ParagraphStyle.new('C2', nil, :c1, true)
73
+ styles.add_style ParagraphStyle.new('TB', nil, :c1, true)
74
+ styles.add_style ParagraphStyle.new('Free_20_Form', nil, :c1, true)
75
+
76
+
77
+ styles.add_style ParagraphStyle.new('NLC1', nil, :code, false)
78
+ styles.add_style ParagraphStyle.new('NLC', nil, :code, false)
79
+ styles.add_style ParagraphStyle.new('NLCX', nil, :code, false)
80
+ styles.add_style ParagraphStyle.new('NLPara', nil, :code, false)
81
+
82
+ styles.add_style ParagraphStyle.new('TX', nil, :code, false)
83
+
84
+ styles.add_style ParagraphStyle.new('HA', nil, :title, true)
85
+ styles.add_style ParagraphStyle.new('HB', nil, :subtitle, true)
86
+ styles.add_style ParagraphStyle.new('HC', nil, :sec, true)
87
+ styles.add_style ParagraphStyle.new('HD', nil, :subsec, true)
88
+ styles.add_style ParagraphStyle.new('TH', nil, :theading, true)
89
+ styles.add_style ParagraphStyle.new('LH', nil, :ltitle, true)
90
+ styles.add_style ParagraphStyle.new('LC', nil, :listing, false)
91
+ styles.add_style ParagraphStyle.new('LC2', nil, :listing, false)
92
+ styles.add_style ParagraphStyle.new('LX', nil, :listing, false)
93
+
94
+ styles.add_style ParagraphStyle.new('BL1', nil, :bullet, true)
95
+ styles.add_style ParagraphStyle.new('BL', nil, :bullet, true)
96
+ styles.add_style ParagraphStyle.new('BX', nil, :bullet, true)
97
+
98
+ styles.add_style ParagraphStyle.new('NL1', nil, :list, true)
99
+ styles.add_style ParagraphStyle.new('NL', nil, :list, true)
100
+ styles.add_style ParagraphStyle.new('NX', nil, :list, true)
101
+
102
+ styles.add_style ParagraphStyle.new('BL Para', nil, :bullet, true)
103
+ styles.add_style ParagraphStyle.new('Quotation_20_Attribution', nil, :attribution, true)
104
+
105
+ additional_paragraph_styles(styles)
106
+ styles
99
107
  end
100
108
 
101
109
  def parse
@@ -148,7 +156,7 @@ class OdtParser
148
156
  def lookup_para_style(name)
149
157
  s = @para_styles[name]
150
158
  log "No such para style #{name}" unless s
151
- #raise "No such para style #{name}" unless s
159
+ raise "No such para style #{name}" unless s
152
160
  s
153
161
  end
154
162
 
@@ -191,13 +199,17 @@ class OdtParser
191
199
  para
192
200
  end
193
201
 
202
+ def parse_indent(el)
203
+ end
204
+
194
205
  def parse_span(el)
195
206
  attrs = el.attributes
196
- style = find_or_create_text_style(attrs['text:style-name'])
197
207
  indent = attrs['text:c'] ? attrs['text:c'].to_i : 0
208
+ style = find_or_create_text_style(attrs['text:style-name'])
198
209
  span = Span.new(style)
199
210
  span.indent = indent
200
211
  span.contents = parse_contents(span, el)
212
+ log("new span: #{span}")
201
213
  span
202
214
  end
203
215
 
@@ -31,19 +31,25 @@ class TextStyle < Style
31
31
  result += ']'
32
32
  end
33
33
 
34
- def render(w)
35
- puts "Rendering style #{self}"
36
-
34
+ def start(w)
37
35
  if @bold
38
- w.switch_text_style(:bold)
36
+ w.start_bold
39
37
  elsif @italic
40
- w.switch_text_style(:italic)
38
+ w.start_italic
41
39
  elsif @code
42
- w.switch_text_style(:code)
43
- else
44
- w.switch_text_style(:normal)
40
+ w.start_code
45
41
  end
46
42
  end
43
+
44
+ def stop(w)
45
+ if @bold
46
+ w.end_bold
47
+ elsif @italic
48
+ w.end_italic
49
+ elsif @code
50
+ w.end_code
51
+ end
52
+ end
47
53
  end
48
54
 
49
55
 
@@ -62,12 +68,19 @@ class ParagraphStyle < Style
62
68
  end
63
69
  end
64
70
 
71
+ class StyleHash < Hash
72
+ def add_style(s)
73
+ self[s.name] = s
74
+ end
75
+ end
76
+
65
77
  class Container
66
78
  attr_accessor :style, :parent, :contents
67
79
 
68
80
  def initialize(style, parent=nil, contents=[])
69
81
  @style = style
70
82
  @contents = contents
83
+ @parent = parent
71
84
  end
72
85
 
73
86
  def <<(content)
@@ -81,23 +94,36 @@ class Container
81
94
  end
82
95
 
83
96
  def render(w)
84
- #puts "========= rendering #{self.class} size: #{contents.size}"
97
+ #log "========= rendering #{self.class} size: #{contents.size}"
85
98
  #pp contents
86
99
  #puts "Rendering:"
87
100
  contents.each {|c| c.render(w)}
88
101
  end
102
+
103
+ def to_s
104
+ result = "Container #{style}"
105
+ if @contents
106
+ result += @contents.join(' ')
107
+ end
108
+ result
109
+ end
89
110
  end
90
111
 
91
112
  class Span < Container
92
113
  attr_accessor :indent
93
114
 
94
115
  def render(w)
95
- debug "\nrendering span #{self} length #{self.length}"
96
- return if self.length == 0
97
- style.render(w)
98
- w.indent(@indent) if @indent
99
- super(w)
100
- style.render(w)
116
+ l = self.length
117
+ #return if l == 0 and indent == 0
118
+ style.start(w)
119
+ log "Indenting #{indent}"
120
+ w.indent(indent) if indent
121
+ super(w) unless l == 0
122
+ style.stop(w)
123
+ end
124
+
125
+ def to_s
126
+ result = " Span: indent #{@indent} #{super} "
101
127
  end
102
128
  end
103
129
 
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.2
4
+ version: 0.3.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-09-03 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rubyzip
16
- requirement: &70333341691900 !ruby/object:Gem::Requirement
16
+ requirement: &70295630346980 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,18 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70333341691900
24
+ version_requirements: *70295630346980
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70295630346220 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70295630346220
25
36
  description: NotRoff A simple text to openoffice filter
26
37
  email: russ@russolsen.com
27
38
  executables:
@@ -62,6 +73,7 @@ files:
62
73
  - lib/notroff/paragraph_joiner.rb
63
74
  - lib/notroff/processor.rb
64
75
  - lib/notroff/skel.odt
76
+ - lib/notroff/spec_renderer.rb
65
77
  - lib/notroff/string_extensions.rb
66
78
  - lib/notroff/template_expander.rb
67
79
  - lib/notroff/text.rb