Add default transition javascript

This commit is contained in:
Sofia 2018-04-19 22:21:49 +03:00
parent dd9d500f9d
commit bd0aae2e70
15 changed files with 167 additions and 43 deletions

View File

@ -4,25 +4,41 @@ built_pages = ["test_page/about.toml", "test_page/other.toml"]
use_default_css = true use_default_css = true
javascript = [] javascript = []
css = [] css = []
#before_navbar_url = "/navbar.html" #before_navbar_url = "test_page/resources/before_navbar.html"
#before_content_url = "/before_content.html" #before_content_url = "test_page/resources/before_content.html"
#after_content_url = "/after_content.html" #after_content_url = "test_page/resources/after_content.html"
#favicon = "favicon.png" #favicon = "favicon.png"
#twitter_author = "@teascade"
#charset = "utf-8"
#mobile_viewport = true
#meta_description = true
#meta_og = true
#output = "public"
#[google]
#google_robots = "all" #google_robots = "all"
#google_site_verification = "" #google_site_verification = "some_verification"
[navbar] #[twitter]
items = ["about", "other", "google"] #twitter_site = "@teascade"
#twitter_creator = "@teascade"
[navbar.item.about] #[navbar]
title = "About" #items = ["about", "other", "google"]
link = "/" #
# [navbar.item.about]
# title = "About"
# link = "/"
[navbar.item.other] # [navbar.item.other]
title = "Other" # title = "Other"
link = "/other" # link = "/other"
[navbar.item.google] # [navbar.item.google]
title = "Google!" # title = "Google!"
link = "https://google.com" # link = "https://google.com"
#[resource.test]
#source = "test_page/resources/copied_resources/"
#destination = ""

View File

@ -2,12 +2,13 @@
website_name = "Test Website Name!" website_name = "Test Website Name!"
built_pages = ["test_page/about.toml", "test_page/other.toml"] built_pages = ["test_page/about.toml", "test_page/other.toml"]
use_default_css = true use_default_css = true
javascript = [] use_default_js = true
css = [] javascript = ["test.js"]
css = ["test.css"]
before_navbar_url = "test_page/resources/before_navbar.html" before_navbar_url = "test_page/resources/before_navbar.html"
before_content_url = "test_page/resources/before_content.html" before_content_url = "test_page/resources/before_content.html"
after_content_url = "test_page/resources/after_content.html" after_content_url = "test_page/resources/after_content.html"
favicon = "favicon.png" favicon = "/img/global.png"
charset = "utf-8" charset = "utf-8"
mobile_viewport = true mobile_viewport = true
@ -40,5 +41,5 @@ title = "Google!"
link = "https://google.com" link = "https://google.com"
[resource.test] [resource.test]
source = "test_page/resources/copied_resources/" source = "test_page/resources/img"
destination = "" destination = "img"

View File

