-
-
Save mattwildig/c1d81126463c128d5515 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'escaped' | |
template = File.read('input.haml') | |
haml_engine = Haml::Engine.new(template) | |
output = haml_engine.render | |
puts output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'haml' | |
module SafeMarkdown | |
include Haml::Filters::Markdown | |
include Haml::Filters::Base | |
#this bit copied from Haml::Filters::Markdown | |
lazy_require 'rdiscount', 'peg_markdown', 'maruku', 'bluecloth' | |
# This bit copied from Haml::Filters::Base with only the addition | |
# of the second parameter to the call to unescape_interpolation added | |
def compile(compiler, text) | |
resolve_lazy_requires | |
filter = self | |
compiler.instance_eval do | |
if contains_interpolation?(text) | |
return if options[:suppress_eval] | |
text = unescape_interpolation(text, true).gsub(/(\\+)n/) do |s| | |
escapes = $1.size | |
next s if escapes % 2 == 0 | |
("\\" * (escapes - 1)) + "\n" | |
end | |
# We need to add a newline at the beginning to get the | |
# filter lines to line up (since the Haml filter contains | |
# a line that doesn't show up in the source, namely the | |
# filter name). Then we need to escape the trailing | |
# newline so that the whole filter block doesn't take up | |
# too many. | |
text = "\n" + text.sub(/\n"\Z/, "\\n\"") | |
push_script <<RUBY.rstrip, :escape_html => false | |
find_and_preserve(#{filter.inspect}.render_with_options(#{text}, _hamlout.options)) | |
RUBY | |
return | |
end | |
rendered = Haml::Helpers::find_and_preserve(filter.render_with_options(text, compiler.options), compiler.options[:preserve]) | |
if !options[:ugly] | |
push_text(rendered.rstrip.gsub("\n", "\n#{' ' * @output_tabs}")) | |
else | |
push_text(rendered.rstrip) | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:safemarkdown | |
Hello #{"<a href=\"evil\">My name</a>"}. | |
<strong>But this remains</strong>. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p>Hello <a href="evil">My name</a>. | |
<strong>But this remains</strong>.</p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment