ulmul 0.1.0 → 0.3.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.
@@ -1,561 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # ulmul.rb
3
- # Time-stamp: <2009-01-04 15:52:34 takeshi>
4
- # Author: Takeshi Nishimatsu
5
- ##
6
- =begin
7
- = ULMUL (ulmul.rb)
8
- ULMUL (ulmul.rb) is a converter from Ultra Lightweight MarkUp Language to
9
- (X)HTML. You can create HTML files, Slidy HTML files and MathML XHTML
10
- files very easily.
11
-
12
- The author is using ULMUL to generate his web pages.
13
- http://loto.sourceforge.net/feram/ is converted from
14
- http://loto.sourceforge.net/feram/README .
15
- http://loto.sourceforge.net/feram/doc/film.xhtml is converted from
16
- http://loto.sourceforge.net/feram/doc/film.txt .
17
-
18
- == Where is the homepage of ULMUL?
19
- http://ulmul.rubyforge.org/
20
-
21
- == Where can I download ulmul.rb?
22
- Go to http://rubyforge.org/projects/ulmul .
23
-
24
- == How can I install ULMUL?
25
- There are two different ways to install ULMUL.
26
- === I. Conservative way; use setup.rb
27
- ulmul-X.Y.Z.tgz package can be installed as:
28
- $ tar zxf ulmul-X.Y.Z.tgz
29
- $ cd ulmul-X.Y.Z
30
- $ su
31
- # ruby setup.rb
32
- Note that the eimxml and mathml libraries are required.
33
- === II. RubyGems users can take an easy way
34
- There is an easy way, if you are a RubyGems user:
35
- $ su
36
- # gem install ulmul
37
- If you do not have the eimxml and mathml libraries, gem will download and install the
38
- library automatically.
39
-
40
- === Files
41
- * ulmul.rb Ruby script.
42
- * slidy.js JavaScript for Slidy. Slightly modified from the original.
43
- * ulmul-slidy.css CSS file for Slidy. Largely modified from the original.
44
- * style.css Example CSS file for normal web pages.
45
- If you installed ULMUL with gem, you may find these files in
46
- /usr/local/lib/ruby/gems/1.8/gems/ulmul-X.Y.Z/
47
- or /usr/lib/ruby/gems/1.8/gems/ulmul-X.Y.Z/ .
48
-
49
- == How can I write a ULMUL text?
50
- The encode of input file must be utf-8.
51
- === Events (each input line)
52
- ==== empty
53
- Empty lines devide paragraphs.
54
- ==== heading
55
- Starting with "= ", "== ", "=== ", "==== ", "===== ", and "====== ".
56
- "= " will be used for the title.
57
- ==== asterisk
58
- Lines starting with
59
- " *"
60
- " *"
61
- " *"
62
- " *"
63
- " *"
64
- become itemize.
65
- ==== offset
66
- Lines starting with some spaces but not asterisks become verbatim lines.
67
- ==== end
68
- EOF or "=end" end the process.
69
- ==== ignore
70
- Lines starting with"#" and "=begin" are ignored.
71
- ==== normal
72
- Other lines.
73
- === Other rules
74
- * Lines after "=end" are ignored.
75
- * Add your substitution rules to @subs_rules.
76
- === Equations
77
- Input:
78
- Mass $m$ can be converted into energy $E$ as
79
- Eq. 1
80
- E=mc^2.
81
- /Eq.
82
-
83
- Output:
84
-
85
- Mass $m$ can be converted into energy $E$ as
86
- Eq. 1
87
- E=mc^2.
88
- /Eq.
89
-
90
- === Figures
91
- Input:
92
- Fig. 1 ruby.jpg
93
- The is a dummy figure for an example.
94
- /Fig.
95
-
96
- Output:
97
- Fig. 1 ruby.jpg
98
- The is a dummy figure for an example.
99
- /Fig.
100
-
101
-
102
- == Usage
103
- === Examples
104
- % ruby ulmul.rb foo.txt
105
- % ruby ulmul.rb --style=style.css --name="John Smith" foo.ulmul > foo.html
106
- % ./ulmul.rb --style=ulmul-slidy.css --javascript=slidy.js \
107
- --name="Takeshi Nishimatsu" presentation.txt > presentation.xhtml
108
- === Command options
109
- ==== -s, --style
110
- Specify stylesheet filename.
111
- ==== -n, --name
112
- Specify your name for copyright notices.
113
- ==== -j, --javascript
114
- Specify JavaScript filename.
115
- ==== -l, --language
116
- Specify natural language. Its default is "en".
117
- ==== -c, --contents-range
118
- Range of "Contents". Its default is "2..3".
119
- If you do not need "Contents" at the beginning of the
120
- output HTML file, set it 3..2.
121
- ==== --help
122
- Show a help message.
123
-
124
- == TODO
125
- * rescue syntax errors (raises) in an input file and report the
126
- errors as #{$FILENAME}:#{file.lineno}:...
127
- * Unit test, tests/ulmul_test.rb
128
- * @body must be XML object, not a String.
129
- * Tables.
130
- * References to figures and tables.
131
- * Citation.
132
-
133
- == Copying
134
- ulmul.rb is distributed in the hope that
135
- it will be useful, but WITHOUT ANY WARRANTY.
136
- You can copy, modify and redistribute ulmul.rb,
137
- but only under the conditions described in
138
- the GNU General Public License (the "GPL").
139
-
140
- W3C has copyrights for slidy.js and ulmul-slidy.css (originally
141
- named slidy.css). Takeshi Nishimatsu modified them.
142
- You can download the original package, slidy.zip, from
143
- http://www.w3.org/Talks/Tools/Slidy/ . You can find
144
- their licenses at http://www.w3.org/Consortium/Legal/copyright-documents
145
- and http://www.w3.org/Consortium/Legal/copyright-software .
146
-
147
- == Author of ULMUL
148
- Takeshi Nishimatsu (t_nissie{at}yahoo.co.jp)
149
-
150
- =end
151
- require "rubygems"
152
- require "date"
153
- require "math_ml/string"
154
- ULMUL_RB_VERSION = '0.1.0'
155
- CONTENTS_RANGE_DEFAULT=2..3
156
-
157
- class String
158
- def apply_subs_rules(rules)
159
- result = self.dup
160
- rules.each do |ary|
161
- result.gsub!(ary[0],ary[1])
162
- end
163
- is_mathml = false
164
- while result =~ /\$(.*?)\$/
165
- pre=$`
166
- tex=Regexp.last_match[1]
167
- post=$'
168
- result = "#{pre}#{tex.to_mathml}#{post}"
169
- is_mathml = true
170
- end
171
- return result, is_mathml
172
- end
173
- end
174
-
175
- module Itemize
176
- def itemize_begin(e)
177
- @level_of_state = 0
178
- end
179
-
180
- def itemize_add_primitive(new_level,str)
181
- if new_level>@level_of_state+1
182
- raise 'Illegal jump of itemize level'
183
- elsif new_level==@level_of_state+1
184
- @body << "\n" << " "*@level_of_state << "<ul>\n"
185
- @body << " "*(new_level-1) << " " << "<li>#{str}"
186
- @level_of_state = new_level
187
- elsif new_level==@level_of_state
188
- @body << "</li>\n" << " "*(new_level-1) << " " << "<li>#{str}"
189
- else
190
- @body << "</li>\n"
191
- (@level_of_state-1).downto(new_level){|i| @body << " "*i << "</ul></li>\n"}
192
- @body << " "*(new_level-1) << " " << "<li>#{str}"
193
- @level_of_state = new_level
194
- end
195
- end
196
-
197
- def itemize_continue_primitive(new_level,str)
198
- (@level_of_state-1).downto(new_level){|i| @body << "</li>\n" << " "*i << "</ul>"}
199
- @body << "\n " << " "*(new_level-1) << " " << str
200
- @level_of_state = new_level
201
- end
202
-
203
- def itemize_end(e)
204
- @body << "</li>\n"
205
- (@level_of_state-1).downto(1){|i| @body << " "*i << "</ul></li>\n"}
206
- @body << "</ul>\n"
207
- @level_of_state = 0
208
- end
209
- end
210
-
211
- class Contents
212
- include Itemize
213
- def initialize()
214
- @state = 'itemize'
215
- @level_of_state = 0
216
- @body = ''
217
- end
218
- attr_reader :body
219
- end
220
-
221
- class Ulmul
222
- CONTENTS_HERE="<!-- Contents -->"
223
- TABLE={
224
- 'ignore' => {'ground' => [],
225
- 'itemize' => [],
226
- 'verbatim' => [],
227
- 'paragraph' => [],
228
- 'equation' => [],
229
- 'figure' => []},
230
-
231
- 'end' => {'ground' => [ :heading_add],
232
- 'itemize' => [ :itemize_end, :heading_add, 'ground'],
233
- 'verbatim' => [ :verbatim_end, :heading_add, 'ground'],
234
- 'paragraph' => [:paragraph_end, :heading_add, 'ground'],
235
- 'equation' => [:equation_error],
236
- 'figure' => [:figure_error]},
237
-
238
- 'heading' => {'ground' => [ :heading_add],
239
- 'itemize' => [ :itemize_end, :heading_add, 'ground'],
240
- 'verbatim' => [ :verbatim_end, :heading_add, 'ground'],
241
- 'paragraph' => [:paragraph_end, :heading_add, 'ground'],
242
- 'equation' => [:equation_continue],
243
- 'figure' => [:figure_continue]},
244
-
245
- 'asterisk' => {'ground' => [:itemize_begin, 'itemize', :itemize_add],
246
- 'itemize' => [ :itemize_add],
247
- 'verbatim' => [ :verbatim_add],
248
- 'paragraph' => [:paragraph_end,
249
- :itemize_begin, 'itemize', :itemize_add],
250
- 'equation' => [:equation_continue],
251
- 'figure' => [:figure_continue]},
252
-
253
- 'offset' => {'ground' => [:verbatim_begin, 'verbatim', :verbatim_add],
254
- 'itemize' => [:itemize_continue],
255
- 'verbatim' => [ :verbatim_add],
256
- 'paragraph' => [:paragraph_end,
257
- :verbatim_begin, 'verbatim', :verbatim_add],
258
- 'equation' => [:equation_continue],
259
- 'figure' => [:figure_continue]},
260
-
261
- 'empty' => {'ground' => [],
262
- 'itemize' => [:itemize_end, 'ground'],
263
- 'verbatim' => [:verbatim_add],
264
- 'paragraph' => [:paragraph_end, 'ground'],
265
- 'equation' => [:equation_error],
266
- 'figure' => [:figure_error]},
267
-
268
- 'normal' => {'ground' => [:paragraph_begin, 'paragraph', :paragraph_add],
269
- 'itemize' => [:itemize_end,
270
- :paragraph_begin, 'paragraph', :paragraph_add],
271
- 'verbatim' => [:verbatim_end,
272
- :paragraph_begin, 'paragraph', :paragraph_add],
273
- 'paragraph' => [:paragraph_add],
274
- 'equation' => [:equation_continue],
275
- 'figure' => [:figure_continue]},
276
-
277
- 'eq_begin' => {'ground' => [:paragraph_begin, :equation_begin, 'equation'],
278
- 'itemize' => [:itemize_end,
279
- :paragraph_begin, :equation_begin, 'equation'],
280
- 'verbatim' => [:verbatim_end,
281
- :paragraph_begin, :equation_begin, 'equation'],
282
- 'paragraph' => [ :equation_begin, 'equation'],
283
- 'equation' => [:equation_error],
284
- 'figure' => [:figure_error]},
285
-
286
- 'eq_end' => {'ground' => [:equation_error],
287
- 'itemize' => [:equation_error],
288
- 'verbatim' => [:equation_error],
289
- 'paragraph' => [:equation_error],
290
- 'equation' => [:equation_end, 'paragraph'],
291
- 'figure' => [:figure_error]},
292
-
293
- 'fig_begin'=> {'ground' => [ :figure_begin, 'figure'],
294
- 'itemize' => [:itemize_end, :figure_begin, 'figure'],
295
- 'verbatim' => [:verbatim_end, :figure_begin, 'figure'],
296
- 'paragraph' => [:paragraph_end, :figure_begin, 'figure'],
297
- 'equation' => [:equation_error],
298
- 'figure' => [:figure_error]},
299
-
300
- 'fig_end' => {'ground' => [:figure_error],
301
- 'itemize' => [:figure_error],
302
- 'verbatim' => [:figure_error],
303
- 'paragraph' => [:figure_error],
304
- 'equation' => [:figure_error],
305
- 'figure' => [:figure_end, 'ground']}
306
- }
307
-
308
- def initialize(contents_range=CONTENTS_RANGE_DEFAULT)
309
- @contents_range = contents_range
310
- @contents = Contents.new()
311
- @state = 'ground'
312
- @level_of_state = 0
313
- @level_of_heading = 0
314
- @i_th_heading = 0
315
- @equation = ''
316
- @figure_caption = ''
317
- @body = ''
318
- @subs_rules = [[/(http:\S*)(\s|$)/, '<a href="\1">\1</a>\2'],
319
- [/(https:\S*)(\s|$)/, '<a href="\1">\1</a>\2']]
320
- @contents.itemize_begin(nil)
321
- @is_mathml = false
322
- end
323
- attr_accessor :subs_rules
324
-
325
- def equation_begin(e)
326
- @is_mathml = true
327
- end
328
-
329
- def equation_continue(e)
330
- @equation << e.line
331
- end
332
-
333
- def equation_end(e)
334
- @body << @equation.to_mathml('block').to_s << "\n"
335
- @equation =''
336
- end
337
-
338
- def figure_begin(e)
339
- dummy, @figure_label, @figure_file = e.line.split
340
- @body << "<div class=\"figure\">
341
- <img src=\"#{@figure_file}\" alt=\"#{@figure_file}\" />
342
- <div class=\"caption\">\n"
343
- end
344
-
345
- def figure_continue(e)
346
- result, is_mathml = e.line.apply_subs_rules(@subs_rules)
347
- @figure_caption << result
348
- @is_mathml = @is_mathml || is_mathml
349
- end
350
-
351
- def figure_end(e)
352
- @body << @figure_caption << " </div>\n" << "</div>\n"
353
- @figure_caption =''
354
- end
355
-
356
- def figure_error(e)
357
- p e
358
- exit 1
359
- end
360
-
361
- def verbatim_begin(e)
362
- @level_of_state = 0
363
- @body << "<pre>"
364
- end
365
-
366
- def verbatim_add(e)
367
- if e.event=='empty'
368
- @level_of_state += 1
369
- else
370
- @body << "\n"*@level_of_state << e.line[1..-1].gsub(/</,'&lt;').gsub(/>/,'&gt;')
371
- @level_of_state=0
372
- end
373
- end
374
-
375
- def verbatim_end(e)
376
- @body << "</pre>\n"
377
- end
378
-
379
- def paragraph_begin(e)
380
- @level_of_state = 0
381
- @body << "<p>\n"
382
- end
383
-
384
- def paragraph_add(e)
385
- result, is_mathml = e.line.apply_subs_rules(@subs_rules)
386
- @body << result
387
- @is_mathml = @is_mathml || is_mathml
388
- end
389
-
390
- def paragraph_end(e)
391
- @body << "</p>\n"
392
- end
393
-
394
- def heading_add(e)
395
- if /^(=+) (.*)$/ =~ e.line
396
- new_level=Regexp.last_match[1].length
397
- str=Regexp.last_match[2]
398
- elsif /^=end/ =~ e.line
399
- @level_of_heading=0
400
- @body << '</div>'
401
- @contents.itemize_end(nil)
402
- return
403
- end
404
- raise 'Illegal jump of heading level' if new_level>@level_of_heading+1
405
- @i_th_heading += 1
406
- @body << CONTENTS_HERE << "\n" if @i_th_heading==2
407
- @body << "</div>\n\n\n" if @i_th_heading!=1 and new_level<=2
408
- @title=str if @i_th_heading==1
409
- cls = case new_level
410
- when 1 then "slide cover"
411
- else "slide"
412
- end
413
- @body << "<div class=\"#{cls}\">\n" if new_level<=2
414
- @body << "<h#{new_level} id=\"LABEL-#{@i_th_heading}\">" << str << "</h#{new_level}>\n"
415
- if @contents_range === new_level
416
- @contents.itemize_add_primitive(1+new_level-@contents_range.first,
417
- "<a href=\"#LABEL-#{@i_th_heading}\">" + str + "</a>")
418
- end
419
- @level_of_heading=new_level
420
- end
421
-
422
- include Itemize
423
- def itemize_add(e)
424
- new_level = case e.line
425
- when /^ \* (.*)/ then 1
426
- when /^ \* (.*)/ then 2
427
- when /^ \* (.*)/ then 3
428
- when /^ \* (.*)/ then 4
429
- when /^ \* (.*)/ then 5
430
- else raise 'Illegal astarisk line for itemize'
431
- end
432
- str, is_mathml = Regexp.last_match[1].apply_subs_rules(@subs_rules)
433
- itemize_add_primitive(new_level,str)
434
- @is_mathml = @is_mathml || is_mathml
435
- end
436
- def itemize_continue(e)
437
- /^(\s+)(\S.*)$/.match(e.line)
438
- new_level = Regexp.last_match[1].length/2
439
- new_level=@level_of_state if new_level>@level_of_state
440
- raise "Illegal indent in itemize" if new_level<=0
441
- str, is_mathml = Regexp.last_match[2].apply_subs_rules(@subs_rules)
442
- itemize_continue_primitive(new_level,str)
443
- @is_mathml = @is_mathml || is_mathml
444
- end
445
-
446
-
447
- def parse(fd)
448
- while line=fd.gets || line="=end\n"
449
- e = Event.new(line)
450
- #STDERR.printf("%s\n", @state)
451
- #STDERR.printf("%-10s:%s", e.event, line)
452
- procs = TABLE[e.event][@state]
453
- procs.each do |i|
454
- send(i,e) if i.instance_of?(Symbol)
455
- @state=i if i.instance_of?(String)
456
- end
457
- break if e.event=='end'
458
- end
459
- end
460
-
461
- def body
462
- if @contents_range.first<=@contents_range.last
463
- @body.sub(CONTENTS_HERE,"<br />Contents:"+@contents.body)
464
- else
465
- @body
466
- end
467
- end
468
-
469
- def html(stylesheets=["style.css"],javascripts=[],name="",language="en")
470
- style_lines=""
471
- stylesheets.each{|s| style_lines << "<link rel=\"stylesheet\" href=\"#{s}\" type=\"text/css\" />\n"}
472
- javascript_lines=""
473
- javascripts.each{|j| javascript_lines << "<script src=\"#{j}\" type=\"text/javascript\"></script>\n"}
474
- if @is_mathml
475
- doctype_lines='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN"
476
- "http://www.w3.org/TR/MathML2/dtd/xhtml-math11-f.dtd">'
477
- html_line="<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"#{language}\" dir=\"ltr\">"
478
- else
479
- doctype_lines='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
480
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
481
- html_line="<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"#{language}\" lang=\"#{language}\" dir=\"ltr\">"
482
- end
483
- return "<?xml version=\"1.0\" encoding=\"utf-8\"?>
484
- #{doctype_lines}
485
-
486
- #{html_line}
487
- <head>
488
- <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
489
- <title>#{@title}</title>
490
- <meta name=\"author\" content=\"#{name}\" />
491
- <meta name=\"copyright\" content=\"Copyright &#169; #{Date.today.year} #{name}\" />
492
- #{style_lines}#{javascript_lines}<link rel=\"shortcut icon\" href=\"favicon.ico\" />
493
- </head>
494
- <body>
495
- #{body()}
496
- </body>
497
- </html>
498
- "
499
- end
500
-
501
- class Event
502
- attr_reader :line, :event
503
- def initialize(line)
504
- @line = line
505
- @event = case line
506
- when /^=begin/,/^#/ then 'ignore'
507
- when /^=end/ then 'end'
508
- when /^=+ / then 'heading'
509
- when /^ +\*/ then 'asterisk'
510
- when /^\s*$/ then 'empty'
511
- when /^\s+/ then 'offset'
512
- when /^Eq\./ then 'eq_begin'
513
- when /^\/Eq/ then 'eq_end'
514
- when /^Fig\./ then 'fig_begin'
515
- when /^\/Fig/ then 'fig_end'
516
- else 'normal'
517
- end
518
- end
519
- end
520
- end
521
-
522
- if $0 == __FILE__
523
- require "optparse"
524
- name = ENV['USER'] || ENV['LOGNAME'] || Etc.getlogin || Etc.getpwuid.name
525
- language = "en"
526
- stylesheets = []
527
- javascripts = []
528
- contents_range = CONTENTS_RANGE_DEFAULT
529
- opts = OptionParser.new
530
- def opts.usage
531
- return to_s.sub(/options/,'options] [filename')
532
- end
533
- opts.on("-s STYLESHEET_FILENAME","--style STYLESHEET_FILENAME",
534
- "Specify stylesheet filename."){|v| stylesheets<<v}
535
- opts.on("-n YOUR_NAME","--name YOUR_NAME","Specify your name."){|v| name=v}
536
- opts.on("-j JAVASCRIPT_FILENAME","--javascript JAVASCRIPT_FILENAME",
537
- "Specify JavaScript filename."){|v| javascripts<<v}
538
- opts.on("-l LANGUAGE","--language LANGUAGE",String,
539
- "Specify natural language. Its defalt is 'en'."){|v| language=v[0..1].downcase}
540
- opts.on("-c CONTENTS_RANGE","--contents-range RANGE_OF_CONTENTS_RANGE","Range of Contents."){|v|
541
- begin
542
- if eval(v).instance_of?(Range)
543
- contents_range=eval(v)
544
- else
545
- raise NameError
546
- end
547
- rescue NameError
548
- raise("Cannot evaluate given \"#{v}\" as a Range")
549
- end
550
- }
551
- opts.on_tail("-h", "--help", "Show this message."){puts opts.usage; exit}
552
-
553
- opts.parse!(ARGV)
554
-
555
- u=Ulmul.new(contents_range)
556
- u.parse(ARGF)
557
- puts u.html(stylesheets,javascripts,name,language)
558
- end
559
- # Local variables:
560
- # compile-command: "./ulmul.rb -c 2..3 -s style.css ulmul.rb > ../README-en.xhtml && ./ulmul.rb -c 2..3 -s style.css ../README-ja > ../README-ja.xhtml"
561
- # End: