<?php
// 2021/03/16
// copied from https://dora.bk.tsukuba.ac.jp/~takeuchi/?pukiwiki%2F%E6%95%B0%E5%BC%8F%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3%2FKaTeX
// then modified.

//
// pukiwiki用 数式プラグイン (katex.inc.php)
//   Copywrite 2018 Osamu Takeuchi <osamu@big.jp>
//
// [履歴]
//   2018.07.02 初期リリース
//
// [インストール]
//   ソースファイルを (pukiwiki)/plugin/katex.inc.php として保存
//
// [使い方]
//   #katex; として一回呼んでおくと、その後の TeX ソース内に
//   記述された katex 形式の tex ソースを katex で処理する
//   ようになる
//

function plugin_katex_header()
{
    return <<<'EOS'
<!--
<link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.css" integrity="sha256-tkzDFSl16wERzhCWg0ge2tON2+D6Qe9iEaJqM4ZGd4E=" crossorigin="anonymous" type="text/css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.js" integrity="sha256-gNVpJCw01Tg4rruvtWJp9vS0rRchXP2YF+U+b2lp8Po=" crossorigin="anonymous" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/contrib/auto-render.min.js" integrity="sha256-ExtbCSBuYA7kq1Pz362ibde9nnsHYPt6JxuxYeZbU+c=" crossorigin="anonymous" type="text/javascript"></script>
-->


<!--  2024/05/29 taken from https://katex.org/docs/browser.html -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.10/dist/katex.min.css" integrity="sha384-wcIxkf4k558AjM3Yz3BBFQUbk/zgIYC2R0QpeeYb+TwlBVMrlgLqwRjRtGZiK7ww" crossorigin="anonymous">
<!-- The loading of KaTeX is deferred to speed up page rendering -->
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.10/dist/katex.min.js" integrity="sha384-hIoBPJpTUs74ddyc4bFZSM1TVlQDA60VBbJS0oA934VSz82sBx1X7kSx2ATBDIyd" crossorigin="anonymous"></script>
<!-- To automatically render math in text elements, include the auto-render extension: -->
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.10/dist/contrib/auto-render.min.js" integrity="sha384-43gviWU0YVjaDtb/GhzOouOXtZMP/7XUzwPTstBeZFe/+rCMvRwr4yROQP43s0Xk" crossorigin="anonymous" onload="renderMathInElement(document.body);"></script>

<script>
  document.addEventListener("DOMContentLoaded", function(){
    renderMathInElement(
      document.body,
      {
        delimiters: [
          {left: "$$", right: "$$", display: true},
          {left: "$", right: "$", display: false}
        ],
        ignoredTags: [
          "script",
          "noscript",
          "style",
          "textarea",
          "pre",
          "code"
        ]
      }
    );
  });
</script>
EOS;
}

function plugin_katex_convert()
{
    return plugin_katex_inline();
}

function plugin_katex_inline()
{
    $header = "";
    if (!defined("PLUGIN_KATEX_LOADED")) {
        define("PLUGIN_KATEX_LOADED", "LOADED");
        $header = plugin_katex_header();
    }

    $aryargs = func_get_args();
    $math = join(",", $aryargs);
    $math = rtrim($math, ",");  //remove extra comma at the end.
    $math = htmlspecialchars($math);

    if($math===""){
        return $header;
    }else{
        return $header . "\\(\\displaystyle\\begin{split}" . $math . "\\end{split}\\)";
    }
}

?>
