Cleanup normalize-whitespace and improve keep-markup integration This patch removes the misleading references to the parse-settings plugin from the normalize-whitespace documentation and adds a description for all supported operations. The keep-markup plugin sets the property Prism.plugins.KeepMarkup to allow the normalize-whitespace plugin to optimize its operation. Furthermore, the normalize-whitespace plugin makes sure, that it is the first plugin to run on a particular code block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
diff --git a/plugins/keep-markup/prism-keep-markup.js b/plugins/keep-markup/prism-keep-markup.js
index 0e20c1e..58e20fd 100644
--- a/plugins/keep-markup/prism-keep-markup.js
+++ b/plugins/keep-markup/prism-keep-markup.js
@@ -4,7 +4,13 @@
return;
}
+ Prism.plugins.KeepMarkup = true;
+
Prism.hooks.add('before-highlight', function (env) {
+ if (!env.element.children.length) {
+ return;
+ }
+
var pos = 0;
var data = [];
var f = function (elt, baseNode) {
diff --git a/plugins/keep-markup/prism-keep-markup.min.js b/plugins/keep-markup/prism-keep-markup.min.js
index b387be7..ecaf13c 100644
--- a/plugins/keep-markup/prism-keep-markup.min.js
+++ b/plugins/keep-markup/prism-keep-markup.min.js
@@ -1 +1 @@
-!function(){"undefined"!=typeof self&&self.Prism&&self.document&&document.createRange&&(Prism.hooks.add("before-highlight",function(e){var n=0,o=[],t=function(e,d){var a={};d||(a.clone=e.cloneNode(!1),a.posOpen=n,o.push(a));for(var r=0,s=e.childNodes.length;s>r;r++){var p=e.childNodes[r];1===p.nodeType?t(p):3===p.nodeType&&(n+=p.data.length)}d||(a.posClose=n)};t(e.element,!0),o&&o.length&&(e.keepMarkup=o)}),Prism.hooks.add("after-highlight",function(e){if(e.keepMarkup&&e.keepMarkup.length){var n=function(e,o){for(var t=0,d=e.childNodes.length;d>t;t++){var a=e.childNodes[t];if(1===a.nodeType){if(!n(a,o))return!1}else 3===a.nodeType&&(!o.nodeStart&&o.pos+a.data.length>o.node.posOpen&&(o.nodeStart=a,o.nodeStartPos=o.node.posOpen-o.pos),o.nodeStart&&o.pos+a.data.length>=o.node.posClose&&(o.nodeEnd=a,o.nodeEndPos=o.node.posClose-o.pos),o.pos+=a.data.length);if(o.nodeStart&&o.nodeEnd){var r=document.createRange();return r.setStart(o.nodeStart,o.nodeStartPos),r.setEnd(o.nodeEnd,o.nodeEndPos),o.node.clone.appendChild(r.extractContents()),r.insertNode(o.node.clone),r.detach(),!1}}return!0};e.keepMarkup.forEach(function(o){n(e.element,{node:o,pos:0})})}}))}();
\ No newline at end of file
+!function(){"undefined"!=typeof self&&self.Prism&&self.document&&document.createRange&&(Prism.plugins.KeepMarkup=!0,Prism.hooks.add("before-highlight",function(e){if(e.element.children.length){var n=0,o=[],t=function(e,d){var r={};d||(r.clone=e.cloneNode(!1),r.posOpen=n,o.push(r));for(var a=0,s=e.childNodes.length;s>a;a++){var p=e.childNodes[a];1===p.nodeType?t(p):3===p.nodeType&&(n+=p.data.length)}d||(r.posClose=n)};t(e.element,!0),o&&o.length&&(e.keepMarkup=o)}}),Prism.hooks.add("after-highlight",function(e){if(e.keepMarkup&&e.keepMarkup.length){var n=function(e,o){for(var t=0,d=e.childNodes.length;d>t;t++){var r=e.childNodes[t];if(1===r.nodeType){if(!n(r,o))return!1}else 3===r.nodeType&&(!o.nodeStart&&o.pos+r.data.length>o.node.posOpen&&(o.nodeStart=r,o.nodeStartPos=o.node.posOpen-o.pos),o.nodeStart&&o.pos+r.data.length>=o.node.posClose&&(o.nodeEnd=r,o.nodeEndPos=o.node.posClose-o.pos),o.pos+=r.data.length);if(o.nodeStart&&o.nodeEnd){var a=document.createRange();return a.setStart(o.nodeStart,o.nodeStartPos),a.setEnd(o.nodeEnd,o.nodeEndPos),o.node.clone.appendChild(a.extractContents()),a.insertNode(o.node.clone),a.detach(),!1}}return!0};e.keepMarkup.forEach(function(o){n(e.element,{node:o,pos:0})})}}))}();
\ No newline at end of file
diff --git a/plugins/normalize-whitespace/demo.html b/plugins/normalize-whitespace/demo.html
index 34e06f4..740475c 100644
--- a/plugins/normalize-whitespace/demo.html
+++ b/plugins/normalize-whitespace/demo.html
@@ -1,9 +1,9 @@
-<body data-prism data-remove-trailing="true">
-<section id="main" class="language-javascript" data-right-trim="true">
+<body>
+<section id="main" class="language-javascript">
<pre>
- <code data-left-trim="true">
+ <code>
var example = {
@@ -20,10 +20,12 @@
</section>
<script src="plugins/normalize-whitespace/prism-normalize-whitespace.js"></script>
<script type="text/javascript">
+// Optional
Prism.plugins.NormalizeWhitespace.setDefaults({
- 'remove-trailing': false,
- 'left-trim': false,
- 'right-trim': false
+ 'remove-trailing': true,
+ 'remove-indent': true,
+ 'left-trim': true,
+ 'right-trim': true,
});
</script>
</body>
\ No newline at end of file
diff --git a/plugins/normalize-whitespace/index.html b/plugins/normalize-whitespace/index.html
index e448e4d..7942b55 100644
--- a/plugins/normalize-whitespace/index.html
+++ b/plugins/normalize-whitespace/index.html
@@ -42,8 +42,7 @@
<p>By default the plugin trims all leading and trailing whitespace of every code block.
It also removes extra indents and trailing whitespace on every line.</p>
- <p>The plugin uses the <a href="plugins/parse-settings/">Parse Settings</a> plugin to get its settings.
- The default settings can be overridden with the <code class="language-javascript">setDefaults()</code> method
+ <p>The default settings can be overridden with the <code class="language-javascript">setDefaults()</code> method
like so:</p>
<pre class="language-javascript"><code>
@@ -60,24 +59,35 @@ Prism.plugins.NormalizeWhitespace.setDefaults({
});
</code></pre>
- <p>It is also possible to change the default settings with <code>data-*</code>-attributes.</p>
-
- <pre><code><code data-left-trim="false">...</code></code></pre>
-
- <p>The class <code>no-whitespace-normalization</code> or the corresponding
- data-attribute can be used to disable the normalization for a particular
- code block.</p>
-
- <pre><code><code class="no-whitespace-normalization"
- data-whitespace-normalization="false">
-...
-</code></code></pre>
+ <p>The following settings are available:</p>
+
+ <dl>
+ <dt>remove-trailing</dt>
+ <dd>Removes trailing whitespace on all lines.</dd>
+ <dt>remove-indent</dt>
+ <dd>If the whole code block is indented too much it removes the extra indent.</dd>
+ <dt>left-trim</dt>
+ <dd>Removes all whitespace from the top of the code block.</dd>
+ <dt>right-trim</dt>
+ <dd>Removes all whitespace from the bottom of the code block.</dd>
+ <dt>break-lines</dt>
+ <dd>Simple way of breaking long lines at a certain length (default is 80 characters).</dd>
+ <dt>indent</dt>
+ <dd>Adds a certain number of tabs to every line.</dd>
+ <dt>remove-initial-line-feed</dt>
+ <dd>Less aggressive version of left-trim.
+ It only removes a single line feed from the top of the code block.</dd>
+ <dt>tabs-to-spaces</dt>
+ <dd>Converts all tabs to a certain number of spaces (default is 4 spaces).</dd>
+ <dt>spaces-to-tabs</dt>
+ <dd>Converts a certain number of spaces to a tab (default is 4 spaces).</dd>
+ </dl>
</section>
<section class="language-markup">
<h1>Examples</h1>
- <p>The following example shows some of the possible ways of configuring this plugin:</p>
+ <p>The following example demonstrates the use of this plugin:</p>
<pre data-src="plugins/normalize-whitespace/demo.html"></pre>
@@ -99,7 +109,7 @@ Prism.plugins.NormalizeWhitespace.setDefaults({
</pre>
- <p>It is also compatible with the <a href="plugins/keep-markup/">keep-markup</a> plugin if you load it after this plugin.</p>
+ <p>It is also compatible with the <a href="plugins/keep-markup/">keep-markup</a> plugin:</p>
<pre>
@@ -122,13 +132,11 @@ Prism.plugins.NormalizeWhitespace.setDefaults({
<footer data-src="templates/footer.html" data-type="text/html"></footer>
<script src="prism.js"></script>
-<script src="plugins/parse-settings/prism-parse-settings.js"></script>
<script src="plugins/normalize-whitespace/prism-normalize-whitespace.js"></script>
<script src="plugins/keep-markup/prism-keep-markup.js"></script>
<script src="utopia.js"></script>
<script src="components.js"></script>
<script src="code.js"></script>
-
</body>
</html>
diff --git a/plugins/normalize-whitespace/prism-normalize-whitespace.js b/plugins/normalize-whitespace/prism-normalize-whitespace.js
index ca2796f..bb5a78c 100644
--- a/plugins/normalize-whitespace/prism-normalize-whitespace.js
+++ b/plugins/normalize-whitespace/prism-normalize-whitespace.js
@@ -156,7 +156,7 @@ Prism.hooks.add('before-highlight', function (env) {
}
}
- if (!env.element.children.length) {
+ if (!env.element.children.length || !Prism.plugins.KeepMarkup) {
env.code = before + env.code + after;
env.code = Normalizer.normalize(env.code, env.settings);
} else {
@@ -166,5 +166,8 @@ Prism.hooks.add('before-highlight', function (env) {
env.code = env.element.textContent;
}
});
+// Make sure our callback runs first
+var hooks = Prism.hooks.all['before-highlight'];
+hooks.unshift(hooks.pop());
}());
\ No newline at end of file
diff --git a/plugins/normalize-whitespace/prism-normalize-whitespace.min.js b/plugins/normalize-whitespace/prism-normalize-whitespace.min.js
index 9243e5c..7511b25 100644
--- a/plugins/normalize-whitespace/prism-normalize-whitespace.min.js
+++ b/plugins/normalize-whitespace/prism-normalize-whitespace.min.js
@@ -1 +1 @@
-!function(){function e(e){this.defaults=r({},e)}function n(e){return e.replace(/-(\w)/g,function(e,n){return n.toUpperCase()})}function t(e){for(var n=0,t=0;t<e.length;++t)e.charCodeAt(t)==" ".charCodeAt(0)&&(n+=3);return e.length+n}if("undefined"!=typeof self&&self.Prism&&self.document){var r=Object.assign||function(e,n){for(var t in n)n.hasOwnProperty(t)&&(e[t]=n[t]);return e};e.prototype={setDefaults:function(e){this.defaults=r(this.defaults,e)},normalize:function(e,t){t=r(this.defaults,t);for(var i in t){var o=n(i);"normalize"!==i&&"setDefaults"!==o&&t[i]&&this[o]&&(e=this[o].call(this,e,t[i]))}return e},leftTrim:function(e){return e.replace(/^\s+/,"")},rightTrim:function(e){return e.replace(/\s+$/,"")},tabsToSpaces:function(e,n){return n=0|n||4,e.replace(/\t/g,new Array(++n).join(" "))},spacesToTabs:function(e,n){return n=0|n||4,e.replace(new RegExp(" {"+n+"}","g")," ")},removeTrailing:function(e){return e.replace(/\s*?$/gm,"")},removeInitialLineFeed:function(e){return e.replace(/^(?:\r?\n|\r)/,"")},removeIndent:function(e){var n=e.match(/^[^\S\n\r]*(?=\S)/gm);return n&&n[0].length?(n.sort(function(e,n){return e.length-n.length}),n[0].length?e.replace(new RegExp("^"+n[0],"gm"),""):e):e},indent:function(e,n){return e.replace(/^[^\S\n\r]*(?=\S)/gm,new Array(++n).join(" ")+"$&")},breakLines:function(e,n){n=n===!0?80:0|n||80;for(var r=e.split("\n"),i=0;i<r.length;++i)if(!(t(r[i])<=n)){for(var o=r[i].split(/(\s+)/g),a=0,l=0;l<o.length;++l){var s=t(o[l]);a+=s,a>n&&(o[l]="\n"+o[l],a=s)}r[i]=o.join("")}return r.join("\n")}},Prism.plugins.NormalizeWhitespace=new e({"remove-trailing":!0,"remove-indent":!0,"left-trim":!0,"right-trim":!0}),Prism.hooks.add("before-highlight",function(e){var n=e.element.parentNode;if(e.code&&n&&"pre"===n.nodeName.toLowerCase()&&(!e.settings||e.settings["whitespace-normalization"]!==!1)){for(var t=n.childNodes,r="",i="",o=!1,a=Prism.plugins.NormalizeWhitespace,l=0;l<t.length;++l){var s=t[l];s==e.element?o=!0:"#text"===s.nodeName&&(o?i+=s.nodeValue:r+=s.nodeValue,n.removeChild(s),--l)}if(e.element.children.length){var c=r+e.element.innerHTML+i;e.element.innerHTML=a.normalize(c,e.settings),e.code=e.element.textContent}else e.code=r+e.code+i,e.code=a.normalize(e.code,e.settings)}})}}();
\ No newline at end of file
+!function(){function e(e){this.defaults=r({},e)}function n(e){return e.replace(/-(\w)/g,function(e,n){return n.toUpperCase()})}function t(e){for(var n=0,t=0;t<e.length;++t)e.charCodeAt(t)==" ".charCodeAt(0)&&(n+=3);return e.length+n}if("undefined"!=typeof self&&self.Prism&&self.document){var r=Object.assign||function(e,n){for(var t in n)n.hasOwnProperty(t)&&(e[t]=n[t]);return e};e.prototype={setDefaults:function(e){this.defaults=r(this.defaults,e)},normalize:function(e,t){t=r(this.defaults,t);for(var i in t){var o=n(i);"normalize"!==i&&"setDefaults"!==o&&t[i]&&this[o]&&(e=this[o].call(this,e,t[i]))}return e},leftTrim:function(e){return e.replace(/^\s+/,"")},rightTrim:function(e){return e.replace(/\s+$/,"")},tabsToSpaces:function(e,n){return n=0|n||4,e.replace(/\t/g,new Array(++n).join(" "))},spacesToTabs:function(e,n){return n=0|n||4,e.replace(new RegExp(" {"+n+"}","g")," ")},removeTrailing:function(e){return e.replace(/\s*?$/gm,"")},removeInitialLineFeed:function(e){return e.replace(/^(?:\r?\n|\r)/,"")},removeIndent:function(e){var n=e.match(/^[^\S\n\r]*(?=\S)/gm);return n&&n[0].length?(n.sort(function(e,n){return e.length-n.length}),n[0].length?e.replace(new RegExp("^"+n[0],"gm"),""):e):e},indent:function(e,n){return e.replace(/^[^\S\n\r]*(?=\S)/gm,new Array(++n).join(" ")+"$&")},breakLines:function(e,n){n=n===!0?80:0|n||80;for(var r=e.split("\n"),i=0;i<r.length;++i)if(!(t(r[i])<=n)){for(var o=r[i].split(/(\s+)/g),a=0,l=0;l<o.length;++l){var s=t(o[l]);a+=s,a>n&&(o[l]="\n"+o[l],a=s)}r[i]=o.join("")}return r.join("\n")}},Prism.plugins.NormalizeWhitespace=new e({"remove-trailing":!0,"remove-indent":!0,"left-trim":!0,"right-trim":!0}),Prism.hooks.add("before-highlight",function(e){var n=e.element.parentNode;if(e.code&&n&&"pre"===n.nodeName.toLowerCase()&&(!e.settings||e.settings["whitespace-normalization"]!==!1)){for(var t=n.childNodes,r="",i="",o=!1,a=Prism.plugins.NormalizeWhitespace,l=0;l<t.length;++l){var s=t[l];s==e.element?o=!0:"#text"===s.nodeName&&(o?i+=s.nodeValue:r+=s.nodeValue,n.removeChild(s),--l)}if(e.element.children.length&&Prism.plugins.KeepMarkup){var u=r+e.element.innerHTML+i;e.element.innerHTML=a.normalize(u,e.settings),e.code=e.element.textContent}else e.code=r+e.code+i,e.code=a.normalize(e.code,e.settings)}});var i=Prism.hooks.all["before-highlight"];i.unshift(i.pop())}}();
\ No newline at end of file