Created
September 20, 2014 13:12
-
-
Save drewsberry/f619be7e41bd2e5f166f to your computer and use it in GitHub Desktop.
KaTeXifier test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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