Skip to content

Instantly share code, notes, and snippets.

@artagnon
Created February 17, 2013 17:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artagnon/4972301 to your computer and use it in GitHub Desktop.
Save artagnon/4972301 to your computer and use it in GitHub Desktop.
A part of claytext.rb from artagnon/clayoven
def process!(body)
htmlescape_rules = {
"&" => "&",
"\"" => """,
"'" => "'",
"<" => "&lt;",
">" => "&gt;"
}.freeze
paragraph_types = [:plain, :emailquote, :codeblock, :header, :footer]
paragraph_rules = {
Proc.new { |line| line.start_with? "&gt; " } => lambda { |paragraph|
paragraph.type = :emailquote },
Proc.new { |line| line.start_with? " " } => lambda { |paragraph|
paragraph.type = :codeblock },
Proc.new { |line| /^\[\d+\]: / =~ line } => lambda { |paragraph|
paragraph.content.gsub!(%r{^(\[\d+\]:) (.*://(.*))}) {
"#{$1} <a href=\"#{$2}\">#{$3[0, 64]}#{%{...} if $3.length > 67}</a>"
}
paragraph.type = :footer
}
}.freeze
# First, htmlescape the body text
body.gsub!(/[&"'<>]/, htmlescape_rules)
# ... and it continues
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment