ulmul 0.4.1 → 0.5.0

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.
Files changed (41) hide show
  1. data/README-en +118 -63
  2. data/README-ja +92 -58
  3. data/Rakefile +26 -10
  4. data/bin/ulmul2html5 +7 -58
  5. data/bin/ulmul2latex +34 -0
  6. data/bin/ulmul2xhtml +6 -58
  7. data/google-code-prettify/CHANGES.html +130 -0
  8. data/google-code-prettify/COPYING +202 -0
  9. data/google-code-prettify/README-zh-Hans.html +143 -0
  10. data/google-code-prettify/README.html +203 -0
  11. data/google-code-prettify/src/lang-apollo.js +51 -0
  12. data/google-code-prettify/src/lang-css.js +78 -0
  13. data/google-code-prettify/src/lang-fortran.js +53 -0
  14. data/google-code-prettify/src/lang-hs.js +101 -0
  15. data/google-code-prettify/src/lang-lisp.js +93 -0
  16. data/google-code-prettify/src/lang-lua.js +59 -0
  17. data/google-code-prettify/src/lang-ml.js +56 -0
  18. data/google-code-prettify/src/lang-proto.js +35 -0
  19. data/google-code-prettify/src/lang-scala.js +54 -0
  20. data/google-code-prettify/src/lang-sql.js +57 -0
  21. data/google-code-prettify/src/lang-vb.js +61 -0
  22. data/google-code-prettify/src/lang-vhdl.js +34 -0
  23. data/google-code-prettify/src/lang-wiki.js +53 -0
  24. data/google-code-prettify/src/lang-yaml.js +27 -0
  25. data/google-code-prettify/src/prettify.css +44 -0
  26. data/google-code-prettify/src/prettify.js +1508 -0
  27. data/google-code-prettify/tests/large_input_test.html +122 -0
  28. data/google-code-prettify/tests/prettify_test.html +2772 -0
  29. data/google-code-prettify/tests/test_base.js +132 -0
  30. data/google-code-prettify/tests/test_styles.css +5 -0
  31. data/hello.c +7 -0
  32. data/index.en.html +221 -120
  33. data/index.ja.html +182 -115
  34. data/lib/ulmul.rb +477 -276
  35. data/test/unit/ulmul_test.rb +21 -0
  36. data/ulmul-slidy.css +21 -8
  37. data/ulmul.gemspec +9 -5
  38. data/ulmul2html5.css +19 -5
  39. data/ulmul2xhtml.css +17 -1
  40. metadata +90 -12
  41. data/tests/ulmul_test.rb +0 -14
data/lib/ulmul.rb CHANGED
@@ -1,11 +1,18 @@
1
1
  #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
2
3
  # ulmul.rb
3
- # Time-stamp: <2010-06-06 20:10:52 takeshi>
4
+ # Time-stamp: <2011-04-02 19:23:18 takeshi>
4
5
  # Author: Takeshi Nishimatsu
5
6
  ##
6
7
  require "rubygems"
7
8
  require "date"
8
9
  require "math_ml/string"
10
+ require "aasm"
11
+
12
+ # For m17n of Ruby 1.9.x. Thanks, Masayoshi Takahashi-san [ruby-list:47159].
13
+ if defined?(Encoding) && Encoding.respond_to?("default_external")
14
+ Encoding.default_external = "UTF-8"
15
+ end
9
16
 
10
17
  class String
11
18
  def apply_subs_rules(rules)
@@ -13,260 +20,308 @@ class String
13
20
  rules.each do |ary|
14
21
  result.gsub!(ary[0],ary[1])
15
22
  end
16
- is_mathml = false
17
- while result =~ /\$(.*?)\$/
18
- pre=$`
19
- tex=Regexp.last_match[1]
20
- post=$'
21
- result = "#{pre}#{tex.to_mathml}#{post}"
22
- is_mathml = true
23
+ if $0 == __FILE__ || /ulmul2(html5|xhtml)$/ =~ $0
24
+ while result =~ /\$(.*?)\$/
25
+ pre=$`
26
+ tex=Regexp.last_match[1]
27
+ post=$'
28
+ result = "#{pre}#{tex.to_mathml}#{post}"
29
+ end
23
30
  end
24
- return result, is_mathml
31
+ return result
25
32
  end
26
33
  end
27
34
 
28
35
  module Itemize
29
- def itemize_begin(e)
36
+ def cb_itemize_begin(filename=nil, lnumber=nil, line=nil)
30
37
  @level_of_state = 0
31
38
  end
32
39
 
33
- def itemize_add_primitive(new_level,str)
40
+ def cb_itemize_add_item(filename=nil, lnumber=nil, line=nil)
41
+ new_level = case line
42
+ when /^ \* (.*)/ then 1
43
+ when /^ \* (.*)/ then 2
44
+ when /^ \* (.*)/ then 3
45
+ when /^ \* (.*)/ then 4
46
+ when /^ \* (.*)/ then 5
47
+ else raise 'Illegal astarisk line for itemize'
48
+ end
49
+ str = Regexp.last_match[1].apply_subs_rules(@subs_rules)
34
50
  if new_level>@level_of_state+1
35
- raise 'Illegal jump of itemize level'
51
+ STDERR << filename << ":#{lnumber}: Illegal jump of itemize level from #{@level_of_state} to #{new_level}: #{line}"
52
+ exit 1
53
+ raise
36
54
  elsif new_level==@level_of_state+1
37
- @body << "\n" << " "*@level_of_state << "<ul>\n"
38
- @body << " "*(new_level-1) << " " << "<li>#{str}"
55
+ @body << "\n" << " "*@level_of_state << ITEMIZE_INITIATOR << "\n"
56
+ @body << " "*(new_level-1) << " " << ITEM_INITIATOR << str
39
57
  @level_of_state = new_level
40
58
  elsif new_level==@level_of_state
41
- @body << "</li>\n" << " "*(new_level-1) << " " << "<li>#{str}"
59
+ @body << ITEM_TERMINATOR << "\n" << " "*(new_level-1) << " " << ITEM_INITIATOR << str
42
60
  else
43
- @body << "</li>\n"
44
- (@level_of_state-1).downto(new_level){|i| @body << " "*i << "</ul></li>\n"}
45
- @body << " "*(new_level-1) << " " << "<li>#{str}"
61
+ @body << ITEM_TERMINATOR<< "\n"
62
+ (@level_of_state-1).downto(new_level){|i| @body << " "*i << ITEMIZE_TERMINATOR << ITEM_TERMINATOR << "\n"}
63
+ @body << " "*(new_level-1) << " " << ITEM_INITIATOR << str
46
64
  @level_of_state = new_level
47
65
  end
48
66
  end
49
67
 
50
- def itemize_continue_primitive(new_level,str)
51
- (@level_of_state-1).downto(new_level){|i| @body << "</li>\n" << " "*i << "</ul>"}
68
+ def cb_itemize_continue_item(filename=nil, lnumber=nil, line=nil)
69
+ new_level = case line
70
+ when /^ (\S.*)/ then 1
71
+ when /^ (\S.*)/ then 2
72
+ when /^ (\S.*)/ then 3
73
+ when /^ (\S.*)/ then 4
74
+ when /^ (\S.*)/ then 5
75
+ else raise 'Illegal astarisk line for itemize'
76
+ end
77
+ str = Regexp.last_match[1].apply_subs_rules(@subs_rules)
78
+ (@level_of_state-1).downto(new_level){|i| @body << ITEM_TERMINATOR << "\n" << " "*i << ITEMIZE_TERMINATOR}
52
79
  @body << "\n " << " "*(new_level-1) << " " << str
53
80
  @level_of_state = new_level
54
81
  end
55
82
 
56
- def itemize_end(e)
57
- @body << "</li>\n"
58
- (@level_of_state-1).downto(1){|i| @body << " "*i << "</ul></li>\n"}
59
- @body << "</ul>\n"
83
+ def cb_itemize_end(filename=nil, lnumber=nil, line=nil)
84
+ @body << ITEM_TERMINATOR << "\n"
85
+ (@level_of_state-1).downto(1){|i| @body << " "*i << ITEMIZE_TERMINATOR << ITEM_TERMINATOR << "\n"}
86
+ @body << ITEMIZE_TERMINATOR << "\n"
60
87
  @level_of_state = 0
61
88
  end
62
89
  end
63
90
 
64
- class Contents
91
+ class Table_of_Contents
65
92
  include Itemize
66
93
  def initialize()
67
- @state = 'itemize'
68
- @level_of_state = 0
69
- @body = ''
94
+ @body = ''
95
+ @subs_rules = []
96
+ cb_itemize_begin()
70
97
  end
71
98
  attr_reader :body
72
99
  end
73
100
 
74
101
  class Ulmul
75
- VERSION = '0.4.1'
76
- CONTENTS_HERE="<!-- Contents -->"
77
- CONTENTS_RANGE_DEFAULT=2..3
78
- TABLE={
79
- 'ignore' => {'ground' => [],
80
- 'itemize' => [],
81
- 'verbatim' => [],
82
- 'paragraph' => [],
83
- 'equation' => [],
84
- 'figure' => []},
85
-
86
- 'end' => {'ground' => [ :heading_add],
87
- 'itemize' => [ :itemize_end, :heading_add, 'ground'],
88
- 'verbatim' => [ :verbatim_end, :heading_add, 'ground'],
89
- 'paragraph' => [:paragraph_end, :heading_add, 'ground'],
90
- 'equation' => [:equation_error],
91
- 'figure' => [:figure_error]},
92
-
93
- 'heading' => {'ground' => [ :heading_add],
94
- 'itemize' => [ :itemize_end, :heading_add, 'ground'],
95
- 'verbatim' => [ :verbatim_end, :heading_add, 'ground'],
96
- 'paragraph' => [:paragraph_end, :heading_add, 'ground'],
97
- 'equation' => [:equation_continue],
98
- 'figure' => [:figure_continue]},
99
-
100
- 'asterisk' => {'ground' => [:itemize_begin, 'itemize', :itemize_add],
101
- 'itemize' => [ :itemize_add],
102
- 'verbatim' => [ :verbatim_add],
103
- 'paragraph' => [:paragraph_end,
104
- :itemize_begin, 'itemize', :itemize_add],
105
- 'equation' => [:equation_continue],
106
- 'figure' => [:figure_continue]},
107
-
108
- 'offset' => {'ground' => [:verbatim_begin, 'verbatim', :verbatim_add],
109
- 'itemize' => [:itemize_continue],
110
- 'verbatim' => [ :verbatim_add],
111
- 'paragraph' => [:paragraph_end,
112
- :verbatim_begin, 'verbatim', :verbatim_add],
113
- 'equation' => [:equation_continue],
114
- 'figure' => [:figure_continue]},
115
-
116
- 'empty' => {'ground' => [],
117
- 'itemize' => [:itemize_end, 'ground'],
118
- 'verbatim' => [:verbatim_add],
119
- 'paragraph' => [:paragraph_end, 'ground'],
120
- 'equation' => [:equation_error],
121
- 'figure' => [:figure_error]},
122
-
123
- 'normal' => {'ground' => [:paragraph_begin, 'paragraph', :paragraph_add],
124
- 'itemize' => [:itemize_end,
125
- :paragraph_begin, 'paragraph', :paragraph_add],
126
- 'verbatim' => [:verbatim_end,
127
- :paragraph_begin, 'paragraph', :paragraph_add],
128
- 'paragraph' => [:paragraph_add],
129
- 'equation' => [:equation_continue],
130
- 'figure' => [:figure_continue]},
131
-
132
- 'eq_begin' => {'ground' => [:paragraph_begin, :equation_begin, 'equation'],
133
- 'itemize' => [:itemize_end,
134
- :paragraph_begin, :equation_begin, 'equation'],
135
- 'verbatim' => [:verbatim_end,
136
- :paragraph_begin, :equation_begin, 'equation'],
137
- 'paragraph' => [ :equation_begin, 'equation'],
138
- 'equation' => [:equation_error],
139
- 'figure' => [:figure_error]},
140
-
141
- 'eq_end' => {'ground' => [:equation_error],
142
- 'itemize' => [:equation_error],
143
- 'verbatim' => [:equation_error],
144
- 'paragraph' => [:equation_error],
145
- 'equation' => [:equation_end, 'paragraph'],
146
- 'figure' => [:figure_error]},
147
-
148
- 'fig_begin'=> {'ground' => [ :figure_begin, 'figure'],
149
- 'itemize' => [:itemize_end, :figure_begin, 'figure'],
150
- 'verbatim' => [:verbatim_end, :figure_begin, 'figure'],
151
- 'paragraph' => [:paragraph_end, :figure_begin, 'figure'],
152
- 'equation' => [:equation_error],
153
- 'figure' => [:figure_error]},
154
-
155
- 'fig_end' => {'ground' => [:figure_error],
156
- 'itemize' => [:figure_error],
157
- 'verbatim' => [:figure_error],
158
- 'paragraph' => [:figure_error],
159
- 'equation' => [:figure_error],
160
- 'figure' => [:figure_end, 'ground']}
161
- }
162
-
163
- def initialize(contents_range=CONTENTS_RANGE_DEFAULT,mode='ulmul2html5')
164
- @contents_range = contents_range
165
- @contents = Contents.new()
166
- @state = 'ground'
167
- @level_of_state = 0
168
- @level_of_heading = 0
169
- @i_th_heading = 0
170
- @equation = ''
171
- @figure_caption = ''
172
- @body = ''
173
- @subs_rules = [[/(http:\S*)(\s|$)/, '<a href="\1">\1</a>\2'],
174
- [/(https:\S*)(\s|$)/, '<a href="\1">\1</a>\2']]
175
- @contents.itemize_begin(nil)
176
- @is_mathml = false
177
-
178
- @mode=mode
179
- if @mode=='ulmul2html5'
180
- @figure_open = '<figure>'
181
- @figure_close = '</figure>'
182
- @caption_open = '<figcaption>'
183
- @caption_close = '</figcaption>'
184
- else
185
- @figure_open = '<div class="figure">'
186
- @figure_close = '</div>'
187
- @caption_open = '<div class="figcaption">'
188
- @caption_close = '</div>'
189
- end
102
+ include AASM
103
+ include Itemize
104
+ VERSION = '0.5.0'
105
+
106
+ aasm_initial_state :st_ground
107
+
108
+ aasm_state :st_ground
109
+ aasm_state :st_itemize
110
+ aasm_state :st_verbatim
111
+ aasm_state :st_paragraph
112
+ aasm_state :st_env
113
+ aasm_state :st_equation
114
+
115
+ aasm_event :ev_ignore do
116
+ transitions :from => :st_ground, :to => :st_ground
117
+ transitions :from => :st_itemize, :to => :st_itemize
118
+ transitions :from => :st_verbatim, :to => :st_verbatim
119
+ transitions :from => :st_paragraph, :to => :st_paragraph
120
+ transitions :from => :st_env, :to => :st_env
121
+ transitions :from => :st_equation, :to => :st_equation
190
122
  end