@ -15,6 +15,7 @@ use options::{BuildOpt, Opt};
use file_writer; use file_writer;
const DEFAULT_CSS: &'static str = include_str!("templates/default-css.css"); const DEFAULT_CSS: &'static str = include_str!("templates/default-css.css");
const DEFAULT_JS: &'static str = include_str!("templates/default-js.js");
const PAGE_TEMPLATE: &'static str = include_str!("templates/page-template.html"); const PAGE_TEMPLATE: &'static str = include_str!("templates/page-template.html");
const NAVBAR_ITEM: &'static str = include_str!("templates/navbar/item-template.html"); const NAVBAR_ITEM: &'static str = include_str!("templates/navbar/item-template.html");
const NAVBAR_IMAGE_ITEM: &'static str = include_str!("templates/navbar/image-item-template.html"); const NAVBAR_IMAGE_ITEM: &'static str = include_str!("templates/navbar/image-item-template.html");
@ -46,6 +47,20 @@ pub fn build(logger: &Logger, opt: &Opt, _: &BuildOpt) -> Result<(), Error> {
file_writer::write_file(&css_path, DEFAULT_CSS, opt.overwrite)?; file_writer::write_file(&css_path, DEFAULT_CSS, opt.overwrite)?;
} }
if config.global_config.website.use_default_js {
let js_path = file_writer::add_to_beginning(
PathBuf::from("js/default.js"),
config
.clone()
.global_config
.website
.output
.unwrap_or("public".to_owned()),
);
logger.log(LogLevel::DETAIL, format!("Adding {:?}", js_path));
file_writer::write_file(&js_path, DEFAULT_JS, opt.overwrite)?;
}
logger.log(LogLevel::INFO, "Generating page templates"); logger.log(LogLevel::INFO, "Generating page templates");
let page_template = Template::new(PAGE_TEMPLATE); let page_template = Template::new(PAGE_TEMPLATE);
let css_tag_template = Template::new(CSS_TAG); let css_tag_template = Template::new(CSS_TAG);
@ -207,15 +222,18 @@ fn write_recursive_resource(
} else { } else {
match File::open(resource_path.clone()) { match File::open(resource_path.clone()) {
Ok(mut read_file) => { Ok(mut read_file) => {
let mut contents = String::new(); let bytes: Vec<u8> = match read_file.bytes().collect() {
read_file.read_to_string(&mut contents)?; Ok(bytes) => bytes,
Err(_) => Vec::new(),
};
let mut dest_path = destination.clone(); let mut dest_path = destination.clone();
if source.is_dir() { if source.is_dir() {
dest_path = dest_path.join(relative.clone()); dest_path = dest_path.join(relative.clone());
dest_path.set_file_name(resource_path.file_name().unwrap()); dest_path.set_file_name(resource_path.file_name().unwrap());
} }
file_writer::write_file(&dest_path, contents, true)?; file_writer::write_bytes(&dest_path, &bytes, true)?;
Ok(()) Ok(())
} }

View File

@ -30,6 +30,7 @@ pub struct WebsiteConfig {
pub website_name: String, pub website_name: String,
pub built_pages: Vec<String>, pub built_pages: Vec<String>,
pub use_default_css: bool, pub use_default_css: bool,
pub use_default_js: bool,
pub javascript: Vec<String>, pub javascript: Vec<String>,
pub css: Vec<String>, pub css: Vec<String>,

View File

@ -42,6 +42,10 @@ pub fn write_file<T: Into<String>>(
content: T, content: T,
overwrite: bool, overwrite: bool,
) -> Result<(), Error> { ) -> Result<(), Error> {
write_bytes(path, content.into().as_bytes(), overwrite)
}
pub fn write_bytes(path: &PathBuf, bytes: &[u8], overwrite: bool) -> Result<(), Error> {
if let Some(parent) = path.clone().parent() { if let Some(parent) = path.clone().parent() {
create_dir_all(parent.clone())?; create_dir_all(parent.clone())?;
} else { } else {
@ -51,7 +55,6 @@ pub fn write_file<T: Into<String>>(
)); ));
} }
let content = content.into();
if path.exists() && !overwrite { if path.exists() && !overwrite {
Err(Error::new( Err(Error::new(
LogLevel::SEVERE, LogLevel::SEVERE,
@ -62,7 +65,7 @@ pub fn write_file<T: Into<String>>(
)) ))
} else { } else {
match File::create(path) { match File::create(path) {
Ok(mut file) => Ok(file.write_all(content.as_bytes()).unwrap()), Ok(mut file) => Ok(file.write_all(bytes).unwrap()),
Err(err) => Err(Error::from(err)), Err(err) => Err(Error::from(err)),
} }
} }

View File

@ -30,6 +30,7 @@ use logger::{LogLevel, Logger};
use options::{Opt, Subcommands}; use options::{Opt, Subcommands};
use config_toml::{GlobalConfigToml, NavbarConfig, NavbarItem, WebsiteConfig}; use config_toml::{GlobalConfigToml, NavbarConfig, NavbarItem, WebsiteConfig};
//TODO: Custom markdown
fn main() { fn main() {
exit(match run() { exit(match run() {
Ok(()) => 0, Ok(()) => 0,
@ -102,6 +103,7 @@ fn run() -> Result<(), ()> {
website_name: init_opt.name, website_name: init_opt.name,
built_pages: Vec::new(), built_pages: Vec::new(),
use_default_css: true, use_default_css: true,
use_default_js: true,
javascript: Vec::new(), javascript: Vec::new(),
css: Vec::new(), css: Vec::new(),
favicon: init_opt.favicon, favicon: init_opt.favicon,

View File

@ -125,6 +125,7 @@ impl Template {
"javascript_links".to_owned() => js_tags, "javascript_links".to_owned() => js_tags,
"use_default_css".to_owned() => config.global_config.website.use_default_css.to_string(), "use_default_css".to_owned() => config.global_config.website.use_default_css.to_string(),
"use_default_js".to_owned() => config.global_config.website.use_default_js.to_string(),
"navbar".to_owned() => config.global_config.navbar.is_some().to_string(), "navbar".to_owned() => config.global_config.navbar.is_some().to_string(),
"navbar_content".to_owned() => navbar_content, "navbar_content".to_owned() => navbar_content,

View File

@ -0,0 +1,81 @@
var _default_js_pageloader = {
begin: (url) => { console.log("Started to load " + url); },
progress: (url, current, total) => { console.log("Progress of " + url + ": " + current + "/" + current + " (" + ((current / total) * 100) + "%)"); },
loaded: (url) => { console.log("Loaded " + url); }
}
function _default_js_main() {
let elements = document.getElementsByTagName("a");
for (element of elements) {
if (element.hostname == window.location.hostname) {
element.onclick = _default_js_onclick;
}
}
window.onpopstate = function () {
var page = window.location
_default_js_open_page(page, false);
};
}
function _default_js_onclick(event) {
event.preventDefault();
let url = event.target.href;
_default_js_open_page(url, true);
}
function _default_js_open_page(url, no_pop_state) {
let request = new XMLHttpRequest();
request.addEventListener("loadstart", (progress) => {
_default_js_pageloader.begin(url);
});
request.addEventListener("progress", (progress) => {
_default_js_pageloader.progress(url, progress.loaded, progress.total);
});
request.addEventListener("loadend", (progress) => {
_default_js_pageloader.loaded(url);
let parser = new DOMParser();
let new_document = parser.parseFromString(request.responseText, "text/html");
change_page(new_document, url, no_pop_state);
});
request.open("GET", url);
request.send();
}
function change_page(new_document, new_url, no_pop_state) {
// Some meta tags
document.title = new_document.title;
let new_favicon = new_document.querySelector("link[rel*='icon']").href;
document.querySelector("link[rel*='icon']").href = new_favicon;
// Replace css/js tags
let list = document.getElementsByClassName("._default_js_meta");
for (i in list) {
let element = list[0];
if (element !== undefined) {
element.parentNode.removeChild(element);
}
}
let new_list = new_document.getElementsByClassName("._default_js_meta");
for (i in new_list) {
let element = new_list[0];
if (element !== undefined) {
document.head.appendChild(element);
}
}
// Actually replace the content
document.body = new_document.body;
_default_js_main();
// Do history modifications
if (no_pop_state) {
window.history.pushState(new_url, new_url, new_url);
}
}

View File

@ -1 +1 @@
<link rel="stylesheet" href="{{link}}"> <link class="._default_js_meta" rel="stylesheet" href="{{link}}">

View File

@ -1 +1 @@
<script type="text/javascript" src="{{link}}"></script> <script class="._default_js_meta" type="text/javascript" src="{{link}}"></script>

View File

@ -22,7 +22,8 @@
<meta name="twitter:creator" content="{{twitter_creator}}"> <meta name="twitter:creator" content="{{twitter_creator}}">
<meta name="twitter:title" content="{{page_title}}"> <meta name="twitter:title" content="{{page_title}}">
<meta name="twitter:description" content="{{page_description}}"> <meta name="twitter:description" content="{{page_description}}">
<meta name="twitter:image" content="{{favicon}}"> {{endif}} {{if use_default_css}} <meta name="twitter:image" content="{{favicon}}"> {{endif}} {{if use_default_js}}
<script type="text/javascript" src="/js/default.js"></script> {{endif}} {{if use_default_css}}
<link rel="stylesheet" href="/css/default.css"> {{endif}} {{css_links}}{{javascript_links}} <link rel="stylesheet" href="/css/default.css"> {{endif}} {{css_links}}{{javascript_links}}
<link rel="shortcut icon" href="{{favicon}}"></link> <link rel="shortcut icon" href="{{favicon}}"></link>
@ -30,7 +31,7 @@
<title>{{page_title}}</title> <title>{{page_title}}</title>
</head> </head>
<body> <body {{if use_default_js}} onload="_default_js_main()" {{endif}}>
{{before_navbar}} {{if navbar}} {{before_navbar}} {{if navbar}}
<nav> <nav>
<ul> <ul>

View File

@ -3,7 +3,7 @@ html_path = "index.html"
title = "About" title = "About"
description = "This is the test About page" description = "This is the test About page"
content_path = "about.md" content_path = "about.md"
favicon = "/favicon.png" #favicon = "/favicon.png"
#before_navbar_url = "/navbar.html" #before_navbar_url = "/navbar.html"
#before_content_url = "/before_content.html" #before_content_url = "/before_content.html"
#after_content_url = "/after_content.html" #after_content_url = "/after_content.html"

View File

@ -3,9 +3,9 @@ html_path = "other/index.html"
title = "Other!" title = "Other!"
description = "This is the .. other page" description = "This is the .. other page"
content_path = "other.md" content_path = "other.md"
#favicon = "optional_favicon.png" favicon = "/img/square_trans.png"
#before_navbar_url = "/navbar.html" #before_navbar_url = "/navbar.html"
before_content_url = "test_page/resources/before_content_other.html" before_content_url = "test_page/resources/before_content_other.html"
#after_content_url = "/after_content.html" #after_content_url = "/after_content.html"
#javascript = ["Additional JavaScript"] javascript = ["test_additional.js"]
#css = ["Additional CSS"] css = ["test_additional.css"]

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 347 B