Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created January 4, 2013 12:35
Show Gist options
  • Save ttscoff/4452318 to your computer and use it in GitHub Desktop.
Save ttscoff/4452318 to your computer and use it in GitHub Desktop.
Example Marked custom processor for previewing Jekyll posts
#!/usr/bin/ruby
# For use with Marked <http://markedapp.com> and Jekyll _posts
# turns
# {% img alignright /images/heythere.jpg 100 100 "Hey there" "hi" %}
# into
# <img src="../images/heythere.jpg" alt="Hey there" class="alignright" title="hi" />
#
# replaces alignleft and alignright classes with appropriate style attribute
#
# Processes final output with /usr/bin/kramdown (install kramdown as system gem: `sudo gem install kramdown`)
content = STDIN.read
def e_sh(str)
str.to_s.gsub(/(?=[^a-zA-Z0-9_.\/\-\x7F-\xFF\n])/, '\\').gsub(/\n/, "'\n'").sub(/^$/, "''")
end
content.gsub!(/\{% img (.*?) %\}/) {|img|
if img =~ /\{% img (\S.*\s+)?(https?:\/\/\S+|\/\S+|\S+\/\s+)(\s+\d+\s+\d+)?(\s+.+)? %\}/i
classes = $1.strip
src = $2
size = $3
title = $4
if /(?:"|')([^"']+)?(?:"|')\s+(?:"|')([^"']+)?(?:"|')/ =~ title
title = $1
alt = $2
else
alt = title.gsub!(/"/, '&#34;') if title
end
classes.gsub!(/"/, '') if classes
end
style = %Q{ style="float:right;margin:0 0 10px 10px"} if classes =~ /alignright/
style = %Q{ style="float:left;margin:0 10px 10px 0"} if classes =~ /alignleft/
%Q{<img src="..#{src}" alt="#{alt}" class="#{classes}" title="#{title}"#{style}>}
# "![#{alt}](..#{src} \"#{title}\")"
}
puts %x{echo #{e_sh content}|/usr/bin/kramdown}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment