Skip to content
Open
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ grass = { version = "0.10.8", default-features = false, features = ["random"] }
tokio = { version = "1.0.1", default-features = false, features = ["rt", "fs", "full"] }
warp = "0.3"
notify = "4"
percent-encoding = "2.1.0"

37 changes: 35 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
errors::{FirnError, FirnErrorType},
org::{self, OrgMetadata},
sitemap::SitemapTree,
templates::{self},
templates::{
data,
Expand Down Expand Up @@ -71,7 +72,7 @@ impl BaseUrl {
Component::Normal(f) => {
link_tail.push(f);
}
_ => ()
_ => (),
}
}
link_res.push(parent_dirs);
Expand Down Expand Up @@ -104,6 +105,7 @@ pub struct Config<'a> {
// Data specifically for templates / user interaction:
pub user_config: UserConfig,
pub sitemap: Vec<LinkData>,
pub sitemap_tree: SitemapTree,
pub sitemap_mru: Vec<LinkData>,
pub sitemap_mrp: Vec<LinkData>,
pub tag_page: PathBuf,
Expand Down Expand Up @@ -163,6 +165,7 @@ impl<'a> Config<'a> {
tags_map: HashMap::new(),
serve_port: 8080,
sitemap: Vec::new(),
sitemap_tree: SitemapTree::new(),
sitemap_mru: Vec::new(),
sitemap_mrp: Vec::new(),
paths_org_files: Vec::new(),
Expand Down Expand Up @@ -366,10 +369,12 @@ impl<'a> Config<'a> {
let mut out: Vec<LinkData> = Vec::new();
for v in self.global_sitemap.values() {
if let org::OrgMetadataType::Sitemap(_fm) = &v.entity {
let web_path = util::path_to_string(&v.originating_file_web_path);
// https://url.spec.whatwg.org/#path-percent-encode-set
let sitemap_item_url = format!(
"{}/{}",
self.user_config.site.url,
util::path_to_string(&v.originating_file_web_path)
util::percent_encode(&web_path)
);
let x = LinkData::new(
sitemap_item_url,
Expand All @@ -382,6 +387,33 @@ impl<'a> Config<'a> {
}
out.sort_by_key(|ld| ld.file.clone());
self.sitemap = out;

let mut values: Vec<&OrgMetadata> = self
.global_sitemap
.values()
.filter_map(|v| match &v.entity {
org::OrgMetadataType::Sitemap(_fm) => Some(v),
_ => None,
})
.collect();
values.sort_by_key(|v| v.originating_file_web_path.clone());
for v in values {
let web_path = util::path_to_string(&v.originating_file_web_path);
// https://url.spec.whatwg.org/#path-percent-encode-set
let sitemap_item_url = format!(
"{}/{}",
self.user_config.site.url,
util::percent_encode(&web_path)
);
let x = LinkData::new(
sitemap_item_url,
v.originating_file.clone(),
LinkMeta::Sitemap,
Some(v.front_matter.clone()),
);
self.sitemap_tree
.insert(v.originating_file_web_path.clone(), x);
}
}

/// render - iterates over all org files and call their render function.
Expand Down Expand Up @@ -430,6 +462,7 @@ impl<'a> Config<'a> {
ctx.insert("title", &tag_name);
ctx.insert("tags", &self.tags_list);
ctx.insert("sitemap", &self.sitemap);
ctx.insert("nodes", &self.sitemap_tree.nodes);
ctx.insert("config", &self.user_config);

let tag_file_name = format!("{}.html", tag_name);
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod serve;
pub mod templates;
pub mod user_config;
pub mod util;
pub mod sitemap;

use std::env;
use std::path::PathBuf;
Expand Down
54 changes: 50 additions & 4 deletions src/new_site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ tags:

# Set `firn` to true if you want to create a [tag].html page for every *firn_tag* front matter.
firn: true

sitemap:
# The layout of the sitemap. Use "tree" if you would like the directories
# where the org files are to be shown on the sitemap.
# Currently, any values other than "tree" render the original "flat" layout.
layout: "flat"
"#;

const TAG_TEMPLATE: &str = r#"{% import "macros.html" as macros %}
Expand Down Expand Up @@ -124,6 +130,32 @@ their #+date_created and #+date_updated frontmatter. #}
{% endif %}
"#;

const PARTIAL_SITEMAP_TREE: &str = r#" {# This partial is used to render the sitemap #}
{% macro sitemap_tree(u, nodes) %}
{% if nodes[u] %}
{% if nodes[u].first_child %}
<details open>
{% if u != 0 %}
<summary>{{ nodes[u].path_comp }}</summary>
{% else %}
<summary>Sitemap</summary>
{% endif %}
<ul>
<li>{{ self::sitemap_tree(u=nodes[u].first_child, nodes=nodes) }}</li>
</ul>
</details>
{% elif nodes[u].data %}
<a href={{ nodes[u].data.path }}>{{ nodes[u].data.file }}
{% if nodes[u].data.kind.Tag %} ({{nodes[u].data.kind.Tag}}) {% endif %}
</a>
{% endif %}
{% if nodes[u].next_sibling %}
<li>{{ self::sitemap_tree(u=nodes[u].next_sibling, nodes=nodes) }}</li>
{% endif %}
{% endif %}
{% endmacro sitemap_tree %}
"#;

const MACROS: &str = r#" {# the link_list macro is used to render a generic list of links (backlinks, sitemap, tags etc.) #}
{% macro link_list(title, list_items) %}
{% if list_items | length > 0 %}
Expand All @@ -143,22 +175,25 @@ const MACROS: &str = r#" {# the link_list macro is used to render a generic list
</section>
{% endif %}
{% endmacro input %}

"#;

const DEFAULT_HTML: &str = r#"{% import "macros.html" as macros %}
{% import "sitemap_tree.html" as sitemap_tree %}
<html>
{% include "partials/head.html" %}
<body style="display: flex;">
<main style="width: 600px; margin: 0 auto; padding: 32px;">
{{render()}}
</main>

<aside style="padding: 32px; width: 300px;">
<section>{{toc()}}</section>
{% if sitemap_layout == "tree" %}
<section>{{sitemap_tree::sitemap_tree(u=0, nodes=nodes)}}</section>
{% else %}
{{macros::link_list(title="Sitemap", list_items=sitemap)}}
{% endif %}
{{macros::link_list(title="Backlinks", list_items=backlinks)}}
{{macros::link_list(title="Related", list_items=related)}}
{{macros::link_list(title="Sitemap", list_items=sitemap)}}
{{macros::link_list(title="Tags", list_items=tags)}}
</aside>
</body>
Expand Down Expand Up @@ -189,11 +224,22 @@ pub fn init(cwd: PathBuf) {
if fs::metadata(&dir_firn).is_ok() {
println!("A '_firn' site already exists at this directory.")
} else {
let dirs = vec!["layouts/", "layouts/partials", "sass", "static/css", "static/js", "_site"];
let dirs = vec![
"layouts/",
"layouts/partials",
"sass",
"static/css",
"static/js",
"_site",
];

let mut files = HashMap::new();
files.insert(String::from("layouts/partials/head.html"), PARTIAL_HEAD);
files.insert(String::from("layouts/macros.html"), MACROS);
files.insert(
String::from("layouts/sitemap_tree.html"),
PARTIAL_SITEMAP_TREE,
);
files.insert(String::from("layouts/partials/recent.html"), PARTIAL_RECENT);
files.insert(String::from("static/js/main.js"), DEFAULT_JS);
files.insert(String::from("sass/main.scss"), DEFAULT_SCSS);
Expand Down
38 changes: 19 additions & 19 deletions src/org.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use orgize::{elements, Element, Event, Org};
use serde::Serialize;
use slugify::slugify;
use std::fs;
use std::path::{PathBuf, Path};
use std::path::{Path, PathBuf};
use tera::Context;

#[derive(Debug, PartialEq, Eq, Serialize, Clone)]
Expand Down Expand Up @@ -53,7 +53,8 @@ impl<'a> OrgMetadata<'a> {
let file_title = front_matter.title.clone();
let originating_file_web_path = web_path.to_path_buf();
let originating_file_path = file_path.to_path_buf();
let originating_file = file_title.unwrap_or_else(|| util::path_to_string(&originating_file_path));
let originating_file =
file_title.unwrap_or_else(|| util::path_to_string(&originating_file_path));
// TODO: write a function that cleans headlines - removes links, etc etc
// etc - from raw. Headline stuff - not every metadata has an associated
// headline necessarily (ie, links in an org doc before a headline
Expand Down Expand Up @@ -256,22 +257,14 @@ impl<'a> OrgFile<'a> {
pub fn valid_for_rendering(&self) -> Result<bool, FirnError> {
if self.front_matter.title.is_none() {
return Err(FirnError::new(
&format!(
"{} {}",
"No title found for: ",
self.file_path.display()
),
&format!("{} {}", "No title found for: ", self.file_path.display()),
FirnErrorType::FrontMatterNoTitle,
));
}

if self.front_matter.is_private() {
return Err(FirnError::new(
&format!(
"{} {}",
"Private file: ",
self.file_path.display()
),
&format!("{} {}", "Private file: ", self.file_path.display()),
FirnErrorType::IsPrivateFile,
));
}
Expand Down Expand Up @@ -322,17 +315,19 @@ impl<'a> OrgFile<'a> {
// if any global tags match any of the tags of this file, include 'em.
if let OrgMetadataType::Tag(global_tag, _) = &g_tag.entity {
for local_tag in &self.tags {
if let OrgMetadataType::Tag(local_tag_name, local_tag_type) = &local_tag.entity {
if let OrgMetadataType::Tag(local_tag_name, local_tag_type) = &local_tag.entity
{
let related_item_url = format!(
"{}/{}",
cfg.user_config.site.url,
util::path_to_string(&g_tag.originating_file_web_path)
util::percent_encode(&util::path_to_string(
&g_tag.originating_file_web_path
))
);

if let OrgTagType::FirnTag = local_tag_type {
if local_tag_name == global_tag
&& local_tag.originating_file_path
!= g_tag.originating_file_path
&& local_tag.originating_file_path != g_tag.originating_file_path
{
let new_link = templates::links::LinkData::new(
related_item_url,
Expand Down Expand Up @@ -360,13 +355,16 @@ impl<'a> OrgFile<'a> {
// if the weblink matches self's web_path it's a match.
if let OrgMetadataType::Link(link) = &g_link.entity {
let new_link_path = link.path.to_owned().to_string();
let web_link =
util::transform_org_link_to_html(cfg.base_url.clone(), new_link_path, self.file_path.clone());
let web_link = util::transform_org_link_to_html(
cfg.base_url.clone(),
new_link_path,
self.file_path.clone(),
);

let backlink_item_url = format!(
"{}/{}",
cfg.user_config.site.url,
util::path_to_string(&g_link.originating_file_web_path)
util::percent_encode(&util::path_to_string(&g_link.originating_file_web_path))
);

if web_link == self.full_url {
Expand Down Expand Up @@ -394,8 +392,10 @@ impl<'a> OrgFile<'a> {
ctx.insert("related", &self.get_related_files(cfg));
ctx.insert("logbook", &logbook_sum.num_hours());
ctx.insert("sitemap", &cfg.sitemap);
ctx.insert("sitemap_layout", &cfg.user_config.sitemap.layout);
ctx.insert("config", &cfg.user_config);
ctx.insert("tags", &cfg.tags_list);
ctx.insert("nodes", &cfg.sitemap_tree.nodes);
}

/// render spits out html to disk.
Expand Down
Loading