Module: Newstile::Utils::HTML

Included in:
Converter::Html, Converter::Markdown, Converter::Newstile
Defined in:
lib/newstile/utils/html.rb

Constant Summary collapse

ESCAPE_MAP =
{
  '<' => '&lt;',
  '>' => '&gt;',
  '&' => '&amp;',
  '"' => '&quot;'
}
ESCAPE_ALL_RE =
/<|>|&/
ESCAPE_TEXT_RE =
Regexp.union(REXML::Parsers::BaseParser::REFERENCE_RE, /<|>|&/)
ESCAPE_ATTRIBUTE_RE =
Regexp.union(REXML::Parsers::BaseParser::REFERENCE_RE, /<|>|&|"/)
ESCAPE_RE_FROM_TYPE =
{
  :all => ESCAPE_ALL_RE,
  :text => ESCAPE_TEXT_RE,
  :attribute => ESCAPE_ATTRIBUTE_RE
}

Instance Method Summary collapse

Instance Method Details

#entity_to_str(e, original = nil) ⇒ Object

Convert the entity to a string.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/newstile/utils/html.rb', line 30

def entity_to_str(e, original = nil)
  if RUBY_VERSION >= '1.9' && @doc.options[:entity_output] == :as_char &&
      (c = e.char.encode(@doc.parse_infos[:encoding]) rescue nil) && !ESCAPE_MAP.has_key?(c)
    c
  elsif (@doc.options[:entity_output] == :as_input || @doc.options[:entity_output] == :as_char) && original
    original
  elsif @doc.options[:entity_output] == :numeric || e.name.nil?
    "&##{e.code_point};"
  else
    "&#{e.name};"
  end
end

#escape_html(str, type = :all) ⇒ Object

Escape the special HTML characters in the string str. The parameter type specifies what is escaped: :all - all special HTML characters as well as entities, :text

  • all special HTML characters except the quotation mark but no entities and

:attribute - all special HTML characters including the quotation mark but no entities.



67
68
69
# File 'lib/newstile/utils/html.rb', line 67

def escape_html(str, type = :all)
  str.gsub(ESCAPE_RE_FROM_TYPE[type]) {|m| ESCAPE_MAP[m] || m}
end

#html_attributes(el) ⇒ Object

Return the string with the attributes of the element el.



44
45
46
# File 'lib/newstile/utils/html.rb', line 44

def html_attributes(el)
  el.attr.map {|k,v| v.nil? ? '' : " #{k}=\"#{escape_html(v.to_s, :attribute)}\"" }.join('')
end