Skip to content

Commit 54561a8

Browse files
committed
Yet another TeXmath-to-HTML gem. This one uses KaTeX and ExecJS.
This is a TeX-to-HTML+MathML+CSS converter class using the Javascript-based KaTeX, interpreted by one of the Javascript engines supported by ExecJS. The intended purpose is to eliminate the need for math-rendering JavaScript in the HTML browser. Consider this a lightweight alternative to mathjax-node-cli. Javascript execution context initialization can be done once and then reused for formula renderings with the same general configuration. As a result, the performance is reasonable. Originally added directly to a kramdown fork. However, relying on config from potentially untrusted sources to find the necessary Javascript files might open security vulnerabilities. Therefore we chose to make that feature upgrade an opt-in requiring dedicated admin action, namely the explicit installation of this gem.
1 parent 94bd865 commit 54561a8

14 files changed

+555
-2
lines changed

COPYING

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
SsKaTeX - Server-side KaTeX for Ruby
2+
(not including KaTeX itself)
3+
MIT License
4+
Copyright (C) 2017 Christian Cornelssen <ccorn@1tein.de>
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a
7+
copy of this software and associated documentation files (the
8+
"Software"), to deal in the Software without restriction, including
9+
without limitation the rights to use, copy, modify, merge, publish,
10+
distribute, sublicense, and/or sell copies of the Software, and to
11+
permit persons to whom the Software is furnished to do so, subject to
12+
the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included
15+
in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+58-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,58 @@
1-
# sskatex
2-
SsKaTeX - Server-side KaTeX for Ruby
1+
# SsKaTeX
2+
## Server-side KaTeX for Ruby
3+
4+
This is a TeX-to-HTML+MathML+CSS converter class using the Javascript-based
5+
[KaTeX], interpreted by one of the Javascript engines supported by [ExecJS].
6+
The intended purpose is to eliminate the need for math-rendering Javascript
7+
in the client's HTML browser. Therefore the name: SsKaTeX means *server-side*
8+
KaTeX.
9+
10+
Javascript execution context initialization can be done once and then reused
11+
for formula renderings with the same general configuration. As a result, the
12+
performance is reasonable. Consider this a fast and lightweight alternative to
13+
[mathjax-node-cli].
14+
15+
Requirements for using SsKaTeX:
16+
17+
* Ruby gem [ExecJS],
18+
* A Javascript engine supported by ExecJS, e.g. via one of
19+
- Ruby gem [therubyracer],
20+
- Ruby gem [therubyrhino],
21+
- Ruby gem [duktape.rb],
22+
- [Node.js],
23+
* `katex.min.js` from [KaTeX].
24+
25+
Although the converter only needs `katex.min.js`, you may need to serve the
26+
rest of the KaTeX package, that is, CSS and fonts, as resources to the
27+
targeted web browsers. The upside is that your HTML templates need no longer
28+
include Javascripts for Math (neither `katex.js` nor any search-and-replace
29+
script). Your HTML templates should continue referencing the KaTeX CSS.
30+
If you host your own copy of the CSS, also keep hosting the fonts.
31+
32+
Minimal usage example:
33+
34+
tex_to_html = SsKaTeX.new(katex_js: 'path-to-katex/katex.min.js')
35+
# Here you could verify contents of tex_to_html.js_source for security...
36+
37+
body_html = '<p>By Pythagoras, %s. Furthermore:</p>' %
38+
tex_to_html.call('a^2 + b^2 = c^2', false) # inline math
39+
body_html << # block display
40+
tex_to_html.call('\frac{1}{2} + \frac{1}{3} + \frac{1}{6} = 1', true)
41+
# etc, etc.
42+
43+
More configuration options are described in the Rdoc. Most options, with the
44+
notable exception of `katex_opts`, do not affect usage nor output, but may be
45+
needed to make SsKaTeX work with all the external parts (JS engine and KaTeX).
46+
Since KaTeX is distributed separately from the SsKaTeX gem, configuration of
47+
the latter must support the specification of Javascript file locations. This
48+
implies that execution of arbitrary Javascript code is possible. Specifically,
49+
options with `js` in their names should be accepted from trusted sources only.
50+
Applications using SsKaTeX need to check this.
51+
52+
[duktape.rb]: https://github.com/judofyr/duktape.rb#duktaperb
53+
[ExecJS]: https://github.com/rails/execjs#execjs
54+
[KaTeX]: https://khan.github.io/KaTeX/
55+
[mathjax-node-cli]: https://github.com/mathjax/mathjax-node-cli
56+
[Node.js]: https://nodejs.org/
57+
[therubyracer]: https://github.com/cowboyd/therubyracer#therubyracer
58+
[therubyrhino]: https://github.com/cowboyd/therubyrhino#therubyrhino

Rakefile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'rake/testtask'
2+
3+
Rake::TestTask.new do |t|
4+
t.libs << 'test'
5+
end
6+
7+
desc "Run tests"
8+
task default: [:test]
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// -*- coding: utf-8 -*-
2+
//
3+
// Copyright (C) 2017 Christian Cornelssen <ccorn@1tein.de>
4+
//
5+
// This file is part of SsKaTeX which is licensed under the MIT.
6+
7+
// Transform non-ASCII characters and '\0' in given string to HTML numeric character references
8+
function escape_nonascii_html(str) {
9+
return str.replace(/[^\x01-\x7F]/g, function (u) {
10+
return "&#x" + u.charCodeAt(0).toString(16) + ";";
11+
});
12+
};

data/sskatex/js/tex_to_html.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// -*- coding: utf-8 -*-
2+
//
3+
// Copyright (C) 2017 Christian Cornelssen <ccorn@1tein.de>
4+
//
5+
// This file is part of SsKaTeX which is licensed under the MIT.
6+
7+
// Given a LaTeX math string, a boolean display_mode
8+
// (true for block display, false for inline),
9+
// and a dict katex_opts with general KaTeX options,
10+
// return a string with corresponding HTML+MathML output.
11+
// The implementation is allowed to set katex_opts.displayMode .
12+
function tex_to_html(tex, display_mode, katex_opts) {
13+
katex_opts.displayMode = display_mode;
14+
return escape_nonascii_html(katex.renderToString(tex, katex_opts));
15+
};

0 commit comments

Comments
 (0)