Skip to content

Instantly share code, notes, and snippets.

@drewsberry
Created September 20, 2014 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drewsberry/f619be7e41bd2e5f166f to your computer and use it in GitHub Desktop.
Save drewsberry/f619be7e41bd2e5f166f to your computer and use it in GitHub Desktop.
KaTeXifier test
require 'execjs'
def convert(content)
singledollar = /(?<![\$])\$([^$]+)\$(?!\$)/
doubledollar = /\$\$([^$]+)\$\$/
path_to_katex = "../public/js/katex.min.js"
katexsrc = open(path_to_katex).read
katex = ExecJS.compile(katexsrc)
content = content.gsub(singledollar) { |eqn| match_single_dollar(katex, eqn) }
content = content.gsub(doubledollar) { |eqn| match_double_dollar(katex, eqn) }
return content
end
def eqn_to_html(katex, string)
return katex.call("katex.renderToString", string)
end
def match_single_dollar(katex, match)
return eqn_to_html(katex, match[1])
end
def match_double_dollar(katex, match)
s = "\\displaystyle " + match[1]
return "<div style='text-align: center;'>" + eqn_to_html(katex, s) + "</div>"
end
content = "This is some content with $ inline equations $ and $$ centred equations $$"
content = convert(content)
html = "<!DOCTYPE HTML>
<html>
<head>
<meta charset='utf-8'>
<link rel='stylesheet' type='text/css' href='../public/css/katex.min.css'>
</head>
<body>
" + content + "
</body>
</html>
"
File.open("TEST.html", "w") { |file| file.write(html) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment