Some Maruku Regexp Refactoring

master
Jacques Distler 2010-06-19 03:02:15 -05:00
parent 0d8f680d4f
commit ce8578d2d0
5 changed files with 29 additions and 22 deletions

View File

@ -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]}

View File

@ -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 = "<code>#{tex.gsub( /&/, "&amp;" ).
gsub( /</, "&lt;" ).gsub( />/, "&gt;" ).
gsub(/'/, "&#39;" ).gsub(/"/, "&quot;" )}</code>"
mathml = "<code>#{html_escape(tex)}</code>"
return Document.new(mathml).root
end

View File

@ -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( /&/, "&amp;" ).
gsub( /</, "&lt;" ).
gsub( />/, "&gt;" ).
gsub( /'/, "&#39;" ).
gsub( /"/, "&quot;" )
end
# Render as an HTML fragment (no head, just the content of BODY). (returns a string)
def to_s5(context={})

View File

@ -74,9 +74,7 @@ module MaRuKu; module Out; module HTML
div.write(xml,indent,transitive=true,ie_hack)
end
xml.gsub!(/\A<dummy>\s*/,'')
xml.gsub!(/\s*<\/dummy>\Z/,'')
xml.gsub!(/\A<dummy\s*\/>/,'')
xml.gsub!(/\A<dummy>\s*|\s*<\/dummy>\Z|\A<dummy\s*\/>/,'')
xml
end
@ -507,8 +505,7 @@ by Maruku, to have the same results in both HTML and LaTeX.
def source2html(source)
# source = source.gsub(/&/,'&amp;')
source = Text.normalize(source)
source = source.gsub(/\&apos;/,'&#39;') # IE bug
source = source.gsub(/'/,'&#39;') # IE bug
source.gsub!(/\&apos;|'/,'&#39;') # 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(/\&apos;/,'&#39;') # IE bug
html = html.gsub(/'/,'&#39;') # IE bug
html.gsub!(/\&apos;|'/,'&#39;') # IE bug
# html = html.gsub(/&/,'&amp;')
code = Document.new(html, {:respect_whitespace =>:all}).root
@ -633,9 +629,7 @@ of the form `#ff00ff`.
s = source
# s = s.gsub(/&/,'&amp;')
s = Text.normalize(s)
s = s.gsub(/\&apos;/,'&#39;') # IE bug
s = s.gsub(/'/,'&#39;') # IE bug
s = Text.normalize(s).gsub(/\&apos;|'/,'&#39;') # IE bug
if get_setting(:code_show_spaces)
# 187 = raquo

View File

@ -141,6 +141,26 @@ module MaRuKu
return s
end
#--
MARUKU_HTML_ESCAPE = {
'&' => '&amp;',
'<' => '&lt;',
'>' => '&gt;',
"'" => '&#39;',
'"' => '&quot;',
}
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