Class: PinkShirt::Attributes

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

Overview

Attributes

the sax parser passes an array of attributes along with each tag attrs = [‘href’, ‘www.example.com’, ‘style’, ‘background-color: snake;’]

Textile displays attributes in a certain way

USAGE

Attributes.new(attrs).write

  • colspan=2 => 2

  • rowspan=3 => /3

  • style=‘padding-left:1em’ => (

  • style=‘padding-right:2em’ => ))

  • style=‘text-align:right’ => >

  • style=‘text-align:left’ => <

  • style=‘text-align:center’ => =

  • style=‘text-align:justify’=> <>

  • style=‘vertical-align:top’ => ^

  • style=‘vertical-align:center’ => -

  • style=‘vertical-align:bottom’ => ~

  • class = ‘panthers’ => (panthers)

  • id = ‘banner’ => (#banner)

  • class=‘this’ id=‘that’ => (this#that)

  • lang=‘fr’ => [fr]

  • style=‘color:red’ => color:red

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Attributes

Returns a new instance of Attributes.

Parameters:

  • attrs (Array)

    formatted [key, value, key, value]



35
36
37
38
39
40
41
42
# File 'lib/pink_shirt/attributes.rb', line 35

def initialize(attrs)
  @attrs = attrs
  @attrs_hash  = Hash[attrs]
  @styles_hash = parse_styles
  @nudges  = steal_nudges
  @padding = steal_padding
  @vertical = steal_vertical
end

Instance Method Details

#attrsObject



44
45
46
# File 'lib/pink_shirt/attributes.rb', line 44

def attrs
  @attrs_hash
end

#colspanObject



133
134
135
136
137
138
139
140
141
# File 'lib/pink_shirt/attributes.rb', line 133

def colspan
  return nil unless attrs['colspan']

  width = attrs['colspan']
  colspan = ""
  colspan << "\\" #literal backslash
  colspan << "#{width}"
  colspan if width
end

#klass_and_idObject



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/pink_shirt/attributes.rb', line 163

def klass_and_id
  klass = attrs['class']
  id    = attrs['id']
  return nil unless klass || id

  output  = ""
  output += "("
  output += klass if klass
  output += "##{id}" if id
  output += ")"
end

#langObject



184
185
186
187
188
189
190
191
# File 'lib/pink_shirt/attributes.rb', line 184

def lang
  return nil unless attrs.include?('lang')

  output  = ""
  output += "["
  output += attrs['lang']
  output += "]"
end

#nudgesObject



150
151
152
# File 'lib/pink_shirt/attributes.rb', line 150

def nudges
  @nudges
end

#paddingObject



154
155
156
# File 'lib/pink_shirt/attributes.rb', line 154

def padding
  @padding
end

#parse_stylesObject



68
69
70
71
72
73
74
75
# File 'lib/pink_shirt/attributes.rb', line 68

def parse_styles
  return nil unless attrs['style']

  rules_list = attrs['style'].split(";").map{|rule|
    rule.split(":")
  }
  Hash[rules_list]
end

#rowspanObject



143
144
145
146
147
148
# File 'lib/pink_shirt/attributes.rb', line 143

def rowspan
  return nil unless attrs['rowspan']

  height = attrs['rowspan']
  colspan = '/' + "#{height}" if height
end

#steal_nudgesObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pink_shirt/attributes.rb', line 81

def steal_nudges

  return nil unless attrs['style']
  nudges = []
  style = attrs['style']

  text_align = case styles.delete('text-align')
  when 'left'
    "<"
  when 'right'
    ">"
  when 'center'
    "="
  when 'justify'
    "<>"
  end

  nudges << text_align
  nudges
end

#steal_paddingObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/pink_shirt/attributes.rb', line 102

def steal_padding
  return nil unless attrs['style']

  left = case styles.delete('padding-left')
  when '1em'; "(" ;
  when '2em'; "((";
  end

  #Alternately, but i'd rather not encourage it
  # left_count = styles.delete('padding-left').to_i
  # left = Array.new(left_count).map{"("}.join

  right = case styles.delete('padding-right')
  when '1em'; ")" ;
  when '2em'; "))";
  end

  padding = "#{left}#{right}"
end

#steal_verticalObject



122
123
124
125
126
127
128
129
130
131
# File 'lib/pink_shirt/attributes.rb', line 122

def steal_vertical
  return nil unless attrs['style']

  vertical = case styles.delete('vertical-align')
  when 'top';    "^";
  when 'middle'; "-";
  when 'bottom'; "~";
  end
  vertical
end

#styleObject



175
176
177
178
179
180
181
182
# File 'lib/pink_shirt/attributes.rb', line 175

def style
  return nil unless styles.length > 0

  output  = ""
  output += "{"
  output += styles_to_s
  output += "}"
end

#stylesObject



48
49
50
# File 'lib/pink_shirt/attributes.rb', line 48

def styles
  @styles_hash ||= {}
end

#styles_to_sObject



77
78
79
# File 'lib/pink_shirt/attributes.rb', line 77

def styles_to_s
  @styles_hash.map{|k, v| "#{k}:#{v}"}.join(";")
end

#verticalObject



158
159
160
# File 'lib/pink_shirt/attributes.rb', line 158

def vertical
  @vertical
end

#writeObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pink_shirt/attributes.rb', line 52

def write
  add = []
  add << colspan
  add << rowspan
  add << nudges
  add << vertical
  add << padding
  add << klass_and_id
  add << style
  add << lang
  out = add.join
  return nil if out == ""

  out
end