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
/// All route creation implementations for `Unreact` struct
mod routes;
use std::fs;
use handlebars::Handlebars;
use crate::{
convert::{register_inbuilt, register_templates, render_page, scss_to_css},
files::{check_source_folders, clean_build_dir, read_folder_recurse},
Config, Error, Object, Port, RouteMap, Unreact, DEV_BUILD_DIR,
};
impl<'a> Unreact<'a> {
/// Create a new empty `Unreact` app
///
/// ## Parameters
///
/// - `config`: Configuration for the app (See [`Config`])
/// - `is_dev`: Whether the app should build in *dev mode* (See [`is_dev`](fn.is_dev.html))
/// - `url`: The url that should be given to rendered templates. Overridden in *dev mode*. Trailing forward-slash is added if not present
///
/// # Examples
///
/// ### Quick Start
///
/// ```rust,no_run
/// use unreact::prelude::*;
///
/// fn main() -> Result<(), Error> {
/// // Create the app
/// // Using default config, not in dev mode, and an example url
/// let mut app = Unreact::new(Config::default(), false, "https://example.com")?;
/// // Create an index route
/// // This uses the template 'page.hbs' in 'templates/'
/// // A json object with a value for 'foo' is passed into the template
/// app.index("page", object! { foo: "World!" });
/// // Compile it!
/// app.run()
/// }
/// ```
///
/// ### Long Example
///
/// ```rust,no_run
/// use unreact::prelude::*;
///
/// fn main() -> Result<(), Error> {
/// // Custom config
/// let config = Config {
/// // Strict mode enabled
/// strict: true,
/// ..Config::default()
/// };
///
/// // Create app, with `is_dev` function
/// let mut app = Unreact::new(config, is_dev(), "https://bruh.news/").expect("Could not create app");
///
/// // Set a global variable named 'smiley'
/// app.globalize(object! {
/// smiley: "(^_^)"
/// });
///
/// // Some routes
/// app.index("page", object! {message: "World"})?
/// .not_found("404", object! {})?
/// .route_raw("hello", "this is my hello page".to_string())
/// .route("article", "other/article", object! {})?;
///
/// // Run app
/// app.run().expect("Could not compile app");
///
/// println!("Compiled successfully");
/// Ok(())
/// }
/// ```
pub fn new(mut config: Config, is_dev: bool, url: &str) -> Result<Self, Error> {
// Use dev build directory if dev mode is active
if is_dev {
config.build = DEV_BUILD_DIR.to_string();
}
// Check that source folders exist and can be accessed
check_source_folders(&config)?;
// Override url if in dev mode
let url = get_url(url, is_dev, config.port);
// Create handlebars registry, and register inbuilt partials and helpers
let mut registry = Handlebars::new();
register_inbuilt(&mut registry, &url)?;
registry.set_dev_mode(is_dev);
Ok(Self {
config,
routes: RouteMap::new(),
globals: Object::new(),
is_dev,
handlebars: registry,
url,
})
}
// pub fn registry(&mut self) -> &mut Handlebars {
// self.registry
// }
/// Set global variables for templates
///
/// # Example
///
/// ```rust,no_run
/// # use unreact::prelude::*;
/// # fn main() -> Result<(), Error> {
/// Unreact::new(Config::default(), false, "https://example.com")?
/// // Index page
/// .index("page", object! {})?
/// // Globalize does not need to be ran before routes
/// .globalize(object! {smiley: "(^_^)"})
/// // Compiles with a smiley face replacing `{{GLOBAL.smiley}}`
/// .run()
/// # }
/// ```
pub fn globalize(&mut self, data: Object) -> &mut Self {
self.globals = data;
self
}
/// Get [`Handlebars`](handlebars) registry as mutable reference
pub fn handlebars(&mut self) -> &mut Handlebars<'a> {
&mut self.handlebars
}
/// Get URL of app (overridden in *dev mode*)
pub fn url(&self) -> &String {
&self.url
}
/// Compile app to build directory
///
/// Does not open a dev server, even in *dev mode*
fn compile(&self) -> Result<(), Error> {
clean_build_dir(&self.config, self.is_dev)?;
// Create handlebars registry
let mut registry = self.handlebars.clone();
// Enable strict mode if active
if self.config.strict {
registry.set_strict_mode(true);
}
// Register custom templates
let templates = read_folder_recurse(&self.config.templates)?;
register_templates(&mut registry, templates)?;
// Render page and write to files
for (name, page) in &self.routes {
// Render page with data
let content = render_page(
&mut registry,
page,
self.globals.clone(),
self.config.minify,
self.is_dev,
self.config.port_ws,
)?;
// Get filepath
let path = if name == "404" {
// Special case for 404 route
format!("{}/404.html", self.config.build)
} else {
// Create folder for `index.html` file
let parent = format!("{}/{}", self.config.build, name);
try_unwrap!(
fs::create_dir_all(&parent),
else Err(err) => return io_fail!(CreateDir, parent, err),
);
// Normal path
format!("{parent}/index.html")
};
// Write file
try_unwrap!(
fs::write(&path, content),
else Err(err) => return io_fail!(WriteFile, path, err),
);
}
// Convert scss to css and write to files
let styles = read_folder_recurse(&self.config.styles)?;
for (name, scss) in styles {
// Create folder for `style.css` file
let parent = format!("{}/styles/{}", self.config.build, name);
try_unwrap!(
fs::create_dir_all(&parent),
else Err(err) => return io_fail!(CreateDir, parent, err),
);
// Convert to scss
let css = scss_to_css(&name, &scss, self.config.minify)?;
// Write file
let path = format!("{}/style.css", parent);
try_unwrap!(
fs::write(&path, css),
else Err(err) => return io_fail!(WriteFile, path, err),
);
}
Ok(())
}
/// Compile app to build directory
///
/// Compile app to build directory
///
/// **NOTE**: The `"dev"` feature is not enabled, so app not open dev server, even in *dev mode*
///
/// Add `features = "dev"` or `features = "watch"` to the `unreact` dependency in `Cargo.toml` to use the 'dev server'
///
/// # Examples
///
/// ### Quick Start
///
/// ```rust,no_run
/// use unreact::prelude::*;
///
/// fn main() -> Result<(), Error> {
/// // Create the app
/// // Using default config, not in dev mode, and an example url
/// let mut app = Unreact::new(Config::default(), false, "https://example.com")?;
/// // Create an index route
/// // This uses the template 'page.hbs' in 'templates/'
/// // A json object with a value for 'foo' is passed into the template
/// app.index("page", object! { foo: "World!" });
/// // Compile it!
/// app.run()
/// }
/// ```
#[cfg(not(feature = "dev"))]
pub fn run(&self) -> Result<(), Error> {
self.compile()
}
/// Compile app to build directly, and open local server if *dev mode* is active
///
/// Only opens a dev server with the `"dev"` or `"watch"` features enabled
///
/// If the `"watch"` feature is enabled, source files will also be watched for changes, and the client will be reloaded automatically
///
/// # Examples
///
/// ```rust,no_run
/// use unreact::prelude::*;
///
/// fn main() -> Result<(), Error> {
/// // Create the app
/// // Using default config, not in dev mode, and an example url
/// let mut app = Unreact::new(Config::default(), true, "https://example.com")?;
/// // Create an index route
/// // This uses the template 'page.hbs' in 'templates/'
/// // A json object with a value for 'foo' is passed into the template
/// app.index("page", object! { foo: "World!" });
/// // Compile it!
/// app.run()
/// }
/// ```
#[cfg(feature = "dev")]
pub fn run(&self) -> Result<(), Error> {
use crate::server;
use stilo::{eprintln_styles, print_styles, println_styles};
// Just compile if not dev mode
if !self.is_dev {
return self.compile();
}
// Create callback with non-breaking error message
let run_compile = || {
// Clear terminal
print!("{esc}[2J{esc}[1;1H", esc = 27 as char);
// Print message before compile
print_styles!(
"Unreact": Blue + bold + italic;
" dev server": Blue + bold;
);
if let Some(name) = crate::get_package_name() {
print_styles!(
" | ": Blue + dim;
"{}": Magenta, name;
);
}
println_styles!(
"\n Listening on ": Green + bold;
"http://localhost:{}": Green + bold + underline, self.config.port;
);
#[cfg(feature = "watch")]
{
println_styles!(" Watching files for changes...": Cyan);
}
#[cfg(not(feature = "watch"))]
{
println_styles!(
" Note: ": Yellow + bold;
"\"watch\"": Yellow + italic;
" feature not enabled": Yellow;
);
}
println!();
// Compile it now
match self.compile() {
// Success
Ok(()) => println_styles!("Compiled successfully!": Green + bold),
// Error
Err(err) => eprintln_styles!(
"Error compiling in dev mode:": Red + bold;
"\n{}": Yellow, err;
),
}
};
// Compile for first time
run_compile();
// For "watch" feature
#[cfg(feature = "watch")]
{
// Open server in new thread
let port = self.config.port;
let port_ws = self.config.port_ws;
let public = self.config.public.clone();
std::thread::spawn(move || server::listen(port, &public, port_ws));
// Folders to watch
let watched_folders = &[
self.config.templates.as_str(),
self.config.styles.as_str(),
self.config.public.as_str(),
];
// Watch files for changes
server::watch(run_compile, watched_folders, self.config.port_ws);
}
// For NOT "watch" feature
#[cfg(not(feature = "watch"))]
{
// Open server in current thread
server::listen(self.config.port, self.config.public, self.config.port_ws);
}
Ok(())
}
}
/// Get the url for the site
///
/// Returns url given, unless `"dev"` feature is enabled and *dev mode* is active
fn get_url(
url: &str,
// Only for "dev" feature
#[allow(unused_variables)] is_dev: bool,
#[allow(unused_variables)] port: Port,
) -> String {
// If `watch` feature is used, and `is_dev`
#[cfg(feature = "dev")]
{
if is_dev {
return format!("http://localhost:{}/", port);
}
}
// Default (add slash to end if not included)
url.to_string() + if url.ends_with('/') { "" } else { "/" }
}