maruku 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/docs/markdown_syntax.md +9 -21
  5. data/lib/maruku/defaults.rb +1 -1
  6. data/lib/maruku/element.rb +18 -3
  7. data/lib/maruku/ext/fenced_code.rb +1 -1
  8. data/lib/maruku/ext/math/mathml_engines/blahtex.rb +1 -1
  9. data/lib/maruku/ext/math/to_html.rb +2 -9
  10. data/lib/maruku/html.rb +5 -8
  11. data/lib/maruku/input/html_helper.rb +94 -81
  12. data/lib/maruku/input/mdline.rb +6 -4
  13. data/lib/maruku/input/parse_block.rb +145 -37
  14. data/lib/maruku/input/parse_span.rb +7 -8
  15. data/lib/maruku/input/rubypants.rb +22 -9
  16. data/lib/maruku/maruku.rb +5 -0
  17. data/lib/maruku/output/to_html.rb +15 -6
  18. data/lib/maruku/output/to_latex.rb +9 -3
  19. data/lib/maruku/output/to_s.rb +0 -1
  20. data/lib/maruku/string_utils.rb +2 -2
  21. data/lib/maruku/version.rb +1 -1
  22. data/spec/block_docs/abbrev.md +18 -18
  23. data/spec/block_docs/attribute_sanitize.md +22 -0
  24. data/spec/block_docs/auto_cdata.md +48 -0
  25. data/spec/block_docs/bug_table.md +4 -4
  26. data/spec/block_docs/code4.md +79 -0
  27. data/spec/block_docs/div_without_newline.md +16 -0
  28. data/spec/block_docs/empty_cells.md +3 -9
  29. data/spec/block_docs/entities.md +6 -12
  30. data/spec/block_docs/extra_table1.md +6 -6
  31. data/spec/block_docs/fenced_code_blocks.md +12 -20
  32. data/spec/block_docs/fenced_code_blocks_highlighted.md +1 -2
  33. data/spec/block_docs/footnotes2.md +4 -1
  34. data/spec/block_docs/ignore_bad_header.md +9 -0
  35. data/spec/block_docs/issue106.md +78 -0
  36. data/spec/block_docs/issue115.md +20 -0
  37. data/spec/block_docs/issue117.md +13 -0
  38. data/spec/block_docs/issue120.md +48 -0
  39. data/spec/block_docs/issue123.md +11 -0
  40. data/spec/block_docs/issue124.md +16 -0
  41. data/spec/block_docs/issue40.md +24 -12
  42. data/spec/block_docs/issue89.md +1 -1
  43. data/spec/block_docs/lists_nested_blankline.md +14 -8
  44. data/spec/block_docs/lists_ol.md +5 -5
  45. data/spec/block_docs/lists_paraindent.md +6 -11
  46. data/spec/block_docs/math-blahtex/equations.md +12 -13
  47. data/spec/block_docs/math-blahtex/math2.md +9 -2
  48. data/spec/block_docs/math/embedded_invalid_svg.md +31 -2
  49. data/spec/block_docs/math/embedded_svg.md +41 -2
  50. data/spec/block_docs/math/equations.md +7 -2
  51. data/spec/block_docs/math/inline.md +2 -2
  52. data/spec/block_docs/math/math2.md +9 -1
  53. data/spec/block_docs/math/spaces_after_inline_math.md +17 -0
  54. data/spec/block_docs/math/table.md +2 -2
  55. data/spec/block_docs/math/table2.md +6 -6
  56. data/spec/block_docs/table_attributes.md +4 -6
  57. data/spec/block_docs/table_colspan.md +41 -0
  58. data/spec/block_docs/tables.md +10 -21
  59. data/spec/block_docs/tables2.md +74 -0
  60. data/spec/block_docs/xml_comments.md +32 -0
  61. data/spec/span_spec.rb +1 -1
  62. data/spec/spec_helper.rb +1 -0
  63. metadata +42 -28
  64. metadata.gz.sig +3 -3
  65. data/spec/block_docs/xml2.md +0 -19
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bad4cd7fdaa9ab0bb7c14962ca76fcb81bc37813
4
- data.tar.gz: d31cadf635ffcb320049286355d719b54c9b0ea8
3
+ metadata.gz: 683162dc0e147b79e1df24f79949ba8521f4ca68
4
+ data.tar.gz: 6fa761f0e28fa67213236f05c07a97937cd98101
5
5
  SHA512:
