-
Notifications
You must be signed in to change notification settings - Fork 353
/
7_hashes.rb
59 lines (51 loc) · 2.07 KB
/
7_hashes.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# This challenge is based off of problem 1
# modify it such that it also accepts in the hash, a color (whose values are red("#FF0000"), green("#00FF00"), and blue(#0000FF) )
# if the color is set, then it should show up in the style
# It should also not be necessary to pass in the hash, if you don't want to specify options
#
#
# EXAMPLE:
#
# html = HTMLTag.new 'p' , 'Hello World' , :multiline => false , :color => :red
# html.to_s # => <p style="color:#FF0000">Hello World</p>
#
# sports = [
# HTMLTag.new( 'li' , 'baseball' , :multiline => false , :color => :red , :font => :serif ) ,
# HTMLTag.new( 'li' , 'soccer' , :multiline => false , :color => :green , :font => :sans_serif ) ,
# HTMLTag.new( 'li' , 'football' , :multiline => false , :color => :blue , :font => :monospace ) ,
# ]
#
# ordered_list = HTMLTag.new 'ol' , sports.join , :multiline => true
#
# puts ordered_list # => (as output)
# <ol >
# <li style='color:#FF0000;font-family:"Times New Roman", "Georgia"'>baseball</li>
# <li style='color:#00FF00;font-family:"Arial", "Verdana"'>soccer</li>
# <li style='color:#0000FF;font-family:"Courier New", "Lucida Console"'>football</li>
# </ol>
class HTMLTag
FONTS = {
:serif => '"Times New Roman", "Georgia"' ,
:sans_serif => '"Arial", "Verdana"' ,
:monospace => '"Courier New", "Lucida Console"'
}
attr_accessor :name , :innerHTML , :options
# options: :multiline should be true or false
def initialize(name,innerHTML,options)
@name , @innerHTML , @options = name , innerHTML , options
end
def font
font = options[:font] # one of :serif , :sans_serif , or :monospace
FONTS[font]
end
def style
return nil unless options[:font]
"style='font-family:#{font}'"
end
def to_s
line_end = if options[:multiline] then "\n" else "" end
"<#{name} #{style}>#{line_end}" \
"#{innerHTML.chomp}#{line_end}" \
"</#{name}>\n"
end
end