Skip to content

Commit

Permalink
Yet another TeXmath-to-HTML gem. This one uses KaTeX and ExecJS.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ccorn committed Nov 21, 2017
1 parent 94bd865 commit 54561a8
Show file tree
Hide file tree
Showing 14 changed files with 555 additions and 2 deletions.
23 changes: 23 additions & 0 deletions COPYING
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
SsKaTeX - Server-side KaTeX for Ruby
(not including KaTeX itself)
MIT License
Copyright (C) 2017 Christian Cornelssen <ccorn@1tein.de>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
60 changes: 58 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,58 @@
# sskatex
SsKaTeX - Server-side KaTeX for Ruby
# SsKaTeX
## Server-side KaTeX for Ruby

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 client's HTML browser. Therefore the name: SsKaTeX means *server-side*
KaTeX.

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. Consider this a fast and lightweight alternative to
[mathjax-node-cli].

Requirements for using SsKaTeX:

* Ruby gem [ExecJS],
* A Javascript engine supported by ExecJS, e.g. via one of
- Ruby gem [therubyracer],
- Ruby gem [therubyrhino],
- Ruby gem [duktape.rb],
- [Node.js],
* `katex.min.js` from [KaTeX].

Although the converter only needs `katex.min.js`, you may need to serve the
rest of the KaTeX package, that is, CSS and fonts, as resources to the
targeted web browsers. The upside is that your HTML templates need no longer
include Javascripts for Math (neither `katex.js` nor any search-and-replace
script). Your HTML templates should continue referencing the KaTeX CSS.
If you host your own copy of the CSS, also keep hosting the fonts.

Minimal usage example:

tex_to_html = SsKaTeX.new(katex_js: 'path-to-katex/katex.min.js')
# Here you could verify contents of tex_to_html.js_source for security...

body_html = '<p>By Pythagoras, %s. Furthermore:</p>' %
tex_to_html.call('a^2 + b^2 = c^2', false) # inline math
body_html << # block display
tex_to_html.call('\frac{1}{2} + \frac{1}{3} + \frac{1}{6} = 1', true)
# etc, etc.

More configuration options are described in the Rdoc. Most options, with the
notable exception of `katex_opts`, do not affect usage nor output, but may be
needed to make SsKaTeX work with all the external parts (JS engine and KaTeX).
Since KaTeX is distributed separately from the SsKaTeX gem, configuration of
the latter must support the specification of Javascript file locations. This
implies that execution of arbitrary Javascript code is possible. Specifically,
options with `js` in their names should be accepted from trusted sources only.
Applications using SsKaTeX need to check this.

[duktape.rb]: https://github.com/judofyr/duktape.rb#duktaperb
[ExecJS]: https://github.com/rails/execjs#execjs
[KaTeX]: https://khan.github.io/KaTeX/
[mathjax-node-cli]: https://github.com/mathjax/mathjax-node-cli
[Node.js]: https://nodejs.org/
[therubyracer]: https://github.com/cowboyd/therubyracer#therubyracer
[therubyrhino]: https://github.com/cowboyd/therubyrhino#therubyrhino
8 changes: 8 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'rake/testtask'

Rake::TestTask.new do |t|
t.libs << 'test'
end

desc "Run tests"
task default: [:test]
12 changes: 12 additions & 0 deletions data/sskatex/js/escape_nonascii_html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// -*- coding: utf-8 -*-
//
// Copyright (C) 2017 Christian Cornelssen <ccorn@1tein.de>
//
// This file is part of SsKaTeX which is licensed under the MIT.

// Transform non-ASCII characters and '\0' in given string to HTML numeric character references
function escape_nonascii_html(str) {
return str.replace(/[^\x01-\x7F]/g, function (u) {
return "&#x" + u.charCodeAt(0).toString(16) + ";";
});
};
15 changes: 15 additions & 0 deletions data/sskatex/js/tex_to_html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// -*- coding: utf-8 -*-
//
// Copyright (C) 2017 Christian Cornelssen <ccorn@1tein.de>
//
// This file is part of SsKaTeX which is licensed under the MIT.

// Given a LaTeX math string, a boolean display_mode
// (true for block display, false for inline),
// and a dict katex_opts with general KaTeX options,
// return a string with corresponding HTML+MathML output.
// The implementation is allowed to set katex_opts.displayMode .
function tex_to_html(tex, display_mode, katex_opts) {
katex_opts.displayMode = display_mode;
return escape_nonascii_html(katex.renderToString(tex, katex_opts));
};
Loading

0 comments on commit 54561a8

Please sign in to comment.