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

Added line numbers to editable sections of code. #1035

Merged
merged 1 commit into from
Sep 26, 2019
Merged
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
1 change: 1 addition & 0 deletions book-example/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mathjax-support = true

[output.html.playpen]
editable = true
line-numbers = true

[output.html.search]
limit-results = 20
Expand Down
2 changes: 2 additions & 0 deletions book-example/src/format/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ Available configuration options for the `[output.html.playpen]` table:
- **editable:** Allow editing the source code. Defaults to `false`.
- **copy-js:** Copy JavaScript files for the editor to the output directory.
Defaults to `true`.
- **line-numbers** Display line numbers on editable sections of code. Requires both `editable` and `copy-js` to be `true`. Defaults to `false`.

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

Expand Down Expand Up @@ -228,6 +229,7 @@ git-repository-icon = "fa-github"
[output.html.playpen]
editable = false
copy-js = true
line-numbers = false

[output.html.search]
enable = true
Expand Down
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,13 +472,16 @@ pub struct Playpen {
/// Copy JavaScript files for the editor to the output directory?
/// Default: `true`.
pub copy_js: bool,
/// Display line numbers on playpen snippets
pub line_numbers: bool,
}

impl Default for Playpen {
fn default() -> Playpen {
Playpen {
editable: false,
copy_js: true,
line_numbers: false,
}
}
}
Expand Down Expand Up @@ -613,6 +616,7 @@ mod tests {
let playpen_should_be = Playpen {
editable: true,
copy_js: true,
line_numbers: false,
};
let html_should_be = HtmlConfig {
curly_quotes: true,
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,9 @@ fn make_data(

if html.playpen.editable && html.playpen.copy_js {
data.insert("playpen_js".to_owned(), json!(true));
if html.playpen.line_numbers {
data.insert("playpen_line_numbers".to_owned(), json!(true));
}
}

let search = html_config.search.clone();
Expand Down
6 changes: 6 additions & 0 deletions src/theme/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@
</script>
{{/if}}

{{#if playpen_line_numbers}}
<script type="text/javascript">
window.playpen_line_numbers = true;
</script>
{{/if}}

{{#if playpen_js}}
<script src="{{ path_to_root }}ace.js" type="text/javascript" charset="utf-8"></script>
<script src="{{ path_to_root }}editor.js" type="text/javascript" charset="utf-8"></script>
Expand Down
6 changes: 4 additions & 2 deletions src/theme/playpen_editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ window.editors = [];
}

Array.from(document.querySelectorAll('.editable')).forEach(function(editable) {
let display_line_numbers = window.playpen_line_numbers || false;

let editor = ace.edit(editable);
editor.setOptions({
highlightActiveLine: false,
showPrintMargin: false,
showLineNumbers: false,
showGutter: false,
showLineNumbers: display_line_numbers,
showGutter: display_line_numbers,
maxLines: Infinity,
fontSize: "0.875em" // please adjust the font size of the code in general.css
});
Expand Down