Created
January 3, 2014 05:10
-
-
Save hecomi/8233097 to your computer and use it in GitHub Desktop.
はてブロで pre 内に行番号出すヤツ
This file contains hidden or 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
// Ref: http://7cc.hatenadiary.jp/entry/hatena-source-line | |
(function(document) { | |
var pres = document.getElementsByTagName('pre'); | |
for (var i = pres.length; i--;) { | |
var elem = makeLineNumber(pres[i]); | |
pres[i].appendChild(elem); | |
} | |
function makeLineNumber(pre) { | |
var ol = document.createElement('ol'), | |
li = document.createElement('li'), | |
df = document.createDocumentFragment(), | |
br = pre.innerHTML.match(/\n/g); | |
ol.className = 'preLine'; | |
ol.setAttribute('role', 'presentation'); | |
for (var i = br.length; i--;) { | |
var cloned_li = li.cloneNode(true); | |
df.appendChild(cloned_li); | |
} | |
ol.appendChild(df); | |
return ol; | |
} | |
})(document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment