Class: Docgenerator::CSS

Inherits:
Object show all
Defined in:
lib/docgenerator/css.rb

Overview

Class to collect css-settings for a Element.

This values can be used inline (style-Attribute) and inside style>-Tags

Constant Summary collapse

@@values =

List of all allowed values. The first value is a regular expression, coninued by fixed values.

{
  'font-size'	=> [ 'xx-small', 'x-small', 'small', 'medium', 'large','x-large','xx-large','smaller','larger', /\A\d+(pt|px|em)/ ],
  'font-style'	=> ['italic', 'oblique', 'normal'],
  'font-weight'	=> ['bold', 'bolder', 'lighter', 'normal', /\A\d00\Z/ ],
  'color'		=> CSS_COLORS,
  'background-color'	=> CSS_COLORS,
  'visibility'	=> ['hidden', 'visible' ],
  'text-decoration' => ['underline', 'overline', 'line-through', 'blink', 'none'],
  #Ausrichtung
  'float'	=> ['left', 'right', 'none' ],
  'display'  => ['inline'],
  'text-align'	=> ['left', 'center', 'right', 'justify', 'char'],
  'vertical-align'	=> ['top', 'middle','bottom','baseline','sub','super','text-top','text-bottom'],
  'clear'			=> ['left', 'right','both','none'],	#Fortsetzung bei Textumfluss
  'white-space'	=> [ 'normal', 'pre', 'nowrap' ],	#Textumbruch innererhalb des tags
  #Borders: CSS-Eigenschaften: Außenrand/Abstand
  'margin'				=> CSS_WIDTH,
  'margin-top'		=> CSS_WIDTH,
  'margin-bottom'	=> CSS_WIDTH,
  'margin-left'		=> CSS_WIDTH,
  'margin-right'		=> CSS_WIDTH,
  #Borders: CSS-Eigenschaften: Innenabstand
  'padding'			=> CSS_WIDTH,
  'padding-top'		=> CSS_WIDTH,
  'padding-bottom'	=> CSS_WIDTH,
  'padding-left'		=> CSS_WIDTH,
  'padding-right'	=> CSS_WIDTH,
  #Tabulars
  'border-style'				=> CSS_BORDER,
  'border-left-style'	=> CSS_BORDER,
  'border-right-style'	=> CSS_BORDER,
  'border-top-style'		=> CSS_BORDER,
  'border-bottom-style'	=> CSS_BORDER,
  'border-width'				=> CSS_WIDTH,
  'border-left-width'	=> CSS_WIDTH,
  'border-right-width'	=> CSS_WIDTH,
  'border-top-width'		=> CSS_WIDTH,
  'border-bottom-width'	=> CSS_WIDTH,
  'border-color'				=> CSS_COLORS,
  'border-left-color'	=> CSS_COLORS,
  'border-right-color'	=> CSS_COLORS,
  'border-top-color'		=> CSS_COLORS,
  'border-bottom-color'	=> CSS_COLORS,
  'table-layout'		=> ['auto', 'fixed'],	#Not in Opera
  'empty-cells'		=> ['show', 'hide'],
  'border-collapse'	=> ['separate', 'collapse'],
  #Positionierung und Anzeige von Elementen
  'position'		=> ['absolute', 'fixed', 'relative', 'static'],
  'top'				=> CSS_WIDTH,
  'left'				=> CSS_WIDTH,
  'bottom'			=> CSS_WIDTH,
  'right'			=> CSS_WIDTH,
  'width'			=> CSS_WIDTH,
  'min-width'		=> CSS_WIDTH,
  'max-width'		=> CSS_WIDTH,
  'height'			=> CSS_WIDTH,
  'min-height'		=> CSS_WIDTH,
  'max-height'		=> CSS_WIDTH,
  'overflow'		=> ['visible', 'hidden', 'scroll', 'auto' ],
  #Lists
  'list-style-type'	=> [	
      'decimal', 'lower-roman', 'upper-roman', 'lower-alpha', 'upper-alpha', 'lower-greek', 'hebrew', 'decimal-leading-zero', 
      'cjk-ideographic', 'hiragana', 'katakana', 'hiragana-iroha', 'katakana-iroha',	#Japanese
      'disc', 'circle', 'square', 	#ul
      'none'],
  'list-style-position'	=> [ 'inside', 'outside'],
  'list-style-image'		=> String,	#URI
  #~ 'list-style'			=> @@values['list-style-type'] + @@values['list-style-position'],
  #Generell settings
#~ ['bottom', 'top', 'middle', 'baseline'],
}

Instance Method Summary collapse

Constructor Details

#initialize(values = {}) ⇒ CSS

Returns a new instance of CSS.



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/docgenerator/css.rb', line 141

def initialize( values ={} )
  @values	= {}
  #~ set_option_defaults(values) #guarantee :log
  @log = values[:log] || DOCGENERATOR_DEFAULT_LOGGER
  values.delete(:log)
  
  values.each{|k,v|
    self[k] = v
  }
  @cr = nil
end

Instance Method Details

#[](k) ⇒ Object



164
165
166
# File 'lib/docgenerator/css.rb', line 164

def []( k )
  return @values[generalize_key( k )]
end

#[]=(k, v) ⇒ Object

Add a setting.

If an existing value is overwritten, you get a warning. To replace a value, use css.replace( value ) (But the you have no check)



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/docgenerator/css.rb', line 174

def []=( k, v )
  key = generalize_key( k )
  if @values[key]
    @log.warn("CSS: Overwrite #{k.inspect}") if @log.warn?
  end
  if ! @@values[key]
    @log.error("CSS: Undefined key #{k.inspect}") if @log.error?
  elsif @@values[key].include?(v)	#Fine
  elsif @@values[key][0].is_a?(Regexp) and @@values[key][0] =~ v.to_s
  else
    @log.error("CSS: #{v} not allowed for #{key}") if @log.error?
  end
  
  @values[key] = v
end

#generalize_key(k) ⇒ Object

Make key unique. convert symbol to String…



155
156
157
158
159
160
161
162
163
# File 'lib/docgenerator/css.rb', line 155

def generalize_key( k )
  key = nil
  if k.is_a?(Symbol)
    key = k.to_s.gsub('_','-')
  else
    key = k
  end
  return key
end

#supports?(key) ⇒ Boolean

Check if a key is valid.

Returns:

  • (Boolean)


137
138
139
# File 'lib/docgenerator/css.rb', line 137

def supports?(key)
  return @@values[key]
end

#to_html(options = {}) ⇒ Object

Returns the values for a direct css in the style-attribute for a HTML-tag.



192
193
194
195
196
197
# File 'lib/docgenerator/css.rb', line 192

def to_html(options={})
  o = {
    :map => "%s;",
  }.update(options)
  return self.to_tab.map{|p| o[:map] % p}.join()
end

#to_sObject



198
199
200
# File 'lib/docgenerator/css.rb', line 198

def to_s()
  return self.to_html
end

#to_tabObject

Returns a tab with the textes for each characteristic.



204
205
206
207
208
209
210
# File 'lib/docgenerator/css.rb', line 204

def to_tab()
  s = []
  @values.sort.each{|k,v|
    s << "#{k}: #{v}"
  }
  return s
end