forked from joelpt/sublimetext-print-to-html
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wordwrap.js
43 lines (36 loc) · 1.82 KB
/
wordwrap.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
// Indent wrapped lines in order to keep them out of the line-numbering column.
// One of these methods should work for a given browser; though this means
// possibly running the hangingIndent routine 3 times, it also ensures we successfully
// wrap all code blocks, all the time, on all tested browsers.
document.addEventListener('DOMContentLoaded', hangingIndentAllCodeBlocks, false);
window.addEventListener('load', hangingIndentAllCodeBlocks, false);
hangingIndentAllCodeBlocks();
// Apply hanging indent CSS to wrapped lines in each div.highlight code block
function hangingIndentAllCodeBlocks() {
var blocks = document.getElementsByClassName('highlight');
for (var i = 0; i < blocks.length; i++) {
hangingIndentCodeBlock(blocks[i]);
}
}
// Apply hanging indent CSS to wrapped lines in given block
function hangingIndentCodeBlock(block) {
var lineNums = block.querySelectorAll('.lineno');
if (lineNums.length == 0) {
return;
}
// Calculate actual width of a character in the line numbers column
var first = lineNums[0];
var text = first.innerText || first.textContent;
var charWidth = first.offsetWidth / text.length;
// Adjustment width is width of line number column plus 1 more character-width
// for the trailing space that comes after the span.lineno element
var adjustWidth = charWidth * (text.length + 1);
// Indent the entire block by adjustWidth ...
block.firstChild.style.marginLeft = adjustWidth + 'px';
// ... and correspondingly de-indent just the starting line-number <span> of
// each line-of-code, resulting in only wrapped lines of code being indented
// past the column of line numbers
for (var i = 0; i < lineNums.length; i++) {
lineNums[i].style.marginLeft = -adjustWidth + 'px';
}
}