Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env php
- <?php
- $tocScript = <<<JSCSS
- <script>
- Array.prototype.inArray = function (value) {
- var i;
- for (i=0; i < this.length; i++) {
- if (this[i] === value) {
- return true;
- }
- }
- return false;
- };
- function getElementsByTagNames(list,obj) {
- if (!obj) var obj = document;
- var tagNames = list.split(',');
- var resultArray = new Array();
- for (var i=0;i<tagNames.length;i++) {
- var tags = obj.getElementsByTagName(tagNames[i]);
- for (var j=0;j<tags.length;j++) {
- resultArray.push(tags[j]);
- }
- }
- var testNode = resultArray[0];
- if (!testNode) return [];
- if (testNode.sourceIndex) {
- resultArray.sort(function (a,b) {
- return a.sourceIndex - b.sourceIndex;
- });
- }
- else if (testNode.compareDocumentPosition) {
- resultArray.sort(function (a,b) {
- return 3 - (a.compareDocumentPosition(b) & 6);
- });
- }
- return resultArray;
- }
- function createTOC() {
- var y = document.createElement('div');
- y.id = 'innertoc';
- var a = y.appendChild(document.createElement('span'));
- a.onclick = showhideTOC;
- a.id = 'contentheader';
- a.innerHTML = 'show page contents';
- var z = y.appendChild(document.createElement('div'));
- z.onclick = showhideTOC;
- var toBeTOCced = getElementsByTagNames('h1,h2,h3,h4,h5');
- if (toBeTOCced.length < 2) return false;
- var idList = [];
- for (var i=0;i<toBeTOCced.length;i++) {
- var textSrc = toBeTOCced[i];
- var firstNode = textSrc.firstChild;
- if (firstNode && firstNode.nodeName == 'A')
- textSrc = firstNode;
- var tmp = document.createElement('a');
- tmp.innerHTML = textSrc.innerHTML;
- tmp.className = 'page';
- z.appendChild(tmp);
- tmp.className += ' '+toBeTOCced[i].nodeName+'indent';
- var headerId = toBeTOCced[i].id || 'link' + i;
- if (idList.inArray(headerId)) {
- headerId += i;
- toBeTOCced[i].id = headerId;
- }
- idList[i] = headerId;
- tmp.href = '#' + headerId;
- toBeTOCced[i].id = headerId;
- if (toBeTOCced[i].nodeName == 'H1') {
- tmp.innerHTML = 'Top';
- tmp.href = '#top';
- toBeTOCced[i].id = 'top';
- }
- }
- return y;
- }
- var TOCstate = 'none';
- var TOCclass = 'tocClosed';
- function showhideTOC() {
- TOCstate = (TOCstate == 'none') ? 'block' : 'none';
- TOCclass = (TOCstate == 'none') ? 'tocClosed' : 'tocOpened';
- document.getElementById('contentheader').innerHTML = (TOCstate == 'none') ? 'show page contents' : 'hide page contents';
- document.getElementById('innertoc').lastChild.style.display = TOCstate;
- document.getElementById('TOCwrapper').className = TOCclass;
- var offset = (TOCclass=='tocClosed') ? '0px' : window.pageYOffset+'px';
- document.getElementById('TOCwrapper').style.top = offset;
- }
- window.onload = function () {
- var ToC = createTOC();
- if (ToC) {
- var body = document.body;
- var firstChild = body.childNodes[0];
- var wrapper = document.createElement('div');
- wrapper.id = 'TOCwrapper';
- wrapper.className = TOCclass;
- var theToc = wrapper.appendChild(document.createElement('div'));
- theToc.id = 'TOC';
- theToc.appendChild(ToC);
- body.insertBefore(wrapper,firstChild);
- }
- }
- </script>
- <style>
- body { margin-right: 20em; }
- /* TABLE OF CONTENTS */
- div#TOC {
- width: 20em;
- position: absolute;
- top: 0px;
- right: 0px;
- line-height: 110%;
- }
- div#TOC a {
- display: list-item;
- margin-left: 16px;
- font-size: 90%;
- margin-top: 5px;
- }
- div#TOC a.H3indent { margin-left: 3em; }
- div#TOC a.H4indent { margin-left: 4em; }
- div#TOC a.H5indent { margin-left: 5em; }
- div#innertoc div {
- border: 1px solid #999999;
- border-width: 1px 0;
- padding: 10px 0;
- padding-bottom: 25px;
- background-color: #ffffff;
- display: none;
- }
- div#TOCwrapper {
- z-index: 4;
- width: 99%;
- }
- .tocClosed { position: fixed; top: 0;}
- .tocOpened { position: absolute; }
- </style>
- JSCSS;
- $html = str_replace('</head>',"$tocScript\n\t</head>",trim(stream_get_contents(STDIN)));
- $file = str_replace(substr($_ENV['TM_FILEPATH'],strrpos($_ENV['TM_FILEPATH'],'.')), ".html", $_ENV['TM_FILEPATH']);
- $fh = fopen($file,'w');
- fwrite($fh,$html);
- fclose($fh);
- passthru("mate $file");
Add Comment
Please, Sign In to add comment