maiku 0.6.1.maiku

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 (44) hide show
  1. data/lib/maruku.rb +141 -0
  2. data/lib/maruku/attributes.rb +175 -0
  3. data/lib/maruku/defaults.rb +71 -0
  4. data/lib/maruku/errors_management.rb +92 -0
  5. data/lib/maruku/ext/div.rb +133 -0
  6. data/lib/maruku/ext/math.rb +41 -0
  7. data/lib/maruku/ext/math/elements.rb +27 -0
  8. data/lib/maruku/ext/math/latex_fix.rb +12 -0
  9. data/lib/maruku/ext/math/mathml_engines/blahtex.rb +107 -0
  10. data/lib/maruku/ext/math/mathml_engines/itex2mml.rb +29 -0
  11. data/lib/maruku/ext/math/mathml_engines/none.rb +20 -0
  12. data/lib/maruku/ext/math/mathml_engines/ritex.rb +24 -0
  13. data/lib/maruku/ext/math/parsing.rb +119 -0
  14. data/lib/maruku/ext/math/to_html.rb +187 -0
  15. data/lib/maruku/ext/math/to_latex.rb +26 -0
  16. data/lib/maruku/helpers.rb +260 -0
  17. data/lib/maruku/input/charsource.rb +326 -0
  18. data/lib/maruku/input/extensions.rb +69 -0
  19. data/lib/maruku/input/html_helper.rb +189 -0
  20. data/lib/maruku/input/linesource.rb +111 -0
  21. data/lib/maruku/input/parse_block.rb +616 -0
  22. data/lib/maruku/input/parse_doc.rb +232 -0
  23. data/lib/maruku/input/parse_span_better.rb +746 -0
  24. data/lib/maruku/input/rubypants.rb +225 -0
  25. data/lib/maruku/input/type_detection.rb +147 -0
  26. data/lib/maruku/input_textile2/t2_parser.rb +163 -0
  27. data/lib/maruku/maruku.rb +33 -0
  28. data/lib/maruku/output/s5/fancy.rb +756 -0
  29. data/lib/maruku/output/s5/to_s5.rb +138 -0
  30. data/lib/maruku/output/to_html.rb +991 -0
  31. data/lib/maruku/output/to_latex.rb +590 -0
  32. data/lib/maruku/output/to_latex_entities.rb +367 -0
  33. data/lib/maruku/output/to_latex_strings.rb +64 -0
  34. data/lib/maruku/output/to_markdown.rb +164 -0
  35. data/lib/maruku/output/to_s.rb +56 -0
  36. data/lib/maruku/string_utils.rb +201 -0
  37. data/lib/maruku/structures.rb +167 -0
  38. data/lib/maruku/structures_inspect.rb +87 -0
  39. data/lib/maruku/structures_iterators.rb +61 -0
  40. data/lib/maruku/textile2.rb +1 -0
  41. data/lib/maruku/toc.rb +199 -0
  42. data/lib/maruku/usage/example1.rb +33 -0
  43. data/lib/maruku/version.rb +39 -0
  44. metadata +167 -0
