Commit 6880b3c1c78d832e861691dc5c162e4f22cd7e0b

Andreas Rohner 2016-02-10T11:08:44

Preserve Markup in Normalize-Whitespace plugin Since the Normalize-Whitespace plugin is only concerned with whitespace, it is easy to preserve the markup for other plugins like keep-markup.

diff --git a/plugins/normalize-whitespace/index.html b/plugins/normalize-whitespace/index.html
index 02a000f..e448e4d 100644
--- a/plugins/normalize-whitespace/index.html
+++ b/plugins/normalize-whitespace/index.html
@@ -8,6 +8,19 @@
 	<base href="../.." />
 	<link rel="stylesheet" href="style.css" />
 	<link rel="stylesheet" href="themes/prism.css" data-noprefix />
+	<style type="text/css">
+		code[class*="language-"] mark,
+		pre[class*="language-"] mark {
+			display: inline-block;
+			color: inherit;
+			background: none;
+			border: 1px solid #000;
+			box-shadow: 0 0 2px #fff;
+			padding: 1px;
+			background: rgba(0,0,0,0.2);
+		}
+	</style>
+
 	<script src="prefixfree.min.js"></script>
 
 	<script>var _gaq = [['_setAccount', 'UA-33746269-1'], ['_trackPageview']];</script>
@@ -39,7 +52,8 @@ Prism.plugins.NormalizeWhitespace.setDefaults({
 	'remove-indent': true,
 	'left-trim': true,
 	'right-trim': true,
-	/*'indent': 2,
+	/*'break-lines': 80,
+	'indent': 2,
 	'remove-initial-line-feed': false,
 	'tabs-to-spaces': 4,
 	'spaces-to-tabs': 4*/
@@ -85,6 +99,24 @@ 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>
+
+	<pre>
+
+	<code class="language-css">
+
+
+	@media <mark>screen</mark> {
+		div {
+			<mark>text</mark>-decoration: <mark><mark>under</mark>line</mark>;
+			back<mark>ground: url</mark>('foo.png');
+		}
+	}</code>
+
+
+</pre>
+
+
 </section>
 
 <footer data-src="templates/footer.html" data-type="text/html"></footer>
@@ -92,6 +124,7 @@ Prism.plugins.NormalizeWhitespace.setDefaults({
 <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>
diff --git a/plugins/normalize-whitespace/prism-normalize-whitespace.js b/plugins/normalize-whitespace/prism-normalize-whitespace.js
index f2df35f..ca2796f 100644
--- a/plugins/normalize-whitespace/prism-normalize-whitespace.js
+++ b/plugins/normalize-whitespace/prism-normalize-whitespace.js
@@ -135,7 +135,8 @@ Prism.hooks.add('before-highlight', function (env) {
 	var children = pre.childNodes,
 	    before = '',
 	    after = '',
-	    codeFound = false;
+	    codeFound = false,
+	    Normalizer = Prism.plugins.NormalizeWhitespace;
 
 	// Move surrounding whitespace from the <pre> tag into the <code> tag
 	for (var i = 0; i < children.length; ++i) {
@@ -154,9 +155,16 @@ Prism.hooks.add('before-highlight', function (env) {
 			--i;
 		}
 	}
-	env.code = before + env.code + after;
 
-	env.code = Prism.plugins.NormalizeWhitespace.normalize(env.code, env.settings);
+	if (!env.element.children.length) {
+		env.code = before + env.code + after;
+		env.code = Normalizer.normalize(env.code, env.settings);
+	} else {
+		// Preserve markup for keep-markup plugin
+		var html = before + env.element.innerHTML + after;
+		env.element.innerHTML = Normalizer.normalize(html, env.settings);
+		env.code = env.element.textContent;
+	}
 });
 
 }());
\ 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 11669d7..9243e5c 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=t({},e)}function n(e){return e.replace(/-(\w)/g,function(e,n){return n.toUpperCase()})}function r(e){for(var n=0,r=0;r<e.length;++r)e.charCodeAt(r)=="	".charCodeAt(0)&&(n+=3);return e.length+n}if("undefined"!=typeof self&&self.Prism&&self.document){var t=Object.assign||function(e,n){for(var r in n)n.hasOwnProperty(r)&&(e[r]=n[r]);return e};e.prototype={setDefaults:function(e){this.defaults=t(this.defaults,e)},normalize:function(e,r){r=t(this.defaults,r);for(var i in r){var o=n(i);"normalize"!==i&&"setDefaults"!==o&&r[i]&&this[o]&&(e=this[o].call(this,e,r[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 t=e.split("\n"),i=0;i<t.length;++i)if(!(r(t[i])<=n)){for(var o=t[i].split(/(\s+)/g),a=0,s=0;s<o.length;++s){var l=r(o[s]);a+=l,a>n&&(o[s]="\n"+o[s],a=l)}t[i]=o.join("")}return t.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 r=n.childNodes,t="",i="",o=!1,a=0;a<r.length;++a){var s=r[a];s==e.element?o=!0:"#text"===s.nodeName&&(o?i+=s.nodeValue:t+=s.nodeValue,n.removeChild(s),--a)}e.code=t+e.code+i,e.code=Prism.plugins.NormalizeWhitespace.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){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