Skip to content

Instantly share code, notes, and snippets.

@sebastiandeutsch
Last active October 19, 2016 21:36
Show Gist options
  • Save sebastiandeutsch/d253cb655bb2457c9ca54b65e63f3664 to your computer and use it in GitHub Desktop.
Save sebastiandeutsch/d253cb655bb2457c9ca54b65e63f3664 to your computer and use it in GitHub Desktop.
I'm trying to implement css modules for haml. I've added a processor for the attributes to archive that: https://github.com/sebastiandeutsch/haml/blob/feature/process_attributes_and_values/lib/haml/compiler.rb#L417. See implementation of the processor below. Now I have the problem that I need to set the context for the processor within a file on…
<div class="test__container___1qISr">
Hello World!
<div class="test__container___1qISr">
Test
</div>
</div>
{
"stylesheets/application.css": {
"container": [
"application__container___1q3ub"
]
},
"stylesheets/index.css": {
"container": [
"index__container___Az6C0"
]
},
"stylesheets/test.css": {
"container": [
"test__container___1qISr"
]
}
}
class CssModulesProcessor
def call(attr, value, options)
puts "********** CssModulesProcessor ************** called"
puts options.inspect
puts options[:css_module_context]
puts options.filename
puts "********** /CssModulesProcessor ************* called"
file = File.read(File.join(Rails.root, 'webpack', 'dist', 'css_classes.json'))
css_modules = JSON.parse(file)
if attr == 'class'
if css_modules[options[:css_module_context]].present?
value = css_modules[options[:css_module_context]][value] if css_modules[options[:css_module_context]].key?(value)
end
end
return attr, value
end
end
Haml::Template.options[:attr_processor] = CssModulesProcessor.new
Haml::Template.options[:css_module_context] = "stylesheets/application.css"
<div class="index__container___Az6C0">
Hello World!
<div class="test__container___1qISr">
Test
</div>
</div>
- Haml::Template.options[:css_module_context] = "stylesheets/index.css"
.container
Hello World!
- Haml::Template.options[:css_module_context] = "stylesheets/test.css"
.container
Test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment