You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

287 lines
10KB

  1. use std::collections::HashMap;
  2. use std::path::{Path, PathBuf};
  3. use tera::{Tera, Context as TeraContext};
  4. use slotmap::Key;
  5. use config::Config;
  6. use front_matter::{SectionFrontMatter, split_section_content};
  7. use errors::{Result, ResultExt};
  8. use utils::fs::{read_file, find_related_assets};
  9. use utils::templates::render_template;
  10. use utils::site::get_reading_analytics;
  11. use rendering::{RenderContext, Header, render_content};
  12. use content::file_info::FileInfo;
  13. use content::ser::SerializingSection;
  14. use library::Library;
  15. #[derive(Clone, Debug, PartialEq)]
  16. pub struct Section {
  17. /// All info about the actual file
  18. pub file: FileInfo,
  19. /// The front matter meta-data
  20. pub meta: SectionFrontMatter,
  21. /// The URL path of the page
  22. pub path: String,
  23. /// The components for the path of that page
  24. pub components: Vec<String>,
  25. /// The full URL for that page
  26. pub permalink: String,
  27. /// The actual content of the page, in markdown
  28. pub raw_content: String,
  29. /// The HTML rendered of the page
  30. pub content: String,
  31. /// All the non-md files we found next to the .md file
  32. pub assets: Vec<PathBuf>,
  33. /// All direct pages of that section
  34. pub pages: Vec<Key>,
  35. /// All pages that cannot be sorted in this section
  36. pub ignored_pages: Vec<Key>,
  37. /// The list of parent sections
  38. pub ancestors: Vec<Key>,
  39. /// All direct subsections
  40. pub subsections: Vec<Key>,
  41. /// Toc made from the headers of the markdown file
  42. pub toc: Vec<Header>,
  43. /// How many words in the raw content
  44. pub word_count: Option<usize>,
  45. /// How long would it take to read the raw content.
  46. /// See `get_reading_analytics` on how it is calculated
  47. pub reading_time: Option<usize>,
  48. }
  49. impl Section {
  50. pub fn new<P: AsRef<Path>>(file_path: P, meta: SectionFrontMatter) -> Section {
  51. let file_path = file_path.as_ref();
  52. Section {
  53. file: FileInfo::new_section(file_path),
  54. meta,
  55. ancestors: vec![],
  56. path: "".to_string(),
  57. components: vec![],
  58. permalink: "".to_string(),
  59. raw_content: "".to_string(),
  60. assets: vec![],
  61. content: "".to_string(),
  62. pages: vec![],
  63. ignored_pages: vec![],
  64. subsections: vec![],
  65. toc: vec![],
  66. word_count: None,
  67. reading_time: None,
  68. }
  69. }
  70. pub fn parse(file_path: &Path, content: &str, config: &Config) -> Result<Section> {
  71. let (meta, content) = split_section_content(file_path, content)?;
  72. let mut section = Section::new(file_path, meta);
  73. section.raw_content = content.clone();
  74. let (word_count, reading_time) = get_reading_analytics(&section.raw_content);
  75. section.word_count = Some(word_count);
  76. section.reading_time = Some(reading_time);
  77. section.path = format!("{}/", section.file.components.join("/"));
  78. section.components = section.path.split('/')
  79. .map(|p| p.to_string())
  80. .filter(|p| !p.is_empty())
  81. .collect::<Vec<_>>();
  82. section.permalink = config.make_permalink(&section.path);
  83. Ok(section)
  84. }
  85. /// Read and parse a .md file into a Page struct
  86. pub fn from_file<P: AsRef<Path>>(path: P, config: &Config) -> Result<Section> {
  87. let path = path.as_ref();
  88. let content = read_file(path)?;
  89. let mut section = Section::parse(path, &content, config)?;
  90. let parent_dir = path.parent().unwrap();
  91. let assets = find_related_assets(parent_dir);
  92. if let Some(ref globset) = config.ignored_content_globset {
  93. // `find_related_assets` only scans the immediate directory (it is not recursive) so our
  94. // filtering only needs to work against the file_name component, not the full suffix. If
  95. // `find_related_assets` was changed to also return files in subdirectories, we could
  96. // use `PathBuf.strip_prefix` to remove the parent directory and then glob-filter
  97. // against the remaining path. Note that the current behaviour effectively means that
  98. // the `ignored_content` setting in the config file is limited to single-file glob
  99. // patterns (no "**" patterns).
  100. section.assets = assets.into_iter()
  101. .filter(|path|
  102. match path.file_name() {
  103. None => true,
  104. Some(file) => !globset.is_match(file)
  105. }
  106. ).collect();
  107. } else {
  108. section.assets = assets;
  109. }
  110. Ok(section)
  111. }
  112. pub fn get_template_name(&self) -> String {
  113. match self.meta.template {
  114. Some(ref l) => l.to_string(),
  115. None => {
  116. if self.is_index() {
  117. return "index.html".to_string();
  118. }
  119. "section.html".to_string()
  120. }
  121. }
  122. }
  123. /// We need access to all pages url to render links relative to content
  124. /// so that can't happen at the same time as parsing
  125. pub fn render_markdown(
  126. &mut self,
  127. permalinks: &HashMap<String, String>,
  128. tera: &Tera,
  129. config: &Config,
  130. ) -> Result<()> {
  131. let mut context = RenderContext::new(
  132. tera,
  133. config,
  134. &self.permalink,
  135. permalinks,
  136. self.meta.insert_anchor_links,
  137. );
  138. context.tera_context.insert("section", &SerializingSection::from_section_basic(self, None));
  139. let res = render_content(&self.raw_content, &context)
  140. .chain_err(|| format!("Failed to render content of {}", self.file.path.display()))?;
  141. self.content = res.body;
  142. self.toc = res.toc;
  143. Ok(())
  144. }
  145. /// Renders the page using the default layout, unless specified in front-matter
  146. pub fn render_html(&self, tera: &Tera, config: &Config, library: &Library) -> Result<String> {
  147. let tpl_name = self.get_template_name();
  148. let mut context = TeraContext::new();
  149. context.insert("config", config);
  150. context.insert("current_url", &self.permalink);
  151. context.insert("current_path", &self.path);
  152. context.insert("section", &self.to_serialized(library));
  153. render_template(&tpl_name, tera, &context, &config.theme)
  154. .chain_err(|| format!("Failed to render section '{}'", self.file.path.display()))
  155. }
  156. /// Is this the index section?
  157. pub fn is_index(&self) -> bool {
  158. self.file.components.is_empty()
  159. }
  160. /// Creates a vectors of asset URLs.
  161. pub fn serialize_assets(&self) -> Vec<String> {
  162. self.assets.iter()
  163. .filter_map(|asset| asset.file_name())
  164. .filter_map(|filename| filename.to_str())
  165. .map(|filename| self.path.clone() + filename)
  166. .collect()
  167. }
  168. pub fn to_serialized<'a>(&'a self, library: &'a Library) -> SerializingSection<'a> {
  169. SerializingSection::from_section(self, library)
  170. }
  171. pub fn to_serialized_basic<'a>(&'a self, library: &'a Library) -> SerializingSection<'a> {
  172. SerializingSection::from_section_basic(self, Some(library))
  173. }
  174. }
  175. /// Used to create a default index section if there is no _index.md in the root content directory
  176. impl Default for Section {
  177. fn default() -> Section {
  178. Section {
  179. file: FileInfo::default(),
  180. meta: SectionFrontMatter::default(),
  181. ancestors: vec![],
  182. path: "".to_string(),
  183. components: vec![],
  184. permalink: "".to_string(),
  185. raw_content: "".to_string(),
  186. assets: vec![],
  187. content: "".to_string(),
  188. pages: vec![],
  189. ignored_pages: vec![],
  190. subsections: vec![],
  191. toc: vec![],
  192. reading_time: None,
  193. word_count: None,
  194. }
  195. }
  196. }
  197. #[cfg(test)]
  198. mod tests {
  199. use std::io::Write;
  200. use std::fs::{File, create_dir};
  201. use tempfile::tempdir;
  202. use globset::{Glob, GlobSetBuilder};
  203. use config::Config;
  204. use super::Section;
  205. #[test]
  206. fn section_with_assets_gets_right_info() {
  207. let tmp_dir = tempdir().expect("create temp dir");
  208. let path = tmp_dir.path();
  209. create_dir(&path.join("content")).expect("create content temp dir");
  210. create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
  211. let nested_path = path.join("content").join("posts").join("with-assets");
  212. create_dir(&nested_path).expect("create nested temp dir");
  213. let mut f = File::create(nested_path.join("_index.md")).unwrap();
  214. f.write_all(b"+++\n+++\n").unwrap();
  215. File::create(nested_path.join("example.js")).unwrap();
  216. File::create(nested_path.join("graph.jpg")).unwrap();
  217. File::create(nested_path.join("fail.png")).unwrap();
  218. let res = Section::from_file(
  219. nested_path.join("_index.md").as_path(),
  220. &Config::default(),
  221. );
  222. assert!(res.is_ok());
  223. let section = res.unwrap();
  224. assert_eq!(section.assets.len(), 3);
  225. assert_eq!(section.permalink, "http://a-website.com/posts/with-assets/");
  226. }
  227. #[test]
  228. fn section_with_ignored_assets_filters_out_correct_files() {
  229. let tmp_dir = tempdir().expect("create temp dir");
  230. let path = tmp_dir.path();
  231. create_dir(&path.join("content")).expect("create content temp dir");
  232. create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
  233. let nested_path = path.join("content").join("posts").join("with-assets");
  234. create_dir(&nested_path).expect("create nested temp dir");
  235. let mut f = File::create(nested_path.join("_index.md")).unwrap();
  236. f.write_all(b"+++\nslug=\"hey\"\n+++\n").unwrap();
  237. File::create(nested_path.join("example.js")).unwrap();
  238. File::create(nested_path.join("graph.jpg")).unwrap();
  239. File::create(nested_path.join("fail.png")).unwrap();
  240. let mut gsb = GlobSetBuilder::new();
  241. gsb.add(Glob::new("*.{js,png}").unwrap());
  242. let mut config = Config::default();
  243. config.ignored_content_globset = Some(gsb.build().unwrap());
  244. let res = Section::from_file(
  245. nested_path.join("_index.md").as_path(),
  246. &config,
  247. );
  248. assert!(res.is_ok());
  249. let page = res.unwrap();
  250. assert_eq!(page.assets.len(), 1);
  251. assert_eq!(page.assets[0].file_name().unwrap().to_str(), Some("graph.jpg"));
  252. }
  253. }