Class: Hum::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/hum/engine.rb

Overview

Hum

Instance Method Summary collapse

Constructor Details

#initializeEngine

Engine



17
18
# File 'lib/hum/engine.rb', line 17

def initialize
end

Instance Method Details

#build_hashesObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/hum/engine.rb', line 129

def build_hashes
  
  i = 1
  
  #parse into an array of hashes
  @sass.each do |code| 
    hash = { 
      :line =>    i, 
      :select =>  _grab_select(code), 
      :tab =>     _grab_tab(code),
      :parent =>   nil,
      :kids =>    []
    }
    @tree << hash
    i += 1
  end
  
  #mark all parent selectors
  _check_for_parents
  
  #collect all kids
  _collect_kids
  
  #process all tags of parent selectors
  _process_parents
  
  #process all tabs
  _process_tabs
  
  #process all mixins by fixing the tabs and removing the mixin
  _process_mixins

  #process all special selectors, like pseudo elements
  _process_special
  
  @tree
end

#clean_sassObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/hum/engine.rb', line 112

def clean_sass
  #remove all property: value; pairs
  @sass.gsub!(/.*: .*/, "")
  
  #make sure tabs are two spaces
  @sass.gsub!(/\t/, "  ")
  
  #make sure nested direct descendant is normal
  @sass.gsub!(/\& > /, "")
  
  #make sure direct descendant is normal
  @sass.gsub!(/ > /, " ")
  
  #remove duplicate new lines
  @sass.gsub!(/\n+/, "\n")
end

#load(file) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hum/engine.rb', line 20

def load(file)
  #the array that holds the parsed CSS
  @tree = []
  
  @input_path = file
  
  #the name of the input file
  @input_name = File.basename(file)
  
  #read the input file
  @input_file = File.open(file, 'r')
  
  @directory = File.dirname(file)
  
  #the output path
  @output_path = File.expand_path(file).gsub(/\..*/, ".html")
  
  #the name of the output file
  @output_name = File.basename(@output_path)
end

#output_hamlObject



182
183
184
# File 'lib/hum/engine.rb', line 182

def output_haml
  return _output_haml
end

#output_htmlObject



186
187
188
189
190
# File 'lib/hum/engine.rb', line 186

def output_html
  #convert HAML to HAML
  @html = Haml::Engine.new(@haml).render
  @html
end

#render_haml_tagsObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/hum/engine.rb', line 167

def render_haml_tags
  #in each line
  @tree.each do |hash|
    hash[:haml] = []
    
    #for each selector
    hash[:select].each do |tag|
      
      #convert the tag to a HAML tag
      hash[:haml].push(_grab_haml_tag(tag))
    end         
  end
  @tree
end

#render_sassObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hum/engine.rb', line 76

def render_sass
  opts = { :cache => false, :read_cache => true, :syntax => :scss }
  
  #read content
  content = @input_file.read
  
  #remove /**/ comments
  content.gsub!(/.*\/\*([\s\S]*?)\*\//, "")

  #remove // coments
  content.gsub!(/.*\/\/.*/, "")
  
  #remove @imports
  content.gsub!(/.*@import.*/, "")
  
  #if CSS render SASS
  if @input_name.match(/\.css/)
    @sass = Sass::CSS.new(content).render(:sass)

  #if SCSS convert to SASS
  elsif @input_name.match(/\.scss/)
    @sass = ::Sass::Engine.new(content, opts).to_tree.send("to_sass")

  #if sass keep as it
  elsif @input_name.match(/\.sass/)
    #doing this at the moment to make sure the sass is formatted correctly, as using sass-convert does some nifty formatting
    scss = ::Sass::Engine.new(content, {:cache => false, :read_cache => true, :syntax => :sass}).to_tree.send("to_scss")
    @sass = ::Sass::Engine.new(scss, opts).to_tree.send("to_sass")
    
  #if nothing then put error
  else
    puts "Hum only works with .scss, .sass and .css files.".bold
    exit 1
  end
end

#runObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hum/engine.rb', line 41

def run
  run_haml
  
  #create the output file
  @output_file = File.open(@output_path, "w")
  
  #output the html
  @output_file.write(output_html)
  
  #close the input file
  @input_file.close()
  
  #close the output file
  @output_file.close()
  
  puts "updated #{@output_name}".bold
end

#run_hamlObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hum/engine.rb', line 59

def run_haml
  #render SASS from CSS
  render_sass
  
  #remove property value pairs
  clean_sass
  
  #parse CSS into an array of hashes
  build_hashes
  
  #generate HAML tags
  render_haml_tags
  
  #generate HAML
  output_haml
end