diff --git a/lib/middleman-syntax/haml_monkey_patch.rb b/lib/middleman-syntax/haml_monkey_patch.rb index 91e9497..5e0bea8 100644 --- a/lib/middleman-syntax/haml_monkey_patch.rb +++ b/lib/middleman-syntax/haml_monkey_patch.rb @@ -1,22 +1,36 @@ # If Haml is around, define a :code filter that can be used to more conveniently output highlighted code. + if defined? Haml - module Haml - module Filters - module Code - include Base + module HamlMonkeyPatch + def render(code) + code = code.rstrip + code = code.encode(Encoding::UTF_8) - def render(code) - code = code.rstrip - code = code.encode(Encoding::UTF_8) + # Allow language to be specified via a special comment like: + # # lang: ruby + if code.lines.first =~ /\A\s*#\s*lang:\s*(\w+)$/ + language = $1 + code = code.lines.to_a[1..-1].join # Strip first line + end - # Allow language to be specified via a special comment like: - # # lang: ruby - if code.lines.first =~ /\A\s*#\s*lang:\s*(\w+)$/ - language = $1 - code = code.lines.to_a[1..-1].join # Strip first line - end + Middleman::Syntax::Highlighter.highlight(code, language) + end + end - Middleman::Syntax::Highlighter.highlight(code, language) + if Gem::Version.new(Haml::VERSION) >= Gem::Version.new("6.0.0") + module Haml + class Filters + class Code < Base + extend HamlMonkeyPatch + end + end + end + else + module Haml + module Filters + module Code + include Base + extend HamlMonkeyPatch end end end