From f5bc807ec2a6e94e8f9faabf16266b92b7d3d44d Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 25 Sep 2020 21:47:11 +0200 Subject: [PATCH] Syntax color for examples (#19) --- Cargo.lock | 248 ++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 +- build.rs | 51 +++++++++ build.sh | 5 +- dev.sh | 2 +- src/buttons/doc.rs | 2 +- src/collapse/doc.rs | 2 +- src/example.rs | 13 ++- src/icon/doc.rs | 2 +- src/lib.rs | 26 +++-- src/tree/doc.rs | 2 +- static/index.html | 13 ++- 12 files changed, 345 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ceec530..0f2ffbc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/Cargo.toml b/Cargo.toml index 745a5a6..9ae1d96 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/build.rs b/build.rs index 6024754..9bcfed6 100644 --- a/build.rs +++ b/build.rs @@ -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>( + 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, + ) } diff --git a/build.sh b/build.sh index 05a1cc8..685956e 100755 --- a/build.sh +++ b/build.sh @@ -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 diff --git a/dev.sh b/dev.sh index 7e50762..44eea42 100755 --- a/dev.sh +++ b/dev.sh @@ -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 diff --git a/src/buttons/doc.rs b/src/buttons/doc.rs index c9e0819..6290b82 100644 --- a/src/buttons/doc.rs +++ b/src/buttons/doc.rs @@ -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! {
diff --git a/src/collapse/doc.rs b/src/collapse/doc.rs index 1c35f3d..0dc537b 100644 --- a/src/collapse/doc.rs +++ b/src/collapse/doc.rs @@ -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! {
diff --git a/src/example.rs b/src/example.rs index 70d9657..a9b96c5 100644 --- a/src/example.rs +++ b/src/example.rs @@ -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 > -
{self.props.source}
+ {self.props.source.clone()}
@@ -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; diff --git a/src/icon/doc.rs b/src/icon/doc.rs index e0ce748..a0e519a 100644 --- a/src/icon/doc.rs +++ b/src/icon/doc.rs @@ -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! {
diff --git a/src/lib.rs b/src/lib.rs index 260720b..d54869d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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")] diff --git a/src/tree/doc.rs b/src/tree/doc.rs index 60ece99..4dc861c 100644 --- a/src/tree/doc.rs +++ b/src/tree/doc.rs @@ -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! {
diff --git a/static/index.html b/static/index.html index 72c3d18..eccc956 100644 --- a/static/index.html +++ b/static/index.html @@ -2,10 +2,12 @@ + Yewprint – Documentation +