• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2016-2018 The Khronos Group Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15require 'asciidoctor/extensions' unless RUBY_ENGINE == 'opal'
16
17include ::Asciidoctor
18
19class ReplaceMathjaxWithKatex < Extensions::Postprocessor
20
21  MathJaXScript = /<script type="text\/x-mathjax-config">((?!<\/script>).)+<\/script>/m
22  MathJaXCDN = '<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.6.0/MathJax.js?config=TeX-MML-AM_HTMLorMML"></script>'
23
24  def process document, output
25
26    if document.attr? 'stem'
27      katexpath = document.attr 'katexpath'
28
29      katexScript = '<link rel="stylesheet" href="' + katexpath + '/katex.min.css">
30<script src="' + katexpath + '/katex.min.js"></script>
31<script src="' + katexpath + '/contrib/auto-render.min.js"></script>
32    <!-- Use KaTeX to render math once document is loaded, see
33         https://github.com/Khan/KaTeX/tree/master/contrib/auto-render -->
34<script>
35    document.addEventListener("DOMContentLoaded", function () {
36        renderMathInElement(
37            document.body,
38            {
39                delimiters: [
40                    { left: "$$", right: "$$", display: true},
41                    { left: "\\\\\[", right: "\\\\\]", display: true},
42                    { left: "$", right: "$", display: false},
43                    { left: "\\\\\(", right: "\\\\\)", display: false}
44                ]
45            }
46        );
47    });
48</script>'
49
50      output.sub! MathJaXScript, ''
51      output.sub! MathJaXCDN, ''
52      output.sub! /(?=<\/head>)/, katexScript
53    end
54    output
55  end
56end
57