191
- attr_accessor :subs_rules
192
123
 
193
- def equation_begin(e)
194
- @is_mathml = true
124
+ aasm_event :ev_asterisk do
125
+ transitions :from => :st_ground, :to => :st_itemize, :on_transition => [:cb_itemize_begin, :cb_itemize_add_item]
126
+ transitions :from => :st_itemize, :to => :st_itemize, :on_transition => [:cb_itemize_add_item]
127
+ transitions :from => :st_verbatim, :to => :st_verbatim, :on_transition => [:cb_verbatim_add]
128
+ transitions :from => :st_paragraph, :to => :st_itemize, :on_transition => [:cb_paragraph_end, :cb_itemize_begin, :cb_itemize_add_item]
129
+ transitions :from => :st_env, :to => :st_env, :on_transition => [:cb_env_continues]
130
+ transitions :from => :st_equation, :to => :st_equation, :on_transition => [:cb_equation_continues]
131
+ end
132
+
133
+ aasm_event :ev_offset do
134
+ transitions :from => :st_ground, :to => :st_verbatim, :on_transition => [:cb_verbatim_begin, :cb_verbatim_add]
135
+ transitions :from => :st_itemize, :to => :st_itemize, :on_transition => [:cb_itemize_continue_item]
136
+ transitions :from => :st_verbatim, :to => :st_verbatim, :on_transition => [:cb_verbatim_add]
137
+ transitions :from => :st_paragraph, :to => :st_verbatim, :on_transition => [:cb_paragraph_end, :cb_verbatim_begin, :cb_verbatim_add]
138
+ transitions :from => :st_env, :to => :st_env, :on_transition => [:cb_env_continues]
139
+ transitions :from => :st_equation, :to => :st_equation, :on_transition => [:cb_equation_continues]
140
+ end
141
+
142
+ aasm_event :ev_heading do
143
+ transitions :from => :st_ground, :to => :st_ground, :on_transition => [:cb_heading]
144
+ transitions :from => :st_itemize, :to => :st_ground, :on_transition => [:cb_itemize_end, :cb_heading]
145
+ transitions :from => :st_verbatim, :to => :st_ground, :on_transition => [:cb_verbatim_end, :cb_heading]
146
+ transitions :from => :st_paragraph, :to => :st_ground, :on_transition => [:cb_paragraph_end, :cb_heading]
147
+ transitions :from => :st_env, :to => :st_ground, :on_transition => [:cb_env_in_env_error]
148
+ transitions :from => :st_equation, :to => :st_equation, :on_transition => [:cb_equation_in_equation_error]
149
+ end
150
+
151
+ aasm_event :ev_empty do
152
+ transitions :from => :st_ground, :to => :st_ground
153
+ transitions :from => :st_itemize, :to => :st_ground, :on_transition => [:cb_itemize_end]
154
+ transitions :from => :st_verbatim, :to => :st_ground, :on_transition => [:cb_verbatim_end]
155
+ transitions :from => :st_paragraph, :to => :st_ground, :on_transition => [:cb_paragraph_end]
156
+ transitions :from => :st_env, :to => :st_env
157
+ transitions :from => :st_equation, :to => :st_equation
195
158
  end
