Skip to content

Commit 73be129

Browse files
authored
Merge pull request #1035 from andymac-2/line-numbers
Added line numbers to editable sections of code.
2 parents 98ecd11 + 81ab2eb commit 73be129

File tree

6 files changed

+20
-2
lines changed

6 files changed

+20
-2
lines changed

book-example/book.toml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mathjax-support = true
99

1010
[output.html.playpen]
1111
editable = true
12+
line-numbers = true
1213

1314
[output.html.search]
1415
limit-results = 20

book-example/src/format/config.md

+2
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ Available configuration options for the `[output.html.playpen]` table:
179179
- **editable:** Allow editing the source code. Defaults to `false`.
180180
- **copy-js:** Copy JavaScript files for the editor to the output directory.
181181
Defaults to `true`.
182+
- **line-numbers** Display line numbers on editable sections of code. Requires both `editable` and `copy-js` to be `true`. Defaults to `false`.
182183

183184
[Ace]: https://ace.c9.io/
184185

@@ -228,6 +229,7 @@ git-repository-icon = "fa-github"
228229
[output.html.playpen]
229230
editable = false
230231
copy-js = true
232+
line-numbers = false
231233

232234
[output.html.search]
233235
enable = true

src/config.rs

+4
Original file line numberDiff line numberDiff line change
@@ -479,13 +479,16 @@ pub struct Playpen {
479479
/// Copy JavaScript files for the editor to the output directory?
480480
/// Default: `true`.
481481
pub copy_js: bool,
482+
/// Display line numbers on playpen snippets
483+
pub line_numbers: bool,
482484
}
483485

484486
impl Default for Playpen {
485487
fn default() -> Playpen {
486488
Playpen {
487489
editable: false,
488490
copy_js: true,
491+
line_numbers: false,
489492
}
490493
}
491494
}
@@ -620,6 +623,7 @@ mod tests {
620623
let playpen_should_be = Playpen {
621624
editable: true,
622625
copy_js: true,
626+
line_numbers: false,
623627
};
624628
let html_should_be = HtmlConfig {
625629
curly_quotes: true,

src/renderer/html_handlebars/hbs_renderer.rs

+3
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,9 @@ fn make_data(
441441

442442
if html_config.playpen.editable && html_config.playpen.copy_js {
443443
data.insert("playpen_js".to_owned(), json!(true));
444+
if html.playpen.line_numbers {
445+
data.insert("playpen_line_numbers".to_owned(), json!(true));
446+
}
444447
}
445448

446449
let search = html_config.search.clone();

src/theme/index.hbs

+6
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@
230230
</script>
231231
{{/if}}
232232

233+
{{#if playpen_line_numbers}}
234+
<script type="text/javascript">
235+
window.playpen_line_numbers = true;
236+
</script>
237+
{{/if}}
238+
233239
{{#if playpen_js}}
234240
<script src="{{ path_to_root }}ace.js" type="text/javascript" charset="utf-8"></script>
235241
<script src="{{ path_to_root }}editor.js" type="text/javascript" charset="utf-8"></script>

src/theme/playpen_editor/editor.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ window.editors = [];
66
}
77

88
Array.from(document.querySelectorAll('.editable')).forEach(function(editable) {
9+
let display_line_numbers = window.playpen_line_numbers || false;
10+
911
let editor = ace.edit(editable);
1012
editor.setOptions({
1113
highlightActiveLine: false,
1214
showPrintMargin: false,
13-
showLineNumbers: false,
14-
showGutter: false,
15+
showLineNumbers: display_line_numbers,
16+
showGutter: display_line_numbers,
1517
maxLines: Infinity,
1618
fontSize: "0.875em" // please adjust the font size of the code in general.css
1719
});

0 commit comments

Comments
 (0)