From 1000f20504e47e0b73d717b2799750c57b735c4e Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Mon, 30 Mar 2015 17:39:20 +0900 Subject: [PATCH] Add markdown filter --- hamlit.gemspec | 1 + lib/hamlit/compilers/filter.rb | 2 ++ lib/hamlit/filters/markdown.rb | 11 +++++++++++ lib/hamlit/filters/tilt.rb | 28 ++++++++++++++++++---------- lib/hamlit/parser.rb | 1 + spec/hamlit/filters/markdown_spec.rb | 28 ++++++++++++++++++++++++++++ 6 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 lib/hamlit/filters/markdown.rb create mode 100644 spec/hamlit/filters/markdown_spec.rb diff --git a/hamlit.gemspec b/hamlit.gemspec index 32a1a46f..dde2b342 100644 --- a/hamlit.gemspec +++ b/hamlit.gemspec @@ -29,6 +29,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency "less" spec.add_development_dependency "pry" spec.add_development_dependency "rake" + spec.add_development_dependency "redcarpet" spec.add_development_dependency "rspec", ">= 3" spec.add_development_dependency "sass" spec.add_development_dependency "slim" diff --git a/lib/hamlit/compilers/filter.rb b/lib/hamlit/compilers/filter.rb index afd34153..b94f0b5f 100644 --- a/lib/hamlit/compilers/filter.rb +++ b/lib/hamlit/compilers/filter.rb @@ -8,6 +8,7 @@ require 'hamlit/filters/plain' require 'hamlit/filters/preserve' require 'hamlit/filters/ruby' require 'hamlit/filters/sass' +require 'hamlit/filters/markdown' require 'hamlit/filters/scss' module Hamlit @@ -24,6 +25,7 @@ module Hamlit register :escaped, Filters::Escaped register :javascript, Filters::Javascript register :less, Filters::Less + register :markdown, Filters::Markdown register :plain, Filters::Plain register :preserve, Filters::Preserve register :ruby, Filters::Ruby diff --git a/lib/hamlit/filters/markdown.rb b/lib/hamlit/filters/markdown.rb new file mode 100644 index 00000000..348fc70a --- /dev/null +++ b/lib/hamlit/filters/markdown.rb @@ -0,0 +1,11 @@ +require 'hamlit/filters/tilt' + +module Hamlit + module Filters + class Markdown < Filters::Tilt + def compile(lines) + compile_with_tilt('markdown', lines.join("\n"), [], indent_width: 0) + end + end + end +end diff --git a/lib/hamlit/filters/tilt.rb b/lib/hamlit/filters/tilt.rb index 3d1c6d47..2b30e7d6 100644 --- a/lib/hamlit/filters/tilt.rb +++ b/lib/hamlit/filters/tilt.rb @@ -7,25 +7,33 @@ module Hamlit class Tilt < Filters::Base include Concerns::StringInterpolation - def self.render(name, source) + def self.render(name, source, indent_width: 2) result = ::Tilt["t.#{name}"].new { source }.render - indent_source(result, indent_width: 2) + indent_source(result, indent_width: indent_width) end private - def compile_with_tilt(name, source, ast) - return runtime_compile(name, source, ast) if contains_interpolation?(source) + def compile_with_tilt(name, source, ast, indent_width: 2) + if contains_interpolation?(source) + return runtime_compile(name, source, ast, indent_width: indent_width) + end - result = [:static, Filters::Tilt.render(name, source)] - content = [:multi, [:static, "\n"], result] - ast << content + content = [:static, Filters::Tilt.render(name, source, indent_width: indent_width)] + build_ast(ast, content) end - def runtime_compile(name, source, ast) + def runtime_compile(name, source, ast, indent_width: 2) literal = string_literal(source) - code = "::Hamlit::Filters::Tilt.render(#{name.inspect}, #{literal})" - content = [:multi, [:static, "\n"], [:dynamic, code]] + code = "::Hamlit::Filters::Tilt.render(#{name.inspect}, #{literal}, indent_width: #{indent_width})" + content = [:dynamic, code] + build_ast(ast, content) + end + + def build_ast(ast, content) + return content if ast.empty? + + content = [:multi, [:static, "\n"], content] ast << content end end diff --git a/lib/hamlit/parser.rb b/lib/hamlit/parser.rb index 34daa832..c63c6608 100644 --- a/lib/hamlit/parser.rb +++ b/lib/hamlit/parser.rb @@ -112,6 +112,7 @@ module Hamlit SKIP_NEWLINE_EXPS.include?(ast.first) || (ast[0..1] == [:haml, :doctype]) || (ast[0..2] == [:haml, :filter, 'ruby']) || + (ast[0..2] == [:haml, :filter, 'markdown']) || @outer_removal.include?(@current_indent) end end diff --git a/spec/hamlit/filters/markdown_spec.rb b/spec/hamlit/filters/markdown_spec.rb new file mode 100644 index 00000000..d693ec53 --- /dev/null +++ b/spec/hamlit/filters/markdown_spec.rb @@ -0,0 +1,28 @@ +describe Hamlit::Filters::Markdown do + describe '#compile' do + it 'renders markdown filter' do + assert_render(<<-HAML, <<-HTML) + :markdown + # Hamlit + Yet another haml implementation + HAML +

Hamlit

+ +

Yet another haml implementation

+ HTML + end + + it 'renders markdown filter with string interpolation' do + assert_render(<<-'HAML', <<-HTML) + - project = 'Hamlit' + :markdown + # #{project} + Yet another haml implementation + HAML +

Hamlit

+ +

Yet another haml implementation

+ HTML + end + end +end