Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
i am not hidden, but my parent directory is
1 change: 1 addition & 0 deletions samples/hidden-content/_partially-hidden-file.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
i am prefixed with _
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1: {{get [/]._partially-hidden-file}}
2: {{get [/].[_partially-hidden-directory/].non-hidden-file}}
3: {{get [/].[non-hidden-directory/]._partially-hidden-file}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
i am prefixed with _, but my parent directory is not prefixed with _
6 changes: 3 additions & 3 deletions samples/realistic-advanced/error-handler.html.hbs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{{#>layout.html.hbs page-title="Error"}}
{{#>_layout.html.hbs page-title="Error"}}
<header>
<h1>Error</h1>
</header>

<nav>{{get "navigation"}}</nav>
<nav>{{get "/_navigation"}}</nav>

<main>
<p>
Expand Down Expand Up @@ -42,4 +42,4 @@
{{#if (eq error-code 505)}}HTTP Version Not Supported{{/if}}
</p>
</main>
{{/layout.html.hbs}}
{{/_layout.html.hbs}}
6 changes: 3 additions & 3 deletions samples/realistic-advanced/gallery.html.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{#>layout.html.hbs page-title="Image gallery"}}
{{#>_layout.html.hbs page-title="Image gallery"}}
<style>
#gallery img {
height: 50vh;
Expand All @@ -13,7 +13,7 @@
<h1>Image gallery</h1>
</header>

<nav>{{get "navigation"}}</nav>
<nav>{{get "/_navigation"}}</nav>

<figure id="gallery">
<img src="gallery/1" />
Expand All @@ -35,4 +35,4 @@
})
}
</script>
{{/layout.html.hbs}}
{{/_layout.html.hbs}}
6 changes: 3 additions & 3 deletions samples/realistic-advanced/home.html.hbs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{{#>layout.html.hbs}}
{{#>_layout.html.hbs}}
<header>
<h1>Welcome to my fancy website!</h1>
</header>

<nav>{{get "/navigation"}}</nav>
<nav>{{get "/_navigation"}}</nav>

<article>
<figure><img src="/dance" /></figure>
Expand All @@ -13,4 +13,4 @@
<footer>
The current <a href="/now">time</a> is {{get "/now"}}.
</footer>
{{/layout.html.hbs}}
{{/_layout.html.hbs}}
8 changes: 4 additions & 4 deletions samples/realistic-advanced/lottery.html.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</ul>
{{/inline}}

{{#>layout.html.hbs page-title="Play The Lottery"}}
{{#>_layout.html.hbs page-title="Play The Lottery"}}
<style>
@keyframes face {
from {
Expand Down Expand Up @@ -41,10 +41,10 @@
<h1>Play the lottery</h1>
</header>

<nav>{{get "/navigation"}}</nav>
<nav>{{get "/_navigation"}}</nav>

<main>
{{#if (get "/play-lottery")}}
{{#if (get "/_play-lottery")}}
{{>smiles}}
<h2>You won!</h2>
{{>smiles}}
Expand All @@ -56,4 +56,4 @@
</main>

<footer>Reload this page to play again.</footer>
{{/layout.html.hbs}}
{{/_layout.html.hbs}}
11 changes: 7 additions & 4 deletions src/content/content_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use super::*;
use handlebars::{self, Handlebars};
use mime_guess::MimeGuess;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::io;
use std::path::Path;
use std::sync::{Arc, RwLock};
Expand Down Expand Up @@ -71,6 +70,8 @@ where
media_type: MediaType,
) -> Result<UnregisteredTemplate, TemplateParseError>;

fn get_internal(&self, route: &Route) -> Option<&ContentRepresentations>;

fn get(&self, route: &Route) -> Option<&ContentRepresentations>;

fn handlebars_registry(&self) -> &Handlebars;
Expand Down Expand Up @@ -370,9 +371,7 @@ where
F: FnOnce() -> RegisteredContent,
{
content_index.try_add(route.clone())?;
let representations = content_registry
.entry(route.clone())
.or_insert_with(HashMap::new);
let representations = content_registry.entry_or_insert_default(route.clone());

match representations.entry(media_type) {
Entry::Occupied(entry) => {
Expand Down Expand Up @@ -417,6 +416,10 @@ where
self.content_registry.get(route)
}

fn get_internal(&self, route: &Route) -> Option<&ContentRepresentations> {
self.content_registry.get_internal(route)
}

fn handlebars_registry(&self) -> &Handlebars {
&self.handlebars_registry
}
Expand Down
26 changes: 25 additions & 1 deletion src/content/content_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,31 @@ use super::content_item::*;
use super::*;
use std::collections::HashMap;

pub type ContentRegistry = HashMap<Route, ContentRepresentations>;
pub struct ContentRegistry(HashMap<Route, ContentRepresentations>);
impl ContentRegistry {
pub fn new() -> Self {
ContentRegistry(HashMap::new())
}

/// Routes that begin with underscore are ignored for external requests
/// (they always 404).
pub fn get(&self, route: &Route) -> Option<&ContentRepresentations> {
if route.as_ref().contains("/_") {
None
} else {
self.get_internal(route)
}
}

pub fn get_internal(&self, route: &Route) -> Option<&ContentRepresentations> {
self.0.get(route)
}

pub fn entry_or_insert_default(&mut self, key: Route) -> &mut ContentRepresentations {
self.0.entry(key).or_insert_with(HashMap::default)
}
}

pub type ContentRepresentations = HashMap<MediaType, RegisteredContent>;

/// A renderable item from the content directory.
Expand Down
2 changes: 1 addition & 1 deletion src/content/handlebars_helpers/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ where
))
})?;

let content_item = content_engine.get(&route).ok_or_else(|| {
let content_item = content_engine.get_internal(&route).ok_or_else(|| {
handlebars::RenderError::new(format!(
"No content found at route passed to `get` helper (\"{}\").",
route,
Expand Down
3 changes: 3 additions & 0 deletions src/content/test_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ impl<'a> ContentEngine<()> for MockContentEngine<'a> {
fn get(&self, _: &Route) -> Option<&ContentRepresentations> {
None
}
fn get_internal(&self, _: &Route) -> Option<&ContentRepresentations> {
None
}
fn handlebars_registry(&self) -> &Handlebars {
&self.0
}
Expand Down
5 changes: 4 additions & 1 deletion tests/snapshots/integration_tests__hidden-content.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ source: tests/integration_tests.rs
expression: contents
input_file: samples/hidden-content
---
{}
_partially-hidden-directory/non-hidden-file.html: "Content not found at route '/_partially-hidden-directory/non-hidden-file'.\n"
_partially-hidden-file.html: "Content not found at route '/_partially-hidden-file'.\n"
includes-partially-hidden-files.html.hbs: "1: i am prefixed with _\n\n2: i am not hidden, but my parent directory is\n\n3: i am prefixed with _, but my parent directory is not prefixed with _\n\n"
non-hidden-directory/_partially-hidden-file.html: "Content not found at route '/non-hidden-directory/_partially-hidden-file'.\n"