Skip to content

Commit 80e3d03

Browse files
committed
Remove overall indentation
1 parent b04a34e commit 80e3d03

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,29 @@ The line numbers specified in `data-sample-mark` are relative to the snippet
104104
itself, not to the file from which the snippet was extracted. Also, line
105105
ranges are supported, just like for extracting snippets from a file.
106106

107+
### Remove indentation
108+
If all lines of the sample have an overall indentation you can remove it using the
109+
attribute `data-sample-indent`.
110+
111+
```html
112+
<pre><code data-sample='path/to/source#sample-name' data-sample-indent="remove"></code></pre>
113+
<pre><code data-sample='path/to/source#sample-name' data-sample-indent="keep"></code></pre>
114+
```
115+
116+
You can change the default behaviour (snippets without the attribute) using
117+
an the option `sampler.removeIndentation`.
118+
119+
```js
120+
{
121+
sampler : {
122+
removeIndentation: true
123+
}
124+
}
125+
```
126+
127+
128+
129+
107130
### Example
108131

109132
It's that simple! To get started, you can find an example of using the plugin

example/index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ <h2>Skip some lines in the file</h2>
9494
<h2>Skip some lines in the file, with line selection</h2>
9595
<pre><code data-sample='skip-sample.cpp#3-9'></code></pre>
9696
</section>
97+
98+
<section>
99+
<h2>Remove indent</h2>
100+
<pre><code data-sample='sample.c#6' data-sample-indent="remove"></code></pre>
101+
</section>
97102
</div>
98103
</div>
99104

sampler.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,33 @@
141141
return function(code) { return postProcess(extractor(code)); };
142142
};
143143

144+
// To remove the indentation find the shortest leading whitespace sequence
145+
// and remove it from all lines.
146+
var removeIndentationFromLines = function(code) {
147+
var indent, indentPattern;
148+
var indentMatch = code.match(/^[ \t]+/mg);
149+
if (indentMatch) {
150+
indent = indentMatch.reduce(
151+
function(previousValue, currentValue) {
152+
if (previousValue.length < currentValue.length) {
153+
return previousValue;
154+
}
155+
return currentValue;
156+
}
157+
);
158+
indentPattern = new RegExp('^' + indent + '', 'mg');
159+
return code.replace(indentPattern, '');
160+
}
161+
return code;
162+
};
163+
164+
// Read configuration options
165+
var config = Reveal.getConfig() || {};
166+
config.sampler = config.sampler || {};
167+
var options = {
168+
removeIndentation: !!config.sampler.removeIndentation
169+
};
170+
144171
var elements = document.querySelectorAll('[data-sample]');
145172
elements.forEach(function(element) {
146173
var slug = element.getAttribute('data-sample').match(/([^#]+)(?:#(.+))?/);
@@ -155,6 +182,22 @@
155182
throw "Could not find sample '" + selector + "' in file '" + file + "'.";
156183
}
157184

185+
// Read indentation behaviour defined by attribute or global option
186+
var removeIndentation;
187+
switch (element.getAttribute('data-sample-indent')) {
188+
case 'keep' :
189+
removeIndentation = false;
190+
break;
191+
case 'remove':
192+
removeIndentation = true;
193+
break;
194+
default :
195+
removeIndentation = options.removeIndentation;
196+
}
197+
if (removeIndentation) {
198+
sample = removeIndentationFromLines(sample);
199+
}
200+
158201
// Mark lines in the sample, if requested.
159202
var marked = expandRangesToLinesIndex(
160203
parseRanges(element.getAttribute('data-sample-mark') || '')

0 commit comments

Comments
 (0)