<script>
(function () {
    var codes = document.querySelectorAll("#content pre code:not(.language-mermaid)");
    if (!codes.length) return;

    function registerGcode() {
        if (window.hljs.getLanguage("gcode")) return;
        window.hljs.registerLanguage("gcode", function (hljs) {
            return {
                name: "G-code (ISO 6983)",
                aliases: ["nc"],
                case_insensitive: true,
                keywords: {
                    $pattern: "[A-Z_][A-Z0-9_.]*",
                    keyword: "IF DO WHILE ENDWHILE CALL ENDIF SUB ENDSUB GOTO REPEAT ENDREPEAT EQ LT GT NE GE LE OR XOR"
                },
                contains: [
                    { className: "meta", begin: /[O][0-9]+/ },
                    { className: "meta", begin: /[%]/ },
                    hljs.C_LINE_COMMENT_MODE,
                    hljs.C_BLOCK_COMMENT_MODE,
                    hljs.COMMENT(/\(/, /\)/),
                    { className: "number", begin: /[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)/ },
                    { className: "string", begin: /["]/, end: /["]/, illegal: null },
                    { className: "name", begin: /[G][0-9]+([.][0-9]+)?/ },
                    { className: "name", begin: /[M][0-9]+([.][0-9]+)?/ },
                    { className: "attr", begin: /(VC|VS)[0-9]+/ },
                    { className: "attr", begin: /[#][0-9]+/ },
                    { className: "built_in", begin: /(ATAN|ABS|ACOS|ASIN|SIN|COS|EXP|FIX|FUP|ROUND|LN|TAN)(\[)/, end: /[\]]/, contains: [{ className: "number", begin: /[+-]?[0-9.]+/ }] },
                    { className: "symbol", begin: /[N][0-9]+/ }
                ]
            };
        });
    }

    function apply() {
        if (!window.hljs) return;
        registerGcode();
        codes.forEach(function (c) {
            c.classList.add("hljs");
            hljs.highlightElement(c);
            if (window.hljs.lineNumbersBlock) {
                hljs.lineNumbersBlock(c);
            }
        });
    }

    function loadPlugin() {
        if (window.hljs && window.hljs.lineNumbersBlock) { apply(); return; }
        var s = document.createElement("script");
        s.src = "api/notes/__HLJS_LN_ID__/download";
        s.onload = apply;
        document.head.appendChild(s);
    }

    if (window.hljs) { loadPlugin(); return; }

    var s = document.createElement("script");
    s.src = "api/notes/__HLJS_ID__/download";
    s.onload = loadPlugin;
    document.head.appendChild(s);
})();
</script>