Skip to content

Commit 51e2e76

Browse files
dylandropAry Borenszweig
authored and
Ary Borenszweig
committedDec 13, 2016
Add documentation to the HTML module
1 parent 278ffff commit 51e2e76

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
 

‎src/html.cr

+24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Handles encoding and decoding of HTML entities.
12
module HTML
23
SUBSTITUTIONS = {
34
'!' => "!",
@@ -21,16 +22,39 @@ module HTML
2122
'\u{a0}' => " ",
2223
}
2324

25+
# Encodes a string with HTML entity substitutions.
26+
#
27+
# ```
28+
# require "html"
29+
#
30+
# HTML.escape("Crystal & You") # => "Crystal & You"
31+
# ```
2432
def self.escape(string : String) : String
2533
string.gsub(SUBSTITUTIONS)
2634
end
2735

36+
# Encodes a string to HTML, but writes to the IO instance provided.
37+
#
38+
# ```
39+
# require "html"
40+
#
41+
# io = IO::Memory.new
42+
# HTML.escape("Crystal & You", io) # => "Crystal & You"
43+
# io.to_s # => "Crystal & You"
44+
# ```
2845
def self.escape(string : String, io : IO)
2946
string.each_char do |char|
3047
io << SUBSTITUTIONS.fetch(char, char)
3148
end
3249
end
3350

51+
# Decodes a string that contains HTML entities.
52+
#
53+
# ```
54+
# require "html"
55+
#
56+
# HTML.unescape("Crystal &amp; You") # => "Crystal & You"
57+
# ```
3458
def self.unescape(string : String)
3559
return string unless string.includes? '&'
3660

0 commit comments

Comments
 (0)
Please sign in to comment.