Syntax color for examples (#19)

This commit is contained in:
Cecile Tonglet 2020-09-25 21:47:11 +02:00 committed by GitHub
parent 1123ec98f1
commit f5bc807ec2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 345 additions and 24 deletions

248
Cargo.lock generated
View file

@ -1,5 +1,11 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "adler"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e"
[[package]]
name = "anyhow"
version = "1.0.32"
@ -18,6 +24,12 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "base64"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff"
[[package]]
name = "bincode"
version = "1.2.1"
@ -28,6 +40,12 @@ dependencies = [
"serde",
]
[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "boolinator"
version = "2.4.0"
@ -52,6 +70,12 @@ version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38"
[[package]]
name = "cc"
version = "1.0.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef611cc68ff783f18535d77ddd080185275713d852c4f5cbb6122c462a7a825c"
[[package]]
name = "cfg-if"
version = "0.1.10"
@ -64,6 +88,16 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8100e46ff92eb85bf6dc2930c73f2a4f7176393c84a9446b3d501e1b354e7b34"
[[package]]
name = "chrono"
version = "0.4.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "942f72db697d8767c22d46a598e01f2d3b475501ea43d0db4f16d90259182d0b"
dependencies = [
"num-integer",
"num-traits",
]
[[package]]
name = "console_error_panic_hook"
version = "0.1.6"
@ -74,6 +108,27 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "crc32fast"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1"
dependencies = [
"cfg-if",
]
[[package]]
name = "flate2"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "766d0e77a2c1502169d4a93ff3b8c15a71fd946cd0126309752104e5f3c46d94"
dependencies = [
"cfg-if",
"crc32fast",
"libc",
"miniz_oxide",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -298,6 +353,33 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lazycell"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]]
name = "libc"
version = "0.2.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2f96b10ec2560088a8e76961b00d47107b3a625fecb76dedb29ee7ccbf98235"
[[package]]
name = "line-wrap"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f30344350a2a51da54c1d53be93fade8a237e545dbcc4bdbe635413f2117cab9"
dependencies = [
"safemem",
]
[[package]]
name = "linked-hash-map"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8dd5a6d5999d9907cda8ed67bbd137d3af8085216c2ac62de5be860bd41f304a"
[[package]]
name = "log"
version = "0.4.11"
@ -313,12 +395,63 @@ version = "2.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
[[package]]
name = "miniz_oxide"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c60c0dfe32c10b43a144bad8fc83538c52f58302c92300ea7ec7bf7b38d5a7b9"
dependencies = [
"adler",
"autocfg",
]
[[package]]
name = "num-integer"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad"
[[package]]
name = "onig"
version = "6.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a155d13862da85473665694f4c05d77fb96598bdceeaf696433c84ea9567e20"
dependencies = [
"bitflags",
"lazy_static",
"libc",
"onig_sys",
]
[[package]]
name = "onig_sys"
version = "69.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9bff06597a6b17855040955cae613af000fc0bfc8ad49ea68b9479a74e59292d"
dependencies = [
"cc",
"pkg-config",
]
[[package]]
name = "pin-project"
version = "0.4.23"
@ -345,6 +478,26 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkg-config"
version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33"
[[package]]
name = "plist"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b336d94e8e4ce29bf15bba393164629764744c567e8ad306cc1fdd0119967fd"
dependencies = [
"base64",
"chrono",
"indexmap",
"line-wrap",
"serde",
"xml-rs",
]
[[package]]
name = "proc-macro-hack"
version = "0.5.18"
@ -396,6 +549,21 @@ version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
[[package]]
name = "safemem"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "serde"
version = "1.0.115"
@ -450,6 +618,28 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "syntect"
version = "4.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e3978df05b5850c839a6b352d3c35ce0478944a4be689be826b53cf75363e88"
dependencies = [
"bincode",
"bitflags",
"flate2",
"fnv",
"lazy_static",
"lazycell",
"onig",
"plist",
"regex-syntax",
"serde",
"serde_derive",
"serde_json",
"walkdir",
"yaml-rust",
]
[[package]]
name = "thiserror"
version = "1.0.20"
@ -482,6 +672,17 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]]
name = "walkdir"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d"
dependencies = [
"same-file",
"winapi",
"winapi-util",
]
[[package]]
name = "wasm-bindgen"
version = "0.2.67"
@ -558,6 +759,52 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "xml-rs"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b07db065a5cf61a7e4ba64f29e67db906fb1787316516c4e6e5ff0fea1efcd8a"
[[package]]
name = "yaml-rust"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39f0c922f1a334134dc2f7a8b67dc5d25f0735263feec974345ff706bcf20b0d"
dependencies = [
"linked-hash-map",
]
[[package]]
name = "yew"
version = "0.17.3"
@ -605,6 +852,7 @@ dependencies = [
"heck",
"id_tree",
"regex",
"syntect",
"wasm-bindgen",
"web-sys",
"yew",

View file

@ -16,7 +16,7 @@ is-it-maintained-open-issues = { repository = "cecton/yewprint" }
[features]
default = []
doc = []
doc = ["syntect"]
[dependencies]
wasm-bindgen = "^0.2"
@ -27,3 +27,4 @@ id_tree = "1.7"
[build-dependencies]
regex = { version = "1", default-features = false, features = ["std"] }
heck = "0.3"
syntect = { version = "4.4.0", optional = true }

View file

@ -58,4 +58,55 @@ fn main() {
fs::write(&dest_path, src).unwrap();
println!("cargo:rerun-if-changed=build.rs");
#[cfg(feature = "doc")]
load_examples_with_colors();
}
#[cfg(feature = "doc")]
fn load_examples_with_colors() {
use std::ffi::OsString;
use syntect::highlighting::{Theme, ThemeSet};
use syntect::parsing::SyntaxSet;
let syntax_set = SyntaxSet::load_defaults_newlines();
let theme_set = ThemeSet::load_defaults();
let out_dir = env::var_os("OUT_DIR").unwrap();
fn recursive<P: AsRef<Path>>(
base_path: P,
syntax_set: &SyntaxSet,
theme: &Theme,
out_dir: &OsString,
) {
for entry in fs::read_dir(base_path).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.is_dir() {
recursive(&path, syntax_set, theme, out_dir);
}
let file_name = path.file_name().unwrap().to_str().unwrap();
if path.starts_with("./src") && file_name == "example.rs" && path.is_file() {
let dest_path = Path::new(&out_dir)
.join(&path)
.with_file_name("doc.rs.html");
let src =
syntect::html::highlighted_html_for_file(&path, syntax_set, theme).unwrap();
let _ = std::fs::create_dir_all(dest_path.parent().unwrap());
fs::write(&dest_path, src.trim_end()).unwrap();
println!("cargo:rerun-if-changed={}", path.display());
}
}
}
recursive(
".",
&syntax_set,
&theme_set.themes["Solarized (dark)"],
&out_dir,
)
}

View file

@ -15,7 +15,10 @@ mkdir -p public
rm -fR public/.gitignore public/*
# copy index.html
cp static/index.html public/
cp static/* public/
# copy favicon
cp src/logo.svg public/favicon.svg
# download blueprint css
tar xzOf core.tgz package/lib/css/blueprint.css > public/blueprint.css

2
dev.sh
View file

@ -1,3 +1,3 @@
#!/bin/sh
exec cargo watch -s './build.sh --dev -- --features doc && simple-http-server -i --nocache --cors public/' -w src -w static
exec cargo watch -s './build.sh --dev -- --features doc && simple-http-server -i --nocache --cors public/' -i /public

View file

@ -19,7 +19,7 @@ impl Component for ButtonDoc {
}
fn view(&self) -> Html {
let source = crate::include_example!("example.rs");
let source = crate::include_example!();
html! {
<div>

View file

@ -19,7 +19,7 @@ impl Component for CollapseDoc {
}
fn view(&self) -> Html {
let source = crate::include_example!("example.rs");
let source = crate::include_example!();
html! {
<div>

View file

@ -13,7 +13,7 @@ pub enum Msg {
#[derive(Clone, PartialEq, Properties)]
pub struct Props {
pub source: &'static str,
pub source: yew::virtual_dom::VNode,
pub children: html::Children,
}
@ -61,7 +61,7 @@ impl Component for ExampleContainer {
is_open=!self.collapsed
keep_children_mounted=true
>
<pre class="bp3-code-block">{self.props.source}</pre>
{self.props.source.clone()}
</Collapse>
</div>
</div>
@ -71,14 +71,17 @@ impl Component for ExampleContainer {
#[macro_export]
macro_rules! include_example {
($file:expr) => {{
() => {{
use crate::ExampleContainer;
let source = include_str!($file);
let source = crate::include_raw_html!(
concat!(env!("OUT_DIR"), "/", file!(), ".html"),
"bp3-code-block"
);
mod source {
// TODO: example.rs files are not formatted because of this include
include!($file);
include!("example.rs");
}
use source::Example;

View file

@ -19,7 +19,7 @@ impl Component for IconDoc {
}
fn view(&self) -> Html {
let source = crate::include_example!("example.rs");
let source = crate::include_example!();
html! {
<div>

View file

@ -39,18 +39,28 @@ macro_rules! log {
#[cfg(feature = "doc")]
#[macro_export]
macro_rules! include_raw_html {
($file:expr) => {{
($file:expr, $class:expr) => {{
yew::virtual_dom::VNode::VRef(yew::web_sys::Node::from({
let div = web_sys::window()
.unwrap()
.document()
.unwrap()
.create_element("span")
.unwrap();
div.set_inner_html(include_str!($file));
let div = crate::include_raw_html!(element $file);
div.set_class_name($class);
div
}))
}};
($file:expr) => {{
yew::virtual_dom::VNode::VRef(yew::web_sys::Node::from({
crate::include_raw_html!(element $file)
}))
}};
(element $file:expr) => {{
let div = web_sys::window()
.unwrap()
.document()
.unwrap()
.create_element("div")
.unwrap();
div.set_inner_html(include_str!($file));
div
}};
}
#[cfg(feature = "doc")]

View file

@ -19,7 +19,7 @@ impl Component for TreeDoc {
}
fn view(&self) -> Html {
let source = crate::include_example!("example.rs");
let source = crate::include_example!();
html! {
<div>

View file

@ -2,10 +2,12 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Yewprint Documentation</title>
<script type="module">
import init from "/wasm.js";
init();
</script>
<link rel="icon" href="/favicon.svg">
<link rel="stylesheet" href="/blueprint.css">
<style>
body {
@ -98,10 +100,13 @@
}
.docs-example-wrapper .bp3-code-block {
/*
display: inline-block;
clear: both;
*/
background-color: #002b36;
white-space: pre-wrap;
}
.docs-example-wrapper .bp3-code-block > pre {
margin: 0;
white-space: pre-wrap;
}
.docs-example-wrapper .docs-example {