Skip to content

Commit

Permalink
misc: handling static and custom static overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
rochacbruno committed Oct 22, 2024
1 parent cbfd967 commit d751493
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,10 @@ Other options are available and can be viewed on [repository](https://github.com
## Theme customization
The embedded templates are created with [picocss.com](https://picocss.com/) and
it is easy to customize, just put a `style.css` in the same folder where the markdown
files are located and use anything that pico supports or just be creative with css.
it is easy to customize, just put a `custom.css` and a `custom.js`
in your root folder and use anything that pico supports or just be creative with css.

If customizing the css and js is not enough then you can create your own theme.

## Creating a new Theme

Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions example/static/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Customize me */
4 changes: 4 additions & 0 deletions example/static/marmite.css
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,7 @@ article footer {
justify-content: space-between;
align-items: center;
}

.spoiler, .spoiler > * { transition: color 0.5s, opacity 0.5s }
.spoiler:not(:hover) { color: transparent;background-color: #808080}
.spoiler:not(:hover) > * { opacity: 0 }
90 changes: 90 additions & 0 deletions example/static/marmite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Theme switcher - light/dark
const themeSwitcher = {
// Config
_scheme: "auto",
toggleButton: document.getElementById("theme-toggle"),
rootAttribute: "data-theme",
localStorageKey: "picoPreferredColorScheme",

// Init
init() {
this.scheme = this.schemeFromLocalStorage;
this.initToggle();
this.updateIcon();
},

// Get color scheme from local storage
get schemeFromLocalStorage() {
return window.localStorage?.getItem(this.localStorageKey) ?? this._scheme;
},

// Preferred color scheme
get preferredColorScheme() {
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
},

// Init toggle
initToggle() {
this.toggleButton.addEventListener(
"click",
(event) => {
event.preventDefault();
// Toggle scheme
this.scheme = this.scheme === "dark" ? "light" : "dark";
this.updateIcon();
},
false
);
},

// Set scheme
set scheme(scheme) {
if (scheme == "auto") {
this._scheme = this.preferredColorScheme;
} else if (scheme == "dark" || scheme == "light") {
this._scheme = scheme;
}
this.applyScheme();
this.schemeToLocalStorage();
},

// Get scheme
get scheme() {
return this._scheme;
},

// Apply scheme
applyScheme() {
document.querySelector("html")?.setAttribute(this.rootAttribute, this.scheme);
// staticBase is defined on content.html template
document.querySelector("#highlightjs-theme")?.setAttribute("href", `${staticBase}/github-${this.scheme}.min.css`);
},

// Store scheme to local storage
schemeToLocalStorage() {
window.localStorage?.setItem(this.localStorageKey, this.scheme);
},

// Update icon based on the current scheme
updateIcon() {
if (this.scheme === "dark") {
this.toggleButton.innerHTML = "☼"; // Sun icon for light mode
} else {
this.toggleButton.innerHTML = "☽"; // Moon icon for dark mode
}
},
};

// Init
themeSwitcher.init();

// Menu

const menuToggle = document.getElementById('menu-toggle');
const headerMenu = document.getElementById('header-menu');

menuToggle.addEventListener('click', function () {
headerMenu.classList.toggle('active');
});


7 changes: 0 additions & 7 deletions example/static/menu.js

This file was deleted.

5 changes: 5 additions & 0 deletions example/static/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
User-agent: Mediapartners-Google
Disallow:

User-agent: *
Allow: /
84 changes: 0 additions & 84 deletions example/static/theme-switcher.js

This file was deleted.

6 changes: 3 additions & 3 deletions example/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
</style>
<link rel="stylesheet" type="text/css" href="{{url_for(path='/static/marmite.css')}}">
<link rel="stylesheet" type="text/css" href="{{url_for(path='/static/style.css')}}">
<link rel="stylesheet" type="text/css" href="{{url_for(path='/static/custom.css')}}">
{% endblock -%}
</head>

Expand Down Expand Up @@ -62,8 +62,8 @@ <h2><a href="{{url_for(path='/')}}" class="contrast">{{ site.name }}</a></h2>
</footer>
</main>
{%- block tail %}
<script src="{{url_for(path='/static/theme-switcher.js')}}"></script>
<script src="{{url_for(path='/static/menu.js')}}"></script>
<script src="{{url_for(path='/static/marmite.js')}}"></script>
<script src="{{url_for(path='/static/custom.js')}}"></script>
{% endblock -%}
</body>

Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ mod config;
mod content;
mod embedded;
mod markdown;
mod robots;
mod server;
mod site;
mod tera_functions;
Expand Down
25 changes: 0 additions & 25 deletions src/robots.rs

This file was deleted.

10 changes: 8 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io::Cursor;
use std::path::PathBuf;
use std::sync::Arc;
use std::{fs::File, path::Path};
use tiny_http::{Response, Server};
use tiny_http::{Header, Response, Server};

pub fn start(bind_address: &str, output_folder: &Arc<PathBuf>) {
let server = Server::http(bind_address).unwrap();
Expand All @@ -28,6 +28,7 @@ pub fn start(bind_address: &str, output_folder: &Arc<PathBuf>) {
}
}

#[allow(clippy::case_sensitive_file_extension_comparisons)]
fn handle_request(
request: &tiny_http::Request,
output_folder: &Path,
Expand All @@ -50,7 +51,12 @@ fn handle_request(
request_path,
request.http_version()
);
Ok(Response::from_data(buffer))
let mut resp = Response::from_data(buffer);
let js_header = Header::from_bytes("Content-Type", "text/javascript").unwrap();
if request_path.ends_with(".js") {
resp.add_header(js_header);
}
Ok(resp)
}
Err(err) => {
error!("Failed to read file {}: {}", file_path.display(), err);
Expand Down
Loading

0 comments on commit d751493

Please sign in to comment.