bai-maruku 0.5.9

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 (190) hide show
  1. data/Rakefile +102 -0
  2. data/bin/marudown +29 -0
  3. data/bin/maruku +181 -0
  4. data/bin/marutest +345 -0
  5. data/bin/marutex +31 -0
  6. data/docs/changelog.md +334 -0
  7. data/docs/div_syntax.md +36 -0
  8. data/docs/entity_test.md +23 -0
  9. data/docs/markdown_syntax.md +899 -0
  10. data/docs/maruku.md +346 -0
  11. data/docs/math.md +194 -0
  12. data/docs/other_stuff.md +51 -0
  13. data/docs/proposal.md +309 -0
  14. data/lib/maruku.rb +141 -0
  15. data/lib/maruku/attributes.rb +227 -0
  16. data/lib/maruku/defaults.rb +71 -0
  17. data/lib/maruku/errors_management.rb +92 -0
  18. data/lib/maruku/ext/div.rb +133 -0
  19. data/lib/maruku/ext/math.rb +41 -0
  20. data/lib/maruku/ext/math/elements.rb +27 -0
  21. data/lib/maruku/ext/math/latex_fix.rb +12 -0
  22. data/lib/maruku/ext/math/mathml_engines/blahtex.rb +107 -0
  23. data/lib/maruku/ext/math/mathml_engines/itex2mml.rb +29 -0
  24. data/lib/maruku/ext/math/mathml_engines/none.rb +20 -0
  25. data/lib/maruku/ext/math/mathml_engines/ritex.rb +24 -0
  26. data/lib/maruku/ext/math/parsing.rb +119 -0
  27. data/lib/maruku/ext/math/to_html.rb +187 -0
  28. data/lib/maruku/ext/math/to_latex.rb +26 -0
  29. data/lib/maruku/helpers.rb +260 -0
  30. data/lib/maruku/input/charsource.rb +326 -0
  31. data/lib/maruku/input/extensions.rb +69 -0
  32. data/lib/maruku/input/html_helper.rb +189 -0
  33. data/lib/maruku/input/linesource.rb +111 -0
  34. data/lib/maruku/input/parse_block.rb +614 -0
  35. data/lib/maruku/input/parse_doc.rb +227 -0
  36. data/lib/maruku/input/parse_span_better.rb +746 -0
  37. data/lib/maruku/input/rubypants.rb +225 -0
  38. data/lib/maruku/input/type_detection.rb +147 -0
  39. data/lib/maruku/input_textile2/t2_parser.rb +163 -0
  40. data/lib/maruku/maruku.rb +33 -0
  41. data/lib/maruku/output/s5/fancy.rb +756 -0
  42. data/lib/maruku/output/s5/to_s5.rb +138 -0
  43. data/lib/maruku/output/to_html.rb +991 -0
  44. data/lib/maruku/output/to_latex.rb +590 -0
  45. data/lib/maruku/output/to_latex_entities.rb +367 -0
  46. data/lib/maruku/output/to_latex_strings.rb +64 -0
  47. data/lib/maruku/output/to_markdown.rb +164 -0
  48. data/lib/maruku/output/to_s.rb +56 -0
  49. data/lib/maruku/string_utils.rb +191 -0
  50. data/lib/maruku/structures.rb +167 -0
  51. data/lib/maruku/structures_inspect.rb +87 -0
  52. data/lib/maruku/structures_iterators.rb +61 -0
  53. data/lib/maruku/tests/benchmark.rb +82 -0
  54. data/lib/maruku/tests/new_parser.rb +373 -0
  55. data/lib/maruku/tests/tests.rb +136 -0
  56. data/lib/maruku/textile2.rb +1 -0
  57. data/lib/maruku/toc.rb +199 -0
  58. data/lib/maruku/usage/example1.rb +33 -0
  59. data/lib/maruku/version.rb +40 -0
  60. data/maruku.gemspec +32 -0
  61. data/tests/bugs/code_in_links.md +101 -0
  62. data/tests/bugs/complex_escaping.md +38 -0
  63. data/tests/math/syntax.md +46 -0
  64. data/tests/math_usage/document.md +13 -0
  65. data/tests/others/abbreviations.md +11 -0
  66. data/tests/others/blank.md +4 -0
  67. data/tests/others/code.md +5 -0
  68. data/tests/others/code2.md +8 -0
  69. data/tests/others/code3.md +16 -0
  70. data/tests/others/email.md +4 -0
  71. data/tests/others/entities.md +19 -0
  72. data/tests/others/escaping.md +16 -0
  73. data/tests/others/extra_dl.md +101 -0
  74. data/tests/others/extra_header_id.md +13 -0
  75. data/tests/others/extra_table1.md +40 -0
  76. data/tests/others/footnotes.md +17 -0
  77. data/tests/others/headers.md +10 -0
  78. data/tests/others/hrule.md +10 -0
  79. data/tests/others/images.md +20 -0
  80. data/tests/others/inline_html.md +42 -0
  81. data/tests/others/links.md +38 -0
  82. data/tests/others/list1.md +4 -0
  83. data/tests/others/list2.md +5 -0
  84. data/tests/others/list3.md +8 -0
  85. data/tests/others/lists.md +32 -0
  86. data/tests/others/lists_after_paragraph.md +44 -0
  87. data/tests/others/lists_ol.md +39 -0
  88. data/tests/others/misc_sw.md +105 -0
  89. data/tests/others/one.md +1 -0
  90. data/tests/others/paragraphs.md +13 -0
  91. data/tests/others/sss06.md +352 -0
  92. data/tests/others/test.md +4 -0
  93. data/tests/s5/s5profiling.md +48 -0
  94. data/tests/unittest/abbreviations.md +64 -0
  95. data/tests/unittest/alt.md +29 -0
  96. data/tests/unittest/attributes/att2.md +32 -0
  97. data/tests/unittest/attributes/att3.md +40 -0
  98. data/tests/unittest/attributes/attributes.md +69 -0
  99. data/tests/unittest/attributes/circular.md +38 -0
  100. data/tests/unittest/attributes/default.md +34 -0
  101. data/tests/unittest/blank.md +36 -0
  102. data/tests/unittest/blanks_in_code.md +87 -0
  103. data/tests/unittest/bug_def.md +28 -0
  104. data/tests/unittest/bug_table.md +58 -0
  105. data/tests/unittest/code.md +46 -0
  106. data/tests/unittest/code2.md +40 -0
  107. data/tests/unittest/code3.md +83 -0
  108. data/tests/unittest/data_loss.md +37 -0
  109. data/tests/unittest/divs/div1.md +179 -0
  110. data/tests/unittest/divs/div2.md +33 -0
  111. data/tests/unittest/divs/div3_nest.md +57 -0
  112. data/tests/unittest/easy.md +27 -0
  113. data/tests/unittest/email.md +32 -0
  114. data/tests/unittest/encoding/iso-8859-1.md +35 -0
  115. data/tests/unittest/encoding/utf-8.md +30 -0
  116. data/tests/unittest/entities.md +106 -0
  117. data/tests/unittest/escaping.md +79 -0
  118. data/tests/unittest/extra_dl.md +64 -0
  119. data/tests/unittest/extra_header_id.md +75 -0
  120. data/tests/unittest/extra_table1.md +49 -0
  121. data/tests/unittest/footnotes.md +109 -0
  122. data/tests/unittest/headers.md +49 -0
  123. data/tests/unittest/hex_entities.md +49 -0
  124. data/tests/unittest/hrule.md +51 -0
  125. data/tests/unittest/html2.md +34 -0
  126. data/tests/unittest/html3.md +43 -0
  127. data/tests/unittest/html4.md +37 -0
  128. data/tests/unittest/html5.md +35 -0
  129. data/tests/unittest/ie.md +61 -0
  130. data/tests/unittest/images.md +102 -0
  131. data/tests/unittest/images2.md +43 -0
  132. data/tests/unittest/inline_html.md +187 -0
  133. data/tests/unittest/inline_html2.md +33 -0
  134. data/tests/unittest/links.md +164 -0
  135. data/tests/unittest/links2.md +34 -0
  136. data/tests/unittest/list1.md +58 -0
  137. data/tests/unittest/list12.md +40 -0
  138. data/tests/unittest/list2.md +68 -0
  139. data/tests/unittest/list3.md +76 -0
  140. data/tests/unittest/list4.md +101 -0
  141. data/tests/unittest/lists.md +204 -0
  142. data/tests/unittest/lists10.md +46 -0
  143. data/tests/unittest/lists11.md +28 -0
  144. data/tests/unittest/lists6.md +53 -0
  145. data/tests/unittest/lists9.md +76 -0
  146. data/tests/unittest/lists_after_paragraph.md +220 -0
  147. data/tests/unittest/lists_ol.md +274 -0
  148. data/tests/unittest/loss.md +28 -0
  149. data/tests/unittest/math/equations.md +86 -0
  150. data/tests/unittest/math/inline.md +58 -0
  151. data/tests/unittest/math/math2.md +88 -0
  152. data/tests/unittest/math/notmath.md +37 -0
  153. data/tests/unittest/math/table.md +52 -0
  154. data/tests/unittest/math/table2.md +54 -0
  155. data/tests/unittest/misc_sw.md +537 -0
  156. data/tests/unittest/notyet/escape.md +33 -0
  157. data/tests/unittest/notyet/header_after_par.md +70 -0
  158. data/tests/unittest/notyet/ticks.md +30 -0
  159. data/tests/unittest/notyet/triggering.md +169 -0
  160. data/tests/unittest/olist.md +57 -0
  161. data/tests/unittest/one.md +27 -0
  162. data/tests/unittest/paragraph.md +28 -0
  163. data/tests/unittest/paragraph_rules/dont_merge_ref.md +54 -0
  164. data/tests/unittest/paragraph_rules/tab_is_blank.md +36 -0
  165. data/tests/unittest/paragraphs.md +58 -0
  166. data/tests/unittest/pending/amps.md +27 -0
  167. data/tests/unittest/pending/empty_cells.md +49 -0
  168. data/tests/unittest/pending/link.md +84 -0
  169. data/tests/unittest/pending/ref.md +33 -0
  170. data/tests/unittest/recover/recover_links.md +27 -0
  171. data/tests/unittest/red_tests/abbrev.md +1388 -0
  172. data/tests/unittest/red_tests/lists7.md +68 -0
  173. data/tests/unittest/red_tests/lists7b.md +128 -0
  174. data/tests/unittest/red_tests/lists8.md +76 -0
  175. data/tests/unittest/references/long_example.md +83 -0
  176. data/tests/unittest/references/spaces_and_numbers.md +27 -0
  177. data/tests/unittest/smartypants.md +126 -0
  178. data/tests/unittest/syntax_hl.md +64 -0
  179. data/tests/unittest/table_attributes.md +46 -0
  180. data/tests/unittest/test.md +31 -0
  181. data/tests/unittest/underscore_in_words.md +27 -0
  182. data/tests/unittest/wrapping.md +79 -0
  183. data/tests/unittest/xml.md +45 -0
  184. data/tests/unittest/xml2.md +31 -0
  185. data/tests/unittest/xml3.md +38 -0
  186. data/tests/unittest/xml_instruction.md +64 -0
  187. data/tests/utf8-files/simple.md +1 -0
  188. data/unit_test_block.sh +5 -0
  189. data/unit_test_span.sh +3 -0
  190. metadata +251 -0
