From ce8578d2d0fb4e529c6ee456acd37cd27f4ad560 Mon Sep 17 00:00:00 2001 From: Jacques Distler Date: Sat, 19 Jun 2010 03:02:15 -0500 Subject: [PATCH] Some Maruku Regexp Refactoring --- lib/instiki_stringsupport.rb | 2 +- .../maruku/ext/math/mathml_engines/none.rb | 6 +++--- .../maruku/lib/maruku/output/s5/to_s5.rb | 9 +-------- .../maruku/lib/maruku/output/to_html.rb | 14 ++++--------- .../plugins/maruku/lib/maruku/string_utils.rb | 20 +++++++++++++++++++ 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/lib/instiki_stringsupport.rb b/lib/instiki_stringsupport.rb index 09010202..5ea52a55 100644 --- a/lib/instiki_stringsupport.rb +++ b/lib/instiki_stringsupport.rb @@ -2313,7 +2313,7 @@ end "'" => ''', '"' => '"', } - TO_ESCAPE_PATTERN = Regexp.union(TO_ESCAPE.keys) + TO_ESCAPE_PATTERN = Regexp.union(*TO_ESCAPE.keys) def escapeHTML self.gsub(TO_ESCAPE_PATTERN){|m| TO_ESCAPE[m]} diff --git a/vendor/plugins/maruku/lib/maruku/ext/math/mathml_engines/none.rb b/vendor/plugins/maruku/lib/maruku/ext/math/mathml_engines/none.rb index bec8283b..c427002b 100644 --- a/vendor/plugins/maruku/lib/maruku/ext/math/mathml_engines/none.rb +++ b/vendor/plugins/maruku/lib/maruku/ext/math/mathml_engines/none.rb @@ -1,14 +1,14 @@ module MaRuKu; module Out; module HTML + require 'maruku/string_utils' + def convert_to_mathml_none(kind, tex) # You can: either return a REXML::Element # return Element.new 'div' # or return an empty array on error # return [] # or have a string parsed by REXML: - mathml = "#{tex.gsub( /&/, "&" ). - gsub( //, ">" ). - gsub(/'/, "'" ).gsub(/"/, """ )}" + mathml = "#{html_escape(tex)}" return Document.new(mathml).root end diff --git a/vendor/plugins/maruku/lib/maruku/output/s5/to_s5.rb b/vendor/plugins/maruku/lib/maruku/output/s5/to_s5.rb index d2dd9f74..36d8d18a 100644 --- a/vendor/plugins/maruku/lib/maruku/output/s5/to_s5.rb +++ b/vendor/plugins/maruku/lib/maruku/output/s5/to_s5.rb @@ -8,20 +8,13 @@ begin rescue LoadError $rexml_new_version = false end + require 'maruku/string_utils' class MDDocument def s5_theme html_escape(self.attributes[:slide_theme] || "default") end - - def html_escape(string) - string.gsub( /&/, "&" ). - gsub( //, ">" ). - gsub( /'/, "'" ). - gsub( /"/, """ ) - end # Render as an HTML fragment (no head, just the content of BODY). (returns a string) def to_s5(context={}) diff --git a/vendor/plugins/maruku/lib/maruku/output/to_html.rb b/vendor/plugins/maruku/lib/maruku/output/to_html.rb index b5f27aae..ebbdbad1 100644 --- a/vendor/plugins/maruku/lib/maruku/output/to_html.rb +++ b/vendor/plugins/maruku/lib/maruku/output/to_html.rb @@ -74,9 +74,7 @@ module MaRuKu; module Out; module HTML div.write(xml,indent,transitive=true,ie_hack) end - xml.gsub!(/\A\s*/,'') - xml.gsub!(/\s*<\/dummy>\Z/,'') - xml.gsub!(/\A/,'') + xml.gsub!(/\A\s*|\s*<\/dummy>\Z|\A/,'') xml end @@ -507,8 +505,7 @@ by Maruku, to have the same results in both HTML and LaTeX. def source2html(source) # source = source.gsub(/&/,'&') source = Text.normalize(source) - source = source.gsub(/\'/,''') # IE bug - source = source.gsub(/'/,''') # IE bug + source.gsub!(/\'|'/,''') # IE bug Text.new(source, true, nil, true ) end @@ -571,8 +568,7 @@ and source = source.gsub(/\n*\Z/,'') html = convertor.convert( source ) - html = html.gsub(/\'/,''') # IE bug - html = html.gsub(/'/,''') # IE bug + html.gsub!(/\'|'/,''') # IE bug # html = html.gsub(/&/,'&') code = Document.new(html, {:respect_whitespace =>:all}).root @@ -633,9 +629,7 @@ of the form `#ff00ff`. s = source # s = s.gsub(/&/,'&') - s = Text.normalize(s) - s = s.gsub(/\'/,''') # IE bug - s = s.gsub(/'/,''') # IE bug + s = Text.normalize(s).gsub(/\'|'/,''') # IE bug if get_setting(:code_show_spaces) # 187 = raquo diff --git a/vendor/plugins/maruku/lib/maruku/string_utils.rb b/vendor/plugins/maruku/lib/maruku/string_utils.rb index 6b3b1d70..832f7fae 100644 --- a/vendor/plugins/maruku/lib/maruku/string_utils.rb +++ b/vendor/plugins/maruku/lib/maruku/string_utils.rb @@ -141,6 +141,26 @@ module MaRuKu return s end +#-- + MARUKU_HTML_ESCAPE = { + '&' => '&', + '<' => '<', + '>' => '>', + "'" => ''', + '"' => '"', + } + MARUKU_HTML_ESCAPE_PATTERN = Regexp.union(*MARUKU_HTML_ESCAPE.keys) +#++ + + # HTML-escapes a string. + # + # @param str [String] + # @return [String] + + def html_escape(string) + string.gsub(MARUKU_HTML_ESCAPE_PATTERN){|m| MARUKU_HTML_ESCAPE[m]} + end + # Escapes a string so that it can be safely used in a Bourne shell command line. # # Note that a resulted string should be used unquoted