Verified Commit fd4b3690 authored by Yasuhiro Яша Asaka's avatar Yasuhiro Яша Asaka 🐺
Browse files

Add debug feature for check on local machine

--features debug option enables link navigations like
`file:///tmp/...` on local machine.
parent 2a5d2e34
......@@ -85,6 +85,12 @@ version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4"
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "log"
version = "0.4.14"
......@@ -242,4 +248,5 @@ name = "workroom"
version = "0.0.1"
dependencies = [
"handlebars",
"lazy_static",
]
......@@ -14,4 +14,8 @@ edition = "2018"
[dependencies]
handlebars = "4.1.2"
lazy_static = "1.4.0"
[features]
debug = []
default = []
......@@ -33,27 +33,24 @@ vet: vet\:check # Alias for vet:check
.PHONY: vet
# test
test\:unit: # Run only unit tests
@cargo test --bins -- --nocapture
.PHONY: test\:unit
test\:debug: # Run unit tests with debug feature
@cargo test --features debug -- --nocapture
.PHONY: test\:debug
test\:doc: # Run only doc tests
@cargo test --doc
.PHONY: test\:doc
test: test\:debug # Alias for test:debug
.PHONY: test
test\:all: test\:doc # Run all tests
@cargo test --bins -- --nocapture
.PHONY: test\:all
test\:release: # Run unit tests without enabling debug feature
@cargo test -- --nocapture
.PHONY: test\:release
test: test\:unit # Alias ofr test:unit
.PHONY: test
# build
build\:debug: # Run packages [synonym: build]
cargo build
cargo build --features debug
.PHONY: build\:debug
build: build\:debug
build: build\:debug # Alias for build:debug
.PHONY: build
build\:release: # Build packages with release mode
......
......@@ -20,10 +20,15 @@ rustc 1.54.0 (a178d0322 2021-07-26)
```zsh
% make
% ./target/release/workroom
% xdg-open dst/index.html
```
```zsh
# debug
% make build:debug && ./target/debug/workroom && xdg-open ./dst/index.html
# release
% make build:release && ./target/release/workroom && xdg-open ./dst/index.html
```
## License
......
<h2>Home</h2>
<div class="main">
<img src="/img/grauwoelfchenskafee.jpg" alt="Logo"/>
<img src="{{host}}/img/grauwoelfchenskafee.jpg" alt="Logo"/>
<p>
Hoi Zäme! I'm Yasha.<br/>
Thank you for visiting my website.<br/>
Please enjoy ;)
</p>
<p>
<img src="/img/workroom-300x245-20171010.jpg"
<img src="{{host}}/img/workroom-300x245-20171010.jpg"
width="310"
height="245"
alt="Workroom"/>
......
......@@ -26,7 +26,7 @@
<a title="License" href="https://gitlab.com/grauwoelfchen/workroom/blob/trunk/README.md">GFDL-1.3</a>
</span>
Copyright &copy; 2014 - 2021
<a title="Author" href="/about" rel="author" class="author">Yasuhiro Яша Asaka</a>
<a title="Author" href="{{host}}/about.html" rel="author" class="author">Yasuhiro Яша Asaka</a>
</div>
</div>
</html>
......@@ -3,8 +3,18 @@ use std::fs;
use std::io;
use std::path::Path;
#[cfg(feature = "debug")]
use std::env;
#[macro_use]
extern crate lazy_static;
use handlebars::{no_escape, Handlebars};
lazy_static! {
static ref HOST: String = get_host();
}
const SITE_TITLE: &str = "Grauwoelfchen's Workroom";
const DST_DIR: &str = "./dst";
......@@ -70,24 +80,36 @@ fn main() -> Result<(), Error> {
let dat = include_str!("./error.hbs");
fs::write(dst.join("error.html"), &dat.as_bytes())?;
let mut page_data = BTreeMap::new();
page_data.insert("host".to_string(), HOST.to_string());
// index
let data = load("Home", include_str!("./index.hbs"));
let page = reg.render_template(include_str!("./index.hbs"), &page_data)?;
let mut data = load("Home", &page);
data.append(&mut page_data);
let result = reg.render("layout", &data)?;
fs::write(dst.join("index.html"), result)?;
// about
let data = load("About", include_str!("./about.hbs"));
let result = reg.render("layout", &data).unwrap();
let page = reg.render_template(include_str!("./about.hbs"), &page_data)?;
let mut data = load("About", &page);
data.append(&mut page_data);
let result = reg.render("layout", &data)?;
fs::write(dst.join("about.html"), result)?;
// software
let data = load("Software", include_str!("./software.hbs"));
let result = reg.render("layout", &data).unwrap();
let page =
reg.render_template(include_str!("./software.hbs"), &page_data)?;
let mut data = load("Software", &page);
data.append(&mut page_data);
let result = reg.render("layout", &data)?;
fs::write(dst.join("software.html"), result)?;
// link
let data = load("Link", include_str!("./link.hbs"));
let result = reg.render("layout", &data).unwrap();
let page = reg.render_template(include_str!("./link.hbs"), &page_data)?;
let mut data = load("Link", &page);
data.append(&mut page_data);
let result = reg.render("layout", &data)?;
fs::write(dst.join("link.html"), result)?;
reg.unregister_escape_fn();
......@@ -102,14 +124,28 @@ fn build_nav(heading: &str, link: &Link) -> String {
} else {
""
};
let host = get_host();
let href = format!("{}{}", host, link.href);
format!(
r#"<li{}>
<a href="{}">{}</a>
</li>"#,
class, link.href, link.title,
class, href, link.title,
)
}
/// Returns the host part for href value of links.
fn get_host() -> String {
#[cfg(not(feature = "debug"))]
let host = "".to_string();
// for local debugging
#[cfg(feature = "debug")]
let host =
format!("{}/dst", env::current_dir().unwrap().display().to_string());
host
}
/// Loads the content as a BTreeMap object that has navi, content
/// {page,site}_title as attributes.
fn load(heading: &str, content: &str) -> BTreeMap<String, String> {
......@@ -134,6 +170,7 @@ fn load(heading: &str, content: &str) -> BTreeMap<String, String> {
mod test {
use super::*;
#[cfg(not(feature = "debug"))]
#[test]
fn test_build_nav() {
let link = Link {
......@@ -160,4 +197,52 @@ mod test {
</li>"#
);
}
#[cfg(feature = "debug")]
#[test]
fn test_build_nav_debug() {
let dir = Path::new("/tmp");
assert!(env::set_current_dir(&dir).is_ok());
let link = Link {
title: "Foo",
href: "/foo.html",
};
let nav = build_nav("Foo", &link);
assert_eq!(
nav,
r#"<li class="active">
<a href="/tmp/dst/foo.html">Foo</a>
</li>"#
);
let link = Link {
title: "Foo",
href: "/foo.html",
};
let nav = build_nav("Bar", &link);
assert_eq!(
nav,
r#"<li>
<a href="/tmp/dst/foo.html">Foo</a>
</li>"#
);
}
#[cfg(not(feature = "debug"))]
#[test]
fn test_get_host() {
let host = get_host();
assert_eq!(host, "");
}
#[cfg(feature = "debug")]
#[test]
fn test_get_host_debug() {
let dir = Path::new("/tmp/");
assert!(env::set_current_dir(&dir).is_ok());
let host = get_host();
assert_eq!(host, "/tmp/dst");
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment