diff --git a/.idea/lint-neo-yt.iml b/.idea/lint-neo-yt.iml index 24643cc..7509035 100644 --- a/.idea/lint-neo-yt.iml +++ b/.idea/lint-neo-yt.iml @@ -6,6 +6,7 @@ + diff --git a/.idea/markdown-navigator.xml b/.idea/markdown-navigator.xml new file mode 100644 index 0000000..c57990a --- /dev/null +++ b/.idea/markdown-navigator.xml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/markdown-navigator/profiles_settings.xml b/.idea/markdown-navigator/profiles_settings.xml new file mode 100644 index 0000000..57927c5 --- /dev/null +++ b/.idea/markdown-navigator/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/requirejsPlugin.xml b/.idea/requirejsPlugin.xml new file mode 100644 index 0000000..697a1ef --- /dev/null +++ b/.idea/requirejsPlugin.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/index.html b/index.html index 2a09d15..1cb9309 100644 --- a/index.html +++ b/index.html @@ -22,6 +22,8 @@
  • HTML
  • JSON
  • XML
  • +
  • JavaScript
  • +
  • CSS
  • diff --git a/index.js b/index.js index 5c2d813..90c2500 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,5 @@ /** * Created by neo on 3/6/17. - * @function require */ // can't get browserify-shim to work right @@ -11,92 +10,101 @@ window.$ = window.jQuery = require('jquery'); require('./app.css'); require('bootstrap'); +// use hasher for application page management +var hasher = require('hasher'); +hasher.prependHash = '!'; + +// include/configure highlight.js +var hljs = require('highlight.js'); +hljs.configure({ + tabReplace: ' '.repeat(4) +}); + +// use HTML Lint (will display later) +var htmlLint = require('htmllint'); + +// js-beautify libraries +var beautify_js = require('js-beautify').js; +var beautify_css = require('js-beautify').css; +var beautify_html = require('js-beautify').html; + +// xml-beautify library +var beautify_xml = require('xml-beautifier'); + $('window').ready(function () { - // use hasher for application page management - var hasher = require('hasher'); - - // include highlight.js for display purposes - var hljs = require('highlight.js'); - hljs.configure({ - tabReplace: ' ' - }); - - // use HTML Lint (will display later) - var htmlLint = require('htmllint'); // JSON linting! var jsonLint = function (json) { - if (typeof json != 'string') { + if (typeof json !== 'string') { return JSON.stringify(json, undefined, 4); } else { return jsonLint(JSON.parse(json)); } }; - // (x|ht)ml beautifying! - // function stolen from https://gist.github.com/sente/1083506 - var xmlBeautify = function(xml) { - var formatted = ''; - var reg = /(>)(<)(\/*)/g; - var pad = 0; - - xml.replace("\t", ' '); - xml = xml.replace(reg, '$1\r\n$2$3'); - - $.each(xml.split('\r\n'), function(index, node) { - var indent = 0; - if (node.match( /.+<\/\w[^>]*>$/ )) { - indent = 0; - } else if (node.match( /^<\/\w/ )) { - if (pad != 0) { - pad -= 1; - } - } else if (node.match( /^<\w([^>]*[^\/])?>.*$/ )) { - indent = 1; - } else { - indent = 0; - } - - var padding = ''; - for (var i = 0; i < pad; i++) { - padding += ' '; - } - - formatted += padding + node + '\r\n'; - pad += indent; - }); - - return formatted; - }; - // hasher page changer function var changeType = function(type) { - if (type == '') { - hasher.setHash('html'); - } switch (type) { - case "html": - case "xml": - case "json": - $("#navbar").find(">ul>li.active").removeClass("active"); - $("#"+type+"_tab").addClass('active'); + case 'html': + case 'xml': + case 'json': + case 'js': + case 'css': + $('#navbar').find('>ul>li.active').removeClass('active'); + $('#'+type+'_tab').addClass('active'); - $("#header").text(type.toUpperCase() + ' Linter'); + $('#header').text(type.toUpperCase() + ' Linter'); hasher.setHash(type); break; + + default: + hasher.setHash('html'); } }; - // hasher setup (now that the functions exist) - hasher.prependHash = "!"; + // hasher setup (now that the function exists) hasher.changed.add(changeType); hasher.initialized.add(changeType); hasher.init(); + var beautify = function (type) { + var stringIn = input.val(); + var stringOut = ''; + try { + switch(type) { + case 'html': + htmlLint(stringIn).then(function (lintOutput) { + console.log(lintOutput); + }, function(e) { + console.log(e); + }).catch(function (e) { + console.log(e); + }); + stringOut = hljs.highlightAuto(beautify_html(stringIn)).value; + break; + case 'xml': + stringOut = hljs.highlightAuto(beautify_xml(stringIn)).value; + break; + case 'json': + stringOut = hljs.highlightAuto(jsonLint(stringIn)).value; + break; + case 'js': + stringOut = hljs.highlightAuto(beautify_js(stringIn)).value; + break; + case 'css': + stringOut = hljs.highlightAuto(beautify_css(stringIn)).value; + break; + } + } catch (e) { + stringOut = 'Error: Unable to parse ' + type + '
    ' + e; + } + output.html(stringOut); + }; + // hook into the navbar tabs and use hasher for them - $("#navbar").find(">ul>li").click(function(e) { + $('#navbar').find('>ul>li').click(function(e) { e.preventDefault(); changeType(this.id.replace('_tab', '')); }); @@ -108,30 +116,7 @@ $('window').ready(function () { // function main () input.change(function() { - switch(hasher.getHash()) { - case "html": - case "xml": - try { - output.html(hljs.highlightAuto(xmlBeautify(input.val())).value); - } catch (e) { - output.html("Error: Unable to parse " + hasher.getHash() + "
    " + e); - } - htmlLint(input.val()).then(function (out) { - console.log(out); - }, function(error) { - console.log(error); - }).catch(function (error) { - console.log(error); - }); - break; - case "json": - try { - output.html(hljs.highlightAuto(jsonLint(input.val())).value); - } catch (e) { - output.html("Error: Unable to parse JSON
    " + e); - } - break; - } + beautify(hasher.getHash()) }); }); diff --git a/package.json b/package.json index 2151fe8..0ed574a 100644 --- a/package.json +++ b/package.json @@ -20,9 +20,12 @@ "bulkify": "^1.4.2", "hasher": "^1.2.0", "highlight.js": "^9.9.0", + "html": "^1.0.0", "htmllint": "^0.6.0", "jquery": "^3.1.1", - "uglifyify": "^3.0.4" + "js-beautify": "^1.6.11", + "uglifyify": "^3.0.4", + "xml-beautifier": "^0.3.0" }, "browserify": { "transform": [