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.

322 lines
12KB

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