Method: HTMLEntities#encode

Defined in:
lib/htmlentities.rb

#encode(source, *instructions) ⇒ Object

Encode codepoints into their corresponding entities. Various operations are possible, and may be specified in order:

:basic

Convert the five XML entities (‘“<>&)

:named

Convert non-ASCII characters to their named HTML 4.01 equivalent

:decimal

Convert non-ASCII characters to decimal entities (e.g. &#1234;)

:hexadecimal

Convert non-ASCII characters to hexadecimal entities (e.g. # &#x12ab;)

You can specify the commands in any order, but they will be executed in the order listed above to ensure that entity ampersands are not clobbered and that named entities are replaced before numeric ones.

If no instructions are specified, :basic will be used.

Examples:

encode(str) - XML-safe
encode(str, :basic, :decimal) - XML-safe and 7-bit clean
encode(str, :basic, :named, :decimal) - 7-bit clean, with all
non-ASCII characters replaced with their named entity where possible, and
decimal equivalents otherwise.

Note: It is the program’s responsibility to ensure that the source contains valid UTF-8 before calling this method.



72
73
74
# File 'lib/htmlentities.rb', line 72

def encode(source, *instructions)
  Encoder.new(@flavor, instructions).encode(source)
end