-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplates.rs
650 lines (576 loc) · 21.8 KB
/
templates.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
use rocket_contrib::Template;
// use handlebars::Handlebars;
use std::collections::{HashMap, BTreeMap};
use chrono::{NaiveDate, NaiveDateTime};
use titlecase::titlecase;
use std::time::{Instant, Duration};
// use ::serde::{Deserialize, Serialize};
// use cookie_data::*;
// not used anymore
// use admin_auth::*;
// use user_auth::*;
use super::{BLOG_URL, INTERNAL_IMGS, BASE, DEFAULT_PAGE_MENU};
use blog::*;
use collate::*;
use layout::*;
// use users;
use ral_administrator::*;
use ral_user::*;
use std::path::{Path, PathBuf};
use std::env;
#[derive(Debug, Clone, Serialize)]
pub struct TagCount {
pub tag: String,
pub url: String,
pub count: u32,
pub size: u16,
}
// style="background: url('') center center no-repeat"
/// The TemplateBody struct determines which template is used and what info is passed to it
#[derive(Debug, Clone)]
// pub enum TemplateBody<T: Collate> {
pub enum TemplateBody {
General(String), // page content and optional message
Article(Article), // article and optional message
Articles(Vec<Article>), // articles and an optional message
ArticlesPages(
Vec<Article>, // articles
Page<Pagination>, // pagination info
u32, // total number of items
Option<String>, // page information - "Showing page x of y - z items found"
),
// Search(Vec<Article>, Option<String>, Option<String>), // articles and an optional message
Search(Vec<Article>, Option<Search>), // articles and an optional message
Login (
String, // Form Action URL
Option<String>, // username that was entered
),
LoginData (
String,
Option<String>,
HashMap<String, String>,
),
Create(String), // form action url and optional message
Edit(String, Article), // form action url and optional message
// Paginated list of articles,
// need to find a way to indicate which column is being sorted on and which way its sorted
// manage/desc|asc/date
// turn sort into sort display
// Manage(String, String, Vec<Article>, Page<Pagination>, u32, Sort, Option<String>), // Edit action, delete action, articles, pagination, total items, sort info
// Manage(Vec<Article>, Page<Pagination>, u32, Sort), // Articles, pagination, total items, sort info
// Manage(Vec<Article>, Page<T>, u32, Sort), // Articles, pagination, total items, sort info
Manage(Vec<Article>, Page<Pagination>, u32, Sort), // Articles, pagination, total items, sort info
Tags(Vec<TagCount>), // list of tags and their counts, and optional message
// Stats(Vec<PageStats>, usize) // list of page stats items and the total number of hits
}
// #[derive(Debug, Clone, Serialize)]
// pub struct PageStats {
// pub route: String,
// pub ip: String,
// pub visits: usize,
// pub hits: usize,
// pub uhits: usize,
// }
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TemplateMenu {
#[serde(default)]
pub separator: bool,
#[serde(default)]
pub name: String,
#[serde(default)]
pub url: String,
#[serde(default)]
pub classes: String,
}
#[derive(Debug, Clone, Serialize)]
pub struct TemplateImg {
pub image: String,
pub selected: bool,
}
/// The TemplateInfo struct contains page metadata
#[derive(Debug, Clone, Serialize)]
pub struct TemplateInfo {
pub title: String,
pub logged_in: bool,
pub is_admin: bool,
pub is_user: bool,
pub username: String,
pub js: String,
pub gentime: String,
pub page: String,
pub pages: Vec<TemplateMenu>,
pub admin_pages: Vec<TemplateMenu>,
pub base_url: &'static str,
pub dropdown: String,
pub msg: String,
}
// START TEMPLATEBODY STRUCTURES
#[derive(Debug, Clone, Serialize)]
pub struct TemplateLogin {
pub action_url: String,
pub tried_user: String,
pub info: TemplateInfo,
}
#[derive(Debug, Clone, Serialize)]
pub struct TemplateLoginData {
pub action_url: String,
pub tried_user: String,
pub fields: HashMap<String, String>,
pub info: TemplateInfo,
}
#[derive(Debug, Clone, Serialize)]
pub struct TemplateCreate {
pub action_url: String,
pub imgs: Vec<String>,
pub info: TemplateInfo,
}
#[derive(Debug, Clone, Serialize)]
pub struct TemplateEdit {
pub action_url: String,
pub body: ArticleDisplay,
pub imgs: Vec<TemplateImg>,
pub info: TemplateInfo,
}
#[derive(Debug, Clone, Serialize)]
pub struct TemplateGeneral {
pub body: String,
pub info: TemplateInfo,
}
// #[derive(Debug, Clone, Serialize)]
// pub struct TemplateStats {
// pub items: Vec<PageStats>,
// pub total: usize,
// pub visitors: usize,
// pub info: TemplateInfo,
// }
#[derive(Debug, Clone, Serialize)]
pub struct TemplateArticle {
pub body: ArticleDisplay,
pub info: TemplateInfo,
}
#[derive(Debug, Clone, Serialize)]
pub struct TemplateArticles {
pub body: Vec<ArticleDisplay>,
pub info: TemplateInfo,
}
#[derive(Serialize)]
pub struct TemplateSearch {
pub body: Vec<ArticleDisplay>,
pub search: SearchDisplay,
pub info: TemplateInfo,
}
#[derive(Debug, Clone, Serialize)]
pub struct TemplateTags {
pub tags: Vec<TagCount>,
pub info: TemplateInfo,
}
#[derive(Debug, Clone, Serialize)]
pub struct TemplateArticlesPages {
pub body: Vec<ArticleDisplay>,
pub links: String,
pub current: String,
pub info: TemplateInfo,
}
#[derive(Debug, Clone, Serialize)]
pub struct TemplateManage {
// pub action_url: String,
pub body: Vec<ArticleDisplay>,
pub links: String,
pub sort: SortDisplay,
pub info: TemplateInfo,
}
// END TEMPLATEBODY STRUCTS
// let end = start.elapsed();
// println!("Served in {}.{:08} seconds", end.as_secs(), end.subsec_nanos());
pub fn create_menu(page: &str, admin_opt: &Option<AdministratorCookie>, user_opt: &Option<UserCookie>) -> (Vec<TemplateMenu>, Vec<TemplateMenu>) {
// let mut pages: Vec<TemplateMenu> = vec![
// TemplateMenu::new(String::from("Home"), String::from("/"), page),
// TemplateMenu::new(String::from("Rust Tutorials"), String::from("/content/tutorials"), page),
// TemplateMenu::new(String::from("Tags"), String::from("/all_tags"), page),
// TemplateMenu::new(String::from("About"), String::from("/content/about-me"), page),
// ];
let mut pages: Vec<TemplateMenu> = DEFAULT_PAGE_MENU.clone().unwrap_or(vec![
TemplateMenu::new(String::from("Home"), String::from("/"), page),
TemplateMenu::new(String::from("Rust Tutorials"), String::from("/content/tutorials"), page),
TemplateMenu::new(String::from("Tags"), String::from("/all_tags"), page),
TemplateMenu::new(String::from("About"), String::from("/content/about-me"), page),
]);
// Displays both admin and user menus if user is logged in as both
let mut admin_pages: Vec<TemplateMenu> = Vec::new();
// if admin_opt.is_some() && user_opt.is_some() {
// admin_pages.push( TemplateMenu::separator() );
// }
// admin_pages.push( TemplateMenu { separator: true, name: "User Menu".to_string(), url: String::new(), classes: String::new() });
admin_pages.push( TemplateMenu::header("User Menu"));
if user_opt.is_some() {
admin_pages.push( TemplateMenu::new(String::from("User Dashboard"), String::from("/user"), page) );
// admin_pages.push( TemplateMenu::separator() );
admin_pages.push( TemplateMenu::new(String::from("Logout User"), String::from("/user_logout"), page) );
} else {
admin_pages.push( TemplateMenu::new(String::from("User Login"), String::from("/user"), page) );
}
// admin_pages.push( TemplateMenu::separator() );
// admin_pages.push( TemplateMenu { separator: true, name: "Admin Menu".to_string(), url: String::new(), classes: String::new() });
admin_pages.push( TemplateMenu::header("Admin Menu"));
if admin_opt.is_some() {
admin_pages.push( TemplateMenu::new(String::from("Admin Dashboard"), String::from("/admin"), page) );
admin_pages.push( TemplateMenu::new(String::from("New Article"), String::from("/create"), page) );
admin_pages.push( TemplateMenu::new(String::from("Page Statistics"), String::from("/pagestats/false"), page) );
admin_pages.push( TemplateMenu::with_class(String::from("Database Backup"), String::from("/backup"), String::from("\" target=\"_blank"), page) );
admin_pages.push( TemplateMenu::new(String::from("Refresh Content"), String::from("/refresh_content"), page) );
// admin_pages.push( TemplateMenu::separator() );
admin_pages.push( TemplateMenu::new(String::from("Logout Administrator"), String::from("/admin_logout"), page) );
} else {
admin_pages.push( TemplateMenu::new(String::from("Admin Login"), String::from("/admin"), page) );
}
// if admin_opt.is_none() && user_opt.is_none() {
// pages.push( TemplateMenu::new(String::from("User Login"), String::from("/user"), page) );
// pages.push( TemplateMenu::new(String::from("Admin Login"), String::from("/admin"), page) );
// }
(pages, admin_pages)
}
// lazy_static! {
// static ref BASE: &'static str = if BLOG_URL.ends_with("/") {
// &BLOG_URL[..BLOG_URL.len()-1]
// } else {
// &BLOG_URL
// };
// }
impl TemplateMenu {
pub fn new(name: String, url: String, current_page: &str) -> TemplateMenu {
let classes = if &url == current_page {
"active".to_string()
} else {
String::new()
};
TemplateMenu {
separator: false,
classes,
name,
url: {
if url.starts_with("http") || url.starts_with("www") {
url
} else {
let mut u = String::with_capacity(BASE.len() + url.len() + 10);
u.push_str(&BASE);
u.push_str(&url);
u
}
},
}
}
pub fn with_class(name: String, url: String, class: String, current_page: &str) -> TemplateMenu {
let mut classes = String::with_capacity(class.len() + 50);
if &url == current_page {
classes.push_str("active");
}
classes.push_str(&class);
TemplateMenu {
separator: false,
classes,
name,
url: {
if url.starts_with("http") || url.starts_with("www") {
url
} else {
let mut u = String::with_capacity(BASE.len() + url.len() + 10);
u.push_str(&BASE);
u.push_str(&url);
u
}
},
}
}
pub fn separator() -> TemplateMenu {
TemplateMenu {
separator: true,
name: String::new(),
url: String::new(),
classes: String::new(),
}
}
pub fn header(text: &str) -> TemplateMenu {
TemplateMenu {
separator: true,
name: text.to_string(),
url: String::new(),
classes: String::new(),
}
}
}
impl TemplateInfo {
pub fn new( title: Option<String>,
admin: Option<AdministratorCookie>,
user: Option<UserCookie>,
js: String,
gen: Option<Instant>,
page: String,
pages: Vec<TemplateMenu>,
admin_pages: Vec<TemplateMenu>,
msg: Option<String>,
) -> TemplateInfo {
let gentime = if let Some(inst) = gen {
let end = inst.elapsed();
format!("{}.{:09} seconds", end.as_secs(), end.subsec_nanos())
// let secs = end.as_secs();
// let nanos = end.subsec_nanos();
// if secs != 0 {
// format!("{} ms", ((end.subsec_nanos() as f64) /1000) )
// } else {
// format!("{}.{:08} seconds", end.as_secs(), end.subsec_nanos())
// // format!("{}-{} seconds", end.as_secs(), end.subsec_nanos())
// }
} else {
String::new()
};
let username = if let Some(a) = admin.clone() {
if let Some(d) = a.display {
titlecase(&d)
} else {
a.username.clone()
}
} else if let Some(u) = user.clone() {
if let Some(d) = u.display {
titlecase(&d)
} else {
u.username.clone()
}
} else {
String::new()
};
// Display the page generation time (up until template processing)???
// println!("Route processed in {}.{:08}", end.as_secs(), end.subsec_nanos());
TemplateInfo {
title: if let Some(t) = title { t } else { String::new() },
logged_in: if admin.is_some() || user.is_some() { true } else { false },
is_admin: if admin.is_some() { true } else { false },
is_user: if user.is_some() { true } else { false },
// username: if let Some(a) = admin { titlecase(&a.username.clone()) } else if let Some(u) = user { titlecase(&u.username.clone()) } else { String::new() },
dropdown: if &username != "" { username.clone() } else { "Login".to_string() },
username,
js,
gentime,
page,
pages,
admin_pages,
base_url: BLOG_URL,
msg: if let Some(m) = msg { m } else { String::new() },
}
}
}
impl TemplateGeneral {
pub fn new(content: String, info: TemplateInfo) -> TemplateGeneral {
TemplateGeneral {
body: content,
info
}
}
}
// impl TemplateStats {
// pub fn new(stats: Vec<PageStats>, total: usize, info: TemplateInfo) -> TemplateGeneral {
// TemplateGeneral {
// stats,
// total,
// visitors: 0, // not implemented yet
// info
// }
// }
// }
impl TemplateArticle {
pub fn new(content: Article, info: TemplateInfo) -> TemplateArticle {
TemplateArticle {
body: content.to_display(),
info,
}
}
}
impl TemplateArticles {
pub fn new(content: Vec<Article>, info: TemplateInfo) -> TemplateArticles {
let mut articles: Vec<ArticleDisplay> = content.iter().map(|a| a.to_display()).collect();
TemplateArticles {
body: articles,
info,
}
}
}
impl TemplateSearch {
pub fn new(content: Vec<Article>, search: Option<Search>, info: TemplateInfo) -> TemplateSearch {
let mut articles: Vec<ArticleDisplay> = content.iter().map(|a| a.to_display()).collect();
TemplateSearch {
body: articles,
search: if let Some(s) = search { s.to_display() } else { SearchDisplay::default() },
info,
}
}
}
impl TemplateLogin {
pub fn new(action_url: String, tried: Option<String>, info: TemplateInfo) -> TemplateLogin {
TemplateLogin {
action_url,
tried_user: if let Some(tuser) = tried { tuser } else { String::new() },
info,
}
}
}
impl TemplateLoginData {
pub fn new(action_url: String, tried: Option<String>, fields: HashMap<String, String>, info: TemplateInfo) -> TemplateLoginData {
TemplateLoginData {
action_url,
tried_user: if let Some(tuser) = tried { tuser } else { String::new() },
fields,
info,
}
}
}
impl TemplateCreate {
pub fn new(action_url: String, info: TemplateInfo) -> TemplateCreate {
use std::io;
use std::fs::{self, DirEntry, read_dir};
use std::path::Path;
let dir_entries = read_dir(INTERNAL_IMGS);
let mut imgs: Vec<String> = Vec::new();
if let Ok(entries) = dir_entries {
for dir_entry in entries {
if let Ok(entry) = dir_entry {
let path = entry.path();
if !path.is_dir() && path.to_string_lossy().into_owned().ends_with(".jpg") {
if let Some(name) = path.file_name() {
let image = name.to_string_lossy().into_owned();
imgs.push(image);
}
}
}
}
}
TemplateCreate {
action_url,
imgs,
info,
}
}
}
impl TemplateEdit {
pub fn new(action_url: String, article: Article, info: TemplateInfo) -> TemplateEdit {
use std::io;
use std::fs::{self, DirEntry, read_dir};
use std::path::Path;
let dir_entries = read_dir(INTERNAL_IMGS);
let mut imgs: Vec<TemplateImg> = Vec::new();
if let Ok(entries) = dir_entries {
for dir_entry in entries {
if let Ok(entry) = dir_entry {
let path = entry.path();
if !path.is_dir() && path.to_string_lossy().into_owned().ends_with(".jpg") {
if let Some(name) = path.file_name() {
let image = name.to_string_lossy().into_owned();
let img = TemplateImg {
selected: if &image == &article.image { true } else { false },
image,
};
imgs.push(img);
}
}
}
}
}
TemplateEdit {
action_url,
body: article.to_display(),
imgs,
info,
}
}
}
impl TemplateTags {
pub fn new(tags: Vec<TagCount>, info: TemplateInfo) -> TemplateTags {
TemplateTags {
tags,
info,
}
}
}
impl TemplateArticlesPages {
// pub fn new(content: Vec<Article>, page: Page<Pagination>, total_items: u32, info_opt: Option<String>, info: TemplateInfo) -> TemplateArticlesPages {
pub fn new<T: Collate>(content: Vec<Article>, page: Page<T>, total_items: u32, info_opt: Option<String>, info: TemplateInfo) -> TemplateArticlesPages {
let mut articles: Vec<ArticleDisplay> = content.iter().map(|a| a.to_display()).collect();
TemplateArticlesPages {
body: articles,
links: page.navigation(total_items),
current: if let Some(curinfo) = info_opt { curinfo } else { page.page_info(total_items) },
info,
}
}
}
impl TemplateManage {
// pub fn new(content: Vec<Article>, page: Page<Pagination>, total_items: u32, sort: Sort, info: TemplateInfo) -> TemplateManage {
pub fn new<T: Collate>(content: Vec<Article>, page: Page<T>, total_items: u32, sort: Sort, info: TemplateInfo) -> TemplateManage {
let mut articles: Vec<ArticleDisplay> = content.iter().map(|a| a.to_display()).collect();
TemplateManage {
// action_url,
body: articles,
links: page.navigation(total_items),
sort: sort.to_display(),
info,
}
}
}
pub fn hbs_template(
content: TemplateBody,
msg: Option<String>,
title: Option<String>,
page: String,
admin_opt: Option<AdministratorCookie>,
user_opt: Option<UserCookie>,
javascript: Option<String>,
gen: Option<Instant>
) -> Template {
let js = if let Some(j) = javascript { j } else { "".to_string() };
let (pages, admin_pages) = create_menu(&page, &admin_opt, &user_opt);
let info = TemplateInfo::new(title, admin_opt, user_opt, js, gen, page, pages, admin_pages, msg);
match content {
TemplateBody::General(contents) => {
let context = TemplateGeneral::new(contents, info);
Template::render("general-template", &context)
},
TemplateBody::Article(article) => {
let context = TemplateArticle::new(article, info);
Template::render("article-template", &context)
},
TemplateBody::Articles(articles) => {
let context = TemplateArticles::new(articles, info);
Template::render("articles-template", &context)
},
TemplateBody::Search(articles, search) => {
let context = TemplateSearch::new(articles, search, info);
Template::render("search-template", &context)
},
TemplateBody::Login(action, tried) => {
let context = TemplateLogin::new(action, tried, info);
Template::render("login-template", &context)
},
TemplateBody::LoginData(action, tried, fields) => {
let context = TemplateLoginData::new(action, tried, fields, info);
Template::render("login-template", &context)
},
TemplateBody::Create(action) => {
let context = TemplateCreate::new(action, info);
Template::render("create-template", &context)
},
TemplateBody::Tags(tags) => {
let context = TemplateTags::new(tags, info);
Template::render("tags-template", &context)
},
TemplateBody::ArticlesPages(articles, page, total, curinfo) => {
let context = TemplateArticlesPages::new(articles, page, total, curinfo, info);
Template::render("articles-pagination-template", &context)
},
TemplateBody::Edit(action, article) => {
let context = TemplateEdit::new(action, article, info);
Template::render("edit-article-template", &context)
}
TemplateBody::Manage(articles, page, total, sort) => {
let context = TemplateManage::new(articles, page, total, sort, info);
Template::render("manage-pagination-template", &context)
}
}
}