Created
January 12, 2020 21:57
-
-
Save Jeswang/b708759f8b29b1d953ef809f80fb56c6 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
#!/usr/bin/ruby | |
# encoding: utf-8 | |
# Example custom processor for use with Marked <http://markedapp.com> and Jekyll _posts | |
# It's geared toward my personal set of plugins and tags, but you'll get the idea. | |
# It 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 | |
# --- | |
# Replaces {% gist XXXXX filename.rb %} with appropriate script tag | |
# | |
# Replace various other OctoPress, Jekyll and custom tags | |
# | |
# Processes final output with /usr/bin/kramdown (install kramdown as system gem: `sudo gem install kramdown`) | |
# | |
# Be sure to run *without* stripping YAML headers in Marked Behavior preferences. | |
Encoding.default_internal = Encoding::UTF_8 | |
Encoding.default_external = Encoding::UTF_8 | |
# full path to image folder | |
full_image_path = "/Users/jswang/Documents/jeswang-blog/source" | |
require "CGI" | |
require "rubygems" | |
content = STDIN.read | |
parts = content.split(/^---\s*$/) | |
# Read YAML headers as needed before cutting them out | |
post_title = parts[1].match(/^title:\s+(\!\s*)?["']?(.*?)["']?\s*$/i)[2].strip | |
# Remove YAML | |
# content.sub!(/^---.*?---\s*$/m,'') | |
content = parts[2] | |
# Fenced code | |
content.gsub!(/^```(\w+)?\s*\n(.*?)\n```/m) { | |
%Q"<pre><code class=\"#{$1}\">#{CGI.escapeHTML($2)}</code></pre>" | |
} | |
content.gsub!(/\{%(\s+)?codeblock (lang:)?(\S+)?(\s+)?%\}\n?([\W\w]*?)\{%(\s+)?endcodeblock(\s+)?%\}/m) { | |
%Q"<pre><code class=\"#{$3}\">#{CGI.escapeHTML($5)}</code></pre>" | |
} | |
# Process image Liquid tags | |
content.gsub!(/\{% img (.*?\s*)%\}/) {|img| | |
if img =~ /\{% img\s+(\S+\s)?(https?:\/\/\S+|\/\S+)\s+(\d+\s+)?(\d+\s+)?(\S.*\s*)?%\}/i | |
classes = $1.strip if $1 | |
src = $2 | |
width = $3 | |
height = $4 | |
title = $5 | |
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 =~ /left/ | |
style = %Q{ style="float:left;margin:0 10px 10px 0"} if classes =~ /right/ | |
%Q{<img src="#{File.join(full_image_path,src)}" alt="#{alt}" class="#{classes}" title="#{title}"#{style} />} | |
} | |
content = "## #{post_title}\n\n#{content}" | |
File.open("/tmp/markdown", 'w:utf-8') { |file| file.write(content) } | |
puts %x{multimarkdown /tmp/markdown } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment