Module: Kramdown::Utils::Html
- Included in:
- Converter::Html, Converter::Kramdown
- Defined in:
- lib/kramdown/utils/html.rb
Overview
Provides convenience methods for HTML related tasks.
Note that this module has to be mixed into a class that has a @root (containing an element of type :root) and an @options (containing an options hash) instance variable so that some of the methods can work correctly.
Constant Summary collapse
- ESCAPE_MAP =
:stopdoc:
{ '<' => '<', '>' => '>', '&' => '&', '"' => '"' }
- 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
-
#entity_to_str(e, original = nil) ⇒ Object
Convert the entity
e
to a string. -
#escape_html(str, type = :all) ⇒ Object
Escape the special HTML characters in the string
str
. -
#html_attributes(attr) ⇒ Object
Return the HTML representation of the attributes
attr
.
Instance Method Details
#entity_to_str(e, original = nil) ⇒ Object
Convert the entity e
to a string. The optional parameter original
may contain the original representation of the entity.
This method uses the option entity_output
to determine the output form for the entity.
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/kramdown/utils/html.rb', line 38 def entity_to_str(e, original = nil) if RUBY_VERSION >= '1.9' && @options[:entity_output] == :as_char && (c = e.char.encode(@root.[:encoding]) rescue nil) && !ESCAPE_MAP.has_key?(c) c elsif (@options[:entity_output] == :as_input || @options[:entity_output] == :as_char) && original original elsif @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.
77 78 79 |
# File 'lib/kramdown/utils/html.rb', line 77 def escape_html(str, type = :all) str.gsub(ESCAPE_RE_FROM_TYPE[type]) {|m| ESCAPE_MAP[m] || m} end |
#html_attributes(attr) ⇒ Object
Return the HTML representation of the attributes attr
.
52 53 54 |
# File 'lib/kramdown/utils/html.rb', line 52 def html_attributes(attr) attr.map {|k,v| v.nil? || (k == 'id' && v.strip.empty?) ? '' : " #{k}=\"#{escape_html(v.to_s, :attribute)}\"" }.join('') end |