diff --git a/components.js b/components.js
index 1a2a881b55..4c1f37ff1f 100644
--- a/components.js
+++ b/components.js
@@ -625,6 +625,11 @@ var components = {
"owner": "zeitgeist87",
"after": "unescaped-markup",
"noCSS": true
+ },
+ "parse-settings": {
+ "title": "Parse Settings",
+ "owner": "zeitgeist87",
+ "noCSS": true
}
}
};
diff --git a/plugins/parse-settings/demo.html b/plugins/parse-settings/demo.html
new file mode 100644
index 0000000000..b33c0c895f
--- /dev/null
+++ b/plugins/parse-settings/demo.html
@@ -0,0 +1,36 @@
+
+
+
+
+
+
+
+ // Example code
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/plugins/parse-settings/index.html b/plugins/parse-settings/index.html
new file mode 100644
index 0000000000..c7433369d6
--- /dev/null
+++ b/plugins/parse-settings/index.html
@@ -0,0 +1,75 @@
+
+
+
+
+
+
+ Parse Settings ▲ Prism plugins
+
+
+
+
+
+
+
+
+
+
+
+
+
+ How to use
+
+ This plugin is not intended to be used on its own.
+ It is a support plugin for other plugins and only included as a dependency.
+
+ All ancestors of the current element are automatically scanned for
+ data-*-attributes and classes. These attributes and classes are then
+ parsed into key-value pairs. An element can inherit the data-*-attributes from its ancestors.
+
+ The plugin author has to declare the settings the plugin uses. The syntax should be self-explanatory
+ and is demonstrated with the following example:
+
+ Prism.plugins.ParseSettings.declare({
+ 'line-numbers': { type: 'bool' },
+ 'user': { type: 'string', 'default': 'user' },
+ 'host': { type: 'string', 'default': 'localhost' },
+ 'spaces-to-tabs': { type: 'int', 'default': 4 },
+ 'left-trim': { type: 'bool', 'default': true }
+});
+
+
+ The following conventions are used to parse the settings:
+
+
+ A normal class name { 'class-name': true }
+ A class name starting with "no-" { 'class-name': false }
+ A boolean setting with the string "true" { 'attr-name': true }
+ A boolean setting with the string "false" { 'attr-name': false }
+ Integers and floats are converted into their respective types { 'attr-name': 4.4 }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/plugins/parse-settings/prism-parse-settings.js b/plugins/parse-settings/prism-parse-settings.js
new file mode 100644
index 0000000000..d67817ab93
--- /dev/null
+++ b/plugins/parse-settings/prism-parse-settings.js
@@ -0,0 +1,97 @@
+(function() {
+
+if (typeof self === 'undefined' || !self.Prism || !self.document) {
+ return;
+}
+
+var assign = Object.assign || function (obj1, obj2) {
+ for (var name in obj2) {
+ if (obj2.hasOwnProperty(name))
+ obj1[name] = obj2[name];
+ }
+ return obj1;
+}
+
+var specs = {},
+ reloadSettings = false;
+
+Prism.plugins.ParseSettings = {
+ declare: function(pluginSpecs) {
+ assign(specs, pluginSpecs);
+ reloadSettings = true;
+ }
+};
+
+function loadSettings(parent) {
+ var settings = {};
+
+ do {
+ for (var i = 0, attrs = parent.attributes; i < attrs.length; ++i) {
+ var name = attrs[i].nodeName,
+ val = attrs[i].nodeValue;
+
+ if (name === 'class') {
+ for (var j = 0, classes = val.split(/\s+/); j < classes.length; ++j) {
+ name = classes[j];
+ if (name.indexOf('no-') === 0) {
+ name = name.substring(3);
+ if (specs[name])
+ settings[name] = false;
+ } else {
+ if (specs[name])
+ settings[name] = true;
+ }
+ }
+ } else if (name.indexOf('data-') === 0) {
+ name = name.substring(5);
+ if (specs[name])
+ settings[name] = val;
+ }
+ }
+ } while ((parent = parent.parentNode) && parent.hasAttribute);
+
+ return settings;
+}
+
+function applySpecs(settings, specs) {
+ var out = {};
+
+ for (var name in specs) {
+ var spec = specs[name] || {},
+ value = settings[name] || spec['default'];
+
+ switch(spec.type) {
+ case 'int':
+ value = parseInt(value);
+ break;
+ case 'boolean':
+ case 'bool':
+ value = (value === 'false' || value === 'no') ? false : (value === '') ? true : !!value;
+ break;
+ case 'number':
+ case 'float':
+ value = parseFloat(value);
+ break;
+ }
+
+ if (value || value === false || value === 0) {
+ out[name] = value;
+ }
+ }
+
+ return out;
+}
+
+function getSettings(pluginSpecs) {
+ if (reloadSettings) {
+ this.rawSettings = loadSettings(this.element);
+ }
+
+ return applySpecs(this.rawSettings || {}, pluginSpecs || specs);
+}
+
+Prism.hooks.add('before-highlight', function (env) {
+ env.getSettings = getSettings;
+});
+
+}());
\ No newline at end of file
diff --git a/plugins/parse-settings/prism-parse-settings.min.js b/plugins/parse-settings/prism-parse-settings.min.js
new file mode 100644
index 0000000000..2d684e53dc
--- /dev/null
+++ b/plugins/parse-settings/prism-parse-settings.min.js
@@ -0,0 +1 @@
+!function(){function e(e){var t={};do for(var n=0,r=e.attributes;n