42 lines
934 B
Ruby
42 lines
934 B
Ruby
require "mathematical"
|
|
|
|
$blockRgx = /((?<block>\$\$[^\$]+\$\$)|[^\$](?<inline>\$[^\$]+\$)[^\$])/
|
|
|
|
module Jekyll
|
|
class InlineLatex < Converter
|
|
safe true
|
|
priority :high
|
|
|
|
def matches(ext)
|
|
ext =~ /^\.md$/i
|
|
end
|
|
|
|
def output_ext(ext)
|
|
".md"
|
|
end
|
|
|
|
def convert(content)
|
|
# first pass
|
|
buf = StringIO.new
|
|
prev = 0
|
|
renderer = Mathematical.new({:base64 => true, :format => :svg})
|
|
content.to_enum(:scan, $blockRgx).map { Regexp.last_match }.to_enum.with_index.each do |match, i|
|
|
if match[:block] != nil
|
|
code = match[:block]
|
|
a, b = match.offset(:block)
|
|
else
|
|
code = match[:inline]
|
|
a, b = match.offset(:inline)
|
|
end
|
|
|
|
buf << content[prev..a].gsub(/^\$?\$/, "").gsub(/\$?\$$/, "")
|
|
|
|
render = renderer.render(code)
|
|
buf << "<img src='" + render[:data] + "' />"
|
|
prev = b - 1
|
|
end
|
|
buf << content[prev..content.length].gsub(/^\$?\$/, "")
|
|
return buf.string
|
|
end
|
|
end
|
|
end
|