196
159
 
197
- def equation_continue(e)
198
- @equation << e.line
160
+ aasm_event :ev_env_begin do
161
+ transitions :from => :st_ground, :to => :st_env, :on_transition => [:cb_env_begin]
162
+ transitions :from => :st_itemize, :to => :st_env, :on_transition => [:cb_itemize_end, :cb_env_begin]
163
+ transitions :from => :st_verbatim, :to => :st_env, :on_transition => [:cb_verbatim_end, :cb_env_begin]
164
+ transitions :from => :st_paragraph, :to => :st_env, :on_transition => [:cb_paragraph_end, :cb_env_begin]
165
+ transitions :from => :st_env, :to => :st_env, :on_transition => [:cb_env_in_env_error]
166
+ transitions :from => :st_equation, :to => :st_equation, :on_transition => [:cb_equation_in_equation_error]
167
+ end
168
+
169
+ aasm_event :ev_env_end do
170
+ transitions :from => [:st_ground, :st_itemize, :st_verbatim, :st_paragraph, :st_equation],
171
+ :to => :st_ground, :on_transition => [:cb_env_not_in_env_error]
172
+ transitions :from => :st_env, :to => :st_ground, :on_transition => [:cb_env_end]
173
+ end
174
+
175
+ aasm_event :ev_equation_begin do
176
+ transitions :from => :st_ground, :to => :st_equation, :on_transition => [:cb_paragraph_begin, :cb_equation_begin]
177
+ transitions :from => :st_itemize, :to => :st_equation, :on_transition => [:cb_itemize_end, :cb_paragraph_begin, :cb_equation_begin]
178
+ transitions :from => :st_verbatim, :to => :st_equation, :on_transition => [:cb_verbatim_end, :cb_paragraph_begin, :cb_equation_begin]
179
+ transitions :from => :st_paragraph, :to => :st_equation, :on_transition => [:cb_equation_begin]
180
+ transitions :from => :st_equation, :to => :st_equation, :on_transition => [:cb_equation_in_equation_error]
181
+ end
182
+
183
+ aasm_event :ev_equation_end do
184
+ transitions :from => [:st_ground, :st_itemize, :st_verbatim, :st_paragraph, :st_env],
185
+ :to => :st_ground, :on_transition => [:cb_equation_not_in_equation_error]
186
+ transitions :from => :st_equation, :to => :st_paragraph, :on_transition => [:cb_equation_end]
187
+ end
188
+
189
+ aasm_event :ev_normal do
190
+ transitions :from => :st_ground, :to => :st_paragraph, :on_transition => [:cb_paragraph_begin, :cb_paragraph_add]
191
+ transitions :from => :st_itemize, :to => :st_paragraph, :on_transition => [:cb_itemize_end, :cb_paragraph_begin, :cb_paragraph_add]
192
+ transitions :from => :st_verbatim, :to => :st_paragraph, :on_transition => [:cb_verbatim_end, :cb_paragraph_begin, :cb_paragraph_add]
193
+ transitions :from => :st_paragraph, :to => :st_paragraph, :on_transition => [:cb_paragraph_add]
194
+ transitions :from => :st_env, :to => :st_env, :on_transition => [:cb_env_continues]
195
+ transitions :from => :st_equation, :to => :st_equation, :on_transition => [:cb_equation_continues]
199
196
  end
200
197
 
201
- def equation_end(e)
202
- @body << @equation.to_mathml('block').to_s << "\n"
203
- @equation =''
198
+ def cb_paragraph_begin(filename=nil, lnumber=nil, line=nil)
199
+ @body << PARAGRAPH_INITIATOR << "\n"
204
200
  end
205
201
 
206
- def figure_begin(e)
207
- dummy, @figure_label, @figure_file = e.line.split
208
- @body << "#{@figure_open}
209
- <img src=\"#{@figure_file}\" alt=\"#{@figure_file}\" />
210
- #{@caption_open}\n"
202
+ def cb_paragraph_add(filename=nil, lnumber=nil, line=nil)
203
+ @body << line.apply_subs_rules(@subs_rules)
211
204
  end
212
205
 
213
- def figure_continue(e)
214
- result, is_mathml = e.line.apply_subs_rules(@subs_rules)
215
- @figure_caption << result
216
- @is_mathml = @is_mathml || is_mathml
206
+ def cb_paragraph_end(filename=nil, lnumber=nil, line=nil)
207
+ @body << PARAGRAPH_TERMINATOR << "\n"
217
208
  end
218
209
 
219
- def figure_end(e)
220
- @body << @figure_caption << " #{@caption_close}\n" << "#{@figure_close}\n"
221
- @figure_caption =''
210
+ def cb_verbatim_begin(filename=nil, lnumber=nil, line=nil)
211
+ @body << VERBATIM_INITIATOR << "\n"
222
212
  end
223
213
 
224
- def figure_error(e)
225
- p e
226
- exit 1
214
+ def cb_verbatim_add(filename=nil, lnumber=nil, line=nil)
215
+ @body << line[1..-1] #.gsub(/</,'&lt;').gsub(/>/,'&gt;')
227
216
  end
228
217
 
229
- def verbatim_begin(e)
230
- @level_of_state = 0
231
- @body << "<pre>"
218
+ def cb_verbatim_end(filename=nil, lnumber=nil, line=nil)
219
+ @body << VERBATIM_TERMINATOR << "\n"
232
220
  end
233
221
 
234
- def verbatim_add(e)
235
- if e.event=='empty'
236
- @level_of_state += 1
237
- else
238
- @body << "\n"*@level_of_state << e.line[1..-1].gsub(/</,'&lt;').gsub(/>/,'&gt;')
239
- @level_of_state=0
222
+ def cb_env_begin(filename=nil, lnumber=nil, line=nil)
223
+ @env_label, @env_file = line.split
224
+ @env_label.sub!(/^\\/,'')
225
+ @env_caption =''
226
+ case @env_label
227
+ when /^Fig:/
228
+ @figures << @env_label
229
+ when /^Table:/
230
+ @tables << @env_label
231
+ when /^Code:/
232
+ @codes << @env_label
240
233
  end
