217 lines
8.9 KiB
JavaScript
217 lines
8.9 KiB
JavaScript
/*! angular-highlightjs
|
|
version: 0.6.1
|
|
build date: 2016-04-02
|
|
author: Chih-Hsuan Fan
|
|
https://github.com/pc035860/angular-highlightjs.git */
|
|
! function(a, b) {
|
|
"object" == typeof exports || "object" == typeof module && module.exports ? module.exports = b(require("angular"), require("highlight.js")) : "function" == typeof define && define.amd ? define(["angular", "hljs"], b) : a.returnExports = b(a.angular, a.hljs)
|
|
}(this, function(a, b) {
|
|
function c(b) {
|
|
return function(c) {
|
|
switch (c) {
|
|
case "escape":
|
|
return a.isDefined(b.hljsEscape) ? b.hljsEscape : b.escape;
|
|
case "no-escape":
|
|
return a.isDefined(b.hljsNoEscape) ? b.hljsNoEscape : b.noEscape;
|
|
case "onhighlight":
|
|
return a.isDefined(b.hljsOnhighlight) ? b.hljsOnhighlight : b.onhighlight
|
|
}
|
|
}
|
|
}
|
|
|
|
function d(b) {
|
|
var c = !0;
|
|
return a.forEach(["source", "include"], function(a) {
|
|
b[a] && (c = !1)
|
|
}), c
|
|
}
|
|
var e = a.module("hljs", []);
|
|
e.provider("hljsService", function() {
|
|
var c = {};
|
|
return {
|
|
setOptions: function(b) {
|
|
a.extend(c, b)
|
|
},
|
|
getOptions: function() {
|
|
return a.copy(c)
|
|
},
|
|
$get: function() {
|
|
return (b.configure || a.noop)(c), b
|
|
}
|
|
}
|
|
}), e.factory("hljsCache", ["$cacheFactory", function(a) {
|
|
return a("hljsCache")
|
|
}]), e.controller("HljsCtrl", ["hljsCache", "hljsService", "$interpolate", "$window", "$log", function(b, c, d, e, f) {
|
|
function g(a, b, c) {
|
|
var d;
|
|
return function() {
|
|
var f = this,
|
|
g = arguments,
|
|
h = function() {
|
|
d = null, c || a.apply(f, g)
|
|
},
|
|
i = c && !d;
|
|
e.clearTimeout(d), d = e.setTimeout(h, b), i && a.apply(f, g)
|
|
}
|
|
}
|
|
|
|
function h(a, b) {
|
|
var c = b ? "\\\\$&" : "\\$&";
|
|
return a.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, c)
|
|
}
|
|
|
|
function i(a) {
|
|
for (var b, c = [], d = new RegExp(r, "g"), e = "", f = 0; null !== (b = d.exec(a));) e += a.substring(f, b.index) + s, f = b.index + b[0].length, c.push(b[0]);
|
|
return e += a.substr(f), {
|
|
code: e,
|
|
tokens: c
|
|
}
|
|
}
|
|
|
|
function j(a, b) {
|
|
for (var c, d = new RegExp(s, "g"), e = "", f = 0; null !== (c = d.exec(a));) e += a.substring(f, c.index) + b.shift(), f = c.index + c[0].length;
|
|
return e += a.substr(f)
|
|
}
|
|
var k = this,
|
|
l = null,
|
|
m = null,
|
|
n = null,
|
|
o = !1,
|
|
p = null,
|
|
q = null,
|
|
r = h(d.startSymbol()) + "((.|\\s)+?)" + h(d.endSymbol()),
|
|
s = "∫";
|
|
k.init = function(a) {
|
|
l = a
|
|
}, k.setInterpolateScope = function(a) {
|
|
o = a, n && k.highlight(n)
|
|
}, k.setLanguage = function(a) {
|
|
m = a, n && k.highlight(n)
|
|
}, k.highlightCallback = function(a) {
|
|
q = a
|
|
}, k._highlight = function(e) {
|
|
if (l) {
|
|
var f, g, h;
|
|
if (n = e, o && (h = i(e), e = h.code), m ? (g = k._cacheKey(m, !!o, e), f = b.get(g), f || (f = c.highlight(m, c.fixMarkup(e), !0), b.put(g, f))) : (g = k._cacheKey(!!o, e), f = b.get(g), f || (f = c.highlightAuto(c.fixMarkup(e)), b.put(g, f))), e = f.value, o) {
|
|
(p || a.noop)(), h && (e = j(e, h.tokens));
|
|
var r = d(e);
|
|
p = o.$watch(r, function(a, b) {
|
|
a !== b && l.html(a)
|
|
}), l.html(r(o))
|
|
} else l.html(e);
|
|
l.addClass(f.language), null !== q && a.isFunction(q) && q()
|
|
}
|
|
}, k.highlight = g(k._highlight, 17), k.clear = function() {
|
|
l && (n = null, l.text(""))
|
|
}, k.release = function() {
|
|
l = null, o = null, (p || a.noop)(), p = null
|
|
}, k._cacheKey = function() {
|
|
var a = Array.prototype.slice.call(arguments),
|
|
b = "!angular-highlightjs!";
|
|
return a.join(b)
|
|
}
|
|
}]);
|
|
var f, g, h, i, j;
|
|
return f = ["$parse", function(b) {
|
|
return {
|
|
restrict: "EA",
|
|
controller: "HljsCtrl",
|
|
compile: function(e, f, g) {
|
|
var h = e[0].innerHTML.replace(/^(\r\n|\r|\n)/m, ""),
|
|
i = e[0].textContent.replace(/^(\r\n|\r|\n)/m, "");
|
|
return e.html('<pre><code contenteditable="true" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" class="hljs"></code></pre>'),
|
|
function(e, f, g, j) {
|
|
var k, l = c(g);
|
|
if (a.isDefined(l("escape")) ? k = b(l("escape")) : a.isDefined(l("no-escape")) && (k = b("false")), j.init(f.find("code")), l("onhighlight") && j.highlightCallback(function() {
|
|
e.$eval(l("onhighlight"))
|
|
}), (h || i) && d(g)) {
|
|
var m;
|
|
m = k && !k(e) ? i : h, j.highlight(m)
|
|
}
|
|
e.$on("$destroy", function() {
|
|
j.release()
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}], h = function(b) {
|
|
return function() {
|
|
return {
|
|
require: "?hljs",
|
|
restrict: "A",
|
|
link: function(c, d, e, f) {
|
|
f && e.$observe(b, function(b) {
|
|
a.isDefined(b) && f.setLanguage(b)
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}, g = function(a) {
|
|
return function() {
|
|
return {
|
|
require: "?hljs",
|
|
restrict: "A",
|
|
link: function(b, c, d, e) {
|
|
e && b.$watch(d[a], function(a, c) {
|
|
(a || a !== c) && e.setInterpolateScope(a ? b : null)
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}, i = function(a) {
|
|
return function() {
|
|
return {
|
|
require: "?hljs",
|
|
restrict: "A",
|
|
link: function(b, c, d, e) {
|
|
e && b.$watch(d[a], function(a, b) {
|
|
a ? e.highlight(a) : e.clear()
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}, j = function(b) {
|
|
return ["$http", "$templateCache", "$q", function(c, d, e) {
|
|
return {
|
|
require: "?hljs",
|
|
restrict: "A",
|
|
compile: function(f, g, h) {
|
|
var i = g[b];
|
|
return function(b, f, g, h) {
|
|
var j = 0;
|
|
h && b.$watch(i, function(b) {
|
|
var f = ++j;
|
|
if (b && a.isString(b)) {
|
|
var g, i;
|
|
g = d.get(b), g || (i = e.defer(), c.get(b, {
|
|
cache: d,
|
|
transformResponse: function(a, b) {
|
|
return a
|
|
}
|
|
}).success(function(a) {
|
|
f === j && i.resolve(a)
|
|
}).error(function() {
|
|
f === j && h.clear(), i.resolve()
|
|
}), g = i.promise), e.when(g).then(function(b) {
|
|
b && (a.isArray(b) ? b = b[1] : a.isObject(b) && (b = b.data), b = b.replace(/^(\r\n|\r|\n)/m, ""), h.highlight(b))
|
|
})
|
|
} else h.clear()
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}]
|
|
},
|
|
function(b) {
|
|
b.directive("hljs", f), a.forEach(["interpolate", "hljsInterpolate", "compile", "hljsCompile"], function(a) {
|
|
b.directive(a, g(a))
|
|
}), a.forEach(["language", "hljsLanguage"], function(a) {
|
|
b.directive(a, h(a))
|
|
}), a.forEach(["source", "hljsSource"], function(a) {
|
|
b.directive(a, i(a))
|
|
}), a.forEach(["include", "hljsInclude"], function(a) {
|
|
b.directive(a, j(a))
|
|
})
|
|
}(e), "hljs"
|
|
});
|