Skip to content

Commit a7f8115

Browse files
authored
Merge pull request #1179 from epage/fixes
fix(serve): Work with custom config
2 parents df119e1 + 9f88d73 commit a7f8115

File tree

85 files changed

+617
-296
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+617
-296
lines changed

crates/config/src/config.rs

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ impl Config {
7979

8080
let mut root = path;
8181
root.pop(); // Remove filename
82+
if root == std::path::Path::new("") {
83+
root = std::path::Path::new(".").to_owned();
84+
}
8285
config.root = root;
8386

8487
Ok(config)

src/bin/cobalt/args.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::env;
21
use std::io::Write;
32
use std::path;
43

@@ -32,11 +31,10 @@ impl ConfigArgs {
3231
// Fetch config information if available
3332
let mut config = if let Some(config_path) = config_path {
3433
cobalt_config::Config::from_file(config_path).with_context(|| {
35-
anyhow::format_err!("Error reading config file {:?}", config_path)
34+
anyhow::format_err!("Error reading config file {}", config_path.display())
3635
})?
3736
} else {
38-
let cwd = env::current_dir().unwrap_or_default();
39-
cobalt_config::Config::from_cwd(cwd)?
37+
cobalt_config::Config::from_cwd(".")?
4038
};
4139

4240
config.abs_dest = self

src/bin/cobalt/new.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::collections;
2-
use std::env;
32
use std::fs;
43
use std::io::Write;
54
use std::path;
@@ -58,7 +57,7 @@ impl NewArgs {
5857

5958
let title = self.title.as_deref();
6059

61-
let mut file = env::current_dir().unwrap_or_default();
60+
let mut file = path::Path::new(".").to_owned();
6261
if let Some(rel_file) = self.file.as_deref() {
6362
file.push(rel_file)
6463
}
@@ -100,7 +99,7 @@ impl RenameArgs {
10099

101100
let title = self.title.as_ref();
102101

103-
let mut file = env::current_dir().unwrap_or_default();
102+
let mut file = path::Path::new(".").to_owned();
104103
if let Some(rel_file) = self.file.as_deref() {
105104
file.push(rel_file)
106105
}
@@ -130,7 +129,7 @@ impl PublishArgs {
130129
let config = cobalt::cobalt_model::Config::from_config(config)?;
131130

132131
let filename = self.filename.as_path();
133-
let mut file = env::current_dir().unwrap_or_default();
132+
let mut file = path::Path::new(".").to_owned();
134133
file.push(path::Path::new(filename));
135134

136135
publish_document(&config, &file)
@@ -248,7 +247,7 @@ pub fn create_new_document(
248247
let interim_path = cobalt_core::SourcePath::from_root(&config.source, &interim_path)
249248
.ok_or_else(|| {
250249
anyhow::format_err!(
251-
"New file {} not project directory ({})",
250+
"New file {} not in project in directory ({})",
252251
file.display(),
253252
config.source.display()
254253
)
@@ -369,7 +368,7 @@ pub fn rename_document(
369368

370369
let target = cobalt_core::SourcePath::from_root(&config.source, &target).ok_or_else(|| {
371370
anyhow::format_err!(
372-
"New file {} not project directory ({})",
371+
"New file {} not in project directory ({})",
373372
target.display(),
374373
config.source.display()
375374
)
@@ -484,7 +483,7 @@ pub fn publish_document(config: &cobalt_model::Config, file: &path::Path) -> Res
484483
let file = move_from_drafts_to_posts(config, file)?;
485484
let file = cobalt_core::SourcePath::from_root(&config.source, &file).ok_or_else(|| {
486485
anyhow::format_err!(
487-
"New file {} not project directory ({})",
486+
"New file {} not in project directory ({})",
488487
file.display(),
489488
config.source.display()
490489
)

src/bin/cobalt/serve.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl ServeArgs {
6767

6868
dest.close()?;
6969
} else {
70-
info!("Watching {:?} for changes", &config.source);
70+
info!("Watching {} for changes", &config.source.display());
7171
thread::spawn(move || {
7272
let e = serve(&server);
7373
if let Some(e) = e.err() {
@@ -105,22 +105,27 @@ fn open_browser(url: String) -> Result<()> {
105105
fn watch(config: &cobalt_model::Config) -> Result<()> {
106106
// canonicalize is to ensure there is no question that `watcher`s paths come back safe for
107107
// Files::includes_file
108-
let source = dunce::canonicalize(path::Path::new(&config.source))
109-
.with_context(|| anyhow::format_err!("Failed in processing source"))?;
108+
let source = dunce::canonicalize(path::Path::new(&config.source)).with_context(|| {
109+
anyhow::format_err!("Failed in processing source `{}`", config.source.display())
110+
})?;
110111

111112
// Also canonicalize the destination folder. In particular for Windows, notify-rs
112113
// generates the absolute path by prepending the above source path.
113114
// On Windows canonicalize() adds a \\?\ to the start of the path.
114-
let destination = dunce::canonicalize(&config.destination)
115-
.with_context(|| anyhow::format_err!("Failed to canonicalize destination folder"))?;
115+
let destination = dunce::canonicalize(&config.destination).with_context(|| {
116+
anyhow::format_err!(
117+
"Failed to canonicalize destination folder `{}`",
118+
config.destination.display()
119+
)
120+
})?;
116121

117122
let (tx, rx) = channel();
118123
let mut watcher =
119124
notify::recommended_watcher(tx).with_context(|| anyhow::format_err!("Notify error"))?;
120125
watcher
121126
.watch(&source, notify::RecursiveMode::Recursive)
122127
.with_context(|| anyhow::format_err!("Notify error"))?;
123-
info!("Watching {:?} for changes", &config.source);
128+
info!("Watching {} for changes", config.source.display());
124129

125130
for event in rx {
126131
let event = event.with_context(|| anyhow::format_err!("Notify error"))?;

src/cobalt_model/files.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ impl FilesBuilder {
3939
}
4040

4141
pub fn add_ignore(&mut self, line: &str) -> Result<&mut Self> {
42-
trace!("{:?}: adding '{}' ignore pattern", self.root_dir, line);
42+
trace!(
43+
"{}: adding '{}' ignore pattern",
44+
self.root_dir.display(),
45+
line
46+
);
4347
self.ignore.push(line.to_owned());
4448
Ok(self)
4549
}
@@ -55,7 +59,7 @@ impl FilesBuilder {
5559
}
5660

5761
pub fn add_extension(&mut self, ext: &str) -> Result<&mut FilesBuilder> {
58-
trace!("{:?}: adding '{}' extension", self.root_dir, ext);
62+
trace!("{}: adding '{}' extension", self.root_dir.display(), ext);
5963
self.extensions.push(ext.into());
6064
Ok(self)
6165
}
@@ -211,11 +215,11 @@ impl Files {
211215
match self.ignore.matched(path, is_dir) {
212216
Match::None => true,
213217
Match::Ignore(glob) => {
214-
trace!("{:?}: ignored {:?}", path, glob.original());
218+
trace!("{}: ignored {:?}", path.display(), glob.original());
215219
false
216220
}
217221
Match::Whitelist(glob) => {
218-
trace!("{:?}: allowed {:?}", path, glob.original());
222+
trace!("{}: allowed {:?}", path.display(), glob.original());
219223
true
220224
}
221225
}

src/cobalt_model/site.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ fn deep_insert(
104104
for part in path.iter() {
105105
let key = part.to_str().ok_or_else(|| {
106106
anyhow::format_err!(
107-
"The data from {:?} can't be loaded as it contains non utf-8 characters",
108-
path
107+
"The data from `{}` can't be loaded as it contains non utf-8 characters",
108+
path.display()
109109
)
110110
})?;
111111
let cur_map = map;
@@ -116,8 +116,8 @@ fn deep_insert(
116116
.as_object_mut()
117117
.ok_or_else(|| {
118118
anyhow::format_err!(
119-
"Aborting: Duplicate in data tree. Would overwrite {:?} ",
120-
path
119+
"Aborting: Duplicate in data tree. Would overwrite {} ",
120+
path.display()
121121
)
122122
})?;
123123
}
@@ -129,8 +129,8 @@ fn deep_insert(
129129
match target_map.insert(target_key.into(), data) {
130130
None => Ok(()),
131131
_ => Err(anyhow::format_err!(
132-
"The data from {:?} can't be loaded: the key already exists",
133-
file_path
132+
"The data from {} can't be loaded: the key already exists",
133+
file_path.display()
134134
)),
135135
}
136136
}
@@ -151,9 +151,9 @@ fn load_data(data_path: &path::Path) -> Result<liquid::model::Value> {
151151
data = toml::from_str(&text)?;
152152
} else {
153153
anyhow::bail!(
154-
"Failed to load of data {:?}: unknown file type '{:?}'.\n\
154+
"Failed to load of data `{}`: unknown file type '{:?}'.\n\
155155
Supported data files extensions are: yml, yaml, json and toml.",
156-
data_path,
156+
data_path.display(),
157157
ext
158158
);
159159
}

src/document.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl Document {
185185
rel_path: &relative_path::RelativePath,
186186
default_front: cobalt_config::Frontmatter,
187187
) -> Result<Document> {
188-
trace!("Parsing {:?}", rel_path);
188+
trace!("Parsing `{}`", rel_path);
189189
let content = files::read_file(src_path)?;
190190
let builder = cobalt_config::Document::parse(&content)?;
191191
let (front, content) = builder.into_parts();
@@ -379,10 +379,10 @@ impl Document {
379379
let template = context
380380
.parser
381381
.parse(layout_data_ref)
382-
.with_context(|| anyhow::format_err!("Failed to parse layout {:?}", layout))?;
382+
.with_context(|| anyhow::format_err!("Failed to parse layout `{}`", layout))?;
383383
let content_html = template
384384
.render(context.globals)
385-
.with_context(|| anyhow::format_err!("Failed to render layout {:?}", layout))?;
385+
.with_context(|| anyhow::format_err!("Failed to render layout `{}`", layout))?;
386386
let content_html = minify_if_enabled(content_html, context, &self.file_path)?;
387387
Ok(content_html)
388388
} else {

tests/cmd/clean.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Clean no-op
22
```console
33
$ cobalt -v clean
44
WARN: No _cobalt.yml file found in current directory, using default config.
5-
DEBUG: No `[CWD]/_site` to clean
5+
DEBUG: No `./_site` to clean
66
DEBUG: [..]
77

88
```

tests/cmd/copy_files.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
```console
22
$ cobalt -v build --destination _dest
3-
DEBUG: Using config file `[CWD]/_cobalt.yml`
4-
Building from `[CWD]` into `[CWD]/_dest`
3+
DEBUG: Using config file `./_cobalt.yml`
4+
Building from `.` into `[CWD]/_dest`
55
DEBUG: glob converted to regex: Glob { glob: "**/.*", re: "(?-u)^(?:/?|.*/)//.[^/]*$", opts: GlobOptions { case_insensitive: false, literal_separator: true, backslash_escape: true }, tokens: Tokens([RecursivePrefix, Literal('.'), ZeroOrMore]) }
66
DEBUG: glob converted to regex: Glob { glob: "**/_*", re: "(?-u)^(?:/?|.*/)_[^/]*$", opts: GlobOptions { case_insensitive: false, literal_separator: true, backslash_escape: true }, tokens: Tokens([RecursivePrefix, Literal('_'), ZeroOrMore]) }
7-
DEBUG: built glob set; 6 literals, 0 basenames, 0 extensions, 0 prefixes, 0 suffixes, 0 required extensions, 2 regexes
8-
DEBUG: Loading data from `[CWD]/_data`
7+
DEBUG: built glob set; 5 literals, 0 basenames, 0 extensions, 0 prefixes, 0 suffixes, 0 required extensions, 2 regexes
8+
DEBUG: Loading data from `./_data`
99
DEBUG: glob converted to regex: Glob { glob: "**/.*", re: "(?-u)^(?:/?|.*/)//.[^/]*$", opts: GlobOptions { case_insensitive: false, literal_separator: true, backslash_escape: true }, tokens: Tokens([RecursivePrefix, Literal('.'), ZeroOrMore]) }
1010
DEBUG: glob converted to regex: Glob { glob: "**/_*", re: "(?-u)^(?:/?|.*/)_[^/]*$", opts: GlobOptions { case_insensitive: false, literal_separator: true, backslash_escape: true }, tokens: Tokens([RecursivePrefix, Literal('_'), ZeroOrMore]) }
1111
DEBUG: built glob set; 0 literals, 0 basenames, 0 extensions, 0 prefixes, 0 suffixes, 0 required extensions, 2 regexes
12-
DEBUG: Loading snippets from `[CWD]/_includes`
13-
DEBUG: Copying `[CWD]/some.js` to `[CWD]/_dest/some.js`
14-
DEBUG: Copying `[CWD]/style/blog.css` to `[CWD]/_dest/style/blog.css`
12+
DEBUG: Loading snippets from `./_includes`
13+
DEBUG: Copying `./some.js` to `[CWD]/_dest/some.js`
14+
DEBUG: Copying `./style/blog.css` to `[CWD]/_dest/style/blog.css`
1515
Build successful
1616

1717
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
syntax_highlight:
2+
enabled: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>test</title>
5+
</head>
6+
<body>
7+
<h1>{{ page.permalink }}</h1>
8+
9+
{{ page.content }}
10+
</body>
11+
</html>
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>My blog - {{ page.permalink }}</title>
5+
</head>
6+
<body>
7+
{{ page.content }}
8+
</body>
9+
</html>
10+
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
layout: default.liquid
3+
---
4+
This is my Index page!
5+
6+
{% for post in collections.posts.pages %}
7+
<a href="{{post.permalink}}">{{ post.title }}</a>
8+
{% endfor %}

tests/cmd/custom_config.in/no-post.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
layout: posts.liquid
3+
4+
title: Custom paths are available to non-posts
5+
published_date: 2014-08-22 15:36:20 +0100
6+
permalink: /test/hello.html
7+
---
8+
# {{ page.title }}
9+
10+
This asserts that you can specify custom file paths
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
layout: posts.liquid
3+
4+
title: All date variables
5+
published_date: 2015-05-03 01:05:20 +0100
6+
permalink: /{{year}}/{{month}}/{{i_month}}/{{day}}/{{i_day}}/{{hour}}/{{minute}}/{{second}}/
7+
---
8+
# {{ page.title }}
9+
10+
This checks that all date variables are interpolated correctly into the path.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
layout: posts.liquid
3+
4+
title: Custom paths with leading slash
5+
published_date: 2015-05-03 02:05:20 +0100
6+
permalink: /test/thing2.html
7+
---
8+
# {{ page.title }}
9+
10+
This asserts that you can specify custom file paths with a leading slash and it doesn't make a difference.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
layout: posts.liquid
3+
4+
title: Custom paths
5+
published_date: 2015-05-03 03:05:20 +0100
6+
permalink: /test/thing.html
7+
---
8+
# {{ page.title }}
9+
10+
This asserts that you can specify custom file paths
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
layout: posts.liquid
3+
4+
title: Date variables
5+
published_date: 2015-05-03 04:05:20 +0100
6+
permalink: /{{year}}/{{data.thing}}/
7+
data:
8+
thing: hello
9+
---
10+
# {{ page.title }}
11+
12+
This asserts interpolation of date variables and other variables.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
layout: posts.liquid
3+
4+
title: Boom without trailing slash
5+
published_date: 2015-05-03 05:05:20 +0100
6+
permalink: /test/thing4
7+
---
8+
# {{ page.title }}
9+
10+
This asserts that custom paths without a file extension get made into a folder with an index.html file, even when the user did not specify a trailing slash.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
layout: posts.liquid
3+
4+
title: Boom
5+
published_date: 2015-05-03 06:05:20 +0100
6+
permalink: /test/thing3/
7+
---
8+
# {{ page.title }}
9+
10+
This asserts that custom paths without a file extension get made into a folder with an index.html file.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
layout: posts.liquid
3+
4+
title: Custom paths
5+
published_date: 2015-05-03 07:05:20 +0100
6+
---
7+
# {{ page.title }}
8+
9+
This asserts that you can have normal and custom file paths side by side.

0 commit comments

Comments
 (0)