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.

355 lines
13KB

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