Created
January 4, 2013 12:35
-
-
Save ttscoff/4452318 to your computer and use it in GitHub Desktop.
Example Marked custom processor for previewing Jekyll posts
This file contains hidden or 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
#!/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!(/"/, '"') 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}>} | |
# "" | |
} | |
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