-
Notifications
You must be signed in to change notification settings - Fork 996
/
Copy pathlib.rs
1238 lines (1098 loc) · 45.7 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
pub mod feeds;
pub mod link_checking;
mod minify;
pub mod sass;
pub mod sitemap;
pub mod tpls;
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, RwLock};
use libs::once_cell::sync::Lazy;
use libs::rayon::prelude::*;
use libs::tera::{Context, Tera};
use libs::walkdir::{DirEntry, WalkDir};
use config::{get_config, Config, IndexFormat};
use content::{Library, Page, Paginator, Section, Taxonomy};
use errors::{anyhow, bail, Result};
use libs::relative_path::RelativePathBuf;
use std::time::Instant;
use templates::{load_tera, render_redirect_template};
use utils::fs::{
clean_site_output_folder, copy_directory, copy_file_if_needed, create_directory, create_file,
};
use utils::net::{get_available_port, is_external_link};
use utils::templates::{render_template, ShortcodeDefinition};
use utils::types::InsertAnchor;
pub static SITE_CONTENT: Lazy<Arc<RwLock<HashMap<RelativePathBuf, String>>>> =
Lazy::new(|| Arc::new(RwLock::new(HashMap::new())));
/// Where are we building the site
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum BuildMode {
/// On the filesystem -> `zola build`, The path is the `output_path`
Disk,
/// In memory for the content -> `zola serve`
Memory,
/// Both on the filesystem and in memory
Both,
}
#[derive(Debug)]
pub struct Site {
/// The base path of the zola site
pub base_path: PathBuf,
/// The parsed config for the site
pub config: Config,
pub tera: Tera,
imageproc: Arc<Mutex<imageproc::Processor>>,
// the live reload port to be used if there is one
pub live_reload: Option<u16>,
pub output_path: PathBuf,
content_path: PathBuf,
pub sass_path: PathBuf,
pub static_path: PathBuf,
pub templates_path: PathBuf,
pub taxonomies: Vec<Taxonomy>,
/// A map of all .md files (section and pages) and their permalink
/// We need that if there are relative links in the content that need to be resolved
pub permalinks: HashMap<String, String>,
/// Contains all pages and sections of the site
pub library: Arc<RwLock<Library>>,
/// Whether to load draft pages
include_drafts: bool,
build_mode: BuildMode,
shortcode_definitions: HashMap<String, ShortcodeDefinition>,
/// Whether to check external links
check_external_links: bool,
}
impl Site {
/// Parse a site at the given path. Defaults to the current dir
/// Passing in a path is used in tests and when --root argument is passed
pub fn new<P: AsRef<Path>, P2: AsRef<Path>>(path: P, config_file: P2) -> Result<Site> {
let path = path.as_ref();
let config_file = config_file.as_ref();
let mut config = get_config(&path.join(config_file))?;
if let Some(theme) = config.theme.clone() {
// Grab data from the extra section of the theme
config.merge_with_theme(path.join("themes").join(&theme).join("theme.toml"), &theme)?;
}
let tera = load_tera(path, &config)?;
let shortcode_definitions = utils::templates::get_shortcodes(&tera);
let content_path = path.join("content");
let sass_path = path.join("sass");
let static_path = path.join("static");
let templates_path = path.join("templates");
let imageproc = imageproc::Processor::new(path.to_path_buf(), &config);
let output_path = path.join(config.output_dir.clone());
let site = Site {
base_path: path.to_path_buf(),
config,
tera,
imageproc: Arc::new(Mutex::new(imageproc)),
live_reload: None,
output_path,
content_path,
sass_path,
static_path,
templates_path,
taxonomies: Vec::new(),
permalinks: HashMap::new(),
include_drafts: false,
// We will allocate it properly later on
library: Arc::new(RwLock::new(Library::default())),
build_mode: BuildMode::Disk,
shortcode_definitions,
check_external_links: true,
};
Ok(site)
}
/// Enable some `zola serve` related options
pub fn enable_serve_mode(&mut self, build_mode: BuildMode) {
SITE_CONTENT.write().unwrap().clear();
self.config.enable_serve_mode();
self.build_mode = build_mode;
}
/// Set the site to load the drafts.
/// Needs to be called before loading it
pub fn include_drafts(&mut self) {
self.include_drafts = true;
}
/// Set the site checker to skip external links check.
pub fn skip_external_links_check(&mut self) {
self.check_external_links = false;
}
/// The index sections are ALWAYS at those paths
/// There are one index section for the default language + 1 per language
fn index_section_paths(&self) -> Vec<(PathBuf, Option<&str>)> {
let mut res = vec![(self.content_path.join("_index.md"), None)];
for (code, _) in self.config.other_languages() {
res.push((self.content_path.join(format!("_index.{}.md", code)), Some(code)));
}
res
}
/// We avoid the port the server is going to use as it's not bound yet
/// when calling this function and we could end up having tried to bind
/// both http and websocket server to the same port
pub fn enable_live_reload(&mut self, port_to_avoid: u16) {
self.live_reload = get_available_port(port_to_avoid);
}
/// Only used in `zola serve` to re-use the initial websocket port
pub fn enable_live_reload_with_port(&mut self, live_reload_port: u16) {
self.live_reload = Some(live_reload_port);
}
/// Reloads the templates and rebuild the site without re-markdown the Markdown.
pub fn reload_templates(&mut self) -> Result<()> {
self.tera.full_reload()?;
// TODO: be smarter than that, no need to recompile sass for example
self.build()
}
pub fn set_base_url(&mut self, base_url: String) {
self.config.base_url = base_url;
let mut imageproc = self.imageproc.lock().expect("Couldn't lock imageproc (set_base_url)");
imageproc.set_base_url(&self.config);
}
pub fn set_output_path<P: AsRef<Path>>(&mut self, path: P) {
self.output_path = path.as_ref().to_path_buf();
}
pub fn minify(&mut self) {
self.config.minify_html = true;
}
/// Reads all .md files in the `content` directory and create pages/sections
/// out of them
pub fn load(&mut self) -> Result<()> {
self.library = Arc::new(RwLock::new(Library::new(&self.config)));
let mut pages_insert_anchors = HashMap::new();
// not the most elegant loop, but this is necessary to use skip_current_dir
// which we can only decide to use after we've deserialised the section
// so it's kinda necessecary
let mut dir_walker =
WalkDir::new(self.base_path.join("content")).follow_links(true).into_iter();
let mut allowed_index_filenames: Vec<_> = self
.config
.other_languages()
.keys()
.map(|code| format!("_index.{}.md", code))
.collect();
allowed_index_filenames.push("_index.md".to_string());
// We will insert colocated pages (those with a index.md filename)
// at the end to detect pages that are actually errors:
// when there is both a _index.md and index.md in the same folder
let mut pages = Vec::new();
let mut sections = HashSet::new();
loop {
let entry: DirEntry = match dir_walker.next() {
None => break,
Some(Err(_)) => continue,
Some(Ok(entry)) => entry,
};
let path = entry.path();
let file_name = match path.file_name() {
None => continue,
Some(name) => name.to_str().unwrap(),
};
// ignore excluded content
match &self.config.ignored_content_globset {
Some(gs) => {
if gs.is_match(path) {
continue;
}
}
None => (),
}
// we process a section when we encounter the dir
// so we can process it before any of the pages
// therefore we should skip the actual file to avoid duplication
if file_name.starts_with("_index.") {
continue;
}
// skip hidden files and non md files
if !path.is_dir() && (!file_name.ends_with(".md") || file_name.starts_with('.')) {
continue;
}
// is it a section or not?
if path.is_dir() {
// if we are processing a section we have to collect
// index files for all languages and process them simultaneously
// before any of the pages
let index_files = WalkDir::new(path)
.follow_links(true)
.max_depth(1)
.into_iter()
.filter_map(|e| match e {
Err(_) => None,
Ok(f) => {
let path_str = f.path().file_name().unwrap().to_str().unwrap();
// https://github.com/getzola/zola/issues/1244
if f.path().is_file()
&& allowed_index_filenames.iter().any(|s| s == path_str)
{
Some(f)
} else {
None
}
}
})
.collect::<Vec<DirEntry>>();
for index_file in index_files {
let section =
Section::from_file(index_file.path(), &self.config, &self.base_path)?;
sections.insert(section.components.join("/"));
// if the section is drafted we can skip the entire dir
if section.meta.draft && !self.include_drafts {
dir_walker.skip_current_dir();
continue;
}
self.add_section(section, false)?;
}
} else {
let page = Page::from_file(path, &self.config, &self.base_path)?;
pages.push(page);
}
}
self.create_default_index_sections()?;
for page in pages {
// should we skip drafts?
if page.meta.draft && !self.include_drafts {
continue;
}
// We are only checking it on load and not in add_page since we have access to
// all the components there.
if page.file.filename == "index.md" {
let is_invalid = match page.components.last() {
Some(_) => sections.contains(&page.components.join("/")),
// content/index.md is always invalid, but content/colocated/index.md is ok
None => page.file.colocated_path.is_none(),
};
if is_invalid {
bail!("We can't have a page called `index.md` in the same folder as an index section in {:?}", page.file.parent);
}
}
pages_insert_anchors.insert(
page.file.path.clone(),
self.find_parent_section_insert_anchor(&page.file.parent.clone(), &page.lang),
);
self.add_page(page, false)?;
}
{
let library = self.library.read().unwrap();
let collisions = library.find_path_collisions();
if !collisions.is_empty() {
let mut msg = String::from("Found path collisions:\n");
for (path, filepaths) in collisions {
let row = format!("- `{}` from files {:?}\n", path, filepaths);
msg.push_str(&row);
}
return Err(anyhow!(msg));
}
}
// taxonomy Tera fns are loaded in `register_early_global_fns`
// so we do need to populate it first.
self.populate_taxonomies()?;
tpls::register_early_global_fns(self)?;
self.populate_sections();
self.render_markdown()?;
{
let mut lib = self.library.write().unwrap();
lib.fill_backlinks();
}
tpls::register_tera_global_fns(self);
// Needs to be done after rendering markdown as we only get the anchors at that point
let internal_link_messages = link_checking::check_internal_links_with_anchors(self);
// log any broken internal links and error out if needed
if !internal_link_messages.is_empty() {
let messages: Vec<String> = internal_link_messages
.iter()
.enumerate()
.map(|(i, msg)| format!(" {}. {}", i + 1, msg))
.collect();
let msg = format!(
"Found {} broken internal anchor link(s)\n{}",
messages.len(),
messages.join("\n")
);
match self.config.link_checker.internal_level {
config::LinkCheckerLevel::Warn => console::warn(&msg),
config::LinkCheckerLevel::Error => return Err(anyhow!(msg)),
}
}
// check external links, log the results, and error out if needed
if self.config.is_in_check_mode() && self.check_external_links {
let external_link_messages = link_checking::check_external_links(self);
if !external_link_messages.is_empty() {
let messages: Vec<String> = external_link_messages
.iter()
.enumerate()
.map(|(i, msg)| format!(" {}. {}", i + 1, msg))
.collect();
let msg = format!(
"Found {} broken external link(s)\n{}",
messages.len(),
messages.join("\n")
);
match self.config.link_checker.external_level {
config::LinkCheckerLevel::Warn => console::warn(&msg),
config::LinkCheckerLevel::Error => return Err(anyhow!(msg)),
}
}
}
Ok(())
}
/// Insert a default index section for each language if necessary so we don't need to create
/// a _index.md to render the index page at the root of the site
pub fn create_default_index_sections(&mut self) -> Result<()> {
for (index_path, lang) in self.index_section_paths() {
if let Some(index_section) = self.library.read().unwrap().sections.get(&index_path) {
if self.config.build_search_index && !index_section.meta.in_search_index {
bail!(
"You have enabled search in the config but disabled it in the index section: \
either turn off the search in the config or remove `in_search_index = true` from the \
section front-matter."
)
}
}
let mut library = self.library.write().expect("Get lock for load");
// Not in else because of borrow checker
if !library.sections.contains_key(&index_path) {
let mut index_section = Section::default();
index_section.file.parent = self.content_path.clone();
index_section.file.filename =
index_path.file_name().unwrap().to_string_lossy().to_string();
if let Some(ref l) = lang {
index_section.file.name = format!("_index.{}", l);
index_section.path = format!("{}/", l);
index_section.permalink = self.config.make_permalink(l);
let filename = format!("_index.{}.md", l);
index_section.file.path = self.content_path.join(&filename);
index_section.file.relative = filename;
index_section.file.canonical = self.content_path.join(format!("_index.{}", l));
} else {
index_section.file.name = "_index".to_string();
index_section.permalink = self.config.make_permalink("");
index_section.file.path = self.content_path.join("_index.md");
index_section.file.relative = "_index.md".to_string();
index_section.file.canonical = self.content_path.join("_index");
index_section.path = "/".to_string();
}
index_section.lang = index_section.file.find_language(
&self.config.default_language,
&self.config.other_languages_codes(),
)?;
library.insert_section(index_section);
}
}
Ok(())
}
/// Render the markdown of all pages/sections
/// Used in a build and in `serve` if a shortcode has changed
pub fn render_markdown(&mut self) -> Result<()> {
// Another silly thing needed to not borrow &self in parallel and
// make the borrow checker happy
let permalinks = &self.permalinks;
let tera = &self.tera;
let config = &self.config;
// This is needed in the first place because of silly borrow checker
let mut pages_insert_anchors = HashMap::new();
for (_, p) in &self.library.read().unwrap().pages {
pages_insert_anchors.insert(
p.file.path.clone(),
self.find_parent_section_insert_anchor(&p.file.parent.clone(), &p.lang),
);
}
let mut library = self.library.write().expect("Get lock for render_markdown");
library
.pages
.values_mut()
.collect::<Vec<_>>()
.par_iter_mut()
.map(|page| {
let insert_anchor = pages_insert_anchors[&page.file.path];
page.render_markdown(
permalinks,
tera,
config,
insert_anchor,
&self.shortcode_definitions,
)
})
.collect::<Result<()>>()?;
library
.sections
.values_mut()
.collect::<Vec<_>>()
.par_iter_mut()
.map(|section| {
section.render_markdown(permalinks, tera, config, &self.shortcode_definitions)
})
.collect::<Result<()>>()?;
Ok(())
}
/// Add a page to the site
/// The `render` parameter is used in the serve command with --fast, when rebuilding a page.
pub fn add_page(&mut self, mut page: Page, render_md: bool) -> Result<()> {
for taxa_name in page.meta.taxonomies.keys() {
if !self.config.has_taxonomy(taxa_name, &page.lang) {
bail!(
"Page `{}` has taxonomy `{}` which is not defined in config.toml",
page.file.path.display(),
taxa_name
);
}
}
self.permalinks.insert(page.file.relative.clone(), page.permalink.clone());
if render_md {
let insert_anchor =
self.find_parent_section_insert_anchor(&page.file.parent, &page.lang);
page.render_markdown(
&self.permalinks,
&self.tera,
&self.config,
insert_anchor,
&self.shortcode_definitions,
)?;
}
let mut library = self.library.write().expect("Get lock for add_page");
library.pages.remove(&page.file.path);
library.insert_page(page);
Ok(())
}
/// Adds a page to the site and render it
/// Only used in `zola serve --fast`
pub fn add_and_render_page(&mut self, path: &Path) -> Result<()> {
let page = Page::from_file(path, &self.config, &self.base_path)?;
self.add_page(page, true)?;
self.populate_sections();
self.populate_taxonomies()?;
let library = self.library.read().unwrap();
let page = library.pages.get(path).unwrap();
self.render_page(page)
}
/// Add a section to the site
/// The `render` parameter is used in the serve command with --fast, when rebuilding a page.
pub fn add_section(&mut self, mut section: Section, render_md: bool) -> Result<()> {
self.permalinks.insert(section.file.relative.clone(), section.permalink.clone());
if render_md {
section.render_markdown(
&self.permalinks,
&self.tera,
&self.config,
&self.shortcode_definitions,
)?;
}
let mut library = self.library.write().expect("Get lock for add_section");
library.sections.remove(§ion.file.path);
library.insert_section(section);
Ok(())
}
/// Adds a section to the site and render it
/// Only used in `zola serve --fast`
pub fn add_and_render_section(&mut self, path: &Path) -> Result<()> {
let section = Section::from_file(path, &self.config, &self.base_path)?;
self.add_section(section, true)?;
self.populate_sections();
let library = self.library.read().unwrap();
let section = library.sections.get(path).unwrap();
self.render_section(section, true)
}
/// Finds the insert_anchor for the parent section of the directory at `path`.
/// Defaults to the global setting if no parent section found
pub fn find_parent_section_insert_anchor(
&self,
parent_path: &Path,
lang: &str,
) -> InsertAnchor {
let parent = if lang != self.config.default_language {
parent_path.join(format!("_index.{}.md", lang))
} else {
parent_path.join("_index.md")
};
self.library
.read()
.unwrap()
.sections
.get(&parent)
.and_then(|s| s.meta.insert_anchor_links)
.unwrap_or(self.config.markdown.insert_anchor_links)
}
/// Find out the direct subsections of each subsection if there are some
/// as well as the pages for each section
pub fn populate_sections(&mut self) {
let mut library = self.library.write().expect("Get lock for populate_sections");
library.populate_sections(&self.config, &self.content_path);
}
/// Find all the tags and categories if it's asked in the config
pub fn populate_taxonomies(&mut self) -> Result<()> {
self.taxonomies = self.library.read().unwrap().find_taxonomies(&self.config);
Ok(())
}
/// Inject live reload script tag if in live reload mode
fn inject_livereload(&self, mut html: String) -> String {
if let Some(port) = self.live_reload {
let script =
format!(r#"<script src="/livereload.js?port={}&mindelay=10"></script>"#, port,);
if let Some(index) = html.rfind("</body>") {
html.insert_str(index, &script);
} else {
html.push_str(&script);
}
}
html
}
/// Copy the main `static` folder and the theme `static` folder if a theme is used
pub fn copy_static_directories(&self) -> Result<()> {
// The user files will overwrite the theme files
if let Some(ref theme) = self.config.theme {
copy_directory(
&self.base_path.join("themes").join(theme).join("static"),
&self.output_path,
false,
None,
)?;
}
// We're fine with missing static folders
if self.static_path.exists() {
if let Some(gs) = &self.config.ignored_static_globset {
copy_directory(
&self.static_path,
&self.output_path,
self.config.hard_link_static,
Some(gs),
)?;
} else {
copy_directory(
&self.static_path,
&self.output_path,
self.config.hard_link_static,
None,
)?;
}
}
Ok(())
}
pub fn num_img_ops(&self) -> usize {
let imageproc = self.imageproc.lock().expect("Couldn't lock imageproc (num_img_ops)");
imageproc.num_img_ops()
}
pub fn process_images(&self) -> Result<()> {
let mut imageproc =
self.imageproc.lock().expect("Couldn't lock imageproc (process_images)");
imageproc.prune()?;
imageproc.do_process()
}
/// Deletes the `public` directory if it exists and the `preserve_dotfiles_in_output` option is set to false,
/// or if set to true: its contents except for the dotfiles at the root level.
pub fn clean(&self) -> Result<()> {
clean_site_output_folder(&self.output_path, self.config.preserve_dotfiles_in_output)
}
/// Handles whether to write to disk or to memory
pub fn write_content(
&self,
components: &[&str],
filename: &str,
content: String,
) -> Result<PathBuf> {
let mut site_path = RelativePathBuf::new();
let mut current_path = self.output_path.to_path_buf();
for component in components {
current_path.push(component);
site_path.push(component);
}
let final_content = if !filename.ends_with("html") || !self.config.minify_html {
content
} else {
match minify::html(content) {
Ok(minified_content) => minified_content,
Err(error) => bail!(error),
}
};
match self.build_mode {
BuildMode::Disk | BuildMode::Both => {
let end_path = current_path.join(filename);
create_file(&end_path, &final_content)?;
}
_ => (),
}
match self.build_mode {
BuildMode::Memory | BuildMode::Both => {
let site_path =
if filename != "index.html" { site_path.join(filename) } else { site_path };
SITE_CONTENT.write().unwrap().insert(site_path, final_content);
}
_ => (),
}
Ok(current_path)
}
fn copy_assets(&self, parent: &Path, assets: &[impl AsRef<Path>], dest: &Path) -> Result<()> {
for asset in assets {
let asset_path = asset.as_ref();
copy_file_if_needed(
asset_path,
&dest.join(
asset_path.strip_prefix(parent).expect("Couldn't get filename from page asset"),
),
self.config.hard_link_static,
)?;
}
Ok(())
}
/// Renders a single content page
pub fn render_page(&self, page: &Page) -> Result<()> {
if !page.meta.render {
return Ok(());
}
let output = page.render_html(&self.tera, &self.config, &self.library.read().unwrap())?;
let content = self.inject_livereload(output);
let components: Vec<&str> = page.path.split('/').collect();
let current_path = self.write_content(&components, "index.html", content)?;
// Copy any asset we found previously into the same directory as the index.html
self.copy_assets(page.file.path.parent().unwrap(), &page.assets, ¤t_path)?;
Ok(())
}
/// Deletes the `public` directory (only for `zola build`) and builds the site
pub fn build(&self) -> Result<()> {
let mut start = Instant::now();
// Do not clean on `zola serve` otherwise we end up copying assets all the time
if self.build_mode == BuildMode::Disk {
self.clean()?;
}
start = log_time(start, "Cleaned folder");
// Generate/move all assets before markdown any content
if let Some(ref theme) = self.config.theme {
let theme_path = self.base_path.join("themes").join(theme);
if theme_path.join("sass").exists() {
sass::compile_sass(&theme_path, &self.output_path)?;
start = log_time(start, "Compiled theme Sass");
}
}
if self.config.compile_sass {
sass::compile_sass(&self.base_path, &self.output_path)?;
start = log_time(start, "Compiled own Sass");
}
if self.config.build_search_index {
self.build_search_index()?;
start = log_time(start, "Built search index");
}
// Render aliases first to allow overwriting
self.render_aliases()?;
start = log_time(start, "Rendered aliases");
self.render_sections()?;
start = log_time(start, "Rendered sections");
self.render_orphan_pages()?;
start = log_time(start, "Rendered orphan pages");
if self.config.generate_sitemap {
self.render_sitemap()?;
start = log_time(start, "Rendered sitemap");
}
let library = self.library.read().unwrap();
if self.config.generate_feeds {
let is_multilingual = self.config.is_multilingual();
let pages: Vec<_> = if is_multilingual {
library.pages.values().filter(|p| p.lang == self.config.default_language).collect()
} else {
library.pages.values().collect()
};
self.render_feeds(pages, None, &self.config.default_language, |c| c)?;
start = log_time(start, "Generated feed in default language");
}
for (code, language) in &self.config.other_languages() {
if !language.generate_feeds {
continue;
}
let pages: Vec<_> = library.pages.values().filter(|p| &p.lang == code).collect();
self.render_feeds(pages, Some(&PathBuf::from(code)), code, |c| c)?;
start = log_time(start, "Generated feed in other language");
}
self.render_themes_css()?;
start = log_time(start, "Rendered themes css");
self.render_404()?;
start = log_time(start, "Rendered 404");
if self.config.generate_robots_txt {
self.render_robots()?;
start = log_time(start, "Rendered robots.txt");
}
self.render_taxonomies()?;
start = log_time(start, "Rendered taxonomies");
// We process images at the end as we might have picked up images to process from markdown
// or from templates
self.process_images()?;
start = log_time(start, "Processed images");
// Processed images will be in static so the last step is to copy it
self.copy_static_directories()?;
log_time(start, "Copied static dir");
Ok(())
}
pub fn render_themes_css(&self) -> Result<()> {
let themes = &self.config.markdown.highlight_themes_css;
if !themes.is_empty() {
create_directory(&self.static_path)?;
}
for t in themes {
let p = self.static_path.join(&t.filename);
if !p.exists() {
let content = &self.config.markdown.export_theme_css(&t.theme)?;
create_file(&p, content)?;
}
}
Ok(())
}
fn index_for_lang(&self, lang: &str) -> Result<()> {
let path = &self.output_path.join(self.config.search.index_format.filename(lang));
let library = self.library.read().unwrap();
let content = match &self.config.search.index_format {
IndexFormat::ElasticlunrJavascript | IndexFormat::ElasticlunrJson => {
search::build_elasticlunr(lang, &library, &self.config)?
}
IndexFormat::FuseJson | IndexFormat::FuseJavascript => {
search::build_fuse(lang, &library, &self.config.search)?
}
};
drop(library); // no need to hold on to this guard while writing
create_file(
path,
match self.config.search.index_format {
IndexFormat::ElasticlunrJson | IndexFormat::FuseJson => content,
IndexFormat::ElasticlunrJavascript | IndexFormat::FuseJavascript => {
format!("window.searchIndex = {}", content)
}
},
)
}
pub fn build_search_index(&self) -> Result<()> {
create_directory(&self.output_path)?;
// TODO: add those to the SITE_CONTENT map
// index first
self.index_for_lang(&self.config.default_language)?;
for (code, language) in &self.config.other_languages() {
if code != &self.config.default_language && language.build_search_index {
self.index_for_lang(code)?;
}
}
match self.config.search.index_format {
IndexFormat::ElasticlunrJavascript | IndexFormat::ElasticlunrJson => {
// then elasticlunr.min.js
create_file(&self.output_path.join("elasticlunr.min.js"), search::ELASTICLUNR_JS)?;
}
_ => {}
}
Ok(())
}
fn render_alias(&self, alias: &str, permalink: &str) -> Result<()> {
let mut split = alias.split('/').collect::<Vec<_>>();
// If the alias ends with an html file name, use that instead of mapping
// as a path containing an `index.html`
let page_name = match split.pop() {
Some(part) if part.ends_with(".html") => part,
Some(part) => {
split.push(part);
"index.html"
}
None => "index.html",
};
let content = render_redirect_template(permalink, &self.tera)?;
self.write_content(&split, page_name, content)?;
Ok(())
}
/// Renders all the aliases for each page/section: a magic HTML template that redirects to
/// the canonical one
pub fn render_aliases(&self) -> Result<()> {
let library = self.library.read().unwrap();
for (_, page) in &library.pages {
for alias in &page.meta.aliases {
self.render_alias(alias, &page.permalink)?;
}
}
for (_, section) in &library.sections {
for alias in §ion.meta.aliases {
self.render_alias(alias, §ion.permalink)?;
}
}
Ok(())
}
/// Renders 404.html
pub fn render_404(&self) -> Result<()> {
let mut context = Context::new();
context.insert("config", &self.config.serialize(&self.config.default_language));
context.insert("lang", &self.config.default_language);
let output = render_template("404.html", &self.tera, context, &self.config.theme)?;
let content = self.inject_livereload(output);
self.write_content(&[], "404.html", content)?;
Ok(())
}
/// Renders robots.txt
pub fn render_robots(&self) -> Result<()> {
let mut context = Context::new();
context.insert("config", &self.config.serialize(&self.config.default_language));
let content = render_template("robots.txt", &self.tera, context, &self.config.theme)?;
self.write_content(&[], "robots.txt", content)?;
Ok(())
}
/// Renders all taxonomies
pub fn render_taxonomies(&self) -> Result<()> {
for taxonomy in &self.taxonomies {
if !taxonomy.kind.render {
continue;
}
self.render_taxonomy(taxonomy)?;
}
Ok(())
}
fn render_taxonomy(&self, taxonomy: &Taxonomy) -> Result<()> {
if taxonomy.items.is_empty() {
return Ok(());
}
let mut components = Vec::new();
if taxonomy.lang != self.config.default_language {
components.push(taxonomy.lang.as_ref());
}
components.push(taxonomy.slug.as_ref());
let list_output =
taxonomy.render_all_terms(&self.tera, &self.config, &self.library.read().unwrap())?;
let content = self.inject_livereload(list_output);
self.write_content(&components, "index.html", content)?;
let library = self.library.read().unwrap();
taxonomy
.items
.par_iter()
.map(|item| {
let mut comp = components.clone();
comp.push(&item.slug);
if taxonomy.kind.is_paginated() {
self.render_paginated(
comp.clone(),
&Paginator::from_taxonomy(
taxonomy,
item,
&library,
&self.tera,
&self.config.theme,
),
)?;
} else {
let single_output =
taxonomy.render_term(item, &self.tera, &self.config, &library)?;
let content = self.inject_livereload(single_output);
self.write_content(&comp, "index.html", content)?;
}
if taxonomy.kind.feed {
let tax_path = if taxonomy.lang == self.config.default_language {
PathBuf::from(format!("{}/{}", taxonomy.slug, item.slug))
} else {
PathBuf::from(format!("{}/{}/{}", taxonomy.lang, taxonomy.slug, item.slug))
};
self.render_feeds(
item.pages.iter().map(|p| library.pages.get(p).unwrap()).collect(),
Some(&tax_path),
&taxonomy.lang,
|mut context: Context| {
context.insert("taxonomy", &taxonomy.kind);
context.insert(
"term",