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.

site.rs 25KB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. extern crate config;
  2. extern crate site;
  3. mod common;
  4. use std::collections::HashMap;
  5. use std::env;
  6. use std::path::Path;
  7. use common::{build_site, build_site_with_setup};
  8. use config::Taxonomy;
  9. use site::Site;
  10. #[test]
  11. fn can_parse_site() {
  12. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  13. path.push("test_site");
  14. let mut site = Site::new(&path, "config.toml").unwrap();
  15. site.load().unwrap();
  16. // Correct number of pages (sections do not count as pages)
  17. assert_eq!(site.library.pages().len(), 22);
  18. let posts_path = path.join("content").join("posts");
  19. // Make sure the page with a url doesn't have any sections
  20. let url_post = site.library.get_page(&posts_path.join("fixed-url.md")).unwrap();
  21. assert_eq!(url_post.path, "a-fixed-url/");
  22. // Make sure the article in a folder with only asset doesn't get counted as a section
  23. let asset_folder_post =
  24. site.library.get_page(&posts_path.join("with-assets").join("index.md")).unwrap();
  25. assert_eq!(asset_folder_post.file.components, vec!["posts".to_string()]);
  26. // That we have the right number of sections
  27. assert_eq!(site.library.sections().len(), 11);
  28. // And that the sections are correct
  29. let index_section = site.library.get_section(&path.join("content").join("_index.md")).unwrap();
  30. assert_eq!(index_section.subsections.len(), 4);
  31. assert_eq!(index_section.pages.len(), 1);
  32. assert!(index_section.ancestors.is_empty());
  33. let posts_section = site.library.get_section(&posts_path.join("_index.md")).unwrap();
  34. assert_eq!(posts_section.subsections.len(), 2);
  35. assert_eq!(posts_section.pages.len(), 10);
  36. assert_eq!(
  37. posts_section.ancestors,
  38. vec![*site.library.get_section_key(&index_section.file.path).unwrap()]
  39. );
  40. // Make sure we remove all the pwd + content from the sections
  41. let basic = site.library.get_page(&posts_path.join("simple.md")).unwrap();
  42. assert_eq!(basic.file.components, vec!["posts".to_string()]);
  43. assert_eq!(
  44. basic.ancestors,
  45. vec![
  46. *site.library.get_section_key(&index_section.file.path).unwrap(),
  47. *site.library.get_section_key(&posts_section.file.path).unwrap(),
  48. ]
  49. );
  50. let tutorials_section =
  51. site.library.get_section(&posts_path.join("tutorials").join("_index.md")).unwrap();
  52. assert_eq!(tutorials_section.subsections.len(), 2);
  53. let sub1 = site.library.get_section_by_key(tutorials_section.subsections[0]);
  54. let sub2 = site.library.get_section_by_key(tutorials_section.subsections[1]);
  55. assert_eq!(sub1.clone().meta.title.unwrap(), "Programming");
  56. assert_eq!(sub2.clone().meta.title.unwrap(), "DevOps");
  57. assert_eq!(tutorials_section.pages.len(), 0);
  58. let devops_section = site
  59. .library
  60. .get_section(&posts_path.join("tutorials").join("devops").join("_index.md"))
  61. .unwrap();
  62. assert_eq!(devops_section.subsections.len(), 0);
  63. assert_eq!(devops_section.pages.len(), 2);
  64. assert_eq!(
  65. devops_section.ancestors,
  66. vec![
  67. *site.library.get_section_key(&index_section.file.path).unwrap(),
  68. *site.library.get_section_key(&posts_section.file.path).unwrap(),
  69. *site.library.get_section_key(&tutorials_section.file.path).unwrap(),
  70. ]
  71. );
  72. let prog_section = site
  73. .library
  74. .get_section(&posts_path.join("tutorials").join("programming").join("_index.md"))
  75. .unwrap();
  76. assert_eq!(prog_section.subsections.len(), 0);
  77. assert_eq!(prog_section.pages.len(), 2);
  78. }
  79. #[test]
  80. fn can_build_site_without_live_reload() {
  81. let (_, _tmp_dir, public) = build_site("test_site");
  82. assert!(&public.exists());
  83. assert!(file_exists!(public, "index.html"));
  84. assert!(file_exists!(public, "sitemap.xml"));
  85. assert!(file_exists!(public, "robots.txt"));
  86. assert!(file_exists!(public, "a-fixed-url/index.html"));
  87. assert!(file_exists!(public, "posts/python/index.html"));
  88. // Shortcodes work
  89. assert!(file_contains!(public, "posts/python/index.html", "Basic shortcode"));
  90. assert!(file_contains!(public, "posts/python/index.html", "Arrrh Bob"));
  91. assert!(file_contains!(public, "posts/python/index.html", "Arrrh Bob_Sponge"));
  92. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  93. assert!(file_exists!(public, "posts/with-assets/index.html"));
  94. assert!(file_exists!(public, "posts/no-section/simple/index.html"));
  95. // Sections
  96. assert!(file_exists!(public, "posts/index.html"));
  97. assert!(file_exists!(public, "posts/tutorials/index.html"));
  98. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  99. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  100. // Ensure subsection pages are correctly filled
  101. assert!(file_contains!(public, "posts/tutorials/index.html", "Sub-pages: 2"));
  102. // Pages and section get their relative path
  103. assert!(file_contains!(public, "posts/tutorials/index.html", "posts/tutorials/_index.md"));
  104. assert!(file_contains!(
  105. public,
  106. "posts/tutorials/devops/nix/index.html",
  107. "posts/tutorials/devops/nix.md"
  108. ));
  109. // aliases work
  110. assert!(file_exists!(public, "an-old-url/old-page/index.html"));
  111. assert!(file_contains!(public, "an-old-url/old-page/index.html", "something-else"));
  112. // html aliases work
  113. assert!(file_exists!(public, "an-old-url/an-old-alias.html"));
  114. assert!(file_contains!(public, "an-old-url/an-old-alias.html", "something-else"));
  115. // redirect_to works
  116. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  117. assert!(file_contains!(public, "posts/tutorials/devops/index.html", "docker"));
  118. // We do have categories
  119. assert_eq!(file_exists!(public, "categories/index.html"), true);
  120. assert_eq!(file_exists!(public, "categories/a-category/index.html"), true);
  121. assert_eq!(file_exists!(public, "categories/a-category/rss.xml"), true);
  122. // But no tags
  123. assert_eq!(file_exists!(public, "tags/index.html"), false);
  124. // Theme files are there
  125. assert!(file_exists!(public, "sample.css"));
  126. assert!(file_exists!(public, "some.js"));
  127. // SASS and SCSS files compile correctly
  128. assert!(file_exists!(public, "blog.css"));
  129. assert!(file_contains!(public, "blog.css", "red"));
  130. assert!(file_contains!(public, "blog.css", "blue"));
  131. assert!(!file_contains!(public, "blog.css", "@import \"included\""));
  132. assert!(file_contains!(public, "blog.css", "2rem")); // check include
  133. assert!(!file_exists!(public, "_included.css"));
  134. assert!(file_exists!(public, "scss.css"));
  135. assert!(file_exists!(public, "sass.css"));
  136. assert!(file_exists!(public, "nested_sass/sass.css"));
  137. assert!(file_exists!(public, "nested_sass/scss.css"));
  138. // no live reload code
  139. assert_eq!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"), false);
  140. // Both pages and sections are in the sitemap
  141. assert!(file_contains!(
  142. public,
  143. "sitemap.xml",
  144. "<loc>https://replace-this-with-your-url.com/posts/simple/</loc>"
  145. ));
  146. assert!(file_contains!(
  147. public,
  148. "sitemap.xml",
  149. "<loc>https://replace-this-with-your-url.com/posts/</loc>"
  150. ));
  151. // Drafts are not in the sitemap
  152. assert!(!file_contains!(public, "sitemap.xml", "draft"));
  153. // robots.txt has been rendered from the template
  154. assert!(file_contains!(public, "robots.txt", "User-agent: zola"));
  155. assert!(file_contains!(
  156. public,
  157. "robots.txt",
  158. "Sitemap: https://replace-this-with-your-url.com/sitemap.xml"
  159. ));
  160. }
  161. #[test]
  162. fn can_build_site_with_live_reload() {
  163. let (_, _tmp_dir, public) = build_site_with_setup("test_site", |mut site| {
  164. site.enable_live_reload(1000);
  165. (site, true)
  166. });
  167. assert!(&public.exists());
  168. assert!(file_exists!(public, "index.html"));
  169. assert!(file_exists!(public, "sitemap.xml"));
  170. assert!(file_exists!(public, "robots.txt"));
  171. assert!(file_exists!(public, "a-fixed-url/index.html"));
  172. assert!(file_exists!(public, "posts/python/index.html"));
  173. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  174. assert!(file_exists!(public, "posts/with-assets/index.html"));
  175. // Sections
  176. assert!(file_exists!(public, "posts/index.html"));
  177. assert!(file_exists!(public, "posts/tutorials/index.html"));
  178. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  179. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  180. // TODO: add assertion for syntax highlighting
  181. // We do have categories
  182. assert_eq!(file_exists!(public, "categories/index.html"), true);
  183. assert_eq!(file_exists!(public, "categories/a-category/index.html"), true);
  184. assert_eq!(file_exists!(public, "categories/a-category/rss.xml"), true);
  185. // But no tags
  186. assert_eq!(file_exists!(public, "tags/index.html"), false);
  187. // no live reload code
  188. assert!(file_contains!(public, "index.html", "/livereload.js"));
  189. // the summary anchor link has been created
  190. assert!(file_contains!(
  191. public,
  192. "posts/python/index.html",
  193. r#"<a name="continue-reading"></a>"#
  194. ));
  195. assert!(file_contains!(public, "posts/draft/index.html", r#"THEME_SHORTCODE"#));
  196. }
  197. #[test]
  198. fn can_build_site_with_taxonomies() {
  199. let (site, _tmp_dir, public) = build_site_with_setup("test_site", |mut site| {
  200. site.load().unwrap();
  201. for (i, (_, page)) in site.library.pages_mut().iter_mut().enumerate() {
  202. page.meta.taxonomies = {
  203. let mut taxonomies = HashMap::new();
  204. taxonomies.insert(
  205. "categories".to_string(),
  206. vec![if i % 2 == 0 { "A" } else { "B" }.to_string()],
  207. );
  208. taxonomies
  209. };
  210. }
  211. site.populate_taxonomies().unwrap();
  212. (site, false)
  213. });
  214. assert!(&public.exists());
  215. assert_eq!(site.taxonomies.len(), 1);
  216. assert!(file_exists!(public, "index.html"));
  217. assert!(file_exists!(public, "sitemap.xml"));
  218. assert!(file_exists!(public, "robots.txt"));
  219. assert!(file_exists!(public, "a-fixed-url/index.html"));
  220. assert!(file_exists!(public, "posts/python/index.html"));
  221. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  222. assert!(file_exists!(public, "posts/with-assets/index.html"));
  223. // Sections
  224. assert!(file_exists!(public, "posts/index.html"));
  225. assert!(file_exists!(public, "posts/tutorials/index.html"));
  226. assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
  227. assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
  228. // Categories are there
  229. assert!(file_exists!(public, "categories/index.html"));
  230. assert!(file_exists!(public, "categories/a/index.html"));
  231. assert!(file_exists!(public, "categories/b/index.html"));
  232. assert!(file_exists!(public, "categories/a/rss.xml"));
  233. assert!(file_contains!(
  234. public,
  235. "categories/a/rss.xml",
  236. "https://replace-this-with-your-url.com/categories/a/rss.xml"
  237. ));
  238. // Extending from a theme works
  239. assert!(file_contains!(public, "categories/a/index.html", "EXTENDED"));
  240. // Tags aren't
  241. assert_eq!(file_exists!(public, "tags/index.html"), false);
  242. // Categories are in the sitemap
  243. assert!(file_contains!(
  244. public,
  245. "sitemap.xml",
  246. "<loc>https://replace-this-with-your-url.com/categories/</loc>"
  247. ));
  248. assert!(file_contains!(
  249. public,
  250. "sitemap.xml",
  251. "<loc>https://replace-this-with-your-url.com/categories/a/</loc>"
  252. ));
  253. }
  254. #[test]
  255. fn can_build_site_and_insert_anchor_links() {
  256. let (_, _tmp_dir, public) = build_site("test_site");
  257. assert!(Path::new(&public).exists());
  258. // anchor link inserted
  259. assert!(file_contains!(
  260. public,
  261. "posts/something-else/index.html",
  262. "<h1 id=\"title\"><a class=\"zola-anchor\" href=\"#title\""
  263. ));
  264. }
  265. #[test]
  266. fn can_build_site_with_pagination_for_section() {
  267. let (_, _tmp_dir, public) = build_site_with_setup("test_site", |mut site| {
  268. site.load().unwrap();
  269. for (_, section) in site.library.sections_mut() {
  270. if section.is_index() {
  271. continue;
  272. }
  273. section.meta.paginate_by = Some(2);
  274. section.meta.template = Some("section_paginated.html".to_string());
  275. }
  276. (site, false)
  277. });
  278. assert!(&public.exists());
  279. assert!(file_exists!(public, "index.html"));
  280. assert!(file_exists!(public, "sitemap.xml"));
  281. assert!(file_exists!(public, "robots.txt"));
  282. assert!(file_exists!(public, "a-fixed-url/index.html"));
  283. assert!(file_exists!(public, "posts/python/index.html"));
  284. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  285. assert!(file_exists!(public, "posts/with-assets/index.html"));
  286. // Sections
  287. assert!(file_exists!(public, "posts/index.html"));
  288. // And pagination!
  289. assert!(file_exists!(public, "posts/page/1/index.html"));
  290. // even if there is no pages, only the section!
  291. assert!(file_exists!(public, "paginated/page/1/index.html"));
  292. assert!(file_exists!(public, "paginated/index.html"));
  293. // should redirect to posts/
  294. assert!(file_contains!(
  295. public,
  296. "posts/page/1/index.html",
  297. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/posts/\""
  298. ));
  299. assert!(file_contains!(public, "posts/index.html", "Num pagers: 5"));
  300. assert!(file_contains!(public, "posts/index.html", "Page size: 2"));
  301. assert!(file_contains!(public, "posts/index.html", "Current index: 1"));
  302. assert!(!file_contains!(public, "posts/index.html", "has_prev"));
  303. assert!(file_contains!(public, "posts/index.html", "has_next"));
  304. assert!(file_contains!(
  305. public,
  306. "posts/index.html",
  307. "First: https://replace-this-with-your-url.com/posts/"
  308. ));
  309. assert!(file_contains!(
  310. public,
  311. "posts/index.html",
  312. "Last: https://replace-this-with-your-url.com/posts/page/5/"
  313. ));
  314. assert_eq!(file_contains!(public, "posts/index.html", "has_prev"), false);
  315. assert!(file_exists!(public, "posts/page/2/index.html"));
  316. assert!(file_contains!(public, "posts/page/2/index.html", "Num pagers: 5"));
  317. assert!(file_contains!(public, "posts/page/2/index.html", "Page size: 2"));
  318. assert!(file_contains!(public, "posts/page/2/index.html", "Current index: 2"));
  319. assert!(file_contains!(public, "posts/page/2/index.html", "has_prev"));
  320. assert!(file_contains!(public, "posts/page/2/index.html", "has_next"));
  321. assert!(file_contains!(
  322. public,
  323. "posts/page/2/index.html",
  324. "First: https://replace-this-with-your-url.com/posts/"
  325. ));
  326. assert!(file_contains!(
  327. public,
  328. "posts/page/2/index.html",
  329. "Last: https://replace-this-with-your-url.com/posts/page/5/"
  330. ));
  331. assert!(file_exists!(public, "posts/page/3/index.html"));
  332. assert!(file_contains!(public, "posts/page/3/index.html", "Num pagers: 5"));
  333. assert!(file_contains!(public, "posts/page/3/index.html", "Page size: 2"));
  334. assert!(file_contains!(public, "posts/page/3/index.html", "Current index: 3"));
  335. assert!(file_contains!(public, "posts/page/3/index.html", "has_prev"));
  336. assert!(file_contains!(public, "posts/page/3/index.html", "has_next"));
  337. assert!(file_contains!(
  338. public,
  339. "posts/page/3/index.html",
  340. "First: https://replace-this-with-your-url.com/posts/"
  341. ));
  342. assert!(file_contains!(
  343. public,
  344. "posts/page/3/index.html",
  345. "Last: https://replace-this-with-your-url.com/posts/page/5/"
  346. ));
  347. assert!(file_exists!(public, "posts/page/4/index.html"));
  348. assert!(file_contains!(public, "posts/page/4/index.html", "Num pagers: 5"));
  349. assert!(file_contains!(public, "posts/page/4/index.html", "Page size: 2"));
  350. assert!(file_contains!(public, "posts/page/4/index.html", "Current index: 4"));
  351. assert!(file_contains!(public, "posts/page/4/index.html", "has_prev"));
  352. assert!(file_contains!(public, "posts/page/4/index.html", "has_next"));
  353. assert!(file_contains!(
  354. public,
  355. "posts/page/4/index.html",
  356. "First: https://replace-this-with-your-url.com/posts/"
  357. ));
  358. assert!(file_contains!(
  359. public,
  360. "posts/page/4/index.html",
  361. "Last: https://replace-this-with-your-url.com/posts/page/5/"
  362. ));
  363. // sitemap contains the pager pages
  364. assert!(file_contains!(
  365. public,
  366. "sitemap.xml",
  367. "<loc>https://replace-this-with-your-url.com/posts/page/4/</loc>"
  368. ));
  369. }
  370. #[test]
  371. fn can_build_site_with_pagination_for_index() {
  372. let (_, _tmp_dir, public) = build_site_with_setup("test_site", |mut site| {
  373. site.load().unwrap();
  374. {
  375. let index = site
  376. .library
  377. .get_section_mut(&site.base_path.join("content").join("_index.md"))
  378. .unwrap();
  379. index.meta.paginate_by = Some(2);
  380. index.meta.template = Some("index_paginated.html".to_string());
  381. }
  382. (site, false)
  383. });
  384. assert!(&public.exists());
  385. assert!(file_exists!(public, "index.html"));
  386. assert!(file_exists!(public, "sitemap.xml"));
  387. assert!(file_exists!(public, "robots.txt"));
  388. assert!(file_exists!(public, "a-fixed-url/index.html"));
  389. assert!(file_exists!(public, "posts/python/index.html"));
  390. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  391. assert!(file_exists!(public, "posts/with-assets/index.html"));
  392. // And pagination!
  393. assert!(file_exists!(public, "page/1/index.html"));
  394. // even if there is no pages, only the section!
  395. assert!(file_exists!(public, "paginated/page/1/index.html"));
  396. assert!(file_exists!(public, "paginated/index.html"));
  397. // should redirect to index
  398. assert!(file_contains!(
  399. public,
  400. "page/1/index.html",
  401. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/\""
  402. ));
  403. assert!(file_contains!(public, "index.html", "Num pages: 1"));
  404. assert!(file_contains!(public, "index.html", "Current index: 1"));
  405. assert!(file_contains!(public, "index.html", "First: https://replace-this-with-your-url.com/"));
  406. assert!(file_contains!(public, "index.html", "Last: https://replace-this-with-your-url.com/"));
  407. assert_eq!(file_contains!(public, "index.html", "has_prev"), false);
  408. assert_eq!(file_contains!(public, "index.html", "has_next"), false);
  409. // sitemap contains the pager pages
  410. assert!(file_contains!(
  411. public,
  412. "sitemap.xml",
  413. "<loc>https://replace-this-with-your-url.com/page/1/</loc>"
  414. ))
  415. }
  416. #[test]
  417. fn can_build_site_with_pagination_for_taxonomy() {
  418. let (_, _tmp_dir, public) = build_site_with_setup("test_site", |mut site| {
  419. site.config.taxonomies.push(Taxonomy {
  420. name: "tags".to_string(),
  421. paginate_by: Some(2),
  422. paginate_path: None,
  423. rss: true,
  424. lang: None,
  425. });
  426. site.load().unwrap();
  427. for (i, (_, page)) in site.library.pages_mut().iter_mut().enumerate() {
  428. page.meta.taxonomies = {
  429. let mut taxonomies = HashMap::new();
  430. taxonomies.insert(
  431. "tags".to_string(),
  432. vec![if i % 2 == 0 { "A" } else { "B" }.to_string()],
  433. );
  434. taxonomies
  435. };
  436. }
  437. site.populate_taxonomies().unwrap();
  438. (site, false)
  439. });
  440. assert!(&public.exists());
  441. assert!(file_exists!(public, "index.html"));
  442. assert!(file_exists!(public, "sitemap.xml"));
  443. assert!(file_exists!(public, "robots.txt"));
  444. assert!(file_exists!(public, "a-fixed-url/index.html"));
  445. assert!(file_exists!(public, "posts/python/index.html"));
  446. assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
  447. assert!(file_exists!(public, "posts/with-assets/index.html"));
  448. // Tags
  449. assert!(file_exists!(public, "tags/index.html"));
  450. // With RSS
  451. assert!(file_exists!(public, "tags/a/rss.xml"));
  452. assert!(file_exists!(public, "tags/b/rss.xml"));
  453. // And pagination!
  454. assert!(file_exists!(public, "tags/a/page/1/index.html"));
  455. assert!(file_exists!(public, "tags/b/page/1/index.html"));
  456. assert!(file_exists!(public, "tags/a/page/2/index.html"));
  457. assert!(file_exists!(public, "tags/b/page/2/index.html"));
  458. // should redirect to posts/
  459. assert!(file_contains!(
  460. public,
  461. "tags/a/page/1/index.html",
  462. "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/tags/a/\""
  463. ));
  464. assert!(file_contains!(public, "tags/a/index.html", "Num pagers: 6"));
  465. assert!(file_contains!(public, "tags/a/index.html", "Page size: 2"));
  466. assert!(file_contains!(public, "tags/a/index.html", "Current index: 1"));
  467. assert!(!file_contains!(public, "tags/a/index.html", "has_prev"));
  468. assert!(file_contains!(public, "tags/a/index.html", "has_next"));
  469. assert!(file_contains!(
  470. public,
  471. "tags/a/index.html",
  472. "First: https://replace-this-with-your-url.com/tags/a/"
  473. ));
  474. assert!(file_contains!(
  475. public,
  476. "tags/a/index.html",
  477. "Last: https://replace-this-with-your-url.com/tags/a/page/6/"
  478. ));
  479. assert_eq!(file_contains!(public, "tags/a/index.html", "has_prev"), false);
  480. // sitemap contains the pager pages
  481. assert!(file_contains!(
  482. public,
  483. "sitemap.xml",
  484. "<loc>https://replace-this-with-your-url.com/tags/a/page/6/</loc>"
  485. ))
  486. }
  487. #[test]
  488. fn can_build_rss_feed() {
  489. let (_, _tmp_dir, public) = build_site("test_site");
  490. assert!(&public.exists());
  491. assert!(file_exists!(public, "rss.xml"));
  492. // latest article is posts/extra-syntax.md
  493. assert!(file_contains!(public, "rss.xml", "Extra Syntax"));
  494. // Next is posts/simple.md
  495. assert!(file_contains!(public, "rss.xml", "Simple article with shortcodes"));
  496. }
  497. #[test]
  498. fn can_build_search_index() {
  499. let (_, _tmp_dir, public) = build_site_with_setup("test_site", |mut site| {
  500. site.config.build_search_index = true;
  501. (site, true)
  502. });
  503. assert!(Path::new(&public).exists());
  504. assert!(file_exists!(public, "elasticlunr.min.js"));
  505. assert!(file_exists!(public, "search_index.en.js"));
  506. }
  507. #[test]
  508. fn can_build_with_extra_syntaxes() {
  509. let (_, _tmp_dir, public) = build_site("test_site");
  510. assert!(&public.exists());
  511. assert!(file_exists!(public, "posts/extra-syntax/index.html"));
  512. assert!(file_contains!(
  513. public,
  514. "posts/extra-syntax/index.html",
  515. r#"<span style="color:#d08770;">test</span>"#
  516. ));
  517. }
  518. #[test]
  519. fn can_apply_page_templates() {
  520. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  521. path.push("test_site");
  522. let mut site = Site::new(&path, "config.toml").unwrap();
  523. site.load().unwrap();
  524. let template_path = path.join("content").join("applying_page_template");
  525. let template_section = site.library.get_section(&template_path.join("_index.md")).unwrap();
  526. assert_eq!(template_section.subsections.len(), 2);
  527. assert_eq!(template_section.pages.len(), 2);
  528. let from_section_config = site.library.get_page_by_key(template_section.pages[0]);
  529. assert_eq!(from_section_config.meta.template, Some("page_template.html".into()));
  530. assert_eq!(from_section_config.meta.title, Some("From section config".into()));
  531. let override_page_template = site.library.get_page_by_key(template_section.pages[1]);
  532. assert_eq!(override_page_template.meta.template, Some("page_template_override.html".into()));
  533. assert_eq!(override_page_template.meta.title, Some("Override".into()));
  534. // It should have applied recursively as well
  535. let another_section =
  536. site.library.get_section(&template_path.join("another_section").join("_index.md")).unwrap();
  537. assert_eq!(another_section.subsections.len(), 0);
  538. assert_eq!(another_section.pages.len(), 1);
  539. let changed_recursively = site.library.get_page_by_key(another_section.pages[0]);
  540. assert_eq!(changed_recursively.meta.template, Some("page_template.html".into()));
  541. assert_eq!(changed_recursively.meta.title, Some("Changed recursively".into()));
  542. // But it should not have override a children page_template
  543. let yet_another_section = site
  544. .library
  545. .get_section(&template_path.join("yet_another_section").join("_index.md"))
  546. .unwrap();
  547. assert_eq!(yet_another_section.subsections.len(), 0);
  548. assert_eq!(yet_another_section.pages.len(), 1);
  549. let child = site.library.get_page_by_key(yet_another_section.pages[0]);
  550. assert_eq!(child.meta.template, Some("page_template_child.html".into()));
  551. assert_eq!(child.meta.title, Some("Local section override".into()));
  552. }
  553. // https://github.com/getzola/zola/issues/571
  554. #[test]
  555. fn can_build_site_custom_builtins_from_theme() {
  556. let (_, _tmp_dir, public) = build_site("test_site");
  557. assert!(&public.exists());
  558. // 404.html is a theme template.
  559. assert!(file_exists!(public, "404.html"));
  560. assert!(file_contains!(public, "404.html", "Oops"));
  561. }