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'=> <>

  • 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]


32
33
34
35
36
37
38
# File 'lib/pink_shirt/attributes.rb', line 32

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

Instance Method Details

#attrsObject


40
41
42
# File 'lib/pink_shirt/attributes.rb', line 40

def attrs
  @attrs_hash
end

#colspanObject


117
118
119
120
121
122
123
124
125
# File 'lib/pink_shirt/attributes.rb', line 117

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

#klass_and_idObject


143
144
145
146
147
148
149
150
151
152
153
# File 'lib/pink_shirt/attributes.rb', line 143

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


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

def lang
  return nil unless attrs.include?('lang')
  
  output  = ""
  output += "["
  output += attrs['lang']
  output += "]"
end

#nudgesObject


134
135
136
# File 'lib/pink_shirt/attributes.rb', line 134

def nudges
  @nudges
end

#paddingObject


138
139
140
# File 'lib/pink_shirt/attributes.rb', line 138

def padding
  @padding
end

#parse_stylesObject


63
64
65
66
67
68
69
70
# File 'lib/pink_shirt/attributes.rb', line 63

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

#rowspanObject


127
128
129
130
131
132
# File 'lib/pink_shirt/attributes.rb', line 127

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

#steal_nudgesObject


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pink_shirt/attributes.rb', line 76

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


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/pink_shirt/attributes.rb', line 97

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

#styleObject


155
156
157
158
159
160
161
162
# File 'lib/pink_shirt/attributes.rb', line 155

def style
  return nil unless styles.length > 0

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

#stylesObject


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

def styles
  @styles_hash ||= {}
end

#styles_to_sObject


72
73
74
# File 'lib/pink_shirt/attributes.rb', line 72

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

#writeObject


48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pink_shirt/attributes.rb', line 48

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