6
- metadata.gz: 59c940bc7b4586dfff68b0508f9630ac1cd20efeb87d64a7d555681c243112ca6c980667b2560a3a2e4d2f03b4dd67c15734659e84d94c38ce3cb2a5d0d241a9
7
- data.tar.gz: e7e518850d8fe65a841d621bea1d7ffa1349402162bc6ba92bf078e884f256d8f0ce8bf91e41f5cf53630fae5c183ab296dafb495d89fdbfc4a29a9b5f5f66e4
6
+ metadata.gz: f8d53ba730189a09d616f91ed91635701334454ff114737769f2e5189db2e52c647ca5815bce72c6faa43f83c9c1daf0a1a115cc0a00916c152004c537c8fbeb
7
+ data.tar.gz: 8330490c4b4787cc1bbc27f470f21c985df7f1847db2238732cb48a73e60374a52910e1a4c49c66b6edb995087cf08080cf41b2b2e1a0f2cf740631983edf6e6
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -1,17 +1,6 @@
1
- css: style.css
2
-
3
1
  Markdown: Syntax
4
2
  ================
5
3
 
6
- <ul id="ProjectSubmenu">
7
- <li><a href="/projects/markdown/" title="Markdown Project Page">Main</a></li>
8
- <li><a href="/projects/markdown/basics" title="Markdown Basics">Basics</a></li>
9
- <li><a class="selected" title="Markdown Syntax Documentation">Syntax</a></li>
10
- <li><a href="/projects/markdown/license" title="Pricing and License Information">License</a></li>
11
- <li><a href="/projects/markdown/dingus" title="Online Markdown Web Form">Dingus</a></li>
12
- </ul>
13
-
14
-
15
4
  * [Overview](#overview)
16
5
  * [Philosophy](#philosophy)
17
6
  * [Inline HTML](#html)
@@ -256,7 +245,7 @@ wrap the text and put a `>` before every line:
256
245
  > This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,
257
246
  > consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.
258
247
  > Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.
259
- >
248
+ >
260
249
  > Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
261
250
  > id sem consectetuer libero luctus adipiscing.
262
251
 
@@ -283,12 +272,12 @@ Blockquotes can contain other Markdown elements, including headers, lists,
283
272
  and code blocks:
284
273
 
285
274
  > ## This is a header.
286
- >
275
+ >
287
276
  > 1. This is the first list item.
288
277
  > 2. This is the second list item.
289
- >
278
+ >
290
279
  > Here's some example code:
291
- >
280
+ >
292
281
  > return shell_exec("echo $input | $markdown_script");
293
282
 
294
283
  Any decent text editor should make email-style quoting easy. For
@@ -569,7 +558,7 @@ Will produce:
569
558
  If you're referring to a local resource on the same server, you can
570
559
  use relative paths:
571
560
 
572
- See my [About](/about/) page for details.
561
+ See my [About](/about/) page for details.
573
562
 
574
563
  Reference-style links use a second set of square brackets, inside
575
564
  which you place a label of your choosing to identify the link:
@@ -643,7 +632,7 @@ multiple words in the link text:
643
632
  Visit [Daring Fireball][] for more information.
644
633
 
645
634
  And then define the link:
646
-
635
+
647
636
  [Daring Fireball]: http://daringfireball.net/
648
637
 
649
638
  Link definitions can be placed anywhere in your Markdown document. I
@@ -767,13 +756,13 @@ one after the opening, one before the closing. This allows you to place
767
756
  literal backtick characters at the beginning or end of a code span:
768
757
 
769
758
  A single backtick in a code span: `` ` ``
770
-
759
+
771
760
  A backtick-delimited string in a code span: `` `foo` ``
772
761
 
773
762
  will produce:
774
763
 
775
764
  <p>A single backtick in a code span: <code>`</code></p>
776
-
765
+
777
766
  <p>A backtick-delimited string in a code span: <code>`foo`</code></p>
778
767
 
779
768
  With a code span, ampersands and angle brackets are encoded as HTML
@@ -844,7 +833,7 @@ use regular HTML `<img>` tags.
844
833
  Markdown supports a shortcut style for creating "automatic" links for URLs and email addresses: simply surround the URL or email address with angle brackets. What this means is that if you want to show the actual text of a URL or email address, and also have it be a clickable link, you can do this:
845
834
 
846
835
  <http://example.com/>
847
-
836
+
848
837
  Markdown will turn this into:
849
838
 
850
839
  <a href="http://example.com/">http://example.com/</a>
@@ -896,4 +885,3 @@ Markdown provides backslash escapes for the following characters:
896
885
  - minus sign (hyphen)
897
886
  . dot
898
887
  ! exclamation mark
899
-
@@ -23,7 +23,7 @@ module MaRuKu
23
23
  :html_png_url => 'pngs/',
24
24
  :html_png_resolution => 200,
25
25
 
26
- :fenced_code_blocks => false,
26
+ :fenced_code_blocks => false,
27
27
  :html_use_syntax => false,
28
28
 
29
29
  :latex_use_listings => false,
@@ -82,9 +82,24 @@ module MaRuKu
82
82
  # If `e_node_type` is specified, only yields nodes of that type.
83
83
  def each_element(e_node_type=nil, &block)
84
84
  @children.each do |c|
85
- next unless c.is_a? MDElement
86
- yield c if e_node_type.nil? || c.node_type == e_node_type
87
- c.each_element(e_node_type, &block)
85
+ if c.is_a? MDElement then
86
+ yield c if e_node_type.nil? || c.node_type == e_node_type
87
+ c.each_element(e_node_type, &block)
88
+ #
89
+ # This handles the case where the children of an
90
+ # element are arranged in a multi-dimensional array
91
+ # (as in the case of a table)
92
+ elsif c.is_a? Array then
93
+ c.each do |cc|
94
+ # A recursive call to each_element will ignore the current element
95
+ # so we handle this case inline
96
+ if cc.is_a? MDElement then
97
+ yield cc if e_node_type.nil? || cc.node_type == e_node_type
98
+ cc.each_element(e_node_type, &block)
99
+ end
100
+ end
101
+ end
102
+
88
103
  end
89
104
  end
90
105
 
@@ -90,7 +90,7 @@ MaRuKu::In::Markdown::register_block_extension(
90
90
  al = ial && doc.read_attribute_list(cs.new(inside))
91
91
  end
92
92
 
93
- source = "\n" + lines.join("\n") + "\n"
93
+ source = lines.join("\n")
94
94
  context.push doc.md_codeblock(source, lang, al)
95
95
  true
96
96
  end
@@ -80,7 +80,7 @@ module MaRuKu::Out::HTML
80
80
 
81
81
  # Run blahtex, return output
82
82
  def run_blahtex(tex, args)
83
- IO.popen(['blahtex', *args], 'w+') do |blahtex|
83
+ IO.popen(['blahtex', *args].join(' '), 'w+') do |blahtex|
84
84
  blahtex.write tex
85
85
  blahtex.close_write
86
86
 
@@ -53,7 +53,7 @@ module MaRuKu
53
53
  end
54
54
 
55
55
  # TODO: Warn here
56
- puts "A method called #{method} should be defined."
56
+ raise "A method called #{method} should be defined."
57
57
  convert_to_mathml_none(kind, tex)
58
58
  end
59
59
 
@@ -68,7 +68,7 @@ module MaRuKu
68
68
  method = "convert_to_png_#{engine}".to_sym
69
69
  return self.send(method, kind, tex) if self.respond_to? method
70
70
 
71
- puts "A method called #{method} should be defined."
71
+ raise "A method called #{method} should be defined."
72
72
  nil
73
73
  end
74
74
 
@@ -143,13 +143,6 @@ module MaRuKu
143
143
  end
144
144
  end
145
145
 
146
- source_span = xelem('span')
147
- add_class_to(source_span, 'maruku-eq-tex')
148
- code = convert_to_mathml_none(:equation, self.math.strip)
149
- code['style'] = 'display: none'
150
- source_span << code
151
- div << source_span
152
-
153
146
  div
154
147
  end
155
148
 
@@ -5,7 +5,7 @@ $warned_nokogiri = false
5
5
  module MaRuKu
6
6
  HTML_INLINE_ELEMS = Set.new %w[a abbr acronym audio b bdi bdo big br button canvas caption cite code
7
7
  col colgroup command datalist del details dfn dir em fieldset font form i img input ins
8
- kbd label legend mark meter optgroup option progress q rp rt ruby s samp section select small
8
+ kbd label legend mark meter optgroup option progress q rp rt ruby s samp select small
9
9
  source span strike strong sub summary sup tbody td tfoot th thead time tr track tt u var video wbr
10
10
  animate animateColor animateMotion animateTransform circle clipPath defs desc ellipse
11
11
  feGaussianBlur filter font-face font-face-name font-face-src foreignObject g glyph hkern
@@ -16,7 +16,7 @@ module MaRuKu
16
16
  mtd mtext mtr munder munderover none semantics]
17
17
 
18
18
  # Parse block-level markdown elements in these HTML tags
19
- BLOCK_TAGS = %w(div)
19
+ BLOCK_TAGS = Set.new %w[div section]
20
20
 
21
21
  # This gets mixed into HTML MDElement nodes to hold the parsed document fragment
22
22
  module HTMLElement
@@ -94,7 +94,7 @@ module MaRuKu
94
94
 
95
95
  # Select all text children of e
96
96
  e.xpath("./text()").each do |original_text|
97
- s = CGI.escapeHTML(original_text.text)
97
+ s = MaRuKu::Out::HTML.escapeHTML(original_text.text)
98
98
  unless s.strip.empty?
99
99
  parsed = parse_blocks ? doc.parse_text_as_markdown(s) : doc.parse_span(s)
100
100
 
@@ -172,9 +172,6 @@ module MaRuKu
172
172
  # Process markdown within the contents of some elements and
173
173
  # replace their contents with the processed version.
174
174
  def process_markdown_inside_elements(doc)
175
- # parse block-level markdown elements in these HTML tags
176
- block_tags = ['div']
177
-
178
175
  elts = []
179
176
  @fragment.each_element('//*[@markdown]') do |e|
180
177
  elts << e
@@ -193,11 +190,11 @@ module MaRuKu
193
190
  e.attributes.delete('markdown')
194
191
 
195
192
  next if "0" == how # user requests no markdown parsing inside
196
- parse_blocks = (how == 'block') || block_tags.include?(e.name)
193
+ parse_blocks = (how == 'block') || BLOCK_TAGS.include?(e.name)
197
194
 
198
195
  # Select all text children of e
199
196
  e.texts.each do |original_text|
200
- s = CGI.escapeHTML(original_text.value)
197
+ s = MaRuKu::Out::HTML.escapeHTML(original_text.value)
201
198
  unless s.strip.empty?
202
199
  # TODO extract common functionality
203
200
  parsed = parse_blocks ? doc.parse_text_as_markdown(s) : doc.parse_span(s)
@@ -10,14 +10,10 @@ module MaRuKu::In::Markdown::SpanLevelParser
10
10
  EverythingElse = %r{^[^<]+}m
11
11
  CommentStart = %r{^<!--}x
12
12
  CommentEnd = %r{-->}
13
- TO_SANITIZE = ['img','hr','br']
13
+ TO_SANITIZE = ['img', 'hr', 'br']
14
14
 
15
15
  attr_reader :rest, :first_tag
16
16
 
17
- def my_debug(s)
18
- # puts "---" * 10 + "\n" + inspect + "\t>>>\t" + s
19
- end
20
-
21
17
  def initialize
22
18
  @rest = ""
23
19
  @tag_stack = []
@@ -26,7 +22,7 @@ module MaRuKu::In::Markdown::SpanLevelParser
26
22
  self.state = :inside_element
27
23
  end
28
24
 
29
- attr_accessor :state # = :inside_element, :inside_tag, :inside_comment, :inside_cdata, :inside_script_style
25
+ attr_accessor :state # = :inside_element, :inside_tag, :inside_comment, :inside_cdata
30
26
 
31
27
  def eat_this(line)
32
28
  @rest = line + @rest
@@ -35,40 +31,44 @@ module MaRuKu::In::Markdown::SpanLevelParser
35
31
  case self.state
36
32
  when :inside_comment
37
33
  if @m = CommentEnd.match(@rest)
38
- my_debug "#{@state}: Comment End: #{@m.to_s.inspect}"
39
- @already << @m.pre_match << @m.to_s
34
+ debug_state 'Comment End'
35
+ # Workaround for https://bugs.ruby-lang.org/issues/9277 and another bug in 1.9.2 where even a
36
+ # single dash in a comment will cause REXML to error.
37
+ @already << @m.pre_match.gsub(/-(?![^\-])/, '- ') << @m.to_s
40
38
  @rest = @m.post_match
41
39
  self.state = :inside_element
42
40
  else
43
- @already << @rest
41
+ @already << @rest.gsub(/-(?![^\-])/, '- ') # Workaround for https://bugs.ruby-lang.org/issues/9277
44
42
  @rest = ""
45
43
  self.state = :inside_comment
46
44
  end
47
45
  when :inside_element
48
46
  if @m = CommentStart.match(@rest)
49
- my_debug "#{@state}: Comment: #{@m.to_s.inspect}"
47
+ debug_state 'Comment'
50
48
  things_read += 1
51
49
  @already << @m.pre_match << @m.to_s
52
50
  @rest = @m.post_match
53
51
  self.state = :inside_comment
54
52
  elsif @m = Tag.match(@rest)
55
- my_debug "#{@state}: Tag: #{@m.to_s.inspect}"
53
+ debug_state 'Tag'
56
54
  things_read += 1
57
55
  self.state = :inside_element
58
56
  handle_tag
59
57
  elsif @m = CData.match(@rest)
60
- my_debug "#{@state}: CDATA: #{@m.to_s.inspect}"
61
- @already << @m.pre_match << @m.to_s
58
+ debug_state 'CDATA'
59
+ @already << @m.pre_match
60
+ close_script_style if script_style?
61
+ @already << @m.to_s
62
62
  @rest = @m.post_match
63
63
  self.state = :inside_cdata
64
64
  elsif @m = PartialTag.match(@rest)
65
- my_debug "#{@state}: PartialTag: #{@m.to_s.inspect}"
65
+ debug_state 'PartialTag'
66
66
  @already << @m.pre_match
67
67
  @rest = @m.post_match
68
68
  @partial_tag = @m.to_s
69
69
  self.state = :inside_tag
70
70
  elsif @m = EverythingElse.match(@rest)
71
- my_debug "#{@state}: Everything: #{@m.to_s.inspect}"
71
+ debug_state 'EverythingElse'
72
72
  @already << @m.pre_match << @m.to_s
73
73
  @rest = @m.post_match
74
74
  self.state = :inside_element
@@ -77,12 +77,14 @@ module MaRuKu::In::Markdown::SpanLevelParser
77
77
  end
78
78
  when :inside_tag
79
79
  if @m = /^[^>]*>/.match(@rest)
80
- my_debug "#{@state}: matched #{@m.to_s.inspect}"
81
80
  @partial_tag << @m.to_s
82
- my_debug "#{@state}: matched TOTAL: #{@partial_tag.to_s.inspect}"
83
81
  @rest = @partial_tag + @m.post_match
84
82
  @partial_tag = nil
85
83
  self.state = :inside_element
84
+ if @m = Tag.match(@rest)
85
+ things_read += 1
86
+ handle_tag
87
+ end
86
88
  else
87
89
  @partial_tag << @rest
88
90
  @rest = ""
@@ -90,60 +92,25 @@ module MaRuKu::In::Markdown::SpanLevelParser
90
92
  end
91
93
  when :inside_cdata
92
94
  if @m = CDataEnd.match(@rest)
93
- my_debug "#{@state}: matched #{@m.to_s.inspect}"
95
+ self.state = :inside_element
94
96
  @already << @m.pre_match << @m.to_s
95
97
  @rest = @m.post_match
96
- self.state = %(script style).include?(@tag_stack.last) ? :inside_script_style : :inside_element
98
+ start_script_style if script_style?
97
99
  else
98
100
  @already << @rest
99
101
  @rest = ""
100
102
  self.state = :inside_cdata
101
103
  end
102
- when :inside_script_style
103
- if @m = CData.match(@rest)
104
- if @already.rstrip.end_with?('<![CDATA[')
105
- @already << @m.pre_match
106
- @rest = @m.post_match
107
- else
108
- my_debug "#{@state}: CDATA: #{@m.to_s.inspect}"
109
- @already << @m.pre_match << @m.to_s
110
- @rest = @m.post_match
111
- self.state = :inside_cdata
112
- end
113
- elsif @m = Tag.match(@rest)
114
- is_closing = !!@m[1]
115
- tag = @m[2]
116
- if is_closing && tag == @tag_stack.last
117
- my_debug "#{@state}: matched #{@m.to_s.inspect}"
118
- @already << @m.pre_match
119
- @rest = @m.post_match
120
- # This is necessary to properly parse
121
- # script tags
122
- @already << "]]>" unless @already.rstrip.end_with?("]]>")
123
- self.state = :inside_element
124
- handle_tag false # don't double-add pre_match
125
- else
126
- @already << @rest
127
- @rest = ""
128
- end
129
- elsif @m = EverythingElse.match(@rest)
130
- my_debug "#{@state}: Everything: #{@m.to_s.inspect}"
131
- @already << @m.pre_match << @m.to_s
132
- @rest = @m.post_match
133
- else
134
- @already << @rest
135
- @rest = ""
136
- end
137
104
  else
138
105
  raise "Bug bug: state = #{self.state.inspect}"
139
- end # not inside comment
106
+ end
140
107
 
141
108
  break if is_finished? && things_read > 0
142
109
  end
143
110
  end
144
111
 
145
- def handle_tag(add_pre_match = true)
146
- @already << @m.pre_match if add_pre_match
112
+ def handle_tag
113
+ @already << @m.pre_match
147
114
  @rest = @m.post_match
148
115
 
149
116
  is_closing = !!@m[1]
@@ -157,44 +124,50 @@ module MaRuKu::In::Markdown::SpanLevelParser
157
124
  is_single = true
158
125
  end
159
126
 
160
- my_debug "Attributes: #{attributes.inspect}"
161
- my_debug "READ TAG #{@m.to_s.inspect} tag = #{tag} closing? #{is_closing} single = #{is_single}"
162
-
163
127
  if TO_SANITIZE.include? tag
164
128
  attributes.strip!
165
- # puts "Attributes: #{attributes.inspect}"
166
129
  if attributes.size > 0
167
- @already << '<%s %s />' % [tag, attributes]
130
+ @already << '<%s %s />' % [tag, attributes]
168
131
  else
169
- @already << '<%s />' % [tag]
132
+ @already << '<%s />' % [tag]
170
133
  end
171
134
  elsif is_closing
172
135
  if @tag_stack.empty?
173
136
  error "Malformed: closing tag #{tag.inspect} in empty list"
174
- end
175
- if @tag_stack.last != tag
137
+ elsif @tag_stack.last != tag
176
138
  error "Malformed: tag <#{tag}> closes <#{@tag_stack.last}>"
177
139
  end
178
140
 
141
+ close_script_style if script_style?
142
+
179
143
  @already << @m.to_s
180
144
  @tag_stack.pop
181
145
  else
182
146
  @already << @m.to_s
147
+ @tag_stack.push(tag) unless is_single
183
148
 
184
- if not is_single
185
- @tag_stack.push(tag)
186
- my_debug "Pushing #{tag.inspect} when read #{@m.to_s.inspect}"
187
- end
188
-
189
- if %w(script style).include?(@tag_stack.last)
190
- # This is necessary to properly parse
191
- # script tags
192
- @already << "<![CDATA["
193
- self.state = :inside_script_style
194
- end
149
+ start_script_style if script_style?
195
150
  end
196
151
  end
197
152
 
153
+ def stuff_you_read
154
+ @already
155
+ end
156
+
157
+ def is_finished?
158
+ self.state == :inside_element && @tag_stack.empty?
159
+ end
160
+
161
+ private
162
+
163
+ def debug_state(note)
164
+ my_debug "#{@state}: #{note}: #{@m.to_s.inspect}"
165
+ end
166
+
167
+ def my_debug(s)
168
+ # puts "---" * 10 + "\n" + inspect + "\t>>>\t" + s
169
+ end
170
+
198
171
  def error(s)
199
172
  raise "Error: #{s} \n" + inspect, caller
200
173
  end
@@ -209,12 +182,52 @@ module MaRuKu::In::Markdown::SpanLevelParser
209
182
  @rest.gsub(/^/, '|') + "\n"
210
183
  end
211
184
 
212
- def stuff_you_read
213
- @already
185
+ # Script and style tag handling
186
+ # -----------------------------
187
+ #
188
+ # XHTML, and XML parsers like REXML, require that certain characters be
189
+ # escaped within script or style tags. However, there are conflicts between
190
+ # documents served as XHTML vs HTML. So we need to be extra careful about
191
+ # how we escape these tags so they will even parse correctly. However, we
192
+ # also try to avoid adding that escaping unnecessarily.
193
+ #
194
+ # See http://dorward.me.uk/www/comments-cdata/ for a good explanation.
195
+
196
+ # Are we within a script or style tag?
197
+ def script_style?
198
+ %w(script style).include?(@tag_stack.last)
214
199
  end
215
200
 
216
- def is_finished?
217
- (self.state == :inside_element) and @tag_stack.empty?
201
+ # Save our @already buffer elsewhere, and switch to using @already for the
202
+ # contents of this script or style tag.
203
+ def start_script_style
204
+ @before_already, @already = @already, ""
205
+ end
206
+
207
+ # Finish script or style tag content, wrapping it in CDATA if necessary,
208
+ # and add it to our original @already buffer.
209
+ def close_script_style
210
+ tag = @tag_stack.last
211
+
212
+ # See http://www.w3.org/TR/xhtml1/#C_4 for character sequences not allowed within an element body.
213
+ if @already =~ /<|&|\]\]>|--/
214
+ new_already = script_style_cdata_start(tag)
215
+ new_already << "\n" unless @already.start_with?("\n")
216
+ new_already << @already
217
+ new_already << "\n" unless @already.end_with?("\n")
218
+ new_already << script_style_cdata_end(tag)
219
+ @already = new_already
220
+ end
221
+ @before_already << @already
222
+ @already = @before_already
223
+ end
224
+
225
+ def script_style_cdata_start(tag)
226
+ (tag == 'script') ? "//<![CDATA[" : "/*<![CDATA[*/"
227
+ end
228
+
229
+ def script_style_cdata_end(tag)
230
+ (tag == 'script') ? "//]]>" : "/*]]>*/"
218
231
  end
219
- end # html helper
232
+ end
220
233
  end