Last active
October 19, 2017 20:07
-
-
Save xbelanch/0c25fa87abe9d03a34c6cf9e4f4d2066 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/env ruby | |
## Deal with media embed output format | |
require "paru/filter" | |
require 'addressable/uri' | |
require "youtube_id" | |
format = if ARGV.first.nil? then "unknown" else ARGV.first end | |
warn "Filter :: Rewrite media embed code for output format: #{format}" | |
Paru::Filter.run do | |
with "Para" do |paragraph| | |
if paragraph.inner_markdown.start_with?('::') then | |
command, type, *args = paragraph.inner_markdown.gsub(/\s+\r\n?/,'').strip.split('::').drop(1) #=> remove first blank element. Sure there's another way... | |
if "insert" == command and "media" == type then | |
if args.length == 1 then | |
link = args.first.lstrip.chop | |
# https://stackoverflow.com/questions/9268407/how-to-convert-markdown-style-links-using-regex | |
title, url = link.split(%r{\[([^\]]+)\]\(([^)]+)\)}x).drop(1) #=> sure is there another way to parse this | |
uri = Addressable::URI.parse url | |
if "www.youtube.com" == uri.host then | |
id_youtube = YoutubeID.from(url) | |
end | |
if "html" == format then | |
html_embed = Paru::PandocFilter::RawBlock.new(["html", "<iframe width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/#{id_youtube}\" frameborder=\"0\" allowfullscreen><\/iframe><p>#{title}<\/p>"]) #=> need to find another way to generate html code | |
paragraph.parent.replace(paragraph, html_embed) #=> replace it html code | |
elsif "latex" == format then | |
latex_embed = Paru::PandocFilter::RawBlock.new(["latex", "\\begin{pspicture}(1in,1in) \\psbarcode{#{url}}{}{qrcode} \\end{pspicture}"]) | |
paragraph.parent.replace(paragraph, latex_embed) | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment