1
+ pub mod link_checking;
1
2
pub mod sass;
2
3
pub mod sitemap;
3
- pub mod link_checking ;
4
+ pub mod tpls ;
4
5
5
6
use std:: collections:: HashMap ;
6
7
use std:: fs:: { copy, remove_dir_all} ;
@@ -12,18 +13,19 @@ use rayon::prelude::*;
12
13
use serde_derive:: Serialize ;
13
14
use tera:: { Context , Tera } ;
14
15
16
+ use crate :: link_checking:: { check_external_links, check_internal_links_with_anchors} ;
17
+ use crate :: tpls:: { load_tera, register_early_global_fns, register_tera_global_fns} ;
15
18
use config:: { get_config, Config , Taxonomy as TaxonomyConfig } ;
16
19
use errors:: { bail, Error , Result } ;
17
20
use front_matter:: InsertAnchor ;
18
21
use library:: {
19
22
find_taxonomies, sort_actual_pages_by_date, Library , Page , Paginator , Section , Taxonomy ,
20
23
TaxonomyItem ,
21
24
} ;
22
- use templates:: { global_fns , render_redirect_template, ZOLA_TERA } ;
25
+ use templates:: render_redirect_template;
23
26
use utils:: fs:: { copy_directory, create_directory, create_file, ensure_directory_exists} ;
24
27
use utils:: net:: get_available_port;
25
- use utils:: templates:: { render_template, rewrite_theme_paths} ;
26
- use crate :: link_checking:: { check_internal_links_with_anchors, check_external_links} ;
28
+ use utils:: templates:: render_template;
27
29
28
30
#[ derive( Debug ) ]
29
31
pub struct Site {
@@ -74,45 +76,12 @@ impl Site {
74
76
let mut config = get_config ( config_file) ;
75
77
config. load_extra_syntaxes ( path) ?;
76
78
77
- let tpl_glob =
78
- format ! ( "{}/{}" , path. to_string_lossy( ) . replace( "\\ " , "/" ) , "templates/**/*.*ml" ) ;
79
- // Only parsing as we might be extending templates from themes and that would error
80
- // as we haven't loaded them yet
81
- let mut tera =
82
- Tera :: parse ( & tpl_glob) . map_err ( |e| Error :: chain ( "Error parsing templates" , e) ) ?;
83
79
if let Some ( theme) = config. theme . clone ( ) {
84
80
// Grab data from the extra section of the theme
85
81
config. merge_with_theme ( & path. join ( "themes" ) . join ( & theme) . join ( "theme.toml" ) ) ?;
86
-
87
- // Test that the templates folder exist for that theme
88
- let theme_path = path. join ( "themes" ) . join ( & theme) ;
89
- if !theme_path. join ( "templates" ) . exists ( ) {
90
- bail ! ( "Theme `{}` is missing a templates folder" , theme) ;
91
- }
92
-
93
- let theme_tpl_glob = format ! (
94
- "{}/{}" ,
95
- path. to_string_lossy( ) . replace( "\\ " , "/" ) ,
96
- format!( "themes/{}/templates/**/*.*ml" , theme)
97
- ) ;
98
- let mut tera_theme = Tera :: parse ( & theme_tpl_glob)
99
- . map_err ( |e| Error :: chain ( "Error parsing templates from themes" , e) ) ?;
100
- rewrite_theme_paths ( & mut tera_theme, & theme) ;
101
- // TODO: we do that twice, make it dry?
102
- if theme_path. join ( "templates" ) . join ( "robots.txt" ) . exists ( ) {
103
- tera_theme
104
- . add_template_file ( theme_path. join ( "templates" ) . join ( "robots.txt" ) , None ) ?;
105
- }
106
- tera. extend ( & tera_theme) ?;
107
82
}
108
- tera. extend ( & ZOLA_TERA ) ?;
109
- tera. build_inheritance_chains ( ) ?;
110
83
111
- // TODO: Tera doesn't use globset right now so we can load the robots.txt as part
112
- // of the glob above, therefore we load it manually if it exists.
113
- if path. join ( "templates" ) . join ( "robots.txt" ) . exists ( ) {
114
- tera. add_template_file ( path. join ( "templates" ) . join ( "robots.txt" ) , Some ( "robots.txt" ) ) ?;
115
- }
84
+ let tera = load_tera ( path, & config) ?;
116
85
117
86
let content_path = path. join ( "content" ) ;
118
87
let static_path = path. join ( "static" ) ;
@@ -145,7 +114,7 @@ impl Site {
145
114
}
146
115
147
116
/// The index sections are ALWAYS at those paths
148
- /// There are one index section for the basic language + 1 per language
117
+ /// There are one index section for the default language + 1 per language
149
118
fn index_section_paths ( & self ) -> Vec < ( PathBuf , Option < String > ) > {
150
119
let mut res = vec ! [ ( self . content_path. join( "_index.md" ) , None ) ] ;
151
120
for language in & self . config . languages {
@@ -164,11 +133,6 @@ impl Site {
164
133
self . live_reload = get_available_port ( port_to_avoid) ;
165
134
}
166
135
167
- /// Get the number of orphan (== without section) pages in the site
168
- pub fn get_number_orphan_pages ( & self ) -> usize {
169
- self . library . read ( ) . unwrap ( ) . get_all_orphan_pages ( ) . len ( )
170
- }
171
-
172
136
pub fn set_base_url ( & mut self , base_url : String ) {
173
137
let mut imageproc = self . imageproc . lock ( ) . expect ( "Couldn't lock imageproc (set_base_url)" ) ;
174
138
imageproc. set_base_url ( & base_url) ;
@@ -359,58 +323,14 @@ impl Site {
359
323
Ok ( ( ) )
360
324
}
361
325
362
- /// Adds global fns that are to be available to shortcodes while
363
- /// markdown
326
+ // TODO: remove me in favour of the direct call to the fn once rebuild has changed
364
327
pub fn register_early_global_fns ( & mut self ) {
365
- self . tera . register_function (
366
- "get_url" ,
367
- global_fns:: GetUrl :: new (
368
- self . config . clone ( ) ,
369
- self . permalinks . clone ( ) ,
370
- vec ! [ self . static_path. clone( ) , self . output_path. clone( ) , self . content_path. clone( ) ] ,
371
- ) ,
372
- ) ;
373
- self . tera . register_function (
374
- "resize_image" ,
375
- global_fns:: ResizeImage :: new ( self . imageproc . clone ( ) ) ,
376
- ) ;
377
- self . tera . register_function (
378
- "get_image_metadata" ,
379
- global_fns:: GetImageMeta :: new ( self . content_path . clone ( ) ) ,
380
- ) ;
381
- self . tera . register_function ( "load_data" , global_fns:: LoadData :: new ( self . base_path . clone ( ) ) ) ;
382
- self . tera . register_function ( "trans" , global_fns:: Trans :: new ( self . config . clone ( ) ) ) ;
383
- self . tera . register_function (
384
- "get_taxonomy_url" ,
385
- global_fns:: GetTaxonomyUrl :: new ( & self . config . default_language , & self . taxonomies ) ,
386
- ) ;
387
- self . tera . register_function (
388
- "get_file_hash" ,
389
- global_fns:: GetFileHash :: new ( vec ! [
390
- self . static_path. clone( ) ,
391
- self . output_path. clone( ) ,
392
- self . content_path. clone( ) ,
393
- ] ) ,
394
- ) ;
328
+ register_early_global_fns ( self ) ;
395
329
}
396
330
331
+ // TODO: remove me in favour of the direct call to the fn once rebuild has changed
397
332
pub fn register_tera_global_fns ( & mut self ) {
398
- self . tera . register_function (
399
- "get_page" ,
400
- global_fns:: GetPage :: new ( self . base_path . clone ( ) , self . library . clone ( ) ) ,
401
- ) ;
402
- self . tera . register_function (
403
- "get_section" ,
404
- global_fns:: GetSection :: new ( self . base_path . clone ( ) , self . library . clone ( ) ) ,
405
- ) ;
406
- self . tera . register_function (
407
- "get_taxonomy" ,
408
- global_fns:: GetTaxonomy :: new (
409
- & self . config . default_language ,
410
- self . taxonomies . clone ( ) ,
411
- self . library . clone ( ) ,
412
- ) ,
413
- ) ;
333
+ register_tera_global_fns ( self ) ;
414
334
}
415
335
416
336
/// Add a page to the site
@@ -703,6 +623,8 @@ impl Site {
703
623
)
704
624
}
705
625
626
+ /// Renders all the aliases for each page/section: a magic HTML template that redirects to
627
+ /// the canonical one
706
628
pub fn render_aliases ( & self ) -> Result < ( ) > {
707
629
ensure_directory_exists ( & self . output_path ) ?;
708
630
let library = self . library . read ( ) . unwrap ( ) ;
@@ -837,6 +759,7 @@ impl Site {
837
759
sitemap_url. pop ( ) ; // Remove trailing slash
838
760
sitemap_index. push ( sitemap_url) ;
839
761
}
762
+
840
763
// Create main sitemap that reference numbered sitemaps
841
764
let mut main_context = Context :: new ( ) ;
842
765
main_context. insert ( "sitemaps" , & sitemap_index) ;
@@ -993,6 +916,7 @@ impl Site {
993
916
Ok ( ( ) )
994
917
}
995
918
919
+ // TODO: remove me when reload has changed
996
920
/// Used only on reload
997
921
pub fn render_index ( & self ) -> Result < ( ) > {
998
922
self . render_section (
@@ -1061,5 +985,3 @@ impl Site {
1061
985
. collect :: < Result < ( ) > > ( )
1062
986
}
1063
987
}
1064
-
1065
- impl Site { }
0 commit comments