rsconstruct 0.9.197

Rust based fast build system
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
diff --git a/components/imageproc/src/processor.rs b/components/imageproc/src/processor.rs
index 2241473..ce1e2de 100644
--- a/components/imageproc/src/processor.rs
+++ b/components/imageproc/src/processor.rs
@@ -214,6 +214,12 @@ impl Processor {
         self.img_ops.len()
     }
 
+    /// Every enqueued operation as `(input image, output file)`, so a build
+    /// plan can list the processed images without performing them.
+    pub fn planned_ops(&self) -> Vec<(PathBuf, PathBuf)> {
+        self.img_ops.iter().map(|op| (op.input_path.clone(), op.output_path.clone())).collect()
+    }
+
     #[allow(clippy::too_many_arguments)]
     pub fn enqueue(
         &mut self,
diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs
index e8d9b99..4705542 100644
--- a/components/site/src/lib.rs
+++ b/components/site/src/lib.rs
@@ -2,6 +2,7 @@ pub mod feeds;
 pub mod link_checking;
 mod md_render;
 mod minify;
+pub mod plan;
 mod queue;
 pub mod sass;
 pub mod sitemap;
@@ -49,6 +50,8 @@ pub enum BuildMode {
 pub struct Site {
     /// The base path of the zola site
     pub base_path: PathBuf,
+    /// The config file the site was loaded from
+    pub config_path: PathBuf,
     /// The parsed config for the site
     pub config: Config,
     pub tera: Tera,
@@ -99,6 +102,7 @@ impl Site {
 
         let site = Site {
             base_path: path.to_path_buf(),
+            config_path: path.join(config_file),
             config,
             tera,
             imageproc: Arc::new(Mutex::new(imageproc)),
@@ -682,6 +686,12 @@ impl Site {
         imageproc.num_img_ops()
     }
 
+    /// The image operations enqueued so far as `(input, output)` pairs.
+    pub fn planned_image_ops(&self) -> Vec<(PathBuf, PathBuf)> {
+        let imageproc = self.imageproc.lock().expect("Couldn't lock imageproc (planned_image_ops)");
+        imageproc.planned_ops()
+    }
+
     pub fn process_images(&self) -> Result<()> {
         let mut imageproc =
             self.imageproc.lock().expect("Couldn't lock imageproc (process_images)");
diff --git a/components/site/src/plan.rs b/components/site/src/plan.rs
new file mode 100644
index 0000000..a604b32
--- /dev/null
+++ b/components/site/src/plan.rs
@@ -0,0 +1,241 @@
+//! `zola plan`: everything `zola build` would write, without writing it.
+//!
+//! A build tool that wraps zola can turn each planned file into a product of
+//! its own — with its own inputs and its own cache entry — instead of
+//! treating the output directory as one opaque blob. For that it needs two
+//! things from zola up front: the full list of output files, and for each
+//! one the input files whose content determines it. This module answers both
+//! from the loaded `Site`, by the same code paths the build uses to decide
+//! where things go (`Queue`, `sass`, the search index, the static copy), so
+//! the plan and the build cannot drift apart.
+
+use std::collections::{BTreeMap, BTreeSet};
+use std::path::{Path, PathBuf};
+
+use walkdir::WalkDir;
+
+use crate::Site;
+use crate::queue::Queue;
+use config::IndexFormat;
+use errors::Result;
+
+/// One file the build will produce.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct PlannedOutput {
+    /// Relative to the output directory.
+    pub path: PathBuf,
+    /// Absolute paths of the files whose content can change this output.
+    pub sources: Vec<PathBuf>,
+}
+
+impl PlannedOutput {
+    pub fn new(path: PathBuf, sources: Vec<PathBuf>) -> Self {
+        let path = path.strip_prefix("/").map(Path::to_path_buf).unwrap_or(path);
+        Self { path, sources }
+    }
+}
+
+/// Regular files under `dir`, following symlinks as the build's walkers do.
+/// A missing directory is simply empty.
+fn files_under(dir: &Path) -> Vec<PathBuf> {
+    WalkDir::new(dir)
+        .follow_links(true)
+        .into_iter()
+        .filter_map(|e| e.ok())
+        .filter(|e| e.path().is_file())
+        .map(|e| e.into_path())
+        .collect()
+}
+
+impl Site {
+    /// Inputs every rendered output depends on: the config file and every
+    /// template of the site and its theme. Listing the whole template tree
+    /// rather than the exact `extends`/`include` chain is deliberate: it is
+    /// always correct, and zola rebuilds the whole site on any change anyway,
+    /// so precision here would change what a wrapper reports as dirty, not
+    /// how often zola runs.
+    fn common_sources(&self) -> Vec<PathBuf> {
+        let mut sources = vec![self.config_path.clone()];
+        sources.extend(files_under(&self.templates_path));
+        if let Some(ref theme) = self.config.theme {
+            sources.extend(files_under(
+                &self.base_path.join("themes").join(theme).join("templates"),
+            ));
+        }
+        sources
+    }
+
+    /// Static files as `copy_static_directories` lays them out: theme files
+    /// first, then the site's own, which overwrite them. Processed images are
+    /// skipped — they are enumerated from the image queue, not from whatever
+    /// a previous build left in `static/processed_images/`.
+    fn plan_static(&self, outputs: &mut Vec<PlannedOutput>) {
+        let processed = self.static_path.join(imageproc::RESIZED_SUBDIR);
+        let mut roots = Vec::new();
+        if let Some(ref theme) = self.config.theme {
+            roots.push((self.base_path.join("themes").join(theme).join("static"), None));
+        }
+        roots.push((self.static_path.clone(), self.config.ignored_static_globset.as_ref()));
+        for (root, ignore) in roots {
+            for file in files_under(&root) {
+                if file.starts_with(&processed) {
+                    continue;
+                }
+                let relative = file.strip_prefix(&root).unwrap().to_path_buf();
+                if let Some(gs) = ignore
+                    && gs.is_match(&relative)
+                {
+                    continue;
+                }
+                outputs.push(PlannedOutput::new(relative, vec![file]));
+            }
+        }
+    }
+
+    /// Every non-partial `.sass`/`.scss` compiles to a `.css` at the same
+    /// relative path (see `sass::compile_sass`). Partials are reachable from
+    /// any of them, so each stylesheet depends on the whole `sass/` tree.
+    fn plan_sass(&self, outputs: &mut Vec<PlannedOutput>) {
+        let mut roots = Vec::new();
+        if let Some(ref theme) = self.config.theme {
+            let theme_path = self.base_path.join("themes").join(theme);
+            if theme_path.join("sass").exists() {
+                roots.push(theme_path);
+            }
+        }
+        if self.config.compile_sass {
+            roots.push(self.base_path.clone());
+        }
+        for root in roots {
+            let sass_path = root.join("sass");
+            let all = files_under(&sass_path);
+            for file in crate::sass::get_non_partial_scss(&sass_path) {
+                let path = file.strip_prefix(&sass_path).unwrap().with_extension("css");
+                outputs.push(PlannedOutput::new(path, all.clone()));
+            }
+        }
+    }
+
+    /// Search indexes as `build_search_index` writes them.
+    fn plan_search_index(&self, outputs: &mut Vec<PlannedOutput>) {
+        if !self.config.needs_search_index() {
+            return;
+        }
+        let mut langs = Vec::new();
+        if self.config.build_search_index {
+            langs.push(self.config.default_language.clone());
+        }
+        for (code, language) in &self.config.other_languages() {
+            if *code != self.config.default_language && language.build_search_index {
+                langs.push((*code).to_string());
+            }
+        }
+        for lang in langs {
+            let sources: Vec<PathBuf> = self
+                .library
+                .pages
+                .values()
+                .filter(|p| p.lang == lang)
+                .map(|p| p.file.path.clone())
+                .collect();
+            let path = PathBuf::from(self.config.search.index_format.filename(&lang));
+            outputs.push(PlannedOutput::new(path, sources));
+        }
+        match self.config.search.index_format {
+            IndexFormat::ElasticlunrJavascript | IndexFormat::ElasticlunrJson => {
+                outputs.push(PlannedOutput::new(PathBuf::from("elasticlunr.min.js"), Vec::new()));
+            }
+            _ => {}
+        }
+    }
+
+    /// Syntax-highlighting theme stylesheets (`render_themes_css`).
+    fn plan_themes_css(&self, outputs: &mut Vec<PlannedOutput>) {
+        let themes = self
+            .config
+            .markdown
+            .highlighting
+            .as_ref()
+            .map(|x| x.generate_themes_css())
+            .unwrap_or_default();
+        for (filename, _) in themes {
+            outputs.push(PlannedOutput::new(PathBuf::from(filename), Vec::new()));
+        }
+    }
+
+    /// Images enqueued while rendering markdown, which the build writes to
+    /// `static/processed_images/` and then copies out with the static tree.
+    /// An image resized from a template is enqueued only while that template
+    /// renders, so it cannot appear here; a wrapper's plan-vs-build check
+    /// will report it, which is the honest answer until templates are
+    /// rendered at plan time.
+    fn plan_images(&self, outputs: &mut Vec<PlannedOutput>) {
+        for (input, output) in self.planned_image_ops() {
+            let relative = output.strip_prefix(&self.static_path).unwrap().to_path_buf();
+            outputs.push(PlannedOutput::new(relative, vec![input]));
+        }
+    }
+
+    /// The complete build plan, sorted by output path, each output listing
+    /// every input that can change it. The site must be loaded.
+    pub fn plan(&self) -> Result<Vec<PlannedOutput>> {
+        let common = self.common_sources();
+        let mut outputs = Vec::new();
+
+        self.plan_sass(&mut outputs);
+        self.plan_search_index(&mut outputs);
+        self.plan_themes_css(&mut outputs);
+        self.plan_images(&mut outputs);
+        self.plan_static(&mut outputs);
+
+        let queue = Queue::full_build(self);
+        let rendered_from = outputs.len();
+        outputs.extend(queue.plan());
+        // Everything the queue renders goes through the templates and reads
+        // the config; copied assets do not, but a few extra inputs on a
+        // copied file are harmless where a missing one would be a stale page.
+        for output in &mut outputs[rendered_from..] {
+            output.sources.extend(common.iter().cloned());
+        }
+        // Aliases: the queue only knows the redirect's URL; the content file
+        // that declares it is what the stub depends on.
+        let mut alias_owner: BTreeMap<PathBuf, PathBuf> = BTreeMap::new();
+        for page in self.library.pages.values() {
+            for alias in &page.meta.aliases {
+                alias_owner.insert(alias_path(alias), page.file.path.clone());
+            }
+        }
+        for section in self.library.sections.values() {
+            for alias in &section.meta.aliases {
+                alias_owner.insert(alias_path(alias), section.file.path.clone());
+            }
+        }
+        for output in &mut outputs {
+            if let Some(owner) = alias_owner.get(&output.path) {
+                output.sources.push(owner.clone());
+            }
+        }
+
+        // Two jobs can name the same file (an alias pointing at an existing
+        // page, a static file shadowing a theme's); the build writes it twice
+        // and the last write wins. The plan lists it once with the union of
+        // its inputs, which is what a change to either would do to it.
+        let mut merged: BTreeMap<PathBuf, BTreeSet<PathBuf>> = BTreeMap::new();
+        for output in outputs {
+            merged.entry(output.path).or_default().extend(output.sources);
+        }
+        Ok(merged
+            .into_iter()
+            .map(|(path, sources)| PlannedOutput { path, sources: sources.into_iter().collect() })
+            .collect())
+    }
+}
+
+/// Where `Queue::render_alias` writes the redirect stub for `from`.
+fn alias_path(from: &str) -> PathBuf {
+    let mut path = PathBuf::from(from);
+    if !from.ends_with(".html") {
+        path.push("index.html");
+    }
+    PlannedOutput::new(path, Vec::new()).path
+}
diff --git a/components/site/src/queue.rs b/components/site/src/queue.rs
index 51d1bf8..eac4c55 100644
--- a/components/site/src/queue.rs
+++ b/components/site/src/queue.rs
@@ -1,6 +1,7 @@
 use std::borrow::Cow;
 use std::path::{Path, PathBuf};
 
+use crate::plan::PlannedOutput;
 use crate::{BuildMode, SITE_CONTENT, Site, feeds, minify, sitemap};
 use content::{Page, Section, Taxonomy, TaxonomyTerm};
 use errors::Result;
@@ -65,12 +66,42 @@ struct RenderedOutput {
     kind: OutputKind,
 }
 
+/// A section's listing depends on its own file and on every page it lists.
+fn section_sources(section: &Section, pages: &[&Page]) -> Vec<PathBuf> {
+    let mut sources = vec![section.file.path.clone()];
+    sources.extend(pages.iter().map(|p| p.file.path.clone()));
+    sources
+}
+
+/// A taxonomy term's pages are the only content that can change its listing.
+/// Term paths are keys into the library, i.e. the pages' source files.
+fn term_sources(term: &TaxonomyTerm) -> Vec<PathBuf> {
+    term.pages.clone()
+}
+
+/// The co-located assets copied next to a rendered page or section, as
+/// `Site::copy_assets` lays them out: `dest` + the asset's path below the
+/// content file's directory.
+fn planned_assets(parent: &Path, assets: &[PathBuf], dest: &Path) -> Vec<PlannedOutput> {
+    assets
+        .iter()
+        .map(|asset| {
+            let relative = asset.strip_prefix(parent).expect("Couldn't get filename from page asset");
+            PlannedOutput::new(dest.join(relative), vec![asset.clone()])
+        })
+        .collect()
+}
+
 #[derive(Debug, Clone)]
 pub struct Queue<'a> {
     site: &'a Site,
     jobs: Vec<Job<'a>>,
     /// Keeping them on the queue so we can use the index to avoid cloning them
     paginators: Vec<Paginator<'a>>,
+    /// Parallel to `paginators`: the content files each paginator is built
+    /// from, for `zola plan`. Recorded here because a `Paginator` does not
+    /// expose its root.
+    paginator_sources: Vec<Vec<PathBuf>>,
 }
 
 impl<'a> Queue<'a> {
@@ -91,6 +122,8 @@ impl<'a> Queue<'a> {
             }
         }
 
+        // Kept for the paginator below: the feed job takes `pages` by value.
+        let listing_sources = section_sources(section, &pages);
         if section.meta.generate_feeds {
             self.jobs.push(Job::Feed(Feed::Section { pages, section }));
         }
@@ -122,6 +155,7 @@ impl<'a> Queue<'a> {
                 });
             }
             self.paginators.push(paginator);
+            self.paginator_sources.push(listing_sources);
         } else {
             // This will handle a normal section, which could also be a redirect
             self.jobs.push(Job::Section { section, path: base_path });
@@ -129,18 +163,22 @@ impl<'a> Queue<'a> {
     }
 
     pub fn single_page(site: &'a Site, page: &'a Page) -> Self {
-        Self { jobs: vec![Job::Page(page)], site, paginators: vec![] }
+        Self { jobs: vec![Job::Page(page)], site, paginators: vec![], paginator_sources: vec![] }
     }
 
     pub fn single_section(site: &'a Site, section: &'a Section, render_pages: bool) -> Self {
-        let mut queue = Self { jobs: vec![], site, paginators: vec![] };
+        let mut queue = Self { jobs: vec![], site, paginators: vec![], paginator_sources: vec![] };
         queue.add_section_jobs(section, render_pages);
         queue
     }
 
     pub fn full_build(site: &'a Site) -> Self {
-        let mut queue =
-            Queue { site, jobs: vec![Job::NotFound], paginators: Vec::with_capacity(100) };
+        let mut queue = Queue {
+            site,
+            jobs: vec![Job::NotFound],
+            paginators: Vec::with_capacity(100),
+            paginator_sources: Vec::with_capacity(100),
+        };
 
         if site.config.generate_sitemap {
             queue.jobs.push(Job::Sitemap);
@@ -203,6 +241,7 @@ impl<'a> Queue<'a> {
                         });
                     }
                     queue.paginators.push(paginator);
+                    queue.paginator_sources.push(term_sources(term));
                 } else {
                     queue.jobs.push(Job::TaxonomyTerm { taxonomy, term, path: term_path.clone() });
                 }
@@ -523,6 +562,139 @@ impl<'a> Queue<'a> {
         Ok(RenderedOutput { path: PathBuf::from("robots.txt"), content, kind: OutputKind::Text })
     }
 
+    /// What every job would write, without rendering anything: the output
+    /// path relative to the output directory and the content files whose
+    /// changes can alter it. Templates and the config file are not listed
+    /// here; they feed every output and the caller adds them once.
+    ///
+    /// Mirrors `execute_job` and `process` job for job, so a new kind of job
+    /// must be added in both places — the match is exhaustive to make that
+    /// a compile error rather than a silent gap in the plan.
+    pub fn plan(&self) -> Vec<PlannedOutput> {
+        let mut outputs = Vec::new();
+        for job in &self.jobs {
+            match job {
+                Job::Alias { from, .. } => {
+                    // The redirect stub depends only on the permalink of its
+                    // target, and permalinks come from the content tree as a
+                    // whole; `Site::plan` attributes aliases to their owner.
+                    let mut path = PathBuf::from(from);
+                    if !from.ends_with(".html") {
+                        path.push("index.html");
+                    }
+                    outputs.push(PlannedOutput::new(path, Vec::new()));
+                }
+                Job::Page(page) => {
+                    let path = PathBuf::from(&page.path).join("index.html");
+                    outputs.push(PlannedOutput::new(path, vec![page.file.path.clone()]));
+                    let dest = PathBuf::from(page.path.strip_prefix('/').unwrap_or(&page.path));
+                    let parent = page.file.path.parent().unwrap();
+                    outputs.extend(planned_assets(parent, &page.assets, &dest));
+                }
+                Job::Section { section, path } => {
+                    let pages: Vec<_> = section
+                        .pages
+                        .iter()
+                        .map(|k| self.site.library.pages.get(k).unwrap())
+                        .collect();
+                    outputs.push(PlannedOutput::new(
+                        path.join("index.html"),
+                        section_sources(section, &pages),
+                    ));
+                }
+                Job::SectionAssets { section, path } => {
+                    let parent = section.file.path.parent().unwrap();
+                    outputs.extend(planned_assets(parent, &section.assets, path));
+                }
+                Job::Paginated { paginator_index, pager_index, path } => {
+                    let paginator = &self.paginators[*paginator_index];
+                    let pager = &paginator.pagers[*pager_index];
+                    let sources = self.paginator_sources[*paginator_index].clone();
+                    if pager.index == 1 {
+                        outputs.push(PlannedOutput::new(path.join("index.html"), sources.clone()));
+                        let redirect_path =
+                            path.join(&paginator.paginate_path).join("1").join("index.html");
+                        outputs.push(PlannedOutput::new(redirect_path, sources));
+                    } else {
+                        let output_path = path
+                            .join(&paginator.paginate_path)
+                            .join(pager.index.to_string())
+                            .join("index.html");
+                        outputs.push(PlannedOutput::new(output_path, sources));
+                    }
+                }
+                Job::TaxonomyList { taxonomy, path } => {
+                    let sources = taxonomy.items.iter().flat_map(term_sources).collect();
+                    outputs.push(PlannedOutput::new(path.join("index.html"), sources));
+                }
+                Job::TaxonomyTerm { term, path, .. } => {
+                    outputs.push(PlannedOutput::new(path.join("index.html"), term_sources(term)));
+                }
+                Job::Feed(feed) => outputs.extend(self.plan_feed(feed)),
+                Job::Sitemap => {
+                    // One sitemap below 30k entries, split above; the split
+                    // threshold is far beyond any site that reaches this.
+                    let sources = self.all_content_files();
+                    outputs.push(PlannedOutput::new(PathBuf::from("sitemap.xml"), sources));
+                }
+                Job::NotFound => {
+                    outputs.push(PlannedOutput::new(PathBuf::from("404.html"), Vec::new()));
+                }
+                Job::Robots => {
+                    outputs.push(PlannedOutput::new(PathBuf::from("robots.txt"), Vec::new()));
+                }
+            }
+        }
+        outputs
+    }
+
+    fn plan_feed(&self, feed: &Feed) -> Vec<PlannedOutput> {
+        let mut res = Vec::new();
+        match feed {
+            Feed::Site { lang } => {
+                let sources: Vec<PathBuf> = self
+                    .site
+                    .library
+                    .pages
+                    .values()
+                    .filter(|p| p.lang == *lang)
+                    .map(|p| p.file.path.clone())
+                    .collect();
+                for feed_filename in &self.site.config.languages[*lang].feed_filenames {
+                    let path = if *lang == self.site.config.default_language {
+                        PathBuf::from(feed_filename)
+                    } else {
+                        PathBuf::from(lang).join(feed_filename)
+                    };
+                    res.push(PlannedOutput::new(path, sources.clone()));
+                }
+            }
+            Feed::Section { section, pages } => {
+                let base_path = PathBuf::from(&section.path[1..]);
+                let sources = section_sources(section, pages);
+                for feed_filename in &self.site.config.languages[&section.lang].feed_filenames {
+                    res.push(PlannedOutput::new(base_path.join(feed_filename), sources.clone()));
+                }
+            }
+            Feed::Taxonomy { taxonomy, term, path } => {
+                for feed_filename in &self.site.config.languages[&taxonomy.lang].feed_filenames {
+                    res.push(PlannedOutput::new(path.join(feed_filename), term_sources(term)));
+                }
+            }
+        }
+        res
+    }
+
+    fn all_content_files(&self) -> Vec<PathBuf> {
+        self.site
+            .library
+            .pages
+            .values()
+            .map(|p| p.file.path.clone())
+            .chain(self.site.library.sections.values().map(|s| s.file.path.clone()))
+            .collect()
+    }
+
     fn execute_job(&self, job: &Job<'a>) -> Result<Vec<RenderedOutput>> {
         match job {
             Job::Alias { from, to } => Ok(vec![self.render_alias(from, to)?]),
diff --git a/components/site/src/sass.rs b/components/site/src/sass.rs
index aa015ab..32a6c79 100644
--- a/components/site/src/sass.rs
+++ b/components/site/src/sass.rs
@@ -56,7 +56,7 @@ fn is_partial_scss(entry: &DirEntry) -> bool {
     entry.file_name().to_str().map(|s| s.starts_with('_')).unwrap_or(false)
 }
 
-fn get_non_partial_scss(sass_path: &Path) -> Vec<PathBuf> {
+pub(crate) fn get_non_partial_scss(sass_path: &Path) -> Vec<PathBuf> {
     let glob = Glob::new("*.{sass,scss}").expect("Invalid glob for sass").compile_matcher();
 
     WalkDir::new(sass_path)
diff --git a/src/cli.rs b/src/cli.rs
index 530e635..db92c09 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -112,6 +112,26 @@ pub enum Command {
         debounce: u64,
     },
 
+    /// Print the files `build` would write, as a JSON manifest, without writing them
+    ///
+    /// Each entry names one output file and the input files whose content can
+    /// change it, for build tools that cache and track outputs individually.
+    /// Paths are relative to the current directory, so run it from the project
+    /// root with the same --output-dir you pass to `build`.
+    Plan {
+        /// Force the base URL to be that value (defaults to the one in the config file)
+        #[clap(short = 'u', long)]
+        base_url: Option<String>,
+
+        /// The output directory the planned paths are reported under (by default 'public' dir in project root)
+        #[clap(short = 'o', long)]
+        output_dir: Option<PathBuf>,
+
+        /// Include drafts when loading the site
+        #[clap(long)]
+        drafts: bool,
+    },
+
     /// Try to build the project without rendering it. Checks links
     Check {
         /// Include drafts when loading the site
diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs
index 004b864..0d94e67 100644
--- a/src/cmd/mod.rs
+++ b/src/cmd/mod.rs
@@ -1,9 +1,11 @@
 mod build;
 mod check;
 mod init;
+mod plan;
 mod serve;
 
 pub use self::build::build;
 pub use self::check::check;
 pub use self::init::create_new_project;
+pub use self::plan::plan;
 pub use self::serve::serve;
diff --git a/src/cmd/plan.rs b/src/cmd/plan.rs
new file mode 100644
index 0000000..646c7db
--- /dev/null
+++ b/src/cmd/plan.rs
@@ -0,0 +1,68 @@
+use std::path::{Path, PathBuf};
+
+use errors::{Context, Result, bail};
+use serde_json::json;
+use site::Site;
+
+/// Schema version of the manifest printed by `zola plan`.
+const MANIFEST_VERSION: u32 = 1;
+
+/// Express `path` relative to `cwd`. A relative path is already that (the
+/// user passed `--output-dir _site`); an absolute one is rewritten, and one
+/// outside the current directory is an error because a consumer would have
+/// no way to refer to it.
+fn relative_to(cwd: &Path, path: &Path) -> Result<PathBuf> {
+    if path.is_relative() {
+        return Ok(path.to_path_buf());
+    }
+    match path.strip_prefix(cwd) {
+        Ok(rel) => Ok(rel.to_path_buf()),
+        Err(_) => bail!(
+            "{} is outside the current directory {}; run `zola plan` from the project root",
+            path.display(),
+            cwd.display()
+        ),
+    }
+}
+
+pub fn plan(
+    root_dir: &Path,
+    config_file: &Path,
+    base_url: Option<&str>,
+    output_dir: Option<&Path>,
+    include_drafts: bool,
+) -> Result<()> {
+    let mut site = Site::new(root_dir, config_file)?;
+    if let Some(output_dir) = output_dir {
+        site.set_output_path(output_dir);
+    }
+    if let Some(b) = base_url {
+        site.set_base_url(b.to_string());
+    }
+    if include_drafts {
+        site.include_drafts();
+    }
+    site.load()?;
+
+    let cwd = std::env::current_dir()
+        .and_then(|d| d.canonicalize())
+        .context("Could not determine the current directory")?;
+    let output_root = relative_to(&cwd, &site.output_path)?;
+
+    let mut outputs = Vec::new();
+    for planned in site.plan()? {
+        let mut sources = Vec::with_capacity(planned.sources.len());
+        for source in &planned.sources {
+            sources.push(relative_to(&cwd, source)?.display().to_string());
+        }
+        outputs.push(json!({
+            "path": output_root.join(&planned.path).display().to_string(),
+            "sources": sources,
+        }));
+    }
+
+    let manifest = json!({ "version": MANIFEST_VERSION, "outputs": outputs });
+    let json = serde_json::to_string_pretty(&manifest).context("Could not serialize the plan")?;
+    println!("{json}");
+    Ok(())
+}
diff --git a/src/main.rs b/src/main.rs
index 6fdc390..e032304 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -190,6 +190,15 @@ fn main() {
                 std::process::exit(1);
             }
         }
+        Command::Plan { base_url, output_dir, drafts } => {
+            let (root_dir, config_file) = get_config_file_path(&cli_dir, cli.config.as_deref());
+            if let Err(e) =
+                cmd::plan(&root_dir, &config_file, base_url.as_deref(), output_dir.as_deref(), drafts)
+            {
+                messages::unravel_errors("Failed to plan the site", &e);
+                std::process::exit(1);
+            }
+        }
         Command::Check { drafts, skip_external_links } => {
             log::info!("Checking site...");
             let start = Instant::now();