Render tex formulas to images
This commit is contained in:
@@ -17,6 +17,7 @@ require File.expand_path('../gollum/page', __FILE__)
|
|||||||
require File.expand_path('../gollum/file', __FILE__)
|
require File.expand_path('../gollum/file', __FILE__)
|
||||||
require File.expand_path('../gollum/markup', __FILE__)
|
require File.expand_path('../gollum/markup', __FILE__)
|
||||||
require File.expand_path('../gollum/sanitization', __FILE__)
|
require File.expand_path('../gollum/sanitization', __FILE__)
|
||||||
|
require File.expand_path('../gollum/tex', __FILE__)
|
||||||
|
|
||||||
module Gollum
|
module Gollum
|
||||||
VERSION = '1.3.1'
|
VERSION = '1.3.1'
|
||||||
|
|||||||
@@ -144,6 +144,12 @@ module Precious
|
|||||||
mustache :compare
|
mustache :compare
|
||||||
end
|
end
|
||||||
|
|
||||||
|
get '/_tex.png' do
|
||||||
|
content_type 'image/png'
|
||||||
|
formula = Base64.decode64(params[:data])
|
||||||
|
Gollum::Tex.render_formula(formula)
|
||||||
|
end
|
||||||
|
|
||||||
get %r{^/(javascript|css|images)} do
|
get %r{^/(javascript|css|images)} do
|
||||||
halt 404
|
halt 404
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
require 'digest/sha1'
|
require 'digest/sha1'
|
||||||
require 'cgi'
|
require 'cgi'
|
||||||
require 'pygments'
|
require 'pygments'
|
||||||
|
require 'base64'
|
||||||
|
|
||||||
module Gollum
|
module Gollum
|
||||||
|
|
||||||
@@ -97,13 +98,7 @@ module Gollum
|
|||||||
def process_tex(data)
|
def process_tex(data)
|
||||||
@texmap.each do |id, spec|
|
@texmap.each do |id, spec|
|
||||||
type, tex = *spec
|
type, tex = *spec
|
||||||
out =
|
out = %{<img src="/_tex.png?type=#{type}&data=#{Base64.encode64(tex).chomp}" alt="#{CGI.escapeHTML(tex)}">}
|
||||||
case type
|
|
||||||
when :block
|
|
||||||
%{<script type="math/tex; mode=display">#{tex}</script>}
|
|
||||||
when :inline
|
|
||||||
%{<script type="math/tex">#{tex}</script>}
|
|
||||||
end
|
|
||||||
data.gsub!(id, out)
|
data.gsub!(id, out)
|
||||||
end
|
end
|
||||||
data
|
data
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
require 'fileutils'
|
||||||
|
require 'shellwords'
|
||||||
|
require 'tmpdir'
|
||||||
|
|
||||||
|
module Gollum
|
||||||
|
module Tex
|
||||||
|
Template = <<-EOS
|
||||||
|
\\documentclass[12pt]{article}
|
||||||
|
\\usepackage{color}
|
||||||
|
\\usepackage[dvips]{graphicx}
|
||||||
|
\\pagestyle{empty}
|
||||||
|
\\pagecolor{white}
|
||||||
|
\\begin{document}
|
||||||
|
{\\color{black}
|
||||||
|
\\begin{eqnarray*}
|
||||||
|
%s
|
||||||
|
\\end{eqnarray*}}
|
||||||
|
\\end{document}
|
||||||
|
EOS
|
||||||
|
|
||||||
|
class << self
|
||||||
|
attr_accessor :latex_path, :dvips_path, :convert_path
|
||||||
|
end
|
||||||
|
|
||||||
|
self.latex_path = 'latex'
|
||||||
|
self.dvips_path = 'dvips'
|
||||||
|
self.convert_path = 'convert'
|
||||||
|
|
||||||
|
def self.check_dependencies!
|
||||||
|
if `which latex` == ""
|
||||||
|
raise "`latex` command not found"
|
||||||
|
end
|
||||||
|
|
||||||
|
if `which dvips` == ""
|
||||||
|
raise "`dvips` command not found"
|
||||||
|
end
|
||||||
|
|
||||||
|
if `which convert` == ""
|
||||||
|
raise "`convert` command not found"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.render_formula(formula)
|
||||||
|
check_dependencies!
|
||||||
|
|
||||||
|
Dir.mktmpdir('tex') do |path|
|
||||||
|
tex_path = ::File.join(path, 'formula.tex')
|
||||||
|
dvi_path = ::File.join(path, 'formula.dvi')
|
||||||
|
eps_path = ::File.join(path, 'formula.eps')
|
||||||
|
png_path = ::File.join(path, 'formula.png')
|
||||||
|
|
||||||
|
::File.open(tex_path, 'w') { |f| f.write(Template % formula) }
|
||||||
|
|
||||||
|
sh latex_path, '-interaction=batchmode', 'formula.tex', :cwd => path
|
||||||
|
sh dvips_path, '-o', eps_path, '-E', dvi_path
|
||||||
|
sh convert_path, '+adjoin',
|
||||||
|
'-antialias',
|
||||||
|
'-transparent', 'white',
|
||||||
|
'-density', '150x150',
|
||||||
|
eps_path, png_path
|
||||||
|
|
||||||
|
::File.read(png_path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def self.sh(*args)
|
||||||
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
||||||
|
cwd = "cd \"#{options[:cwd]}\" && " if options[:cwd]
|
||||||
|
`#{cwd}#{Shellwords.join(args)} 2>&1`
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user