|
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 |
0 commit comments