Override base_url in serve cmd

This commit is contained in:
Vincent Prouillet 2017-03-20 19:00:00 +09:00
parent 540bbcc1b6
commit eb6fa3c314
4 changed files with 19 additions and 5 deletions

View file

@ -5,5 +5,7 @@ use gutenberg::Site;
pub fn build() -> Result<()> { pub fn build() -> Result<()> {
Site::new(env::current_dir().unwrap())?.build() let mut site = Site::new(env::current_dir().unwrap())?;
site.parse()?;
site.build()
} }

View file

@ -62,11 +62,19 @@ pub fn serve(interface: &str, port: &str) -> Result<()> {
println!("Building site..."); println!("Building site...");
let start = Instant::now(); let start = Instant::now();
let mut site = Site::new(env::current_dir().unwrap())?; let mut site = Site::new(env::current_dir().unwrap())?;
let address = format!("{}:{}", interface, port);
// Override the base url so links work in localhost
site.config.base_url = if site.config.base_url.ends_with('/') {
format!("http://{}/", address)
} else {
format!("http://{}", address)
};
site.parse()?;
site.enable_live_reload(); site.enable_live_reload();
site.build()?; site.build()?;
report_elapsed_time(start); report_elapsed_time(start);
let address = format!("{}:{}", interface, port);
let ws_address = format!("{}:{}", interface, "1112"); let ws_address = format!("{}:{}", interface, "1112");
// Start a webserver that serves the `public` directory // Start a webserver that serves the `public` directory

View file

@ -74,7 +74,7 @@ impl Site {
let mut tera = Tera::new(&tpl_glob).chain_err(|| "Error parsing templates")?; let mut tera = Tera::new(&tpl_glob).chain_err(|| "Error parsing templates")?;
tera.extend(&GUTENBERG_TERA)?; tera.extend(&GUTENBERG_TERA)?;
let mut site = Site { let site = Site {
base_path: path.to_path_buf(), base_path: path.to_path_buf(),
config: get_config(path), config: get_config(path),
pages: HashMap::new(), pages: HashMap::new(),
@ -85,7 +85,6 @@ impl Site {
tags: HashMap::new(), tags: HashMap::new(),
categories: HashMap::new(), categories: HashMap::new(),
}; };
site.parse()?;
Ok(site) Ok(site)
} }

View file

@ -16,7 +16,8 @@ use gutenberg::{Site};
fn test_can_parse_site() { fn test_can_parse_site() {
let mut path = env::current_dir().unwrap().to_path_buf(); let mut path = env::current_dir().unwrap().to_path_buf();
path.push("test_site"); path.push("test_site");
let site = Site::new(&path).unwrap(); let mut site = Site::new(&path).unwrap();
site.parse().unwrap();
// Correct number of pages (sections are pages too) // Correct number of pages (sections are pages too)
assert_eq!(site.pages.len(), 10); assert_eq!(site.pages.len(), 10);
@ -88,6 +89,7 @@ fn test_can_build_site_without_live_reload() {
let mut path = env::current_dir().unwrap().to_path_buf(); let mut path = env::current_dir().unwrap().to_path_buf();
path.push("test_site"); path.push("test_site");
let mut site = Site::new(&path).unwrap(); let mut site = Site::new(&path).unwrap();
site.parse().unwrap();
let tmp_dir = TempDir::new("example").expect("create temp dir"); let tmp_dir = TempDir::new("example").expect("create temp dir");
let public = &tmp_dir.path().join("public"); let public = &tmp_dir.path().join("public");
site.set_output_path(&public); site.set_output_path(&public);
@ -127,6 +129,7 @@ fn test_can_build_site_with_live_reload() {
let mut path = env::current_dir().unwrap().to_path_buf(); let mut path = env::current_dir().unwrap().to_path_buf();
path.push("test_site"); path.push("test_site");
let mut site = Site::new(&path).unwrap(); let mut site = Site::new(&path).unwrap();
site.parse().unwrap();
let tmp_dir = TempDir::new("example").expect("create temp dir"); let tmp_dir = TempDir::new("example").expect("create temp dir");
let public = &tmp_dir.path().join("public"); let public = &tmp_dir.path().join("public");
site.set_output_path(&public); site.set_output_path(&public);
@ -163,6 +166,7 @@ fn test_can_build_site_with_categories() {
let mut path = env::current_dir().unwrap().to_path_buf(); let mut path = env::current_dir().unwrap().to_path_buf();
path.push("test_site"); path.push("test_site");
let mut site = Site::new(&path).unwrap(); let mut site = Site::new(&path).unwrap();
site.parse().unwrap();
for (i, page) in site.pages.values_mut().enumerate() { for (i, page) in site.pages.values_mut().enumerate() {
page.meta.category = if i % 2 == 0 { page.meta.category = if i % 2 == 0 {
@ -212,6 +216,7 @@ fn test_can_build_site_with_tags() {
let mut path = env::current_dir().unwrap().to_path_buf(); let mut path = env::current_dir().unwrap().to_path_buf();
path.push("test_site"); path.push("test_site");
let mut site = Site::new(&path).unwrap(); let mut site = Site::new(&path).unwrap();
site.parse().unwrap();
for (i, page) in site.pages.values_mut().enumerate() { for (i, page) in site.pages.values_mut().enumerate() {
page.meta.tags = if i % 2 == 0 { page.meta.tags = if i % 2 == 0 {