Move the logic into convert_blog_post_page_to_render_context.
I was writing it in the build command's rust files for convenience, but now its getting long enough to warrant moving it into its final location.
This commit is contained in:
parent
178ce877bc
commit
3ac7826d2c
@ -1,12 +1,75 @@
|
|||||||
|
use std::path::Path;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use crate::config::Config;
|
||||||
|
use crate::error::CustomError;
|
||||||
|
|
||||||
use super::render_context::GlobalSettings;
|
use super::render_context::GlobalSettings;
|
||||||
use super::render_context::RenderBlogPostPage;
|
use super::render_context::RenderBlogPostPage;
|
||||||
use super::BlogPost;
|
use super::BlogPost;
|
||||||
use super::BlogPostPage;
|
use super::BlogPostPage;
|
||||||
|
|
||||||
pub(crate) fn convert_blog_post_page_to_render_context(
|
pub(crate) fn convert_blog_post_page_to_render_context<D: AsRef<Path>, F: AsRef<Path>>(
|
||||||
|
config: &Config,
|
||||||
|
output_directory: D,
|
||||||
|
output_file: F,
|
||||||
_post: &BlogPost,
|
_post: &BlogPost,
|
||||||
page: &BlogPostPage,
|
page: &BlogPostPage,
|
||||||
global_settings: GlobalSettings,
|
) -> Result<RenderBlogPostPage, CustomError> {
|
||||||
) -> RenderBlogPostPage {
|
let output_directory = output_directory.as_ref();
|
||||||
RenderBlogPostPage::new(global_settings, page.title.clone())
|
let output_file = output_file.as_ref();
|
||||||
|
let css_files = vec![get_web_path(
|
||||||
|
config,
|
||||||
|
output_directory,
|
||||||
|
output_file,
|
||||||
|
"main.css",
|
||||||
|
)?];
|
||||||
|
let js_files = vec![get_web_path(
|
||||||
|
config,
|
||||||
|
output_directory,
|
||||||
|
output_file,
|
||||||
|
"blog_post.js",
|
||||||
|
)?];
|
||||||
|
let global_settings = GlobalSettings::new(page.title.clone(), css_files, js_files);
|
||||||
|
|
||||||
|
let ret = RenderBlogPostPage::new(global_settings, page.title.clone());
|
||||||
|
Ok(ret)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_web_path<D: AsRef<Path>, F: AsRef<Path>, P: AsRef<Path>>(
|
||||||
|
config: &Config,
|
||||||
|
output_directory: D,
|
||||||
|
containing_file: F,
|
||||||
|
path_from_web_root: P,
|
||||||
|
) -> Result<String, CustomError> {
|
||||||
|
let path_from_web_root = path_from_web_root.as_ref();
|
||||||
|
if config.use_relative_paths() {
|
||||||
|
let output_directory = output_directory.as_ref();
|
||||||
|
let containing_file = containing_file.as_ref();
|
||||||
|
// Subtracting 1 from the depth to "remove" the file name.
|
||||||
|
let depth_from_web_root = containing_file
|
||||||
|
.strip_prefix(output_directory)?
|
||||||
|
.components()
|
||||||
|
.count()
|
||||||
|
- 1;
|
||||||
|
let prefix = "../".repeat(depth_from_web_root);
|
||||||
|
let final_path = PathBuf::from(prefix).join(path_from_web_root);
|
||||||
|
let final_string = final_path
|
||||||
|
.as_path()
|
||||||
|
.to_str()
|
||||||
|
.map(str::to_string)
|
||||||
|
.ok_or("Path should be valid utf-8.")?;
|
||||||
|
Ok(final_string)
|
||||||
|
} else {
|
||||||
|
let web_root = config
|
||||||
|
.get_web_root()
|
||||||
|
.ok_or("Must either use_relative_paths or set the web_root in the config.")?;
|
||||||
|
let final_path = PathBuf::from(web_root).join(path_from_web_root);
|
||||||
|
let final_string = final_path
|
||||||
|
.as_path()
|
||||||
|
.to_str()
|
||||||
|
.map(str::to_string)
|
||||||
|
.ok_or("Path should be valid utf-8.")?;
|
||||||
|
Ok(final_string)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ impl BlogPost {
|
|||||||
root_dir: R,
|
root_dir: R,
|
||||||
post_dir: P,
|
post_dir: P,
|
||||||
) -> Result<BlogPost, CustomError> {
|
) -> Result<BlogPost, CustomError> {
|
||||||
async fn inner(root_dir: &Path, post_dir: &Path) -> Result<BlogPost, CustomError> {
|
async fn inner(_root_dir: &Path, post_dir: &Path) -> Result<BlogPost, CustomError> {
|
||||||
let post_id = post_dir
|
let post_id = post_dir
|
||||||
.file_name()
|
.file_name()
|
||||||
.expect("The post directory should have a name.");
|
.expect("The post directory should have a name.");
|
||||||
|
@ -5,4 +5,3 @@ mod render_context;
|
|||||||
pub(crate) use convert::convert_blog_post_page_to_render_context;
|
pub(crate) use convert::convert_blog_post_page_to_render_context;
|
||||||
pub(crate) use definition::BlogPost;
|
pub(crate) use definition::BlogPost;
|
||||||
pub(crate) use page::BlogPostPage;
|
pub(crate) use page::BlogPostPage;
|
||||||
pub(crate) use render_context::GlobalSettings;
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::path::Path;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use include_dir::include_dir;
|
use include_dir::include_dir;
|
||||||
@ -7,7 +6,6 @@ use include_dir::Dir;
|
|||||||
|
|
||||||
use crate::blog_post::convert_blog_post_page_to_render_context;
|
use crate::blog_post::convert_blog_post_page_to_render_context;
|
||||||
use crate::blog_post::BlogPost;
|
use crate::blog_post::BlogPost;
|
||||||
use crate::blog_post::GlobalSettings;
|
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::error::CustomError;
|
use crate::error::CustomError;
|
||||||
use crate::render::DusterRenderer;
|
use crate::render::DusterRenderer;
|
||||||
@ -65,25 +63,14 @@ impl SiteRenderer {
|
|||||||
.join("posts")
|
.join("posts")
|
||||||
.join(&blog_post.id)
|
.join(&blog_post.id)
|
||||||
.join(blog_post_page.get_output_path());
|
.join(blog_post_page.get_output_path());
|
||||||
let css_files = vec![get_web_path(
|
|
||||||
config,
|
|
||||||
&self.output_directory,
|
|
||||||
&output_path,
|
|
||||||
"main.css",
|
|
||||||
)?];
|
|
||||||
let js_files = vec![get_web_path(
|
|
||||||
config,
|
|
||||||
&self.output_directory,
|
|
||||||
&output_path,
|
|
||||||
"blog_post.js",
|
|
||||||
)?];
|
|
||||||
let global_settings =
|
|
||||||
GlobalSettings::new(blog_post_page.title.clone(), css_files, js_files);
|
|
||||||
let render_context = convert_blog_post_page_to_render_context(
|
let render_context = convert_blog_post_page_to_render_context(
|
||||||
|
config,
|
||||||
|
&self.output_directory,
|
||||||
|
&output_path,
|
||||||
blog_post,
|
blog_post,
|
||||||
blog_post_page,
|
blog_post_page,
|
||||||
global_settings,
|
)?;
|
||||||
);
|
|
||||||
let rendered_output = renderer_integration.render(render_context)?;
|
let rendered_output = renderer_integration.render(render_context)?;
|
||||||
println!("Rendered: {}", rendered_output);
|
println!("Rendered: {}", rendered_output);
|
||||||
let parent_directory = output_path
|
let parent_directory = output_path
|
||||||
@ -110,41 +97,3 @@ fn build_name_contents_pairs<'a>(
|
|||||||
let contents = std::str::from_utf8(entry.contents())?;
|
let contents = std::str::from_utf8(entry.contents())?;
|
||||||
Ok((name, contents))
|
Ok((name, contents))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_web_path<D: AsRef<Path>, F: AsRef<Path>, P: AsRef<Path>>(
|
|
||||||
config: &Config,
|
|
||||||
output_directory: D,
|
|
||||||
containing_file: F,
|
|
||||||
path_from_web_root: P,
|
|
||||||
) -> Result<String, CustomError> {
|
|
||||||
let path_from_web_root = path_from_web_root.as_ref();
|
|
||||||
if config.use_relative_paths() {
|
|
||||||
let output_directory = output_directory.as_ref();
|
|
||||||
let containing_file = containing_file.as_ref();
|
|
||||||
// Subtracting 1 from the depth to "remove" the file name.
|
|
||||||
let depth_from_web_root = containing_file
|
|
||||||
.strip_prefix(output_directory)?
|
|
||||||
.components()
|
|
||||||
.count()
|
|
||||||
- 1;
|
|
||||||
let prefix = "../".repeat(depth_from_web_root);
|
|
||||||
let final_path = PathBuf::from(prefix).join(path_from_web_root);
|
|
||||||
let final_string = final_path
|
|
||||||
.as_path()
|
|
||||||
.to_str()
|
|
||||||
.map(str::to_string)
|
|
||||||
.ok_or("Path should be valid utf-8.")?;
|
|
||||||
Ok(final_string)
|
|
||||||
} else {
|
|
||||||
let web_root = config
|
|
||||||
.get_web_root()
|
|
||||||
.ok_or("Must either use_relative_paths or set the web_root in the config.")?;
|
|
||||||
let final_path = PathBuf::from(web_root).join(path_from_web_root);
|
|
||||||
let final_string = final_path
|
|
||||||
.as_path()
|
|
||||||
.to_str()
|
|
||||||
.map(str::to_string)
|
|
||||||
.ok_or("Path should be valid utf-8.")?;
|
|
||||||
Ok(final_string)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user