summaryrefslogtreecommitdiff
path: root/pandoc.rb
diff options
context:
space:
mode:
authorLuke Shumaker <LukeShu@sbcglobal.net>2013-11-24 04:40:17 -0500
committerLuke Shumaker <LukeShu@sbcglobal.net>2013-11-24 04:40:17 -0500
commitc46222fd2c1e02f695b544576f8605676be4d502 (patch)
tree29893a84cbc8d06c138306ddad63eecabda0d50c /pandoc.rb
parentfee8f8267106650d24b5047ee7e0abfa905f5760 (diff)
Switch from rdiscount to Pandoc.
But, I am still using ERB for the templating; I wrote my own Ruby Pandoc bindings because pandoc-ruby sucks; it has more code but does less. This was slightly painful, as I had to switch all of the articles from my hacked-on metadata format to Pandoc's format.
Diffstat (limited to 'pandoc.rb')
-rw-r--r--pandoc.rb65
1 files changed, 65 insertions, 0 deletions
diff --git a/pandoc.rb b/pandoc.rb
new file mode 100644
index 0000000..47f638e
--- /dev/null
+++ b/pandoc.rb
@@ -0,0 +1,65 @@
+require 'open3'
+require 'json'
+
+module Pandoc
+ def self.prog
+ @prog ||= 'pandoc'
+ end
+ def self.prog=(val)
+ @prog = val
+ end
+ def self.load(fmt, input)
+ cmd = Pandoc::prog + " -t json"
+ unless fmt.nil?
+ cmd += " -f " + fmt
+ end
+ str = input
+ if str.respond_to? :read
+ str = str.read
+ end
+ json = ''
+ Open3::popen3(cmd) do |stdin, stdout, stderr|
+ stdin.puts(str)
+ stdin.close
+ json = stdout.read
+ end
+ return Pandoc::AST::new(json)
+ end
+
+ class AST
+ def initialize(json)
+ @js = JSON::parse(json)
+ end
+
+ def [](key)
+ Pandoc::AST::js2sane(@js[0]["unMeta"][key])
+ end
+
+ def to(format)
+ cmd = Pandoc::prog + " -f json -t " + format.to_s
+ output = ''
+ Open3::popen3(cmd) do |stdin, stdout, stderr|
+ stdin.puts @js.to_json
+ stdin.close
+ output = stdout.read
+ end
+ output
+ end
+
+ def self.js2sane(js)
+ if js.nil?
+ return js
+ end
+ case js["t"]
+ when "MetaList"
+ js["c"].map{|c| js2sane(c)}
+ when "MetaInlines"
+ js["c"].map{|c| js2sane(c)}.join()
+ when "Space"
+ " "
+ when "Str"
+ js["c"]
+ end
+ end
+ end
+end