This commit is contained in:
Tyrel Souza 2023-07-03 00:37:36 -04:00
parent 657be940d8
commit 177c23da42
No known key found for this signature in database
GPG Key ID: F3614B02ACBE438E
3 changed files with 58 additions and 53 deletions

37
src/links/mod.rs Normal file
View File

@ -0,0 +1,37 @@
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct RustyLinks {
config: Config,
links: Vec<Links>,
pub metadata: Option<MetaData>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct MetaData {
pub last_updated: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
title: String,
name: String,
description: String,
avatar: String,
background: String,
background_opacity: f64,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Links {
title: String,
links: Vec<Link>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Link {
text: String,
icon: String,
href: Option<String>,
copy: Option<String>,
}

View File

@ -1,53 +1,15 @@
use std::fs; use std::{fs,io};
use std::io;
use std::path::Path; use std::path::Path;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use serde::{Serialize, Deserialize};
use serde_yaml::{self}; use serde_yaml::{self};
use tera::{Tera,Context}; use tera::{Tera,Context};
mod links;
#[derive(Serialize, Deserialize, Debug)] use links::{RustyLinks,MetaData};
pub struct RustyLinks {
config: Config,
links: Vec<Links>,
metadata: Option<MetaData>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct MetaData {
last_updated: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
title: String,
name: String,
description: String,
avatar: String,
background: String,
background_opacity: f64,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Links {
title: String,
links: Vec<Link>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Link {
text: String,
icon: String,
href: Option<String>,
copy: Option<String>,
}
pub fn copy_recursively(source: impl AsRef<Path>, destination: impl AsRef<Path>) -> io::Result<()> { pub fn copy_recursively(source: impl AsRef<Path>, destination: impl AsRef<Path>) -> io::Result<()> {
// https://nick.groenen.me/notes/recursively-copy-files-in-rust/ // Credit: https://nick.groenen.me/notes/recursively-copy-files-in-rust/
fs::create_dir_all(&destination)?; fs::create_dir_all(&destination)?;
for entry in fs::read_dir(source)? { for entry in fs::read_dir(source)? {
let entry = entry?; let entry = entry?;
@ -64,7 +26,10 @@ pub fn copy_recursively(source: impl AsRef<Path>, destination: impl AsRef<Path>)
fn load_links(file_name: &str) -> RustyLinks { fn load_links(file_name: &str) -> RustyLinks {
let links_yaml = std::fs::File::open(file_name).expect("Could not find file"); let links_yaml = std::fs::File::open(file_name).expect("Could not find file");
let mut rusty_links: RustyLinks = serde_yaml::from_reader(links_yaml).expect("Could not read values"); let mut rusty_links: RustyLinks = serde_yaml::from_reader(links_yaml).expect("Could not read values");
// Set last updated time to now.
let now : DateTime<Utc> = Utc::now(); let now : DateTime<Utc> = Utc::now();
let metadata : MetaData = MetaData { last_updated: now.to_rfc2822()}; let metadata : MetaData = MetaData { last_updated: now.to_rfc2822()};
rusty_links.metadata = Some(metadata); rusty_links.metadata = Some(metadata);
@ -73,7 +38,6 @@ fn load_links(file_name: &str) -> RustyLinks {
} }
fn render_links(rusty_links: RustyLinks) -> String { fn render_links(rusty_links: RustyLinks) -> String {
let context: Context = Context::from_serialize(&rusty_links).expect("Could not parse");
let tera = match Tera::new("templates/*.tera") { let tera = match Tera::new("templates/*.tera") {
Ok(t) => t, Ok(t) => t,
Err(e) => { Err(e) => {
@ -81,11 +45,15 @@ fn render_links(rusty_links: RustyLinks) -> String {
::std::process::exit(1); ::std::process::exit(1);
} }
}; };
let context: Context = Context::from_serialize(&rusty_links).expect("Could not parse");
tera.render("main.tera", &context).expect("Could not parse") tera.render("main.tera", &context).expect("Could not parse")
} }
fn write_file(html: String ) { fn write_file(html: String ) {
fs::remove_dir_all("./output").expect("could not remove directory"); if Path::new("./output").exists() {
fs::remove_dir_all("./output").expect("could not remove directory");
}
fs::create_dir("./output").expect("Could not create output directory"); fs::create_dir("./output").expect("Could not create output directory");
copy_recursively("./static", "./output").expect("Could not copy static directory"); copy_recursively("./static", "./output").expect("Could not copy static directory");
fs::write("./output/index.html", html).expect("Could not write to index.html"); fs::write("./output/index.html", html).expect("Could not write to index.html");

View File

@ -40,24 +40,24 @@
</div> </div>
<div class="links"> <div class="links">
{% for link in section.links %} {% for link in section.links %}
{% if 'href' in link and 'copy' in link %} {% if link.href and link.copy %}
<a class="link" href="{{link.href}}" target="_blank"> <a class="link" href="{{link.href}}" target="_blank">
<i class="{{link.icon}}"></i> {{link.text}} <i class="{{link.icon}}"></i> {{link.text}}
<div class="tooltip"> <div class="tooltip">
<span class="" href="#" onclick="copytext('{{link.copy}}','link._id')" onmouseout="outFunc('link._id')"> <span class="" href="#" onclick="copytext('{{link.copy}}','{{loop.index}}')" onmouseout="outFunc('{{loop.index}}')">
<span class="tooltiptext" id="link._id">Copy to clipboard</span> <span class="tooltiptext" id="{{loop.index}}">Copy to clipboard</span>
<i class="fa-solid fa-copy"></i> <i class="fa-solid fa-copy"></i>
</span> </span>
</div> </div>
</a> </a>
{% elif 'href' in link %} {% elif link.href %}
<a class="link" href="{{link.href}}" target="_blank"> <a class="link" href="{{link.href}}" target="_blank">
<i class="{{link.icon}}"></i> {{link.text}} <i class="{{link.icon}}"></i> {{link.text}}
</a> </a>
{% elif 'copy' in link %} {% elif link.copy %}
<div class="tooltip block"> <div class="tooltip block">
<a class="link" href="#" onclick="copytext('{{link.copy}}','link._id')" onmouseout="outFunc('link._id')"> <a class="link" href="#" onclick="copytext('{{link.copy}}','{{loop.index}}')" onmouseout="outFunc('{{loop.index}}')">
<span class="tooltiptext" id="link._id">Copy to clipboard</span> <span class="tooltiptext" id="{{loop.index}}">Copy to clipboard</span>
<i class="{{link.icon}}"></i> {{link.text}} <i class="{{link.icon}}"></i> {{link.text}}
</a> </a>
</div> </div>
@ -74,7 +74,7 @@
</body> </body>
<script> <script>
/*
function copytext(val, linkid) { function copytext(val, linkid) {
event.preventDefault(); event.preventDefault();
console.log(val); console.log(val);
@ -87,6 +87,6 @@
var tooltip = document.getElementById(linkid); var tooltip = document.getElementById(linkid);
tooltip.innerHTML = "Copy to clipboard"; tooltip.innerHTML = "Copy to clipboard";
} }
*/
</script> </script>
</html> </html>