class Hermeneutics::Css

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

Constants

INDENT

Attributes

main[RW]

Public Class Methods

document(*args, &block) click to toggle source
# File lib/hermeneutics/css.rb, line 57
def document *args, &block
  open do |i|
    i.document *args, &block
  end
end
inherited(cls) click to toggle source
# File lib/hermeneutics/css.rb, line 48
def inherited cls
  Css.main = cls
end
new() click to toggle source
# File lib/hermeneutics/css.rb, line 132
def initialize
  @selector = Selector.new
end
open(out = nil) { |i| ... } click to toggle source
# 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) { |i| ... } click to toggle source
# 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

Public Instance Methods

comment(str) click to toggle source
# 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) click to toggle source
# File lib/hermeneutics/css.rb, line 136
def document *args, &block
  build *args, &block
end
generate(out = nil) { || ... } click to toggle source
# File lib/hermeneutics/css.rb, line 77
def generate out = nil
  o = @out
  begin
    @out = out||$stdout
    yield
  ensure
    @out = o
  end
end
path() click to toggle source
# File lib/hermeneutics/css.rb, line 140
def path
  @out.path
rescue NoMethodError
end
properties(*args) click to toggle source
# File lib/hermeneutics/css.rb, line 205
def properties *args
  write @selector.to_s, *args
end
selector() click to toggle source
# File lib/hermeneutics/css.rb, line 209
def selector
  @selector.dup
end
selectors(*args) click to toggle source
# File lib/hermeneutics/css.rb, line 213
def selectors *args
  s = []
  while Selector === args.first do
    s.push args.shift
  end
  t = s.join ", "
  write t, *args
end
space() click to toggle source
# File lib/hermeneutics/css.rb, line 162
def space
  @out << $/
end
tag(*args) { || ... } click to toggle source
# 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

Private Instance Methods

mask_comment(str) click to toggle source
# File lib/hermeneutics/css.rb, line 224
def mask_comment str
  str.gsub /\*\//, "* /"
end
method_missing(sym, *args, &block) click to toggle source
Calls superclass method
# File lib/hermeneutics/css.rb, line 187
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
single(hash) { |"#{k}: #{v};"| ... } click to toggle source
# File lib/hermeneutics/css.rb, line 246
def single hash
  if block_given? then
    hash.map { |k,v|
      if Symbol === k then k = k.to_s ; k.gsub! /_/, "-" end
      if Array  === v then v = v.join " "                end
      yield "#{k}: #{v};"
    }
  else
    r = []
    single hash do |s|
      r.push s
    end
    r
  end
end
write(sel, *args) click to toggle source
# File lib/hermeneutics/css.rb, line 230
def write sel, *args
  p = {}
  args.each { |a| p.update a }
  @out << sel << " {"
  nl, ind = if p.size > 1 then
    @out << $/
    [ $/, INDENT]
  else
    [ " ", " "]
  end
  single p do |s|
    @out << ind << s << nl
  end
  @out << "}" << $/
end