|
| 1 | +/* code-highlight.js |
| 2 | + Converts static code blocks to read-only CodeMirror instances. |
| 3 | + Handles: <pre><code>, <pre>, .syntax-block, .impl-block */ |
| 4 | + |
| 5 | +(function () { |
| 6 | + function highlight() { |
| 7 | + // Handle <pre> and <pre><code> blocks |
| 8 | + document.querySelectorAll('pre').forEach(function (pre) { |
| 9 | + if (pre.dataset.cmDone) return; |
| 10 | + pre.dataset.cmDone = '1'; |
| 11 | + var code = pre.querySelector('code'); |
| 12 | + var text = code ? code.textContent : pre.textContent; |
| 13 | + |
| 14 | + var wrapper = document.createElement('div'); |
| 15 | + wrapper.className = 'cm-static-wrap'; |
| 16 | + pre.parentNode.insertBefore(wrapper, pre); |
| 17 | + pre.style.display = 'none'; |
| 18 | + |
| 19 | + CodeMirror(wrapper, { |
| 20 | + value: text.replace(/^\n/, ''), |
| 21 | + mode: 'cppmode', |
| 22 | + theme: 'cppmode', |
| 23 | + readOnly: true, |
| 24 | + lineNumbers: false, |
| 25 | + lineWrapping: false, |
| 26 | + scrollbarStyle: 'null', |
| 27 | + viewportMargin: Infinity, |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + // Handle .syntax-block divs (reference pages - function signatures) |
| 32 | + document.querySelectorAll('.syntax-block').forEach(function (el) { |
| 33 | + if (el.dataset.cmDone) return; |
| 34 | + el.dataset.cmDone = '1'; |
| 35 | + var text = el.textContent; |
| 36 | + |
| 37 | + var wrapper = document.createElement('div'); |
| 38 | + wrapper.className = 'cm-static-wrap cm-syntax-wrap'; |
| 39 | + el.parentNode.insertBefore(wrapper, el); |
| 40 | + el.style.display = 'none'; |
| 41 | + |
| 42 | + CodeMirror(wrapper, { |
| 43 | + value: text.replace(/^\n/, ''), |
| 44 | + mode: 'cppmode', |
| 45 | + theme: 'cppmode', |
| 46 | + readOnly: true, |
| 47 | + lineNumbers: false, |
| 48 | + lineWrapping: false, |
| 49 | + scrollbarStyle: 'null', |
| 50 | + viewportMargin: Infinity, |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + // Handle .impl-block divs (reference pages - implementation examples) |
| 55 | + document.querySelectorAll('.impl-block').forEach(function (el) { |
| 56 | + if (el.dataset.cmDone) return; |
| 57 | + el.dataset.cmDone = '1'; |
| 58 | + var text = el.textContent; |
| 59 | + |
| 60 | + var wrapper = document.createElement('div'); |
| 61 | + wrapper.className = 'cm-static-wrap cm-impl-wrap'; |
| 62 | + el.parentNode.insertBefore(wrapper, el); |
| 63 | + el.style.display = 'none'; |
| 64 | + |
| 65 | + CodeMirror(wrapper, { |
| 66 | + value: text.replace(/^\n/, ''), |
| 67 | + mode: 'cppmode', |
| 68 | + theme: 'cppmode', |
| 69 | + readOnly: true, |
| 70 | + lineNumbers: true, |
| 71 | + lineWrapping: false, |
| 72 | + scrollbarStyle: 'native', |
| 73 | + viewportMargin: Infinity, |
| 74 | + }); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + if (document.readyState === 'loading') { |
| 79 | + document.addEventListener('DOMContentLoaded', highlight); |
| 80 | + } else { |
| 81 | + highlight(); |
| 82 | + } |
| 83 | +})(); |
0 commit comments