From 0f4cd3ca200f8a35aab268756c17b6be3b5d05ad Mon Sep 17 00:00:00 2001 From: Maciej Jur Date: Thu, 2 May 2024 15:26:08 +0200 Subject: [PATCH] feat: link to file in footer --- .../posts/writing-code-considered-harmful.md | 65 ------------------- src/gen/load.rs | 6 +- src/gen/sack.rs | 18 ++++- src/html/base.rs | 11 +++- src/html/home.rs | 2 +- src/html/list.rs | 2 +- src/html/page.rs | 13 ++-- src/html/post.rs | 17 +++-- src/html/special.rs | 4 +- src/html/wiki.rs | 2 +- src/main.rs | 4 +- 11 files changed, 54 insertions(+), 90 deletions(-) delete mode 100644 content/posts/writing-code-considered-harmful.md diff --git a/content/posts/writing-code-considered-harmful.md b/content/posts/writing-code-considered-harmful.md deleted file mode 100644 index 05e52d9..0000000 --- a/content/posts/writing-code-considered-harmful.md +++ /dev/null @@ -1,65 +0,0 @@ ---- -title: Writing code considered harmful -date: 2023-06-14T22:09:06.127Z ---- - -When I initially delved into programming, I held the belief that it revolved solely around composing sets of instructions for the computer to execute. At a glance, it indeed appeared as such - merely commanding the computer to carry out tasks in a predetermined sequence, with the anticipation of obtaining a desirable outcome. This perception was further reinforced when observing how computers interpret code in assembly language, where each step is executed in a specific order within the CPU. - -However, my view gradually changed as I was studying computer science at an university. First it was the object-oriented programming, which showed me the importance of choosing the right high level abstractions for a given problem. It started to seem to me as if programming was more about composing such abstractions to make problems easier to solve and easier to modify as needs change :cite[gamma1994design]. - -I also once thought that mathematics are not that important when it comes to computer science. I remember that at the start I had linear algebra and calculus courses, but I didn't really enjoy them much. Back then I didn't really understand the value of mathematics in this context, I just thought that they were things I had to pass on the way to „real” programming. To my horror it never seemed to end! It just became more subtly embedded within what I was doing all around. - -For instance, statistics, and linear algebra, turned out to be the key to understanding machine learning. Without sufficient knowledge in that area of mathematics you can't really grasp how machine learning works on the fundamental level :cite[chollet2021deep]. It was at that point that I regretted not having worked harder at my linear algebra foundations. - -A highlight of my studies was when I had a functional programming course, which felt like a U-turn probably on everything I learned up until that point. Suddenly, mutability was a bad thing, and everything was a function. I had to give up living in the object-oriented world. At that point I realized that there is in fact a different way to write software, and it's one deeply connected with nothing other than - mathematics. Okay. - -Functional programming has the additional advantage over imperative and object-oriented paradigms that its abstractions are founded in mathematics. In fact, you can even write your programs using denotational demantics, which essentially means that code is more or less equivalent to mathematical equations. You can prove the program is correct by proving that the mathematical equivalent of it is :cite[10.1145/360303.360308]. - -It's really powerful. - -## Summary - -It was a pretty fun and interesting adventure, difficult at times, but worth it over all. In hindsight, I regret not paying more attention to mathematics right from the start. If I were to start over I would probably focus more on that and the theory of computation. If you're only just starting out then please remember, writing code is not computer science :cat: - - -## Bibliography - -:::bibtex -@book{gamma1994design, - title = {Design Patterns: Elements of Reusable Object-Oriented Software}, - author = {Gamma, E. and Helm, R. and Johnson, R. and Vlissides, J.}, - isbn = {9780321700698}, - lccn = {94034264}, - series = {Addison-Wesley Professional Computing Series}, - year = {1994}, - publisher = {Pearson Education} -} - -@book{chollet2021deep, - title = {Deep Learning with Python, Second Edition}, - author = {Chollet, F.}, - isbn = {9781617296864}, - lccn = {2021425412}, - year = {2021}, - publisher = {Manning} -} - -@article{10.1145/360303.360308, - author = {Tennent, R. D.}, - title = {The Denotational Semantics of Programming Languages}, - year = {1976}, - issue_date = {Aug. 1976}, - publisher = {Association for Computing Machinery}, - address = {New York, NY, USA}, - volume = {19}, - number = {8}, - issn = {0001-0782}, - url = {https://doi.org/10.1145/360303.360308}, - doi = {10.1145/360303.360308}, - journal = {Commun. ACM}, - month = {aug}, - pages = {437–453}, - numpages = {17}, - keywords = {semantics, GEDANKEN, store, environment, LOOP, continuation, applicative, higher-order function, imperative, programming language, theory of computation, recursive definition} -} -::: diff --git a/src/gen/load.rs b/src/gen/load.rs index b563f4e..afdf547 100644 --- a/src/gen/load.rs +++ b/src/gen/load.rs @@ -137,7 +137,11 @@ fn to_source(path: Utf8PathBuf, exts: &HashSet<&'static str>) -> FileItem { pub fn render_all(items: &[Output]) { for item in items { - render(item, &Sack::new(items, &item.path)); + let file = match &item.kind { + OutputKind::Real(a) => Some(&a.meta.path), + OutputKind::Fake(_) => None, + }; + render(item, &Sack::new(items, &item.path, file)); } } diff --git a/src/gen/sack.rs b/src/gen/sack.rs index 3ec1da6..01b7bca 100644 --- a/src/gen/sack.rs +++ b/src/gen/sack.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use camino::Utf8PathBuf; +use camino::{Utf8Path, Utf8PathBuf}; use hayagriva::Library; use crate::html::{Link, LinkDate, Linkable}; @@ -41,11 +41,13 @@ pub struct Sack<'a> { hole: &'a [Output], /// Current path for page path: &'a Utf8PathBuf, + /// Original file location + file: Option<&'a Utf8PathBuf>, } impl<'a> Sack<'a> { - pub fn new(hole: &'a [Output], path: &'a Utf8PathBuf) -> Self { - Self { hole, path } + pub fn new(hole: &'a [Output], path: &'a Utf8PathBuf, file: Option<&'a Utf8PathBuf>) -> Self { + Self { hole, path, file } } pub fn get_links(&self, path: &str) -> Vec { @@ -96,4 +98,14 @@ impl<'a> Sack<'a> { _ => None, }) } + + /// Get the path for output + pub fn get_path(&self) -> &'a Utf8Path { + self.path.as_path() + } + + /// Get the path for original file location + pub fn get_file(&self) -> Option<&'a Utf8Path> { + self.file.map(Utf8PathBuf::as_ref) + } } diff --git a/src/html/base.rs b/src/html/base.rs index 62de0cd..45b95e3 100644 --- a/src/html/base.rs +++ b/src/html/base.rs @@ -1,3 +1,4 @@ +use camino::Utf8Path; use hypertext::{html_elements, maud, maud_move, GlobalAttributes, Raw, Renderable}; use crate::REPO; @@ -81,11 +82,15 @@ pub fn navbar() -> impl Renderable { ) } -pub fn footer() -> impl Renderable { +pub fn footer(path: Option<&Utf8Path>) -> impl Renderable { let copy = format!("Copyright © {} Maciej Jur", &REPO.year); let mail = "maciej@kamoshi.org"; let href = format!("mailto:{}", mail); - let repo = format!("{}/tree/{}", &REPO.link, &REPO.hash); + let link = Utf8Path::new(&REPO.link).join("tree").join(&REPO.hash); + let link = match path { + Some(path) => link.join(path), + None => link, + }; maud_move!( footer .footer { @@ -98,7 +103,7 @@ pub fn footer() -> impl Renderable { } } div .repo { - a href=(repo) { + a href=(link.as_str()) { (&REPO.hash) } div { diff --git a/src/html/home.rs b/src/html/home.rs index d91ea71..2b96c06 100644 --- a/src/html/home.rs +++ b/src/html/home.rs @@ -73,5 +73,5 @@ pub fn home<'data, 'home, R>(main: R) -> impl Renderable + 'home } ); - page("Home", main) + page("Home", main, None) } diff --git a/src/html/list.rs b/src/html/list.rs index acbbf7f..b965082 100644 --- a/src/html/list.rs +++ b/src/html/list.rs @@ -45,7 +45,7 @@ pub fn list<'data, 'list>( } ); - page(title, list) + page(title, list, None) } fn section(year: i32, group: &[LinkDate]) -> impl Renderable + '_ { diff --git a/src/html/page.rs b/src/html/page.rs index e5ebbd5..088f536 100644 --- a/src/html/page.rs +++ b/src/html/page.rs @@ -1,3 +1,4 @@ +use camino::Utf8Path; use hypertext::{html_elements, maud_move, GlobalAttributes, Raw, Renderable}; use crate::html::base::{head, navbar, footer}; @@ -19,10 +20,14 @@ pub fn bare<'data, 'html, R>(title: &'data str, main: R) -> impl Renderable + 'h ) } -pub fn page<'data, 'main, 'page, T>(title: &'data str, main: T) -> impl Renderable + 'page +pub fn page<'data, 'main, 'html, T>( + title: &'data str, + main: T, + path: Option<&'data Utf8Path>, +) -> impl Renderable + 'html where - 'main : 'page, - 'data : 'page, + 'main : 'html, + 'data : 'html, T: Renderable + 'main { maud_move!( @@ -33,7 +38,7 @@ pub fn page<'data, 'main, 'page, T>(title: &'data str, main: T) -> impl Renderab body { (navbar()) (main) - (footer()) + (footer(path)) } } ) diff --git a/src/html/post.rs b/src/html/post.rs index e138e8e..d9b5bc7 100644 --- a/src/html/post.rs +++ b/src/html/post.rs @@ -1,21 +1,24 @@ use hypertext::{html_elements, maud_move, GlobalAttributes, Renderable}; +use crate::gen::Sack; use crate::html::misc::{show_bibliography, show_outline}; use crate::html::page; use crate::md::Post; use crate::text::md::Outline; -pub fn post<'fm, 'md, 'post, T>( - fm: &'fm Post, +pub fn post<'f, 'm, 's, 'html, T>( + fm: &'f Post, content: T, outline: Outline, bib: Option>, -) -> impl Renderable + 'post + sack: &'s Sack, +) -> impl Renderable + 'html where - 'fm: 'post, - 'md: 'post, - T: Renderable + 'md + 'f: 'html, + 'm: 'html, + 's: 'html, + T: Renderable + 'm { let main = maud_move!( main .wiki-main { @@ -46,5 +49,5 @@ pub fn post<'fm, 'md, 'post, T>( } ); - page(&fm.title, main) + page(&fm.title, main, sack.get_file()) } diff --git a/src/html/special.rs b/src/html/special.rs index f2260c0..6b74c0c 100644 --- a/src/html/special.rs +++ b/src/html/special.rs @@ -12,7 +12,7 @@ pub fn map() -> impl Renderable { (Raw("import 'photos';")) } } - )) + ), None) } pub fn search() -> impl Renderable { @@ -20,5 +20,5 @@ pub fn search() -> impl Renderable { main { } - )) + ), None) } diff --git a/src/html/wiki.rs b/src/html/wiki.rs index 2318163..4b52842 100644 --- a/src/html/wiki.rs +++ b/src/html/wiki.rs @@ -53,5 +53,5 @@ pub fn wiki<'data, 'html, 'sack, T>( } ); - page(&fm.title, main) + page(&fm.title, main, sack.get_file()) } diff --git a/src/main.rs b/src/main.rs index a7511c0..9a5fba6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,7 +57,7 @@ impl Content for md::Post { &'f self, content: T, outline: Outline, - _: &'s Sack, + sack: &'s Sack, bib: Option>, ) -> impl Renderable + 'html where @@ -65,7 +65,7 @@ impl Content for md::Post { 'm: 'html, 's: 'html, T: Renderable + 'm { - html::post(self, content, outline, bib) + html::post(self, content, outline, bib, sack) } fn as_link(&self, path: Utf8PathBuf) -> Option {