Skip to content

Instantly share code, notes, and snippets.

@radamant
Created July 19, 2010 14:16
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save radamant/481456 to your computer and use it in GitHub Desktop.
Save radamant/481456 to your computer and use it in GitHub Desktop.
Simple haml-sass conversion for jekyll
module Jekyll
require 'haml'
class HamlConverter < Converter
safe true
priority :low
def matches(ext)
ext =~ /haml/i
end
def output_ext(ext)
".html"
end
def convert(content)
engine = Haml::Engine.new(content)
engine.render
end
end
require 'sass'
class SassConverter < Converter
safe true
priority :low
def matches(ext)
ext =~ /sass/i
end
def output_ext(ext)
".css"
end
def convert(content)
engine = Sass::Engine.new(content)
engine.render
end
end
end
@dtjm
Copy link

dtjm commented Aug 11, 2010

Hi there! I wrote pretty much the exact same code and put it in my plugins folder, but my HAML layouts are not being parsed, and none of my SASS files are being processed. Is this working for you?

@dtjm
Copy link

dtjm commented Aug 11, 2010

Ah, nevermind, I forgot to put the YAML Front Matter on my SASS file. Are you using HAML for your layout files?

@radamant
Copy link
Author

Glad you got it. Perhaps I should put an example of a jekyll+haml file on that blog post...

@dtjm
Copy link

dtjm commented Aug 13, 2010

That could be helpful for newbies like me =)

@nicalpi
Copy link

nicalpi commented Sep 10, 2010

Hello guys. Did you any solution regarding the _layouts not being converted? I've just installed that script but my layouts are not converted neither :/ cheers

@dtjm
Copy link

dtjm commented Sep 10, 2010

No I never figured out how to convert layouts.

@shmuel
Copy link

shmuel commented Oct 29, 2010

I'm wondering about how to use HAML in my layouts as well. Let me know if you figure this out.

@nicalpi
Copy link

nicalpi commented Oct 29, 2010

I finally choose to use html instead of HAML for the layout. After all, you only do them once.

But if you really want haml, I think the easiest thing is to create a rake file, with a rake preview command. That will allows you to put whatever you want to execute, I've done it for exemple to execute compass watch and then jekyll command

task :preview do
system("compass watch & jekyll --pygments --auto --server")
end

Then instead of compass watch if you do something like

for i in ./_layouts/*.haml; do [ -e $i ] && haml $i $i.html; done

I'm sure that should work.

@shmuel
Copy link

shmuel commented Oct 30, 2010

Thanks Spyou, that almost worked and was very helpful in getting me pointed in the right direction. The only problem I had was that your one-liner resulted in the conversion of my _layout/default.haml file to _layout/default.haml.html. Jekyll didn't like that much so I updated it to result in a _layout/default.html which seems to be working like a charm.

Here was my Rake task in the end:

desc "Launch preview environment."
task :preview do
system("for i in ./_layouts/*.haml; do [ -e $i ] && haml $i ${i%.haml}.html; done & compass watch & jekyll --auto --server")
end

@nicalpi
Copy link

nicalpi commented Oct 30, 2010

Nice, I didn't really test my line to be honest, so didn't realise the problem. But it's good to know that all is good now. Ho and you can remove the compass watch if you're not using compass (but you should :D).

I'm going to put my Rakefile the rest of the blog on github now.

@shmuel
Copy link

shmuel commented Oct 31, 2010

Yes, I too love compass. I actually updated the command so I can keep the haml files separate from the html:

system("for i in ./layouts_haml/.haml; do [ -e $i ] &amp;&amp; n=${i%._} && haml $i ./_layouts/${n##*/}.html; done & compass watch & jekyll --auto --server")

The only thing annoying about this solution is it doesn't behave the same way 'compass watch' or 'jekyll --auto' do. I have to stop/start the server every time I make a change to my layout.

I've added an issue to the mojombo/jekyll git project if anyone would like to join me in asking for this to get fixed:

http://github.com/mojombo/jekyll/issues#issue/225

@mwotton
Copy link

mwotton commented May 22, 2011

sorry if this is obvious/stupid - how do you use the stuff defined in the YAML front-matter inside the haml chunk?

i could always move it down as a variable, but that's a bit ugly...

@dtjm
Copy link

dtjm commented May 22, 2011

@mwotton, you have to put it inside Moustache markup. See the docs here: https://github.com/mojombo/jekyll/wiki/YAML-Front-Matter

@mwotton
Copy link

mwotton commented May 22, 2011

@dtjm so no variables are actually defined for the use of the Haml renderer? Does the Moustache get run first or second? I guess " - @InputData = {{ inputdata }}" isn't terrible, if the Moustach gets run first...

@dtjm
Copy link

dtjm commented May 23, 2011

@mwotton, oh I see what you are saying. I don't know if there are variables available in the HAML scope.

@rubyonrailstutor
Copy link

is it possible to mix haml and markdown ? or is that a crazy question because both are a kind of templating sugar and why would one be used with the other ?

@samurailink3
Copy link

@rubyonrailstutor Absolutely! Check out this section of the HAML docs. It requires the markdown to be put in it's own section, but it should work nicely from what others on the net have said.

Copy link

ghost commented Aug 22, 2014

@radamant how can I prevent Jekyll completely from compiling my SCSS files? I want to handle it myself with 3rd party solutions outside of Jekyll.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment