Add markdown filter
This commit is contained in:
parent
7bf8ce1f88
commit
1000f20504
|
@ -29,6 +29,7 @@ Gem::Specification.new do |spec|
|
||||||
spec.add_development_dependency "less"
|
spec.add_development_dependency "less"
|
||||||
spec.add_development_dependency "pry"
|
spec.add_development_dependency "pry"
|
||||||
spec.add_development_dependency "rake"
|
spec.add_development_dependency "rake"
|
||||||
|
spec.add_development_dependency "redcarpet"
|
||||||
spec.add_development_dependency "rspec", ">= 3"
|
spec.add_development_dependency "rspec", ">= 3"
|
||||||
spec.add_development_dependency "sass"
|
spec.add_development_dependency "sass"
|
||||||
spec.add_development_dependency "slim"
|
spec.add_development_dependency "slim"
|
||||||
|
|
|
@ -8,6 +8,7 @@ require 'hamlit/filters/plain'
|
||||||
require 'hamlit/filters/preserve'
|
require 'hamlit/filters/preserve'
|
||||||
require 'hamlit/filters/ruby'
|
require 'hamlit/filters/ruby'
|
||||||
require 'hamlit/filters/sass'
|
require 'hamlit/filters/sass'
|
||||||
|
require 'hamlit/filters/markdown'
|
||||||
require 'hamlit/filters/scss'
|
require 'hamlit/filters/scss'
|
||||||
|
|
||||||
module Hamlit
|
module Hamlit
|
||||||
|
@ -24,6 +25,7 @@ module Hamlit
|
||||||
register :escaped, Filters::Escaped
|
register :escaped, Filters::Escaped
|
||||||
register :javascript, Filters::Javascript
|
register :javascript, Filters::Javascript
|
||||||
register :less, Filters::Less
|
register :less, Filters::Less
|
||||||
|
register :markdown, Filters::Markdown
|
||||||
register :plain, Filters::Plain
|
register :plain, Filters::Plain
|
||||||
register :preserve, Filters::Preserve
|
register :preserve, Filters::Preserve
|
||||||
register :ruby, Filters::Ruby
|
register :ruby, Filters::Ruby
|
||||||
|
|
|
@ -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
|
|
@ -7,25 +7,33 @@ module Hamlit
|
||||||
class Tilt < Filters::Base
|
class Tilt < Filters::Base
|
||||||
include Concerns::StringInterpolation
|
include Concerns::StringInterpolation
|
||||||
|
|
||||||
def self.render(name, source)
|
def self.render(name, source, indent_width: 2)
|
||||||
result = ::Tilt["t.#{name}"].new { source }.render
|
result = ::Tilt["t.#{name}"].new { source }.render
|
||||||
indent_source(result, indent_width: 2)
|
indent_source(result, indent_width: indent_width)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def compile_with_tilt(name, source, ast)
|
def compile_with_tilt(name, source, ast, indent_width: 2)
|
||||||
return runtime_compile(name, source, ast) if contains_interpolation?(source)
|
if contains_interpolation?(source)
|
||||||
|
return runtime_compile(name, source, ast, indent_width: indent_width)
|
||||||
|
end
|
||||||
|
|
||||||
result = [:static, Filters::Tilt.render(name, source)]
|
content = [:static, Filters::Tilt.render(name, source, indent_width: indent_width)]
|
||||||
content = [:multi, [:static, "\n"], result]
|
build_ast(ast, content)
|
||||||
ast << content
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def runtime_compile(name, source, ast)
|
def runtime_compile(name, source, ast, indent_width: 2)
|
||||||
literal = string_literal(source)
|
literal = string_literal(source)
|
||||||
code = "::Hamlit::Filters::Tilt.render(#{name.inspect}, #{literal})"
|
code = "::Hamlit::Filters::Tilt.render(#{name.inspect}, #{literal}, indent_width: #{indent_width})"
|
||||||
content = [:multi, [:static, "\n"], [:dynamic, code]]
|
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
|
ast << content
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -112,6 +112,7 @@ module Hamlit
|
||||||
SKIP_NEWLINE_EXPS.include?(ast.first) ||
|
SKIP_NEWLINE_EXPS.include?(ast.first) ||
|
||||||
(ast[0..1] == [:haml, :doctype]) ||
|
(ast[0..1] == [:haml, :doctype]) ||
|
||||||
(ast[0..2] == [:haml, :filter, 'ruby']) ||
|
(ast[0..2] == [:haml, :filter, 'ruby']) ||
|
||||||
|
(ast[0..2] == [:haml, :filter, 'markdown']) ||
|
||||||
@outer_removal.include?(@current_indent)
|
@outer_removal.include?(@current_indent)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue