# encoding: utf-8 require 'base64' require 'digest/sha1' require 'patron' module HTML class Pipeline class SVGTeX class PreFilter < TextFilter BLOCK_SVGTEX = /^\$\$([^$]+)\$\$\s*$/ INLINE_SVGTEX = /(? code } "\n#{id}" end end # Code taken from gollum (http://github.com/github/gollum) def extract_fenced_code! @text.gsub!(FENCED_CODE) do id = Digest::SHA1.hexdigest($2) @codemap[id] = { :lang => $1, :code => $2 } id end end def reinsert_code! @inline.each do |id, code| @text.gsub!(id) { "`#{code}`" } end @codemap.each do |id, spec| @text.gsub!(id) { "```#{spec[:lang]}\n#{spec[:code]}\n```" } end end end class PostFilter < Filter def call doc.search('code.mathjax').each do |node| eqn = node.inner_text rsp = session.post(context[:svgtex_url], :q => eqn) if rsp.status == 200 node.parent.replace rsp.body.gsub(/margin-(left|right): 0px; /, "") else node.remove_attribute 'class' end end doc.search('code:not([class])').each do |node| eqn = node.inner_text next unless eqn.sub!(/\A\{mathjax\} /, '') rsp = session.post(context[:svgtex_url], :q => eqn) if rsp.status == 200 node.replace "#{CGI.escape_html eqn}" else node.inner_text = eqn end end doc end def session @session ||= Patron::Session.new end end end end end