Created
August 9, 2011 20:30
-
-
Save ktoso/1135115 to your computer and use it in GitHub Desktop.
Confluence macro to include all files to a page (awesome for keeping XSD/XMLs!)
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
| <!-- | |
| ## @noparams | |
| #requireResource("confluence.web.resources:jquery") | |
| --> | |
| <script type="text/javascript"> | |
| jQuery(document).ready(function() { | |
| var formatXml = this.formatXml = function (xml) { | |
| var reg = /(>)(<)(\/*)/g; | |
| var wsexp = / *(.*) +\n/g; | |
| var contexp = /(<.+>)(.+\n)/g; | |
| xml = xml.replace(reg, '$1\n$2$3').replace(wsexp, '$1\n').replace(contexp, '$1\n$2'); | |
| var pad = 0; | |
| var formatted = ''; | |
| var lines = xml.split('\n'); | |
| var indent = 0; | |
| var lastType = 'other'; | |
| // 4 types of tags - single, closing, opening, other (text, doctype, comment) - 4*4 = 16 transitions | |
| var transitions = { | |
| 'single->single': 0, | |
| 'single->closing': -1, | |
| 'single->opening': 0, | |
| 'single->other': 0, | |
| 'closing->single': 0, | |
| 'closing->closing': -1, | |
| 'closing->opening': 0, | |
| 'closing->other': 0, | |
| 'opening->single': 1, | |
| 'opening->closing': 0, | |
| 'opening->opening': 1, | |
| 'opening->other': 1, | |
| 'other->single': 0, | |
| 'other->closing': -1, | |
| 'other->opening': 0, | |
| 'other->other': 0 | |
| }; | |
| for (var i = 0; i < lines.length; i++) { | |
| var ln = lines[i]; | |
| var single = Boolean(ln.match(/<.+\/>/)); // is this line a single tag? ex. <br /> | |
| var closing = Boolean(ln.match(/<\/.+>/)); // is this a closing tag? ex. </a> | |
| var opening = Boolean(ln.match(/<[^!].*>/)); // is this even a tag (that's not <!something>) | |
| var type = single ? 'single' : closing ? 'closing' : opening ? 'opening' : 'other'; | |
| var fromTo = lastType + '->' + type; | |
| lastType = type; | |
| var padding = ''; | |
| indent += transitions[fromTo]; | |
| for (var j = 0; j < indent; j++) { | |
| padding += '\t'; | |
| } | |
| if (fromTo == 'opening->closing') { | |
| formatted = formatted.substr(0, formatted.length - 1) + ln + '\n'; | |
| } // substr removes line break (\n) from prev loop | |
| else { | |
| formatted += padding + ln + '\n'; | |
| } | |
| } | |
| return formatted; | |
| }; | |
| var initExampleMacro = function() { | |
| var loadFilesHere = jQuery('#loadFilesHere'); | |
| jQuery('a.filename').each(function(i, el) { | |
| if (el.href.indexOf('?version=') === -1) { | |
| console.log("loading file: " + el); | |
| jQuery.get(el, function(data) { | |
| console.log('got data'); | |
| var header = jQuery("<h2>" + el.title + "</h2>") | |
| loadFilesHere.append(header); | |
| var node = jQuery("<pre style='border: black solid 1px; padding: 10px; margin: 10px; background: #f5f4f4'></pre>").text(formatXml(data)); | |
| loadFilesHere.append(node); | |
| }); | |
| } | |
| }); | |
| }; | |
| console.log("Loading attached file previews..."); | |
| initExampleMacro(); | |
| }); | |
| </script> | |
| <!-- | |
| <a href="#" style="font-weight:bold" onclick="loadPreviews()"> | |
| Load file previews | |
| </a> | |
| --> | |
| <pre id="loadFilesHere"> | |
| </pre> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment