1
0
Fork 0
mirror of https://github.com/lise-henry/crowbook synced 2024-06-01 13:56:25 +02:00

Add highlight.js for html single file renderer

This commit is contained in:
Elisabeth Henry 2016-09-15 21:08:50 +02:00
parent fcb0705874
commit 61a6900412
8 changed files with 191 additions and 5 deletions

View File

@ -1,6 +1,12 @@
Crowbook
========
```rust
fn test() {
println!("{}", 42);
}
```
[![Build Status](https://travis-ci.org/lise-henry/crowbook.svg?branch=master)](https://travis-ci.org/lise-henry/crowbook)
Render a book written in markdown to HTML, Epub or PDF.
@ -174,6 +180,38 @@ Contributors
Acknowledgements
----------------
Crowbook includes binary (minified) CSS and Javascript files from
[Highlight.js](https://highlightjs.org/), written by Ivan
Sagalaev, licensed under the following terms:
> Copyright (c) 2006, Ivan Sagalaev
>
> All rights reserved.
>
> Redistribution and use in source and binary forms, with or without
> modification, are permitted provided that the following conditions are met:
> * Redistributions of source code must retain the above copyright
> notice, this list of conditions and the following disclaimer.
> * Redistributions in binary form must reproduce the above copyright
> notice, this list of conditions and the following disclaimer in the
> documentation and/or other materials provided with the
> distribution.
> * Neither the name of highlight.js nor the names of its contributors
> may be used to endorse or promote products derived from this software
> without specific prior written permission.
>
> THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
> EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
> WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
> DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
> DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
> (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
> LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
> ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
> (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
> SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Besides the [Rust](https://www.rust-lang.org/) compiler and standard library, Crowbook uses the
following libraries:

View File

@ -31,6 +31,7 @@ toc_name:str:Table of contents # Name of the table of contents if it is dis
verbose:bool:false # Make Crowbook display more messages
# HTML options
html.highlight_code:bool:true # Provides syntax highlighting for code blocks (using highlight.js)
html.template:path # Path of an HTML template
html.css:path # Path of a stylesheet for HTML rendering
html.print_css:path # Path of a media print stylesheet for HTML rendering

View File

@ -23,7 +23,7 @@ use number::Number;
use toc::Toc;
use resource_handler::ResourceHandler;
use std::borrow::Cow;
use templates::html;
use templates::{html, highlight};
use renderer::Renderer;
use mustache;
@ -125,7 +125,10 @@ impl<'a> HtmlRenderer<'a> {
let pages_svg = html::PAGES_SVG.to_base64(base64::STANDARD);
let pages_svg = format!("data:image/svg+xml;base64,{}", pages_svg);
let highlight_js = highlight::JS.to_base64(base64::STANDARD);
let highlight_js = format!("data:text/javascript;base64,{}", highlight_js);
for (i, filename) in self.book.filenames.iter().enumerate() {
self.handler.add_link(filename.clone(), format!("#chapter-{}", i));
}
@ -232,7 +235,7 @@ impl<'a> HtmlRenderer<'a> {
let js = String::from_utf8_lossy(&res);
// Render the HTML document
let data = self.book.get_mapbuilder("none")
let mut mapbuilder = self.book.get_mapbuilder("none")
.insert_str("content", content)
.insert_str("toc", toc)
.insert_str("script", js)
@ -242,8 +245,13 @@ impl<'a> HtmlRenderer<'a> {
.insert_str("print_style", self.book.get_template("html.print_css").unwrap())
.insert_str("menu_svg", menu_svg)
.insert_str("book_svg", book_svg)
.insert_str("pages_svg", pages_svg)
.build();
.insert_str("pages_svg", pages_svg);
if self.book.options.get_bool("html.highlight_code") == Ok(true) {
mapbuilder = mapbuilder.insert_bool("highlight_code", true)
.insert_str("highlight_css", highlight::CSS)
.insert_str("highlight_js", highlight_js);
}
let data = mapbuilder.build();
let template = mustache::compile_str(try!(self.book.get_template("html.template")).as_ref());
let mut res = vec!();
template.render_data(&mut res, &data);

View File

@ -15,6 +15,11 @@
// You should have received ba copy of the GNU Lesser General Public License
// along with Crowbook. If not, see <http://www.gnu.org/licenses/>.
pub mod highlight {
pub static CSS:&'static str = include_str!("../../templates/highlight/default.css");
pub static JS:&'static [u8] = include_bytes!("../../templates/highlight/highlight.pack.js");
}
pub mod html {
pub static TEMPLATE:&'static str = include_str!("../../templates/template.html");
pub static CSS:&'static str = concat!(include_str!("../../templates/epub/stylesheet.css"), include_str!("../../templates/template.css"));

View File

@ -0,0 +1,24 @@
Copyright (c) 2006, Ivan Sagalaev
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of highlight.js nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -0,0 +1,99 @@
/*
Original highlight.js style (c) Ivan Sagalaev <maniac@softwaremaniacs.org>
*/
.hljs {
display: block;
overflow-x: auto;
padding: 0.5em;
background: #F0F0F0;
}
/* Base color: saturation 0; */
.hljs,
.hljs-subst {
color: #444;
}
.hljs-comment {
color: #888888;
}
.hljs-keyword,
.hljs-attribute,
.hljs-selector-tag,
.hljs-meta-keyword,
.hljs-doctag,
.hljs-name {
font-weight: bold;
}
/* User color: hue: 0 */
.hljs-type,
.hljs-string,
.hljs-number,
.hljs-selector-id,
.hljs-selector-class,
.hljs-quote,
.hljs-template-tag,
.hljs-deletion {
color: #880000;
}
.hljs-title,
.hljs-section {
color: #880000;
font-weight: bold;
}
.hljs-regexp,
.hljs-symbol,
.hljs-variable,
.hljs-template-variable,
.hljs-link,
.hljs-selector-attr,
.hljs-selector-pseudo {
color: #BC6060;
}
/* Language color: hue: 90; */
.hljs-literal {
color: #78A960;
}
.hljs-built_in,
.hljs-bullet,
.hljs-code,
.hljs-addition {
color: #397300;
}
/* Meta color: hue: 200 */
.hljs-meta {
color: #1f7199;
}
.hljs-meta-string {
color: #4d99bf;
}
/* Misc effects */
.hljs-emphasis {
font-style: italic;
}
.hljs-strong {
font-weight: bold;
}

File diff suppressed because one or more lines are too long

View File

@ -12,6 +12,15 @@
<style type = "text/css" media = "print">
{{{print_style}}}
</style>
{{#highlight_code}}
<style>
{{{highlight_css}}}
</style>
<script src = "{{{highlight_js}}}"></script>
<script>
hljs.initHighlightingOnLoad();
</script>
{{/highlight_code}}
<script>
{{{script}}}
</script>