Re: off-topic (?) Javascript um Latex Dokumente zu de-macro-en

Uwe Brauer <[email protected]>
Newsgroups gmane.comp.tex.german
Message-ID <[email protected]>
>>> "AB" == Axel Berger <(via tex-d-l Mailing List) <[email protected]>> writes:

> "Albrecht Backhaus (via tex-d-l Mailing List)" wrote:
>> Wofür bräuchte man dann so ein Script ?


[Snip]...

Hallo Albrecht,


> Für Uwe:
> Skripte rufen sehr oft im Lauf andere Skripte auf. Ein Webkopierer, der nur
> das HTML auswertet, findet die dann nicht. Lied einmal das Skript, das Du
> schon hast und suche dort nach URLs.

Danke, ich erlaube mir mal das Skript hier zu posten

--8<---------------cut here---------------start------------->8---
function expandMacros(tex) {
    function nestBrackets(level) {
        var level = level || 5, re = c = "(?:[^\\r\\n\\{\\}]|\\\\[\\{\\}]|\\r?\\n(?!\\r?\\n))*?";
        while (level--) re = c + "(?:\\{" + re + "\}" + c + ")*?";
        return " *(\\{" + re + "\\}|[^\\{])";
    }    
    function getRegExp(name, macro) {
        var num = macro.num, def = macro.def, re = "";
        while (num--) re += nestBrackets();
        re = "\\" + name + "(?![a-zA-Z\\}])" + re;
        return new RegExp(re, "g");
    }
    function trimString(s) {
        return s.replace(/^ +| +$/g, '').replace(/^\{|\}$/g, "");
    }
    function extractMacros() {
        var cs = "\\\\\\w+", re;
        // \def, \gdef, \edef and \xdef
        re = new RegExp("\\\\[gex]?def\\*? *(" + cs + ") *(#\\d)*" + nestBrackets(), "g");
        tex = tex.replace(re, function(match){
            var m = arguments;
            var macro = {
                num:  m[2] ? Math.min(m[2].length / 2, 9) : 0,
                def:  trimString(m[3])
            };
            macros[trimString(m[1])] = macro;
            return "";
        });
        // \newcommand, \newcommand*, \renewcommand and \renewcommand*
        re = new RegExp("\\\\(?:re)?newcommand\\*? *(" + cs + "|\\{" + cs + "\}) *(\\[(\\d)\\])?"
                        + nestBrackets(), "g");
        tex = tex.replace(re, function(match){
            var m = arguments;
            var macro = {
                num:  m[3] || 0,
                def:  trimString(m[4])
            };
            macros[trimString(m[1])] = macro;
            return "";
        });
        // \DeclareMathOperator and \DeclareMathOperator* inside amsmath
        re = new RegExp("\\\\DeclareMathOperator(\\*?) *(" + cs + "|\\{" + cs + "\}) *"
                        + nestBrackets(), "g");
        tex = tex.replace(re, function(match){
            var m = arguments;
            var macro = {
                num:  0,
                def:  "\\operatorname" + m[1] + "{" + trimString(m[3]) + "}"
            };
            macros[trimString(m[2])] = macro;
            return "";
        });
    }
    function replaceMacros() {
        var i = 0, m, re, num;
        for (name in macros) {
            m = macros[name];
            re = getRegExp(name, m), num = m.num;
            //console.log(re);
            tex = tex.replace(re, function(match){
                //console.log(arguments);
                var args = [], result = m.def, k;
                for (k = 1; k <= num; k++) {
                    args[k] = trimString(arguments[k]);
                }
                //console.log(args);
                for (k = 1; k <= num; k++) {
                    result = result.replace(new RegExp("#" + k, "g"), args[k]);
                }
                return result;
            });
        }
    }
    var macros = {};
    extractMacros();
    replaceMacros();
    return tex;
}

document.getElementById("run").onclick = function() {
    var input = document.getElementById("input"),
        output = document.getElementById("output");
    output.value = expandMacros(input.value);
}
--8<---------------cut here---------------end--------------->8---

ich habe mir noch einmal den index.html in 
https://jsfiddle.net/zoho/ed84y/

Angeschaut, da wird dieses Skript aufgerufen.

Ich verstehe das nicht.

Grüße

Uwe Braure 


> -- 
> /¯\   No  |    Dipl.-Ing. F. Axel Berger    Tel: +49/ 221/ 7771 8067
> \ /  HTML |    Roald-Amundsen-Straße 2a     Fax: +49/ 221/ 7771 8069
>  X    in  |    D-50829 Köln-Ossendorf      http://berger-odenthal.de
> / \  Mail | -- No unannounced, large, binary attachments, please! --

-- 
I strongly condemn Hamas heinous atrocities on Israel, especially the despicable pogroms.
I strongly condemn Putin's war of aggression against Ukraine.
I support to deliver weapons to Ukraine's military. 
I support the EU and NATO membership of Ukraine.
smime.p7s (application/pkcs7-signature, 5.5 KB) - not displayed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.