Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Refactor to simple parser approach #13

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ class Foo {
// end-sample
```

Lines containing a comment `mark-sample` will be marked and the comment will be removed.
The plugin recognizes the following strings as mark comments:

* `// mark-sample`
* `# mark-sample`
* `/* mark-sample */`
* `<!-- mark-sample -->`

```c++
// sample(foo)
class Foo {
void hello() { std::cout << "hello!" << std::endl; } // mark-sample
};
// end-sample
```

### Marking lines in a sample
Specific lines or line ranges can be marked in a sample. To do this, use the
`data-sample-mark` attribute as follows:
Expand All @@ -104,6 +120,27 @@ The line numbers specified in `data-sample-mark` are relative to the snippet
itself, not to the file from which the snippet was extracted. Also, line
ranges are supported, just like for extracting snippets from a file.

### Line numbers
You can let the plugin add line numbers to the sample. `true` adds line numbers starting with 1, a number
adds line number starting with it and `original` copies the line numbers from the source file.

```html
<pre><code data-sample='path/to/source#sample-name' data-sample-line-numbers="true"></code></pre>
<pre><code data-sample='path/to/source#sample-name' data-sample-line-numbers="42"></code></pre>
<pre><code data-sample='path/to/source#sample-name' data-sample-line-numbers="original"></code></pre>
```

The global option `sampler.lineNumbers` allows for `true`, `false` and `'original'` it controls the
line numbers on code blocks without the `data-sample-line-numbers`. The default value is `false` (no line numbers).

```js
{
sampler : {
lineNumbers: true
}
}
```

### Remove indentation
If all lines of the sample have an overall indentation you can remove it using the
attribute `data-sample-indent`.
Expand All @@ -124,8 +161,52 @@ the option `sampler.removeIndentation`. The default value is `false`.
}
```

### Skip lines
To skip lines add a the attribute `data-sample-skip`.

```html
<pre><code data-sample='path/to/source' data-sample-skip="delimiter"></code></pre>
<pre><code data-sample='path/to/source' data-sample-skip="1-3,4"></code></pre>
<pre><code data-sample='path/to/source' data-sample-skip="delimiter,12"></code></pre>
```

#### Skip: delimiter, delimiters

`delimiter` or `delimiters` will skip lines that mark the start/end of a sample. Add this to the option to skip these
lines for whole file or line number samples. This will not affect named samples (they never include that lines).

You can set this globally using the option `sampler.skip`.

```js
{
sampler : {
skip: ['delimiter']
}
}
```

#### Skip: line ranges

The line numbers and ranges specified in `data-sample-skip` are relative to the snippet
itself, not to the file from which the snippet was extracted.

Delimiter lines will count if included, only.

### Proxy URL
Defining the proxy URL will prefix all snippet requests with it. This allows you to use a script to access the
snippet files.

```js
{
sampler : {
proxyURL: 'task.php?file='
}
}
```

If you're trying to load a language that your webserver understands (for example PHP) you
can create a proxy script that passes the source of the file trough. Be careful to
limit the script to the source files you would like to deliver.

### Example

Expand Down
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "reveal-sampler",
"version": "1.0.0",
"description": "A reveal.js plugin to include code samples in slides",
"homepage": "https://github.com/ldionne/reveal-sampler",
"license": "MIT",
Expand All @@ -9,7 +8,7 @@
],
"keywords": [
"reveal-js",
"revieal-plugin",
"reveal-plugin",
"slide",
"sampler"
],
Expand All @@ -18,6 +17,7 @@
],
"ignore": [
"**/.*",
"docs/",
"example/"
]
}
42 changes: 41 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,23 @@ <h2>Ruby code</h2>
<pre><code data-sample='sample.rb#main'></code></pre>
</section>

<section>
<h2>XML markup</h2>
<pre><code data-sample='sample.xml#address' data-sample-indent="remove"></code></pre>
</section>

<section>
<h2>Many samples in the same file</h2>
<pre><code data-sample='many-samples.cpp#sample-1'></code></pre>
<pre><code data-sample='many-samples.cpp#sample-2'></code></pre>
</section>

<section>
<h2>Overlapping samples</h2>
<pre><code data-sample='overlapping-samples.cpp#first'></code></pre>
<pre><code data-sample='overlapping-samples.cpp#second'></code></pre>
</section>

<section>
<h2>Samples with the same tag in a file are concatenated</h2>
<pre><code data-sample='concatenate.cpp#main'></code></pre>
Expand All @@ -68,18 +79,24 @@ <h2>Samples with language specified explicitly</h2>

<section>
<h2>When no sample-name is specified, the whole file is included</h2>
<pre><code data-sample='whole_file.cpp'></code></pre>
<pre><code data-sample='whole_file.cpp' data-sample-skip="delimiter"></code></pre>
</section>

<section>
<h2>Lines 5 to 9</h2>
<pre><code data-sample='whole_file.cpp#5-9'></code></pre>
</section>

<section>
<h2>Lines 12,13,14</h2>
<pre><code data-sample='whole_file.cpp#12,13,14'></code></pre>
</section>

<section>
<h2>Mark some lines in the file</h2>
<pre><code data-sample='mark-sample.cpp#main'></code></pre>
</section>

<section>
<h2>Mark lines 3 and 5</h2>
<pre><code data-sample='sample.cpp#main' data-sample-mark="3,5"></code></pre>
Expand All @@ -95,10 +112,33 @@ <h2>Skip some lines in the file, with line selection</h2>
<pre><code data-sample='skip-sample.cpp#3-9'></code></pre>
</section>

<section>
<h2>Skip line by index</h2>
<h4>fetch 7-9</h4>
<pre><code data-sample='whole_file.cpp#7-9'></code></pre>
<h4>fetch 7-9, skip sample line 2</h4>
<pre><code data-sample='whole_file.cpp#7-9' data-sample-skip="2"></code></pre>
</section>

<section>
<h2>Remove indent</h2>
<pre><code data-sample='sample.c#6' data-sample-indent="remove"></code></pre>
</section>

<section>
<h2>Line numbers</h2>
<pre><code data-sample='whole_file.cpp#5-9' data-sample-line-numbers="true"></code></pre>
</section>

<section>
<h2>Line numbers with offset</h2>
<pre><code data-sample='whole_file.cpp#5-9' data-sample-line-numbers="8"></code></pre>
</section>

<section>
<h2>Line numbers from file</h2>
<pre><code data-sample='whole_file.cpp#5-9' data-sample-line-numbers="original"></code></pre>
</section>
</div>
</div>

Expand Down
9 changes: 9 additions & 0 deletions example/mark-sample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <iostream>

// sample(main)
int main() {
this line should be marked // mark-sample
unmarked line
this line should be marked too /* mark-sample */
}
// end-sample
16 changes: 16 additions & 0 deletions example/overlapping-samples.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// The whole file should be included, even if
// there's something that could match a sample

// sample(first)
#include <iostream>

// sample(second)
void hello() {
std::cout << "Hello world!" << std::endl;
}
// end-sample(first)

int main() {

}
// end-sample(second)
24 changes: 24 additions & 0 deletions example/sample.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8" ?>
<contact>
<firstName>John</firstName>
<lastName>Smith</lastName>
<age>25</age>
<!-- sample(address) -->
<address>
<streetAddress>21 2nd Street</streetAddress> <!-- mark-sample -->
<city>New York</city>
<state>NY</state> <!-- skip-sample -->
<postalCode>10021</postalCode>
</address>
<!-- end-sample -->
<phoneNumbers>
<phoneNumber>
<type>home</type>
<number>212 555-1234</number>
</phoneNumber>
<phoneNumber>
<type>fax</type>
<number>646 555-4567</number>
</phoneNumber>
</phoneNumbers>
</contact>
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "reveal-sampler",
"description": "Embed code samples from files into reveal.js presentations",
"main": "sampler.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"docs": "jsdoc -d docs/ sampler.js README.md"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ldionne/reveal-sampler.git"
},
"keywords": [
"reveal-js",
"reveal-plugin",
"slide",
"slide",
"sampler"
],
"author": "",
"license": "MIT",
"bugs": {
"url": "https://github.com/ldionne/reveal-sampler/issues"
},
"homepage": "https://github.com/ldionne/reveal-sampler#readme"
}
Loading