Module: Themizer

Defined in:
lib/themizer.rb

Constant Summary collapse

ARGUMENT_ERROR =
"Themizer: argument error in 'init(args)': "\
"args should be Hash, "\
"args should contain ':themes' key, "\
"args[:themes] should be Array of strings"
SASS_VAR_TILDA_IMPORTANT =
/(?<sass_var>\$[^\s:~;]*)~(?<important>[^;]*);/

Class Method Summary collapse

Class Method Details

.init(args) ⇒ Object

Raises:



8
9
10
11
12
13
14
15
# File 'lib/themizer.rb', line 8

def init(args)
  raise ARGUMENT_ERROR unless args.class == Hash
  raise ARGUMENT_ERROR unless args[:themes]
  raise ARGUMENT_ERROR unless args[:themes].class == Array
  raise ARGUMENT_ERROR unless args[:themes].map { |e| e.to_s } == args[:themes]
  @themes = args[:themes]
  @debug = args[:debug] || false
end

.themize(action = :expand, theme = "\"\"", &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/themizer.rb', line 17

def themize(action = :expand, theme = "\"\"", &block)
  return unless block_given?
  src_erb = _extract_block(block)
  return unless src_erb
  _debug(src_erb, "processing block")
  src_sass = ERB.new(src_erb).result

  case action
  when :expand
    maps, selectors = _mapize(src_sass)
    src_sass_expanded = maps
    unless selectors.strip.empty?
      # if you add "\"\"" to @themes, sass won't compile
      # because it cannot iterate through list with empty strings in it
      # therefore, you have to process unthemed sass separately
      src_sass_expanded << _expand_tilda(selectors, "\"\"")

      # apply & sass operator
      #
      # ".form {
      #   ...
      # }"
      # =>
      # "@each $theme in theme1, theme2 {
      #   #{$theme} { &
      #     ...
      #   }
      # }"
      src_sass_expanded << "\n@each $theme in #{@themes.join(', ')} {"
      src_sass_expanded << ".\#{$theme} { & "
      src_sass_expanded << _expand_tilda(selectors)
      src_sass_expanded << "}}"
      @themes.each do |theme|
        src_sass_expanded.gsub!(".#{theme} body", "body.#{theme}")
      end
    end

  when :contract
    src_sass_expanded = _contract_tilda(src_sass, theme)
  end

  _debug(src_sass_expanded, "resulting sass")

  yield.gsub!(src_sass, src_sass_expanded)
end

.unthemize(theme = "\"\"", &block) ⇒ Object



63
64
65
# File 'lib/themizer.rb', line 63

def unthemize(theme = "\"\"", &block)
  themize(:contract, theme, &block)
end