data/Rakefile ADDED
@@ -0,0 +1,102 @@
1
+ require 'rubygems'
2
+ Gem::manage_gems
3
+ require 'rake/gempackagetask'
4
+
5
+ require 'lib/maruku/version'
6
+ spec = Gem::Specification.new do |s|
7
+ s.name = 'maruku'
8
+ s.version = MaRuKu::Version
9
+ s.summary = "Maruku is a Markdown-superset interpreter written in Ruby."
10
+ s.description = %{Maruku is a Markdown interpreter in Ruby.
11
+ It features native export to HTML and PDF (via Latex). The
12
+ output is really beautiful!
13
+ }
14
+ s.files = Dir['lib/**/*.rb'] + Dir['lib/*.rb'] +
15
+ Dir['docs/*.md'] + Dir['docs/*.html'] +
16
+ Dir['tests/**/*.md'] +
17
+ Dir['bin/*'] + Dir['*.sh'] + ['Rakefile', 'maruku.gemspec']
18
+
19
+ s.bindir = 'bin'
20
+ s.executables = ['maruku','marutex']
21
+
22
+ s.require_path = 'lib'
23
+ s.autorequire = 'maruku'
24
+
25
+ s.add_dependency('syntax', '>= 1.0.0')
26
+
27
+ s.author = "Andrea Censi"
28
+ s.email = "andrea@rubyforge.org"
29
+ s.homepage = "http://maruku.rubyforge.org"
30
+ end
31
+
32
+ task :default => [:package]
33
+
34
+ Rake::GemPackageTask.new(spec) do |pkg|
35
+ pkg.need_zip = true
36
+ pkg.need_tar = true
37
+ end
38
+
39
+ PKG_NAME = 'maruku'
40
+ PKG_FILE_NAME = "#{PKG_NAME}-#{MaRuKu::Version}"
41
+ RUBY_FORGE_PROJECT = PKG_NAME
42
+ RUBY_FORGE_USER = 'andrea'
43
+
44
+ RELEASE_NAME = MaRuKu::Version
45
+ RUBY_FORGE_GROUPID = '2795'
46
+ RUBY_FORGE_PACKAGEID = '3292'
47
+
48
+ desc "Publish the release files to RubyForge."
49
+ task :release => [:gem, :package] do
50
+ system("rubyforge login --username #{RUBY_FORGE_USER}")
51
+
52
+ gem = "pkg/#{PKG_FILE_NAME}.gem"
53
+ # -n notes/#{Maruku::Version}.txt
54
+ cmd = "rubyforge add_release %s %s \"%s\" %s" %
55
+ [RUBY_FORGE_GROUPID, RUBY_FORGE_PACKAGEID, RELEASE_NAME, gem]
56
+
57
+ puts cmd
58
+ system(cmd)
59
+
60
+ files = ["gem", "tgz", "zip"].map { |ext| "pkg/#{PKG_FILE_NAME}.#{ext}" }
61
+ files.each do |file|
62
+ # system("rubyforge add_file %s %s %s %s" %
63
+ # [RUBY_FORGE_GROUPID, RUBY_FORGE_PACKAGEID, RELEASE_NAME, file])
64
+ end
65
+ end
66
+
67
+ task :test => [:markdown_span_tests, :markdown_block_tests]
68
+
69
+ task :markdown_block_tests do
70
+ tests = Dir['tests/unittest/**/*.md'].join(' ')
71
+ puts "Executing tests #{tests}"
72
+ # ok = marutest(tests)
73
+ ok = system "ruby -Ilib bin/marutest #{tests}"
74
+ raise "Failed block unittest" if not ok
75
+ end
76
+
77
+ task :markdown_span_tests do
78
+ ok = system( "ruby -Ilib lib/maruku/tests/new_parser.rb v b")
79
+ raise "Failed span unittest" if not ok
80
+ end
81
+
82
+ require 'rake/rdoctask'
83
+
84
+ Rake::RDocTask.new do |rdoc|
85
+ files = [#'README', 'LICENSE', 'COPYING',
86
+ 'lib/**/*.rb',
87
+ 'rdoc/*.rdoc'#, 'test/*.rb'
88
+ ]
89
+ rdoc.rdoc_files.add(files)
90
+ rdoc.main = "rdoc/main.rdoc" # page to start on
91
+ rdoc.title = "Maruku Documentation"
92
+ rdoc.template = "jamis.rb"
93
+ rdoc.rdoc_dir = 'doc' # rdoc output folder
94
+ rdoc.options << '--line-numbers' << '--inline-source'
95
+ end
96
+
97
+ desc "Generate a gemspec file for GitHub"
98
+ task :gemspec do
99
+ File.open("#{spec.name}.gemspec", 'w') do |f|
100
+ f.write spec.to_ruby
101
+ end
102
+ end
data/bin/marudown ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'maruku'
4
+
5
+ # If we are given filenames, convert each file
6
+ if not ARGV.empty?
7
+ ARGV.each do |f|
8
+ puts "Opening #{f}"
9
+
10
+ # read file content
11
+ input = File.open(f,'r').read
12
+
13
+ # create Maruku
14
+ doc = Maruku.new(input, {:on_error=>:warning})
15
+ # convert to a complete html document
16
+ output = doc.to_md
17
+
18
+ # write to file
19
+ dir = File.dirname(f)
20
+ filename = File.basename(f, File.extname(f)) + ".txt"
21
+
22
+ output = File.join(dir, filename)
23
+ File.open(output,'w') do |f| f.puts html end
24
+ end
25
+ else
26
+ # else, act as a filter
27
+ data = $stdin.read
28
+ puts Maruku.new(data, {:on_error=>:warning}).to_md
29
+ end
data/bin/maruku ADDED
@@ -0,0 +1,181 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'maruku'
4
+ require 'optparse'
5
+
6
+
7
+ def cli_puts(s)
8
+ $stderr.puts(s) if MaRuKu::Globals[:verbose]
9
+ end
10
+
11
+
12
+ export = :html
13
+ break_on_error = false
14
+ using_math = false
15
+ using_mathml = false
16
+ output_file = nil
17
+
18
+ opt = OptionParser.new do |opts|
19
+ opts.banner = "Usage: maruku [options] [file1.md [file2.md ..."
20
+
21
+ opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
22
+ MaRuKu::Globals[:verbose] = v end
23
+ opts.on("-u", "--[no-]unsafe", "Use unsafe features") do |v|
24
+ MaRuKu::Globals[:unsafe_features] = v end
25
+
26
+ opts.on("-b", "Break on error") do |v|
27
+ break_on_error = true end
28
+
29
+
30
+ opts.on("-i", "--math-images ENGINE", "Uses ENGINE to render TeX to PNG.") do |s|
31
+ using_math = true
32
+ MaRuKu::Globals[:html_math_output_png] = true
33
+ MaRuKu::Globals[:html_math_output_mathml] = false
34
+ MaRuKu::Globals[:html_png_engine] = s
35
+ cli_puts "Using png engine #{s}."
36
+ end
37
+
38
+ opts.on("-m", "--math-engine ENGINE", "Uses ENGINE to render MathML") do |s|
39
+ MaRuKu::Globals[:html_math_output_png] = false
40
+ MaRuKu::Globals[:html_math_output_mathml] = true
41
+ using_math = true
42
+ using_mathml = true
43
+ MaRuKu::Globals[:html_math_engine] = s
44
+ end
45
+
46
+ opts.on("-o", "--output FILE", "Output filename") do |s|
47
+ output_file = s
48
+ end
49
+
50
+ opts.on_tail("--pdf", "Write PDF","First writes LaTeX, then calls pdflatex." ) do export = :pdf end
51
+ opts.on_tail("--s5", "Write S5 slideshow") do export = :s5 end
52
+ opts.on_tail("--html", "Write HTML") do export = :html end
53
+ opts.on_tail("--html-frag", "Write the contents of the BODY.") do export = :html_frag end
54
+ opts.on_tail("--tex", "Write LaTeX" ) do export = :tex end
55
+ opts.on_tail("--inspect", "Shows the parsing result" ) do export = :inspect end
56
+
57
+ opts.on_tail("--version", "Show version") do
58
+ puts "Maruku #{MaRuKu::Version}"; exit
59
+ end
60
+
61
+ opts.on_tail("--ext EXTENSIONS", "Use maruku extensions (comma separated)" ) do |s|
62
+ s.split(",").each do |e| require "maruku/ext/#{e}"; end
63
+ end
64
+
65
+ opts.on_tail("-h", "--help", "Show this message") do
66
+ puts opts
67
+ exit
68
+ end
69
+
70
+ end
71
+
72
+ begin
73
+ opt.parse!
74
+ rescue OptionParser::InvalidOption=>e
75
+ $stderr.puts e
76
+ $stderr.puts opt
77
+ exit
78
+ end
79
+
80
+
81
+ if using_math
82
+ cli_puts "Using Math extensions."
83
+ require 'maruku/ext/math'
84
+ end
85
+
86
+ #p ARGV
87
+ #p MaRuKu::Globals
88
+
89
+
90
+ inputs =
91
+ # If we are given filenames, convert each file
92
+ if not ARGV.empty?
93
+ ARGV.map do |f|
94
+ # read file content
95
+ cli_puts "Reading from file #{f.inspect}."
96
+ [f, File.open(f,'r').read]
97
+ end
98
+ else
99
+ export = :html_frag if export == :html
100
+ export = :tex_frag if export == :tex
101
+
102
+ cli_puts "Reading from standard input."
103
+ [[nil, $stdin.read]]
104
+ end
105
+
106
+ inputs.each do |f, input|
107
+
108
+ # create Maruku
109
+ params = {}
110
+ params[:on_error] = break_on_error ? :raise : :warning
111
+
112
+ t = Time.now
113
+ doc = Maruku.new(input, params)
114
+
115
+ cli_puts ("Parsing in %.2f seconds." % (Time.now-t))
116
+
117
+ out=""; suffix = "?"
118
+ t = Time.now
119
+ case export
120
+ when :html
121
+ suffix = using_mathml ? '.xhtml' : '.html'
122
+ out = doc.to_html_document( {:indent => -1})
123
+ when :html_frag
124
+ suffix='.html_frag'
125
+ out = doc.to_html( {:indent => -1})
126
+ when :pdf, :tex
127
+ suffix='.tex'
128
+ out = doc.to_latex_document
129
+ when :tex_frag
130
+ suffix='.tex_frag'
131
+ out = doc.to_latex
132
+ when :inspect
133
+ suffix='.txt'
134
+ out = doc.inspect
135
+ when :markdown
136
+ suffix='.pretty_md'
137
+ out = doc.to_markdown
138
+ when :s5
139
+ suffix='_s5slides.html'
140
+ out = doc.to_s5({:content_only => false})
141
+ end
142
+
143
+ cli_puts("Rendering in %.2f seconds." % (Time.now-t))
144
+
145
+ # write to file or stdout
146
+ if f
147
+
148
+ if not output_file
149
+ dir = File.dirname(f)
150
+ job = File.join(dir, File.basename(f, File.extname(f)))
151
+ output_file = job + suffix
152
+ else
153
+ job = File.basename(output_file, File.extname(output_file))
154
+ end
155
+
156
+ if output_file == "-"
157
+ cli_puts "Writing to standard output"
158
+ $stdout.puts out
159
+ else
160
+
161
+ if not (export == :pdf)
162
+ cli_puts "Writing to #{output_file}"
163
+ File.open(output_file,'w') do |f| f.puts out end
164
+ else
165
+ cli_puts "Writing to #{job}.tex"
166
+ File.open("#{job}.tex",'w') do |f| f.puts out end
167
+ cmd = "pdflatex '#{job}.tex' -interaction=nonstopmode "+
168
+ "'-output-directory=#{dir}' "
169
+ cli_puts "maruku: executing $ #{cmd}"
170
+ # run twice for cross references
171
+ system cmd
172
+ system cmd
173
+ end
174
+
175
+ end
176
+ else # write to stdout
177
+ cli_puts "Writing to standard output"
178
+ $stdout.puts out
179
+ end
180
+ end
181
+
data/bin/marutest ADDED
@@ -0,0 +1,345 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'maruku'
4
+ require 'maruku/textile2'
5
+
6
+ $marutest_language = :markdown
7
+
8
+ #MARKER = "\n***EOF***\n"
9
+ SPLIT = %r{\n\*\*\*[^\*]+\*\*\*\n}m
10
+
11
+ def marker(x)
12
+ "\n*** Output of #{x} ***\n"
13
+ end
14
+
15
+ def write_lines(i, j, lines, prefix, i_star)
16
+ i = [i, 0].max
17
+ j = [j, lines.size-1].min
18
+ for a in i..j
19
+ l = lines[a].gsub(/\t/,' ')
20
+ puts( ("%s %3d" % [prefix, a]) +
21
+ (a==i_star ? " -->" : " ")+lines[a])
22
+ end
23
+ end
24
+
25
+ # a = expected b = found
26
+ def equals(a, b)
27
+ a = a.split("\n")
28
+ b = b.split("\n")
29
+
30
+ for i in 0..([a.size-1,b.size-1].max)
31
+ la = a[i]
32
+ lb = b[i]
33
+ if la != lb
34
+ puts "\n"
35
+
36
+
37
+ write_lines(i-3, i+3, a, "expected", i )
38
+ write_lines(i-3, i+3, b, " found", i )
39
+ return false
40
+ end
41
+ end
42
+ return true
43
+ end
44
+
45
+ TOTEST = [:inspect,:to_html,:to_latex,:to_md,:to_s]
46
+
47
+ def run_test(filename, its_ok, verbose=true)
48
+ # read file content
49
+ input = (f=File.open(filename,'r')).read; f.close
50
+
51
+ output_html = File.join(File.dirname(filename),
52
+ File.basename(filename, File.extname(filename)) + ".html")
53
+
54
+ # split the input in sections
55
+
56
+ stuff = input.split(SPLIT)
57
+ if stuff.size == 1
58
+ stuff[2] = stuff[0]
59
+ stuff[0] = "Write a comment here"
60
+ stuff[1] = "{} # params "
61
+ end
62
+
63
+ comment = stuff.shift
64
+ params_s = stuff.shift
65
+
66
+ params = eval(params_s||'{}')
67
+ if params == nil
68
+ raise "Null params? #{params_s.inspect}"
69
+ end
70
+
71
+ markdown = stuff.shift
72
+
73
+ # puts "comment: #{markdown.inspect}"
74
+ # puts "markdown: #{markdown.inspect}"
75
+
76
+ failed = []
77
+ relaxed = []
78
+ crashed = []
79
+ actual = {}
80
+
81
+ doc =
82
+ if $marutest_language == :markdown
83
+ Maruku.new(markdown, params)
84
+ else
85
+ MaRuKu.textile2(markdown, params)
86
+ end
87
+
88
+ for s in TOTEST
89
+ begin
90
+ if s==:to_html
91
+ actual[s] = doc.to_html
92
+ else
93
+ actual[s] = doc.send s
94
+ raise "Methods #{s} gave nil" if not actual[s]
95
+ end
96
+ rescue Exception => e
97
+ crashed << s
98
+ actual[s] = e.inspect+ "\n"+ e.backtrace.join("\n")
99
+ puts actual[s]
100
+ end
101
+ end
102
+
103
+ File.open(output_html, 'w') do |f|
104
+ f.write doc.to_html_document
105
+ end
106
+
107
+ begin
108
+ m = Maruku.new
109
+ d = m.instance_eval(actual[:inspect])
110
+ rescue Exception => e
111
+ s = e.inspect + e.backtrace.join("\n")
112
+ raise "Inspect is not correct:\n ========\n#{actual[:inspect]}"+
113
+ "============\n #{s}"
114
+ end
115
+
116
+ expected = {}
117
+ if (stuff.size < TOTEST.size)
118
+ $stdout.write " (first time!) "
119
+ TOTEST.each do |x| expected[x] = actual[x] end
120
+ else
121
+ TOTEST.each_index do |i|
122
+ symbol = TOTEST[i]
123
+ expected[symbol] = stuff[i]
124
+ # puts "symbol: #{symbol.inspect} = #{stuff[i].inspect}"
125
+ end
126
+ end
127
+
128
+ m = Maruku.new
129
+
130
+
131
+ if not its_ok.include? :inspect
132
+ begin
133
+ d = m.instance_eval(expected[:inspect])
134
+ # puts "Eval: #{d.inspect}"
135
+ expected[:inspect] = d.inspect
136
+ rescue Exception => e
137
+ s = e.inspect + e.backtrace.join("\n")
138
+ raise "Cannot eval user-provided string:\n #{expected[:inspect].to_s}"+
139
+ "\n #{s}"
140
+ end
141
+ end
142
+
143
+ # m.instance_eval(actual[:inspect]) != m.instance_eval(expected[:inspect])
144
+
145
+ # actual[:inspect] = m.instance_eval(actual[:inspect])
146
+ # expected[:inspect] = m.instance_eval(expected[:inspect])
147
+
148
+
149
+ TOTEST.each do |x|
150
+ expected[x].strip!
151
+ actual[x].strip!
152
+ if not equals(expected[x], actual[x])
153
+ if its_ok.include? x
154
+ expected[x] = actual[x]
155
+ $stdout.write " relax:#{x} "
156
+ relaxed << x
157
+ else
158
+ actual[x] = "-----| WARNING | -----\n" + actual[x].to_s
159
+ failed << x
160
+ end
161
+ end
162
+ end
163
+
164
+ f = File.open(filename, 'w')
165
+
166
+ f.write comment
167
+ f.write "\n*** Parameters: ***\n"
168
+ f.write params_s
169
+ f.write "\n*** Markdown input: ***\n"
170
+ f.write markdown
171
+
172
+ TOTEST.each do |x|
173
+ f.write marker(x)
174
+ f.write expected[x]
175
+ end
176
+ f.write "\n*** EOF ***\n"
177
+
178
+ if not failed.empty? or not crashed.empty?
179
+
180
+ f.puts "\n\n\n\nFailed tests: #{failed.inspect} \n"
181
+
182
+ TOTEST.each do |x|
183
+ f.write marker(x)
184
+ f.write actual[x]
185
+ end
186
+
187
+ else
188
+ f.puts "\n\n\n\tOK!\n\n\n"
189
+ end
190
+
191
+
192
+ if false
193
+ md_pl = markdown_pl(markdown)
194
+
195
+ f.write "\n*** Output of Markdown.pl ***\n"
196
+ f.write md_pl
197
+
198
+ f.write "\n*** Output of Markdown.pl (parsed) ***\n"
199
+ begin
200
+ doc = REXML::Document.new("<div>#{md_pl}</div>",{
201
+ :compress_whitespace=>['div','p'],
202
+ :ignore_whitespace_nodes=>['div','p'],
203
+ :respect_whitespace=>['pre','code']
204
+ })
205
+ div = doc.root
206
+ xml =""
207
+ div.write_children(xml,indent=1,transitive=true,ie_hack=false)
208
+ f.write xml
209
+ rescue Exception=>e
210
+ f.puts "Error: #{e.inspect}"
211
+ end
212
+ f.close
213
+ else
214
+ f.write "\n*** Output of Markdown.pl ***\n"
215
+ f.write "(not used anymore)"
216
+
217
+ f.write "\n*** Output of Markdown.pl (parsed) ***\n"
218
+ f.write "(not used anymore)"
219
+ end
220
+
221
+ return failed, relaxed, crashed
222
+ end
223
+
224
+ def markdown_pl(markdown)
225
+ tmp1 = "/tmp/marutest1"
226
+ tmp2 = "/tmp/marutest2"
227
+ File.open(tmp1,'w') do |f| f.puts markdown end
228
+ system "Markdown.pl < #{tmp1} > #{tmp2}"
229
+ f = File.open(tmp2,'r')
230
+ s = f.read
231
+ f.close
232
+ s
233
+ end
234
+
235
+ def passed?(args, arg)
236
+ if args.include? arg
237
+ args.delete arg
238
+ true
239
+ else
240
+ false
241
+ end
242
+ end
243
+
244
+ def marutest(args)
245
+ dont_worry = []
246
+ TOTEST.each do |x|
247
+ arg = "ok:#{x}"
248
+ # puts arg
249
+ if passed?(args, arg)
250
+ dont_worry << x
251
+ end
252
+ end
253
+
254
+ if passed?(args, 'ok')
255
+ dont_worry = TOTEST.clone
256
+ end
257
+
258
+ if dont_worry.size > 0
259
+ puts "Relaxed on #{dont_worry.inspect}"
260
+ end
261
+
262
+
263
+ failed = {}
264
+ relaxed = {}
265
+
266
+ args.each do |f|
267
+ $stdout.write f + ' '*(50-f.size) + " "
268
+ $stdout.flush
269
+ tf, tr, tcrashed = run_test(f, dont_worry)
270
+
271
+ tf = tf + tcrashed
272
+
273
+
274
+ if tr.size > 0
275
+ $stdout.write " relax #{tr.inspect} "
276
+ end
277
+
278
+ if tf.size>0
279
+ $stdout.write " failed on #{tf.inspect} "
280
+ else
281
+ $stdout.write " OK "
282
+ end
283
+
284
+ if tcrashed.size > 0
285
+ $stdout.write " CRASHED on #{tcrashed.inspect}"
286
+ end
287
+
288
+ $stdout.write "\n"
289
+
290
+ failed[f] = tf
291
+ relaxed[f] = tr
292
+ end
293
+
294
+ num_failed = 0
295
+ failed_cat = {}
296
+
297
+ puts "\n\n\n**** FINAL REPORT ****\n\n"
298
+
299
+
300
+ if failed.size > 0
301
+ failed.each do |file, fl|
302
+ num_failed += fl.size
303
+ if fl.size > 0
304
+ puts "\t#{file}\tfailed on #{fl.inspect}"
305
+ end
306
+ fl.each do |x|
307
+ failed_cat[x] = failed_cat[x] || 0
308
+ failed_cat[x] = failed_cat[x] + 1
309
+ end
310
+ end
311
+ end
312
+
313
+ if dont_worry.size > 0
314
+ puts "Relaxed on #{dont_worry.inspect}"
315
+ end
316
+
317
+ if relaxed.size > 0
318
+ relaxed.each do |file, r|
319
+ if r.size > 0
320
+ puts "\t#{file}\t\trelaxed on #{r.inspect}"
321
+ end
322
+ end
323
+ end
324
+
325
+ if failed_cat.size > 0
326
+ puts "\nCategories:\n"
327
+
328
+ failed_cat.each do |x, num|
329
+ puts "\t#{x.inspect} \tfailed #{num}/#{args.size}"
330
+ end
331
+ end
332
+
333
+ return num_failed == 0
334
+ end
335
+
336
+ if File.basename(__FILE__) == 'marutest'
337
+ if ARGV.empty?
338
+ puts "marutest is a tool for running Maruku's unittest."
339
+ exit 1
340
+ end
341
+ ok = marutest(ARGV.clone)
342
+
343
+ exit ok ? 0 : -1
344
+ end
345
+