Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/html_tagger.rb
Instance Method Summary collapse
- #add_class(added_class) ⇒ Object
-
#add_style(style) ⇒ Object
compat with 0.0.1 gem.
- #bold ⇒ Object
- #color(text_color) ⇒ Object
- #italics ⇒ Object
- #link(linked_to_url) ⇒ Object
- #oblique ⇒ Object
- #strikeout ⇒ Object
- #styles(*styles) ⇒ Object
- #subscript ⇒ Object
- #superscript ⇒ Object
- #tag_id(id_to_set) ⇒ Object
- #underline ⇒ Object
- #underscore ⇒ Object
Instance Method Details
#add_class(added_class) ⇒ Object
49 50 51 |
# File 'lib/html_tagger.rb', line 49 def add_class(added_class) styles('class:'+added_class) end |
#add_style(style) ⇒ Object
compat with 0.0.1 gem
114 115 116 |
# File 'lib/html_tagger.rb', line 114 def add_style(style) '<span style="' + style + '">' + self + "</span>" end |
#bold ⇒ Object
9 10 11 |
# File 'lib/html_tagger.rb', line 9 def bold styles('bold') end |
#color(text_color) ⇒ Object
13 14 15 |
# File 'lib/html_tagger.rb', line 13 def color(text_color) styles('color:'+text_color) end |
#italics ⇒ Object
17 18 19 |
# File 'lib/html_tagger.rb', line 17 def italics styles('italics') end |
#link(linked_to_url) ⇒ Object
5 6 7 |
# File 'lib/html_tagger.rb', line 5 def link(linked_to_url) styles('link:'+linked_to_url) end |
#oblique ⇒ Object
21 22 23 |
# File 'lib/html_tagger.rb', line 21 def oblique styles('oblique') end |
#strikeout ⇒ Object
33 34 35 |
# File 'lib/html_tagger.rb', line 33 def strikeout styles('strikeout') end |
#styles(*styles) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/html_tagger.rb', line 53 def styles(*styles) tag_attrs = {} = ['span', 'div', 'p'] = { 'before' => [], 'after' => []} block = 'span' if (styles.first.is_a?(Symbol)) # specify block type block = styles.first.to_s raise "Supported wrapper tags are "+ .join(' and ') unless .include?(block) styles.shift end styles.reverse.each do |styl| case styl when /^id:(.+)/ raise "Only one ID allowed for an element." if tag_attrs['id'] tag_attrs['id'] = $1 when /^link:(.+)/ ['before'] << "<a href=\"#{$1}\">" ['after'] = ['</a>'] + ['after'] when /^class:(.+)/ tag_attrs['class'] = $1 + (tag_attrs['class'].nil? ? '' : ' ' + tag_attrs['class']) when /^color:(.+)/ tag_attrs['color'] = $1 # first wins if specified > 1 time when 'bold' tag_attrs['style'] = 'font-weight:bold; ' + tag_attrs['style'].to_s when /^italics?$/ tag_attrs['style'] = 'font-style:italic; ' + tag_attrs['style'].to_s when /^under(line|score)$/ tag_attrs['style'] = 'text-decoration:underline; ' + tag_attrs['style'].to_s when 'oblique' tag_attrs['style'] = 'font-style:oblique; ' + tag_attrs['style'].to_s when /^strike(out|through)$/ tag_attrs['style'] = 'text-decoration:line-through; ' + tag_attrs['style'].to_s when 'subscript' tag_attrs['style'] = 'font-size:xx-small; vertical-align:bottom; ' + tag_attrs['style'].to_s when 'superscript' tag_attrs['style'] = 'font-size:xx-small; vertical-align:top; ' + tag_attrs['style'].to_s else raise "Unexpected style element." end end block_wrapper = '' block_closer = '' unless tag_attrs.empty? block_wrapper = '<' + block + ' ' + tag_attrs.keys.inject(''){ |accum, html_attr| accum + html_attr + '="' + tag_attrs[html_attr].chomp('; ') + '" '}.chomp(' ') + '>' block_closer = '</' + block + '>' end ['before'].join + block_wrapper + self + block_closer + ['after'].join end |
#subscript ⇒ Object
37 38 39 |
# File 'lib/html_tagger.rb', line 37 def subscript styles('subscript') end |
#superscript ⇒ Object
45 46 47 |
# File 'lib/html_tagger.rb', line 45 def superscript styles('superscript') end |
#tag_id(id_to_set) ⇒ Object
41 42 43 |
# File 'lib/html_tagger.rb', line 41 def tag_id(id_to_set) styles('id:'+id_to_set) end |
#underline ⇒ Object
29 30 31 |
# File 'lib/html_tagger.rb', line 29 def underline styles('underline') end |
#underscore ⇒ Object
25 26 27 |
# File 'lib/html_tagger.rb', line 25 def underscore styles('underline') end |