Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/html_tagger.rb

Instance Method Summary collapse

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

#boldObject



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

#italicsObject



17
18
19
# File 'lib/html_tagger.rb', line 17

def italics
  styles('italics')
end


5
6
7
# File 'lib/html_tagger.rb', line 5

def link(linked_to_url)
  styles('link:'+linked_to_url)
end

#obliqueObject



21
22
23
# File 'lib/html_tagger.rb', line 21

def oblique
  styles('oblique')
end

#strikeoutObject



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 = {}

  supported_wrapper_tags = ['span', 'div', 'p']

  wrap_tags = { 'before' => [], 'after' => []}

  block = 'span'

  if (styles.first.is_a?(Symbol))  # specify block type
    block = styles.first.to_s
    raise "Supported wrapper tags are "+ supported_wrapper_tags.join(' and ') unless supported_wrapper_tags.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:(.+)/
        wrap_tags['before'] <<  "<a href=\"#{$1}\">"
        wrap_tags['after']  = ['</a>'] + wrap_tags['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
  wrap_tags['before'].join +  block_wrapper + self + block_closer + wrap_tags['after'].join

end

#subscriptObject



37
38
39
# File 'lib/html_tagger.rb', line 37

def subscript
  styles('subscript')
end

#superscriptObject



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

#underlineObject



29
30
31
# File 'lib/html_tagger.rb', line 29

def underline
 styles('underline')
end

#underscoreObject



25
26
27
# File 'lib/html_tagger.rb', line 25

def underscore
  styles('underline')
end