Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created June 30, 2014 22:49
Show Gist options
  • Save ttscoff/090de25f020b11385495 to your computer and use it in GitHub Desktop.
Save ttscoff/090de25f020b11385495 to your computer and use it in GitHub Desktop.
Search a concatenated Marked index file for include comments and adjust [[image.jpg]] tags to point to paths relative to included file
#!/usr/bin/ruby
# Marked 2 preprocessor
# Search a concatenated Marked index file for include comments and adjust
# [[image.jpg]] tags to point to paths relative to included file
# Update: Converts [[tags]] to Markdown image tags using the generated paths
input = STDIN.read
includes = []
output = input.split(/\n/).map! { |line|
if line =~ /<!-- include \[(.*?)\]/
dir = File.dirname($1)
includes.push(dir) unless includes.include?(dir)
elsif line =~ /<!-- end include \[(.*?)\]/
includes = includes.slice(0,includes.index(File.dirname($1)))
end
# line.gsub(/\[\[(.*?\|)?(.*?\.(?:png|svg|gif|jpg))\]\]/, "[[\\1#{includes[-1]}/\\2]]")
line.gsub(/\[\[((.*?)\|)?(.*?\.(?:png|svg|gif|jpg))\]\]/, "![\\2](#{includes[-1]}/images/\\3)")
}.join("\n")
$stdout.print output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment