Add markdown filter

This commit is contained in:
Takashi Kokubun 2015-03-30 17:39:20 +09:00
parent 7bf8ce1f88
commit 1000f20504
6 changed files with 61 additions and 10 deletions

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
<h1>Hamlit</h1>
<p>Yet another haml implementation</p>
HTML
end
it 'renders markdown filter with string interpolation' do
assert_render(<<-'HAML', <<-HTML)
- project = 'Hamlit'
:markdown
# #{project}
Yet another haml implementation
HAML
<h1>Hamlit</h1>
<p>Yet another haml implementation</p>
HTML
end
end
end