(function ($){ var markup = { } ; $.fn.extend({ markup: function (id, data){ var result = this; this.each(function (){ var el = $.markup(id, data); $(this).append(el); result = el; } ); return result; } } ); $.markup = function (id, data){ if (typeof markup[id] === "undefined") throw new Error("jquery: markup: ERROR: no such markup with id '" + id + "' found") return $($.parseHTML(markup[id](data))); } ; $.markup.version = "1.0.2"; $.markup.debug = 0; var debug = function (level, msg){ if ($.markup.debug >= level && typeof console !== "undefined" && typeof console.log === "function") console.log("jquery: markup: DEBUG[" + level + "]: " + msg); } ; var esctxt = function (txt){ return "\"" + _AN_Call_replace("replace", _AN_Call_replace("replace", _AN_Call_replace("replace", txt, /\r/g, "\\r"), /\n/g, "\\n"), /"/g, "\\\"") + "\""; } ; var registry = { } ; $.markup.register = function (lang){ registry[lang.id] = lang; } ; var compile = function (type, id, txt){ debug(1, "compile: type=" + type + " id=" + id); debug(4, "compile: txt=" + esctxt(txt)); if (typeof registry[type] === "undefined") throw new Error("jquery: markup: ERROR: no template language registered under id '" + type + "'") if (!registry[type].available()) throw new Error("jquery: markup: ERROR: template language '" + type + "' (" + registry[type].name + ", " + (_AN_Read_url("url", registry[type])) + ") " + "known but not found under run-time") markup[id] = registry[type].compile(txt); } ; var parse = function (type, txt){ debug(3, "parse: type=" + type + " txt=" + esctxt(txt)); var section = [] ; section.push({ txt: "", attr: { id: "", type: type, include: "false"} } ); var m; while (txt !== ""){ if ((m = txt.match(/^/)) !== null ) { debug(4, "parse: section opening tag: " + esctxt(m[0])); var attr = { } ; var txt2 = m[1], m2; while ((m2 = txt2.match(/^\s+([a-z]+)="([^"]*)"/)) !== null ){ debug(4, "parse: section attribute: name=" + esctxt(m2[1]) + " value=" + esctxt(m2[2])); attr[m2[1]] = m2[2].match(/^true|false$/)? (m2[2] === "true"): m2[2]; txt2 = txt2.substr(_AN_Read_length("length", m2[0])); } if (typeof attr.id === "undefined") throw new Error("jquery: markup: ERROR: missing required 'id' attribute") if (section[_AN_Read_length("length", section) - 1].attr.id !== "") attr.id = section[_AN_Read_length("length", section) - 1].attr.id + "/" + attr.id; if (typeof attr.type === "undefined") attr.type = section[_AN_Read_length("length", section) - 1].attr.type; if (typeof attr.include === "undefined") attr.include = false ; section.push({ txt: "", attr: attr} ); txt = txt.substr(_AN_Read_length("length", m[0])); } else if ((m = txt.match(/^<\/markup\s*>/)) !== null ) { debug(4, "parse: section closing tag: " + esctxt(m[0])); var s = section.pop(); compile(s.attr.type, s.attr.id, s.txt); if (s.attr.include) section[_AN_Read_length("length", section) - 1].txt += s.txt; txt = txt.substr(_AN_Read_length("length", m[0])); } else if ((m = txt.match(/^((?:.|\r?\n)+?)(?=<\/?markup|$)/)) !== null ) { debug(4, "parse: plain-text (arbitrary long): " + esctxt(m[0])); section[_AN_Read_length("length", section) - 1].txt += m[1]; txt = txt.substr(_AN_Read_length("length", m[0])); } else { debug(4, "parse: plain-text (single character fallback): " + esctxt(txt.substr(0, 1))); section[_AN_Read_length("length", section) - 1].txt += txt.substr(0, 1); txt = txt.substr(1); } } } ; var queue = [] ; $.markup.queue = function (url, type){ debug(2, "queue: url=" + url + " type=" + type); queue.push({ url: url, type: type} ); } ; $.markup.load = function (onDone){ debug(1, "load: loading all queued markup requests"); var todo = queue; queue = [] ; var done = 0; for (var i = 0; i < _AN_Read_length("length", todo); i++ ){ var type = todo[i].type; debug(2, "load: url=" + _AN_Read_url("url", todo[i]) + " type=" + type); $.get(_AN_Read_url("url", todo[i]), function (txt){ parse(type, txt); done++ ; if (done >= _AN_Read_length("length", todo)) onDone(); } ); } } ; $(document).ready(function (){ debug(1, "ready: processing all tags"); $("head > link").each(function (){ var h = $(this).attr("href"); var r = $(this).attr("rel"); var t = $(this).attr("type"); if (h !== "" && r !== "" && t !== "") { var mr = r.match(/^markup(?:\/([a-z]+))?$/); var mt = t.match(/^text\/(?:html|x-markup-([a-z]+))$/); if (mr !== null && mt !== null ) { var type = (typeof mr[1] === "string" && mr[1] !== "")? mr[1]: (typeof mt[1] === "string" && mt[1] !== "")? mt[1]: "plain"; $.markup.queue(h, type); } } } ); } ); var isfn = function (path){ var p = path.split(/\./); var o = window; for (var i = 0; i < _AN_Read_length("length", p); i++ ){ o = o[p[i]]; if ((i < _AN_Read_length("length", p) - 1 && typeof o === "undefined") || (i === _AN_Read_length("length", p) - 1 && typeof o !== "function")) return false ; } return true ; } ; $.markup.register({ id: "plain", name: "Plain HTML", url: "-", available: function (){ return true ; } , compile: function (txt){ return function (){ return txt; } ; } } ); $.markup.register({ id: "handlebars", name: "Handlebars", url: "http://handlebarsjs.com/", available: function (){ return isfn("Handlebars.compile"); } , compile: function (txt){ return Handlebars.compile(txt); } } ); $.markup.register({ id: "dust", name: "DUST", url: "http://akdubya.github.io/dustjs/", available: function (){ return isfn("dust.compile"); } , compile: function (txt){ return dust.compile(txt); } } ); $.markup.register({ id: "jade", name: "Jade", url: "http://jade-lang.com/", available: function (){ return isfn("jade.compile"); } , compile: function (txt){ return jade.compile(txt); } } ); $.markup.register({ id: "mustache", name: "Mustache", url: "http://mustache.github.io/", available: function (){ return isfn("Mustache.compile"); } , compile: function (txt){ return Mustache.compile(txt); } } ); $.markup.register({ id: "haml", name: "HAML-JS", url: "https://github.com/creationix/haml-js", available: function (){ return isfn("Haml"); } , compile: function (txt){ return Haml(txt); } } ); $.markup.register({ id: "underscore", name: "Underscore Template", url: "http://underscorejs.org/", available: function (){ return isfn("_.template"); } , compile: function (txt){ return _.template(txt); } } ); $.markup.register({ id: "markup", name: "Markup.js", url: "https://github.com/adammark/Markup.js/", available: function (){ return isfn("Mark.up"); } , compile: function (txt){ return function (data){ return Mark.up(txt, data); } ; } } ); $.markup.register({ id: "markup", name: "Markup.js", url: "http://emmet.io/", available: function (){ return isfn("emmet.expandAbbreviation"); } , compile: function (txt){ return function (){ return emmet.expandAbbreviation(txt); } ; } } ); } )(jQuery);