241
234
  end
242
235
 
243
- def verbatim_end(e)
244
- @body << "</pre>\n"
236
+ def cb_env_continues(filename=nil, lnumber=nil, line=nil)
237
+ @env_caption << line
245
238
  end
246
239
 
247
- def paragraph_begin(e)
248
- @level_of_state = 0
249
- @body << "<p>\n"
240
+ def cb_env_end(filename=nil, lnumber=nil, line=nil)
241
+ if line.chomp.sub(/^\//,'') != @env_label
242
+ STDERR << filename << ":#{lnumber}: Current environment is #{@env_label}, but it is terminated with: #{line}"
243
+ exit 1
244
+ end
245
+ cb_env_end2()
246
+ @env_caption =''
247
+ @env_label =''
248
+ end
249
+
250
+ def cb_env_in_env_error(filename=nil, lnumber=nil, line=nil)
251
+ STDERR << filename << ":#{lnumber}: It is already/still in the environment of #{@env_label}, but you tried: #{line}"
252
+ exit 1
250
253
  end
251
254
 
252
- def paragraph_add(e)
253
- result, is_mathml = e.line.apply_subs_rules(@subs_rules)
254
- @body << result
255
- @is_mathml = @is_mathml || is_mathml
255
+ def cb_env_not_in_env_error(filename=nil, lnumber=nil, line=nil)
256
+ STDERR << filename << ":#{lnumber}: It is not in any environment, but you tried: #{line}"
257
+ exit 1
258
+ end
259
+
260
+ def cb_equation_begin(filename=nil, lnumber=nil, line=nil)
261
+ @equation_label = line.strip.sub!(/^\\/,'')
262
+ @equations << @equation_label
263
+ @equation_contents = ''
256
264
  end
257
265
 
258
- def paragraph_end(e)
259
- @body << "</p>\n"
266
+ def cb_equation_continues(filename=nil, lnumber=nil, line=nil)
267
+ @equation_contents << line
260
268
  end
261
269
 
262
- def heading_add(e)
263
- if /^(=+) (.*)$/ =~ e.line
270
+ def cb_equation_end(filename=nil, lnumber=nil, line=nil)
271
+ cb_equation_end2()
272
+ @equation_contents =''
273
+ @equation_label =''
274
+ end
275
+
276
+ def parse(fd)
277
+ while true
278
+ if line = fd.gets
279
+ lnumber = ARGF.file.lineno
280
+ else
281
+ line = "=end\n"
282
+ end
283
+ case line
284
+ when /^=begin/,/^#/ then ev_ignore(nil, $FILENAME, lnumber, line)
285
+ when /^=end/ then ev_heading(nil, $FILENAME, lnumber, line); break
286
+ when /^=+ / then ev_heading(nil, $FILENAME, lnumber, line)
287
+ when /^ +\*/ then ev_asterisk(nil, $FILENAME, lnumber, line)
288
+ when /^$/ then ev_empty(nil, $FILENAME, lnumber, line)
289
+ when /^\s+/ then ev_offset(nil, $FILENAME, lnumber, line)
290
+ when /^\\(Fig|Table|Code):/
291
+ then ev_env_begin(nil, $FILENAME, lnumber, line)
292
+ when /^\/(Fig|Table|Code):/
293
+ then ev_env_end(nil, $FILENAME, lnumber, line)
294
+ when /^\\Eq:/ then ev_equation_begin(nil, $FILENAME, lnumber, line)
295
+ when /^\/Eq:/ then ev_equation_end(nil, $FILENAME, lnumber, line)
296
+ else ev_normal(nil, $FILENAME, lnumber, line)
297
+ end
298
+ end
299
+ end
300
+
301
+ def initialize()
302
+ @toc = Table_of_Contents.new()
303
+ @body = ''
304
+ @level_of_heading = 0
305
+ @i_th_heading = 0
306
+ @equations = []
307
+ @figures = []
308
+ @tables = []
309
+ @codes = []
310
+ @subs_rules = []
311
+ end
312
+ attr_accessor :subs_rules
313
+ end
314
+
315
+ module HTML
316
+ CONTENTS_HERE="<!-- Contents -->"
317
+ def cb_heading(filename=nil, lnumber=nil, line=nil)
318
+ if /^(=+) (.*)$/ =~ line
264
319
  new_level=Regexp.last_match[1].length
265
320
  str=Regexp.last_match[2]
266
- elsif /^=end/ =~ e.line
321
+ elsif /^=end/ =~ line
267
322
  @level_of_heading=0
268
323
  @body << '</div>'
269
- @contents.itemize_end(nil)
324
+ @toc.cb_itemize_end()
270
325
  return
271
326
  end
272
327
  raise 'Illegal jump of heading level' if new_level>@level_of_heading+1
@@ -274,121 +329,267 @@ class Ulmul
274
329
  @body << CONTENTS_HERE << "\n" if @i_th_heading==2
275
330
  @body << "</div>\n\n\n" if @i_th_heading!=1 and new_level<=2
276
331
  @title=str if @i_th_heading==1
277
- cls = case new_level
278
- when 1 then "slide cover"
279
- else "slide"
280
- end
332
+ cls = if new_level==1 then "slide cover" else "slide" end
281
333
  @body << "<div class=\"#{cls}\">\n" if new_level<=2
282
334
  @body << "<h#{new_level} id=\"LABEL-#{@i_th_heading}\">" << str << "</h#{new_level}>\n"
283
- if @contents_range === new_level
284
- @contents.itemize_add_primitive(1+new_level-@contents_range.first,
285
- "<a href=\"#LABEL-#{@i_th_heading}\">" + str + "</a>")
335
+ if 2 <= new_level && new_level <= $MAX_TABLE_OF_CONTENTS
336
+ @toc.cb_itemize_add_item('HTML Table of Contents', @i_th_heading, " "*(new_level-2) + " * <a href=\"#LABEL-#{@i_th_heading}\">" + str + "</a>")
286
337
  end
287
338
  @level_of_heading=new_level
288
339
  end
289
340
 
290
- include Itemize
291
- def itemize_add(e)
292
- new_level = case e.line
293
- when /^ \* (.*)/ then 1
294
- when /^ \* (.*)/ then 2
295
- when /^ \* (.*)/ then 3
296
- when /^ \* (.*)/ then 4
297
- when /^ \* (.*)/ then 5
298
- else raise 'Illegal astarisk line for itemize'
299
- end
300
- str, is_mathml = Regexp.last_match[1].apply_subs_rules(@subs_rules)
301
- itemize_add_primitive(new_level,str)
302
- @is_mathml = @is_mathml || is_mathml
341
+ def body
342
+ b = @body.dup
343
+ @equations.each_with_index{|r,i| b.gsub!(Regexp.new('(^|\s+)'+r+'(\s+|\.|\,|\!|\?|$)'), "\\1<a href=\"##{r}\">Eq. (#{i+1})</a>\\2")}
344
+ @figures.each_with_index{|r,i| b.gsub!(Regexp.new('(^|\s+)'+r+'(\s+|\.|\,|\!|\?|$)'), "\\1<a href=\"##{r}\">Fig. #{i+1}</a>\\2")}
345
+ @tables.each_with_index{|r,i| b.gsub!(Regexp.new('(^|\s+)'+r+'(\s+|\.|\,|\!|\?|$)'), "\\1<a href=\"##{r}\">Table #{i+1}</a>\\2")}
346
+ @codes.each_with_index{|r,i| b.gsub!(Regexp.new('(^|\s+)'+r+'(\s+|\.|\,|\!|\?|$)'), "\\1<a href=\"##{r}\">Code #{i+1}</a>\\2")}
347
+ if $MAX_TABLE_OF_CONTENTS>=2
348
+ b.sub(CONTENTS_HERE, "<br />\n<div class=\"table of contents\">\nTable of Contents:" +
349
+ @toc.body + "</div>\n")
350
+ else
351
+ b
352
+ end
303
353
  end
304
- def itemize_continue(e)
305
- /^(\s+)(\S.*)$/.match(e.line)
306
- new_level = Regexp.last_match[1].length/2
307
- new_level=@level_of_state if new_level>@level_of_state
308
- raise "Illegal indent in itemize" if new_level<=0
309
- str, is_mathml = Regexp.last_match[2].apply_subs_rules(@subs_rules)
310
- itemize_continue_primitive(new_level,str)
311
- @is_mathml = @is_mathml || is_mathml
354
+
355
+ def cb_env_end2table()
356
+ @body << "<table id=\"#{@env_label}\">\n"
357
+ @body << " <caption>\n"
358
+ @body << " Table #{@tables.length}: " << @env_caption.apply_subs_rules(@subs_rules)
359
+ @body << " </caption>\n"
360
+ @body << " <thead><tr><th> TABLE </th></tr></thead>\n"
361
+ @body << " <tbody><tr><td>Not yet implemented</td></tr></tbody>\n"
362
+ @body << '</table>' << "\n"
312
363
  end
313
364
 
365
+ def cb_env_end2code()
366
+ @body << "<p id=\"#{@env_label}\" class=\"code caption\">\n"
367
+ @body << " Code #{@codes.length}: " << @env_caption.apply_subs_rules(@subs_rules) << "(download: <a href=\"#{@env_file}\">#{File.basename(@env_file)}</a>)" << "</p>\n"
368
+ @body << "<pre class=\"prettyprint\">\n"
369
+ f = open(@env_file)
370
+ @body << f.read.gsub(/&/,'&amp;').gsub(/</,'&lt;').gsub(/>/,'&gt;')
371
+ f.close
372
+ @body << "</pre>\n"
373
+ end
314
374
 
315
- def parse(fd)
316
- while line=fd.gets || line="=end\n"
317
- e = Event.new(line)
318
- #STDERR.printf("%s\n", @state)
319
- #STDERR.printf("%-10s:%s", e.event, line)
320
- procs = TABLE[e.event][@state]
321
- procs.each do |i|
322
- send(i,e) if i.instance_of?(Symbol)
323
- @state=i if i.instance_of?(String)
324
- end
325
- break if e.event=='end'
326
- end
375
+ def cb_equation_end2()
376
+ @body << @equation_contents.to_mathml('block').to_s.sub(/<math /,"<math id=\"#{@equation_label}\" ") << "\n"
327
377
  end
378
+ end
328
379
 
329
- def body
330
- if @contents_range.first<=@contents_range.last
331
- @body.sub(CONTENTS_HERE,"<br />Contents:"+@contents.body)
332
- else
333
- @body
380
+ module HTML5
381
+ def cb_env_end2()
382
+ case @env_label
383
+ when /^Fig:/
384
+ @body << "<figure id=\"#{@env_label}\">\n" << " <img src=\"#{@env_file}\" alt=\"#{@env_file}\" />\n"
385
+ @body << " <figcaption>\n"
386
+ @body << " Figure #{@figures.length}: " << @env_caption.apply_subs_rules(@subs_rules)
387
+ @body << " </figcaption>\n" << '</figure>' << "\n"
388
+ when /^Table:/
389
+ cb_env_end2table()
390
+ when /^Code:/
391
+ cb_env_end2code()
334
392
  end
335
393
  end
336
394
 
337
- def html(stylesheets=["style.css"],javascripts=[],name="",language="en")
395
+ def file(stylesheets=["ulmul2html5.css"],javascripts=[],name="",language="en")
338
396
  style_lines=""
339
- stylesheets.each{|s| style_lines << "<link rel=\"stylesheet\" href=\"#{s}\" type=\"text/css\" />\n"}
397
+ stylesheets.each{|s| style_lines << " <link rel=\"stylesheet\" href=\"#{s}\" type=\"text/css\" />\n"}
340
398
  javascript_lines=""
341
- javascripts.each{|j| javascript_lines << "<script src=\"#{j}\" type=\"text/javascript\"></script>\n"}
342
- if @mode=='ulmul2html5'
343
- xml_line=''
344
- meta_charset_line="<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n "
345
- doctype_lines="<!DOCTYPE html>"
346
- html_line="<html lang=\"#{language}\">"
347
- else
348
- xml_line="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
349
- meta_charset_line="<meta http-equiv=\"content-type\" content=\"application/xhtml+xml; charset=utf-8\" />\n "
350
- if @is_mathml
351
- doctype_lines='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN"
352
- "http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd">'
353
- html_line="<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"#{language}\" dir=\"ltr\">"
354
- else
355
- doctype_lines='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
356
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
357
- html_line="<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"#{language}\" lang=\"#{language}\" dir=\"ltr\">"
358
- end
399
+ javascripts.each{|j| javascript_lines << " <script src=\"#{j}\" type=\"text/javascript\"></script>\n"}
400
+ return "<!DOCTYPE html>
401
+ <html lang=\"#{language}\">
402
+ <head>
403
+ <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
404
+ <title>#{@title}</title>
405
+ <meta name=\"author\" content=\"#{name}\" />
406
+ <meta name=\"copyright\" content=\"Copyright &#169; #{Date.today.year} #{name}\" />
407
+ #{style_lines}#{javascript_lines} <link rel=\"shortcut icon\" href=\"favicon.ico\" />
408
+ </head>
409
+ <body onload=\"prettyPrint()\">
410
+ #{body()}
411
+ </body>
412
+ </html>
413
+ "
414
+ end
415
+ end
416
+
417
+ module XHTML
418
+ def cb_env_end2()
419
+ case @env_label
420
+ when /^Fig:/
421
+ @body << "<div class=\"figure\" id=\"#{@env_label}\">\n" << " <img src=\"#{@env_file}\" alt=\"#{@env_file}\" />\n"
422
+ @body << " <div class=\"figcaption\">\n"
423
+ @body << " Figure #{@figures.length}. " << @env_caption.apply_subs_rules(@subs_rules)
424
+ @body << " </div>\n"
425
+ @body << '</div>' << "\n"
426
+ when /^Table:/
427
+ cb_env_end2table()
428
+ when /^Code:/
429
+ cb_env_end2code()
359
430
  end
360
- return "#{xml_line}#{doctype_lines}
361
- #{html_line}
431
+ end
432
+
433
+ def file(stylesheets=["ulmul2xhtml.css"],javascripts=[],name="",language="en")
434
+ style_lines=""
435
+ stylesheets.each{|s| style_lines << " <link rel=\"stylesheet\" href=\"#{s}\" type=\"text/css\" />\n"}
436
+ javascript_lines=""
437
+ javascripts.each{|j| javascript_lines << "<script src=\"#{j}\" type=\"text/javascript\"></script>\n"}
438
+ return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
439
+ <!DOCTYPE html
440
+ PUBLIC \"-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN\"
441
+ \"http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd\">
442
+ <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">
443
+
362
444
  <head>
363
- #{meta_charset_line}<title>#{@title}</title>
445
+ <meta name=\"language\" content=\"#{language}\" />
446
+ <title>#{@title}</title>
364
447
  <meta name=\"author\" content=\"#{name}\" />
365
448
  <meta name=\"copyright\" content=\"Copyright &#169; #{Date.today.year} #{name}\" />
366
449
  #{style_lines}#{javascript_lines} <link rel=\"shortcut icon\" href=\"favicon.ico\" />
367
450
  </head>
368
- <body>
451
+ <body onload=\"prettyPrint()\">
369
452
  #{body()}
370
453
  </body>
371
454
  </html>
455
+ "
456
+ end
457
+ end
458
+
459
+ module LaTeX
460
+ require 'exifr'
461
+ def cb_heading(filename=nil, lnumber=nil, line=nil)
462
+ if /^(=+) (.*)$/ =~ line
463
+ new_level=Regexp.last_match[1].length
464
+ str=Regexp.last_match[2]
465
+ elsif /^=end/ =~ line
466
+ @level_of_heading=0
467
+ @toc.cb_itemize_end()
468
+ return
469
+ end
470
+ raise 'Illegal jump of heading level' if new_level>@level_of_heading+1
471
+ @title=str if new_level==1
472
+ @body << '\section{' << str << "}\n" if new_level==2
473
+ @body << '\subsection{' << str << "}\n" if new_level==3
474
+ @body << '\subsubsection{' << str << "}\n" if new_level==4
475
+ @body << '\bf{' << str << "}\n" if new_level==5
476
+ if 2 <= new_level && new_level <= $MAX_TABLE_OF_CONTENTS
477
+ @toc.cb_itemize_add_item('LaTeX Table of Contents', @i_th_heading, " "*(new_level-2) + " * " + str)
478
+ end
479
+ @level_of_heading=new_level
480
+ end
481
+
482
+ def cb_env_end2()
483
+ case @env_label
484
+ when /^Fig:/
485
+ width = EXIFR::JPEG.new(@env_file).width
486
+ height = EXIFR::JPEG.new(@env_file).height
487
+ @body << "\\begin{figure}\n" << " \\center\n \\includegraphics[width=5cm,bb=0 0 #{width} #{height}]{#{@env_file}}\n"
488
+ @body << ' \caption{' << @env_caption.apply_subs_rules(@subs_rules).strip << "}\n"
489
+ @body << " \\label{#{@env_label}}\n" << "\\end{figure}\n"
490
+ when /^Table:/
491
+ @body << "\\begin{table}\n"
492
+ @body << ' \caption{' << @env_caption.apply_subs_rules(@subs_rules).strip << "}\n"
493
+ @body << " \\label{#{@env_label}}\n"
494
+ @body << " \\center\n"
495
+ @body << " {TABLE is not yet implemented.}\n"
496
+ @body << "\\end{table}\n"
497
+ when /^Code:/
498
+ @body << "\\begin{lstlisting}[caption={#{@env_caption.apply_subs_rules(@subs_rules).strip}},label=#{@env_label}]\n"
499
+ f = open(@env_file)
500
+ @body << f.read
501
+ f.close
502
+ @body << "\\end{lstlisting}\n"
503
+ end
504
+ end
505
+
506
+ def cb_equation_end2()
507
+ @body << "\\begin{equation}\n" << " \\label{#{@equation_label}}\n"
508
+ @body << @equation_contents
509
+ @body << "\\end{equation}\n"
510
+ end
511
+
512
+ def file(packages,name)
513
+ return "\\documentclass{article}
514
+ \\usepackage{graphicx}
515
+ \\usepackage{bm}
516
+ \\usepackage{listings}
517
+ \\renewcommand{\\lstlistingname}{Code}
518
+ \\lstset{language=c,
519
+ % basicstyle=\\ttfamily\\scriptsize,
520
+ % commentstyle=\\textit,
521
+ % classoffset=1,
522
+ % keywordstyle=\\bfseries,
523
+ frame=shadowbox,
524
+ % framesep=5pt,
525
+ % showstringspaces=false,
526
+ % numbers=left,
527
+ % stepnumber=1,
528
+ % numberstyle=\\tiny,
529
+ % tabsize=2
530
+ }
531
+ \\title{#{@title}}
532
+ \\author{#{name}}
533
+ \\begin{document}
534
+ \\maketitle
535
+ #{body}
536
+ \\end{document}
372
537
  "
373
538
  end
374
539
 
375
- class Event
376
- attr_reader :line, :event
377
- def initialize(line)
378
- @line = line
379
- @event = case line
380
- when /^=begin/,/^#/ then 'ignore'
381
- when /^=end/ then 'end'
382
- when /^=+ / then 'heading'
383
- when /^ +\*/ then 'asterisk'
384
- when /^\s*$/ then 'empty'
385
- when /^\s+/ then 'offset'
386
- when /^Eq\./ then 'eq_begin'
387
- when /^\/Eq/ then 'eq_end'
388
- when /^Fig\./ then 'fig_begin'
389
- when /^\/Fig/ then 'fig_end'
390
- else 'normal'
391
- end
540
+ def body
541
+ b = @body.dup
542
+ @equations.each{|r| b.gsub!(Regexp.new('(^|\s+)'+r+'(\s+|\.|\,|\!|\?|$)'), "\\1Eq.~(\\ref{#{r}})\\2")}
543
+ @figures.each{|r| b.gsub!(Regexp.new('(^|\s+)'+r+'(\s+|\.|\,|\!|\?|$)'), "\\1Fig.~\\ref{#{r}}\\2")}
544
+ @tables.each{|r| b.gsub!(Regexp.new('(^|\s+)'+r+'(\s+|\.|\,|\!|\?|$)'), "\\1Table~\\ref{#{r}}\\2")}
545
+ @codes.each{|r| b.gsub!(Regexp.new('(^|\s+)'+r+'(\s+|\.|\,|\!|\?|$)'), "\\1Code~\\ref{#{r}}\\2")}
546
+ return b
547
+ end
548
+ end
549
+
550
+ if $0 == __FILE__ || /ulmul2(html5|xhtml)$/ =~ $0
551
+ require "optparse"
552
+ name = ENV['USER'] || ENV['LOGNAME'] || Etc.getlogin || Etc.getpwuid.name
553
+ language = "en"
554
+ stylesheets = []
555
+ javascripts = []
556
+ $MAX_TABLE_OF_CONTENTS = 3
557
+ opts = OptionParser.new
558
+ opts.program_name = $0
559
+ opts.on("-s STYLESHEET_FILENAME","--style STYLESHEET_FILENAME",
560
+ "Specify stylesheet filename."){|v| stylesheets<<v}
561
+ opts.on("-n YOUR_NAME","--name YOUR_NAME","Specify your name."){|v| name=v}
562
+ opts.on("-j JAVASCRIPT_FILENAME","--javascript JAVASCRIPT_FILENAME",
563
+ "Specify JavaScript filename."){|v| javascripts<<v}
564
+ opts.on("-l LANGUAGE","--language LANGUAGE",String,
565
+ "Specify natural language. Its defalt is 'en'."){|v| language=v[0..1].downcase}
566
+ opts.on("-m MAX_TABLE_OF_CONTENTS","--max-table-of-contents MAX_TABLE_OF_CONTENTS",Integer,
567
+ "Specify the maximum level for table of contents."){|v| $MAX_TABLE_OF_CONTENTS=v}
568
+ opts.on_tail("-h", "--help", "Show this message."){puts opts.to_s.sub(/options/,'options] [filename'); exit}
569
+ opts.parse!(ARGV)
570
+
571
+ Itemize::ITEMIZE_INITIATOR = '<ul>'
572
+ Itemize::ITEMIZE_TERMINATOR = '</ul>'
573
+ Itemize::ITEM_INITIATOR = '<li>'
574
+ Itemize::ITEM_TERMINATOR = '</li>'
575
+ Ulmul::PARAGRAPH_INITIATOR = '<p>'
576
+ Ulmul::PARAGRAPH_TERMINATOR = '</p>'
577
+ Ulmul::VERBATIM_INITIATOR = '<pre>'
578
+ Ulmul::VERBATIM_TERMINATOR = '</pre>'
579
+ class Ulmul
580
+ include HTML
581
+ if /ulmul2xhtml$/ =~ $0
582
+ include XHTML
583
+ else
584
+ include HTML5
392
585
  end
393
586
  end
587
+ u=Ulmul.new()
588
+ u.subs_rules = [[/(http:\S*)(\s|$)/, '<a href="\1">\1</a>\2'],
589
+ [/(https:\S*)(\s|$)/, '<a href="\1">\1</a>\2']]
590
+ u.parse(ARGF)
591
+ puts u.file(stylesheets,javascripts,name,language)
394
592
  end
593
+ # Local variables:
594
+ # compile-command: "./ulmul.rb -s ../ulmul2html5.css -s ../google-code-prettify/src/prettify.css -j ../google-code-prettify/src/prettify.js test.ulmul | tee test.html"
595
+ # End: