Class: Hermeneutics::Css

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

Overview

Example

require "hermeneutics/css"
require "hermeneutics/color"
class MyCss < Css

  COL1  = "904f02".to_rgb
  COL2  = COL1.edit_hsv { |h,s,v| [h+15,s,v] }

  ATTR_COL1  = { color: COL1 }
  ATTR_COL2  = { color: COL2 }
  ATTR_DECON = { text_decoration: "none" }
  ATTR_DECOU = { text_decoration: "underline" }

  def build
    a ":link",    ATTR_COL1, ATTR_DECON
    a ":visited", ATTR_COL2, ATTR_DECON
    a ":active", ATTR_COL1, ATTR_DECON
    a ":focus",  ATTR_COL1, ATTR_DECOU
    space

    body "#dummy" do
      properties background_color: "f7f7f7".to_rgb
      div ".child", background_color: "e7e7e7".to_rgb
      @b = selector
      td do
        @bt = selector
      end
    end
    selectors @b, @bt, font_size: :large
  end
end
Hermeneutics::Css.document

Defined Under Namespace

Classes: Selector

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCss

Returns a new instance of Css.



132
133
134
# File 'lib/hermeneutics/css.rb', line 132

def initialize
  @selector = Selector.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/hermeneutics/css.rb', line 185

def method_missing sym, *args, &block
  if Html::TAGS[ sym] then
    if args.any? and not Hash === args.first then
      sub = args.shift
    end
    if args.any? and not Hash === args.first then
      desc, sub = sub, args.shift
    elsif sub !~ /[a-z]/i or Symbol === sub then
      desc, sub = sub, nil
    end
    tag desc, sym, sub, *args, &block
  else
    super
  end
end

Class Attribute Details

.mainObject

Returns the value of attribute main.



47
48
49
# File 'lib/hermeneutics/css.rb', line 47

def main
  @main
end

Class Method Details

.document(*args, &block) ⇒ Object



57
58
59
60
61
# File 'lib/hermeneutics/css.rb', line 57

def document *args, &block
  open do |i|
    i.document *args, &block
  end
end

.inherited(cls) ⇒ Object



48
49
50
# File 'lib/hermeneutics/css.rb', line 48

def inherited cls
  Css.main = cls
end

.open(out = nil) ⇒ Object



51
52
53
54
55
56
# File 'lib/hermeneutics/css.rb', line 51

def open out = nil
  i = (@main||self).new
  i.generate out do
    yield i
  end
end

.write_file(name = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/hermeneutics/css.rb', line 62

def write_file name = nil
  name ||= (File.basename $0, ".rb") + ".css"
  File.open name, "w" do |f|
    open f do |i|
      if block_given? then
        yield i
      else
        i.document
      end
    end
  end
end

Instance Method Details

#comment(str) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/hermeneutics/css.rb', line 145

def comment str
  @out << "/*"
  str = mask_comment str
  ml = str =~ %r(#$/)
  if ml then
    @out << $/
    str.each_line { |l|
      l.chomp!
      @out << " * " << l << $/
    }
  else
    @out << " " << str
  end
  @out << " */"
  ml and @out << $/
end

#document(*args, &block) ⇒ Object



136
137
138
# File 'lib/hermeneutics/css.rb', line 136

def document *args, &block
  build *args, &block
end

#generate(out = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/hermeneutics/css.rb', line 77

def generate out = nil
  o = @out
  begin
    @out = out||$stdout
    yield
  ensure
    @out = o
  end
end

#pathObject



140
141
142
143
# File 'lib/hermeneutics/css.rb', line 140

def path
  @out.path
rescue NoMethodError
end

#properties(*args) ⇒ Object



201
202
203
# File 'lib/hermeneutics/css.rb', line 201

def properties *args
  write @selector.to_s, *args
end

#selectorObject



205
206
207
# File 'lib/hermeneutics/css.rb', line 205

def selector
  @selector.dup
end

#selectors(*args) ⇒ Object



209
210
211
212
213
214
215
216
# File 'lib/hermeneutics/css.rb', line 209

def selectors *args
  s = []
  while Selector === args.first do
    s.push args.shift
  end
  t = s.join ", "
  write t, *args
end

#spaceObject



162
163
164
# File 'lib/hermeneutics/css.rb', line 162

def space
  @out << $/
end

#tag(*args) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/hermeneutics/css.rb', line 166

def tag *args
  p = []
  while Hash === args.last do
    p.unshift args.pop
  end
  @selector.tag *args do
    if p.empty? then
      yield
    else
      properties *p
    end
  end
end