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.

372 lines
13KB

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