motemen (owner)

Revisions

gist: 87495 Download_button fork
public
Public Clone URL: git://gist.github.com/87495.git
Embed All Files: show embed
hatena-diary-replace-mimetex.user.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// ==UserScript==
// @name Hatena::Diary - Replace mimeTeX by ASCIIMathML
// @namespace http://subtech.g.hatena.ne.jp/motemen/
// @include http://d.hatena.ne.jp/*
// @include http://*.g.hatena.ne.jp/*
// @include https://*.g.hatena.ne.jp/*
// ==/UserScript==
// Thanks to ASCIIMathML http://www1.chapman.edu/~jipsen/asciimath.html
 
var texImages = document.evaluate('//img[@class="tex"]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
if (texImages.snapshotLength) {
    var script = document.createElement('script');
        script.type = 'text/javascript';
        //script.src = 'http://www1.chapman.edu/~jipsen/mathml/ASCIIMathML.js';
        script.src = 'http://motemen.g.hatena.ne.jp/files/motemen/be7bcf3f04bd47ec.js';
        script.addEventListener('load', function() {
            unsafeWindow.translateOnLoad = false;
            unsafeWindow.mathcolor = '';
            for (var i = 0; i < unsafeWindow.LMsymbols.length; i++) {
                var s = unsafeWindow.LMsymbols[i];
                if (!s.input.match(/^\\\w+$/)) continue;
                s.input = s.input.replace(/^\\/, '');
                unsafeWindow.AMsymbols.push(s);
            }
            unsafeWindow.AMinitSymbols();
 
            for (var i = 0; i < texImages.snapshotLength; i++) {
                var img = texImages.snapshotItem(i);
                var span = document.createTextNode('amath ' + convertSyntax(img.alt));
                img.parentNode.insertBefore(span, img);
                unsafeWindow.AMprocessNode(span);
                img.style.display = 'none';
            }
        }, true);
    document.getElementsByTagName('head')[0].appendChild(script);
}
 
function convertSyntax(text) {
    return text.replace(/¥/g, '\\')
               .replace(/\\left\\?(.)?\\begin{array}(?:\[.*?\])?(.+?)\\end{array}\\right\\?(.)?/g, function(_, l, m, r) { return '`' + (l == '.' ? '(:' : l) + '(' + m.replace(/&/g, ',').split(/\\\\/).join('),(') + ')' + (r == '.' ? ':)' : r) + '`' })
               .replace(/\*/g, '\\star')
               .replace(/([^\\a-zA-Z])((?:(?!_)\w)+)/g, function(_, s, i) { return s + i.replace(/\B/g, ' ') })
               .replace(/\\textrm\b/g, '\\mathrm')
               .replace(/\\(?:left|right)(?=[\[\]{}()])/g, '')
               .replace(/\\lfloor/g, '|__')
               .replace(/\\rfloor/g, '__|')
               .replace(/\\([{}\[\]])/g, '$1')
               .replace(/}\[/g, '} [');
}
 
// TODO 戻すボタンほしい