data/lib/maruku.rb ADDED
@@ -0,0 +1,141 @@
1
+ #--
2
+ # Copyright (C) 2006 Andrea Censi <andrea (at) rubyforge.org>
3
+ #
4
+ # This file is part of Maruku.
5
+ #
6
+ # Maruku is free software; you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation; either version 2 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Maruku is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Maruku; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
+ #++
20
+
21
+ require 'rexml/document'
22
+
23
+ # :include:MaRuKu.txt
24
+ module MaRuKu
25
+
26
+ module In
27
+ module Markdown
28
+ module SpanLevelParser; end
29
+ module BlockLevelParser; end
30
+ end
31
+ # more to come?
32
+ end
33
+
34
+ module Out
35
+ # Functions for exporting to MarkDown.
36
+ module Markdown; end
37
+ # Functions for exporting to HTML.
38
+ module HTML; end
39
+ # Functions for exporting to Latex
40
+ module Latex; end
41
+ end
42
+
43
+ # These are strings utilities.
44
+ module Strings; end
45
+
46
+ module Helpers; end
47
+
48
+ module Errors; end
49
+
50
+ class MDElement
51
+ include REXML
52
+ include MaRuKu
53
+ include Out::Markdown
54
+ include Out::HTML
55
+ include Out::Latex
56
+ include Strings
57
+ include Helpers
58
+ include Errors
59
+ end
60
+
61
+
62
+ class MDDocument < MDElement
63
+ include In::Markdown
64
+ include In::Markdown::SpanLevelParser
65
+ include In::Markdown::BlockLevelParser
66
+ end
67
+ end
68
+
69
+ # This is the public interface
70
+ class Maruku < MaRuKu::MDDocument; end
71
+
72
+
73
+
74
+ require 'rexml/document'
75
+
76
+ # Structures definition
77
+ require 'maruku/structures'
78
+ require 'maruku/structures_inspect'
79
+
80
+ require 'maruku/defaults'
81
+ # Less typing
82
+ require 'maruku/helpers'
83
+
84
+ # Code for parsing whole Markdown documents
85
+ require 'maruku/input/parse_doc'
86
+
87
+ # Ugly things kept in a closet
88
+ require 'maruku/string_utils'
89
+ require 'maruku/input/linesource'
90
+ require 'maruku/input/type_detection'
91
+
92
+ # A class for reading and sanitizing inline HTML
93
+ require 'maruku/input/html_helper'
94
+
95
+ # Code for parsing Markdown block-level elements
96
+ require 'maruku/input/parse_block'
97
+
98
+ # Code for parsing Markdown span-level elements
99
+ require 'maruku/input/charsource'
100
+ require 'maruku/input/parse_span_better'
101
+ require 'maruku/input/rubypants'
102
+
103
+ require 'maruku/input/extensions'
104
+
105
+ require 'maruku/attributes'
106
+
107
+ require 'maruku/structures_iterators'
108
+
109
+ require 'maruku/errors_management'
110
+
111
+ # Code for creating a table of contents
112
+ require 'maruku/toc'
113
+
114
+ # Support for div Markdown extension
115
+ require 'maruku/ext/div'
116
+
117
+ # Version and URL
118
+ require 'maruku/version'
119
+
120
+
121
+ # Exporting to html
122
+ require 'maruku/output/to_html'
123
+
124
+ # Exporting to latex
125
+ require 'maruku/output/to_latex'
126
+ require 'maruku/output/to_latex_strings'
127
+ require 'maruku/output/to_latex_entities'
128
+
129
+ # Pretty print
130
+ require 'maruku/output/to_markdown'
131
+
132
+ # S5 slides
133
+ require 'maruku/output/s5/to_s5'
134
+ require 'maruku/output/s5/fancy'
135
+
136
+ # Exporting to text: strips all formatting (not complete)
137
+ require 'maruku/output/to_s'
138
+
139
+ # class Maruku is the global interface
140
+ require 'maruku/maruku'
141
+
@@ -0,0 +1,175 @@
1
+ #--
2
+ # Copyright (C) 2006 Andrea Censi <andrea (at) rubyforge.org>
3
+ #
4
+ # This file is part of Maruku.
5
+ #
6
+ # Maruku is free software; you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation; either version 2 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Maruku is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Maruku; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
+ #++
20
+
21
+
22
+ class String
23
+ def quote_if_needed
24
+ if /[\s\'\"]/.match self
25
+ inspect
26
+ else
27
+ self
28
+ end
29
+ end
30
+ end
31
+
32
+ module MaRuKu;
33
+ MagicChar = ':'
34
+
35
+ class AttributeList < Array
36
+
37
+ # An attribute list becomes
38
+ # {#id .cl key="val" ref}
39
+ # [ [:id, 'id'], [:class, 'id'], ['key', 'val'], [ :ref, 'ref' ]]
40
+
41
+ private :push
42
+
43
+ def push_key_val(key, val);
44
+ raise "Bad #{key.inspect}=#{val.inspect}" if not key and val
45
+ push [key, val]
46
+ end
47
+ def push_ref(ref_id);
48
+
49
+ raise "Bad :ref #{ref_id.inspect}" if not ref_id
50
+ push [:ref, ref_id+""]
51
+
52
+ # p "Now ", self ########################################
53
+ end
54
+ def push_class(val);
55
+ raise "Bad :id #{val.inspect}" if not val
56
+ push [:class, val]
57
+ end
58
+ def push_id(val);
59
+ raise "Bad :id #{val.inspect}" if not val
60
+ push [:id, val]
61
+ end
62
+
63
+ def to_s
64
+ map do |k,v|
65
+ case k
66
+ when :id; "#" + v.quote_if_needed
67
+ when :class; "." + v.quote_if_needed
68
+ when :ref; v.quote_if_needed
69
+ else k.quote_if_needed + "=" + v.quote_if_needed
70
+ end
71
+ end . join(' ')
72
+ end
73
+ alias to_md to_s
74
+ end
75
+
76
+ end
77
+
78
+ module MaRuKu; module In; module Markdown; module SpanLevelParser
79
+
80
+ def md_al(s=[]); AttributeList.new(s) end
81
+
82
+ # returns nil or an AttributeList
83
+ def read_attribute_list(src, con, break_on_chars)
84
+
85
+ separators = break_on_chars + [?=,?\ ,?\t]
86
+ escaped = Maruku::EscapedCharInQuotes
87
+
88
+ al = AttributeList.new
89
+ while true
90
+ src.consume_whitespace
91
+ break if break_on_chars.include? src.cur_char
92
+
93
+ case src.cur_char
94
+ when nil
95
+ maruku_error "Attribute list terminated by EOF:\n "+
96
+ "#{al.inspect}" , src, con
97
+ tell_user "I try to continue and return partial attribute list:\n"+
98
+ al.inspect
99
+ break
100
+ when ?= # error
101
+ maruku_error "In attribute lists, cannot start identifier with `=`."
102
+ tell_user "I try to continue"
103
+ src.ignore_char
104
+ when ?# # id definition
105
+ src.ignore_char
106
+ if id = read_quoted_or_unquoted(src, con, escaped, separators)
107
+ al.push_id id
108
+ else
109
+ maruku_error 'Could not read `id` attribute.', src, con
110
+ tell_user 'Trying to ignore bad `id` attribute.'
111
+ end
112
+ when ?. # class definition
113
+ src.ignore_char
114
+ if klass = read_quoted_or_unquoted(src, con, escaped, separators)
115
+ al.push_class klass
116
+ else
117
+ maruku_error 'Could not read `class` attribute.', src, con
118
+ tell_user 'Trying to ignore bad `class` attribute.'
119
+ end
120
+ else
121
+ if key = read_quoted_or_unquoted(src, con, escaped, separators)
122
+ if src.cur_char == ?=
123
+ src.ignore_char # skip the =
124
+ if val = read_quoted_or_unquoted(src, con, escaped, separators)
125
+ al.push_key_val(key, val)
126
+ else
127
+ maruku_error "Could not read value for key #{key.inspect}.",
128
+ src, con
129
+ tell_user "Ignoring key #{key.inspect}."
130
+ end
131
+ else
132
+ al.push_ref key
133
+ end
134
+ else
135
+ maruku_error 'Could not read key or reference.'
136
+ end
137
+ end # case
138
+ end # while true
139
+ al
140
+ end
141
+
142
+
143
+ # We need a helper
144
+ def is_ial(e); e.kind_of? MDElement and e.node_type == :ial end
145
+
146
+ def merge_ial(elements, src, con)
147
+
148
+ # Apply each IAL to the element before
149
+ elements.each_with_index do |e, i|
150
+ if is_ial(e) && i>= 1 then
151
+ before = elements[i-1]
152
+ after = elements[i+1]
153
+ if before.kind_of? MDElement
154
+ before.al = e.ial
155
+ elsif after.kind_of? MDElement
156
+ after.al = e.ial
157
+ else
158
+ maruku_error "It is not clear to me what element this IAL {:#{e.ial.to_md}} \n"+
159
+ "is referring to. The element before is a #{before.class.to_s}, \n"+
160
+ "the element after is a #{after.class.to_s}.\n"+
161
+ "\n before: #{before.inspect}"+
162
+ "\n after: #{after.inspect}",
163
+ src, con
164
+ # xxx dire se c'è empty vicino
165
+ end
166
+ end
167
+ end
168
+
169
+ if not Globals[:debug_keep_ials]
170
+ elements.delete_if {|x| is_ial(x) unless x == elements.first}
171
+ end
172
+ end
173
+
174
+ end end end end
175
+ #module MaRuKu; module In; module Markdown; module SpanLevelParser
@@ -0,0 +1,71 @@
1
+ #--
2
+ # Copyright (C) 2006 Andrea Censi <andrea (at) rubyforge.org>
3
+ #
4
+ # This file is part of Maruku.
5
+ #
6
+ # Maruku is free software; you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation; either version 2 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Maruku is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Maruku; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
+ #++
20
+
21
+
22
+ module MaRuKu
23
+
24
+ Globals = {
25
+ :unsafe_features => false,
26
+ :on_error => :warning,
27
+
28
+
29
+ :use_numbered_headers => false,
30
+
31
+ :maruku_signature => false,
32
+ :code_background_color => '#fef',
33
+ :code_show_spaces => false,
34
+
35
+ :filter_html => false,
36
+
37
+ :html_math_output_mathml => true, # also set :html_math_engine
38
+ :html_math_engine => 'none', #ritex, itex2mml
39
+
40
+ :html_math_output_png => false,
41
+ :html_png_engine => 'none',
42
+ :html_png_dir => 'pngs',
43
+ :html_png_url => 'pngs/',
44
+ :html_png_resolution => 200,
45
+
46
+ :html_use_syntax => false,
47
+
48
+ :latex_use_listings => false,
49
+ :latex_cjk => false,
50
+ :latex_cache_file => "blahtex_cache.pstore", # cache file for blahtex filter
51
+
52
+ :debug_keep_ials => false,
53
+ :doc_prefix => ''
54
+ }
55
+
56
+ class MDElement
57
+ def get_setting(sym)
58
+ if self.attributes.has_key?(sym) then
59
+ return self.attributes[sym]
60
+ elsif self.doc && self.doc.attributes.has_key?(sym) then
61
+ return self.doc.attributes[sym]
62
+ elsif MaRuKu::Globals.has_key?(sym)
63
+ return MaRuKu::Globals[sym]
64
+ else
65
+ $stderr.puts "Bug: no default for #{sym.inspect}"
66
+ nil
67
+ end
68
+ end
69
+ end
70
+
71
+ end
@@ -0,0 +1,92 @@
1
+ #--
2
+ # Copyright (C) 2006 Andrea Censi <andrea (at) rubyforge.org>
3
+ #
4
+ # This file is part of Maruku.
5
+ #
6
+ # Maruku is free software; you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation; either version 2 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Maruku is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Maruku; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
+ #++
20
+
21
+
22
+
23
+ #m Any method that detects formatting error calls the
24
+ #m maruku_error() method.
25
+ #m if @meta[:on_error] ==
26
+ #m
27
+ #m - :warning write on the standard err (or @error_stream if defined),
28
+ #m then do your best.
29
+ #m - :ignore be shy and try to continue
30
+ #m - :raise raises a MarukuException
31
+ #m
32
+ #m default is :raise
33
+
34
+ module MaRuKu
35
+
36
+ class Exception < RuntimeError
37
+ end
38
+
39
+ module Errors
40
+
41
+ def maruku_error(s,src=nil,con=nil)
42
+ policy = get_setting(:on_error)
43
+
44
+ case policy
45
+ when :ignore
46
+ when :raise
47
+ raise_error create_frame(describe_error(s,src,con))
48
+ when :warning
49
+ tell_user create_frame(describe_error(s,src,con))
50
+ else
51
+ raise "BugBug: policy = #{policy.inspect}"
52
+ end
53
+ end
54
+
55
+ def maruku_recover(s,src=nil,con=nil)
56
+ tell_user create_frame(describe_error(s,src,con))
57
+ end
58
+
59
+ alias error maruku_error
60
+
61
+ def raise_error(s)
62
+ raise MaRuKu::Exception, s, caller
63
+ end
64
+
65
+ def tell_user(s)
66
+ error_stream = self.attributes[:error_stream] || $stderr
67
+ error_stream << s
68
+ end
69
+
70
+ def create_frame(s)
71
+ n = 75
72
+ "\n" +
73
+ " "+"_"*n + "\n"+
74
+ "| Maruku tells you:\n" +
75
+ "+" + ("-"*n) +"\n"+
76
+ add_tabs(s,1,'| ') + "\n" +
77
+ "+" + ("-"*n) + "\n" +
78
+ add_tabs(caller[0, 5].join("\n"),1,'!') + "\n" +
79
+ "\\" + ("_"*n) + "\n"
80
+ end
81
+
82
+ def describe_error(s,src,con)
83
+ t = s
84
+ src && (t += "\n#{src.describe}\n")
85
+ con && (t += "\n#{con.describe}\n")
86
+ t
87
+ end
88
+
89
+ end # Errors
90
+ end # MaRuKu
91
+
92
+