JSFiddle - React, Tailwind, and code Playground
by netsi1964
HTML
<link rel="stylesheet" href="http://bootswatch.com/cyborg/bootstrap.min.css">
<div class="container-fluid">
<div class="row-fluid">
<h4>Autogenerate ST2 triggers for DWTemplate Tags v/1</h4>
<input type="text" id="context" placeholder="Enter context (Default=DW)" class="span6" />
<textarea id="result" placeholder="Here completions for DW will be placed"></textarea><br />
<input type="button" id="andAction" value="Generate" class="btn" />
<hr />
<h2>Your source code</h2>
<textarea id="source" placeholder="You should paste the HTML from the page where you have inserted a DwTemplateTags here. Use only the HTML code inside the BODY tag"></textarea>
</div>
<footer class="container-fluid">
Used for Github project which is aimed at creating Dynamicweb CMS support to Sublime Text 2 Editor <a href="https://github.com/rcogm/ST2_DynamicWeb">ST2_DynamicWeb</a>
</footer>
CSS
textarea {width: 90%; height: 10em;}
JavaScript
jQuery(function() {
jQuery('#andAction').on('click', function() {
var context = $.trim($('#context').val());
context = ((context === '') ? 'DW' : context); // +'-'
var log = jQuery('#result');
var template = '{ "trigger": "' + context + '%tag%", "contents": "<!--@%tag%-->" },\n';
var s = '// CONTEXT: "'+context +'"\n';
var bFirst = true;
var sourceHTML = jQuery.trim(jQuery('#source').val());
if (sourceHTML==='') {
alert('Please paste in some HTML code');
return false;
};
var dom = jQuery('<div></div>').html(sourceHTML);
try {
jQuery('table:contains("Index")', dom).find('tr td:nth-child(4n+2)').not(':contains("Global:")').find(':contains("<!--@")').each(function() {
if(!bFirst) {
var dwt = jQuery(this).text().replace(/[<!-@>]/g, '');
s += template.replace(/%tag%/g, dwt);
} else {
bFirst = false;
};
});
s=s.substr(0, s.length-2)+'\n';
s+='// END CONTEXT: "'+context +'"\n';
log.val(s);
alert('Done\nIf nothing is generated, try only pasting content of BODY');
} catch(e) {
log.val(e.message);
};
});
});