Created
March 29, 2009 19:28
-
-
Save motemen/87495 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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 戻すボタンほしい |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment