From df51e4d8b61e064c4ef6b314d1027aeb7e430723 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Sat, 22 Apr 2017 13:58:22 +0900 Subject: [PATCH] Add a special section for home page --- README.md | 10 +++++++--- src/site.rs | 15 +++++++++++---- test_site/content/_index.md | 4 ++++ tests/site.rs | 3 +++ 4 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 test_site/content/_index.md diff --git a/README.md b/README.md index d747337d..6c4b91fc 100644 --- a/README.md +++ b/README.md @@ -123,14 +123,18 @@ which template will be used to render that section. Sections will also automatically pick up their subsections, allowing you to make some complex pages layout and table of contents. +A special case is the `_index.md` at the root of the `content` directory which represents the homepage. It is only there +to control pagination and sorting of the homepage. + ### Code highlighting themes Code highlighting can be turned on by setting `highlight_code = true` in `config.toml`. When turned on, all text between backticks will be highlighted, like the example below. - ```rust - let site = Site::new(); - ``` +```rust +let site = Site::new(); +``` + If the name of the language is not given, it will default to plain-text highlighting. Gutenberg uses Sublime Text themes for syntax highlighting. It comes with the following theme diff --git a/src/site.rs b/src/site.rs index 74612ce3..6cd5cf68 100644 --- a/src/site.rs +++ b/src/site.rs @@ -64,6 +64,7 @@ pub struct Site { pub config: Config, pub pages: HashMap, pub sections: BTreeMap, + pub index: Option
, pub tera: Tera, live_reload: bool, output_path: PathBuf, @@ -91,6 +92,7 @@ impl Site { config: get_config(path, config_file), pages: HashMap::new(), sections: BTreeMap::new(), + index: None, tera: tera, live_reload: false, output_path: path.join("public"), @@ -117,16 +119,21 @@ impl Site { /// Reads all .md files in the `content` directory and create pages/sections /// out of them pub fn load(&mut self) -> Result<()> { - let path = self.base_path.to_string_lossy().replace("\\", "/"); - let content_glob = format!("{}/{}", path, "content/**/*.md"); + let base_path = self.base_path.to_string_lossy().replace("\\", "/"); + let content_glob = format!("{}/{}", base_path, "content/**/*.md"); // TODO: make that parallel, that's the main bottleneck // `add_section` and `add_page` can't be used in the parallel version afaik for entry in glob(&content_glob).unwrap().filter_map(|e| e.ok()) { let path = entry.as_path(); - if path.file_name().unwrap() == "_index.md" { - self.add_section(path)?; + // Index section + if path.parent().unwrap() == self.base_path.join("content") { + self.index = Some(Section::from_file(path, &self.config)?); + } else { + // all the other sections + self.add_section(path)?; + } } else { self.add_page(path)?; } diff --git a/test_site/content/_index.md b/test_site/content/_index.md new file mode 100644 index 00000000..a51bfdc4 --- /dev/null +++ b/test_site/content/_index.md @@ -0,0 +1,4 @@ ++++ +title = "Home" +description = "" ++++ diff --git a/tests/site.rs b/tests/site.rs index cd2500dc..fbdbbac0 100644 --- a/tests/site.rs +++ b/tests/site.rs @@ -22,6 +22,9 @@ fn test_can_parse_site() { assert_eq!(site.pages.len(), 10); let posts_path = path.join("content").join("posts"); + // We have an index page + assert!(site.index.is_some()); + // Make sure we remove all the pwd + content from the sections let basic = &site.pages[&posts_path.join("simple.md")]; assert_eq!(basic.components, vec!["posts".to_string()]);