Last active
December 19, 2015 12:39
-
-
Save jlewin/5956245 to your computer and use it in GitHub Desktop.
Attempting to build up a simple but usable language definition from wiki output at:
http://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language
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
| var ran = 0, | |
| loops = 0, | |
| helpDoc = { toc: $('#toc ul:first').html(), terms: [] }; | |
| $('.mw-headline').each(function () { | |
| var stash = $('<div>'), | |
| $this = $(this), | |
| context = $this.parent().next(), | |
| l = 0, | |
| helpTerm = { id: $this.text().trim() }; | |
| // Skips and limits while debugging | |
| if (loops++ < 2) return true; // Ignore the first n items | |
| // if(ran++ > 30) return false; // Limit processing to the first n items | |
| // Process the tree by looping until the next element containing a '.mw-headline' child is found | |
| do { | |
| var tag = context[0].tagName.toLowerCase(); | |
| if (l++ == 0 && tag == 'p') { | |
| helpTerm.abstract = context.html(); | |
| context = context.next(); | |
| continue; | |
| } | |
| // Add the markup for this element to the output | |
| stash.append(context.clone()); | |
| // Extract the contents of dl elements into the parameters ojbect | |
| if (tag == "dl") { | |
| var params = []; | |
| context.find('dt').each(function () { | |
| var dt = $(this) | |
| dd = dt.next(); | |
| if (dd.length == 1 && dd[0].tagName.toLowerCase() == 'dd') { | |
| params.push({ term: dt.text().trim(), details: dd.html() }); | |
| } else { | |
| console.log('Missing definition: ', context[0], dt, dd); | |
| } | |
| }); | |
| helpTerm.parameters = params; | |
| } | |
| context = context.next(); | |
| // Continue looping until we find an element which has a '.mw-headline' child | |
| } while (context.find('.mw-headline').length == 0 && context.length > 0); | |
| helpTerm.helpText = stash.html(); | |
| helpDoc.terms.push(helpTerm); | |
| }); | |
| function dumpToTextarea() { | |
| var $footer = $('#footer-info'); | |
| $footer.empty(); | |
| $('<textarea>').css({ width: '100%', height: '300px' }).val(JSON.stringify(helpDoc)).appendTo($footer); | |
| } | |
| // dumpToTextarea(); | |
| // Prevent the Chrome console from dumping the matches from the previous jQuery call | |
| helpDoc; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment