From 1178fa3f01d499051489008795e8a3b2f5936090 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Sat, 17 Dec 2022 03:19:39 +0100 Subject: [PATCH] Add source repository to config & docs. --- crates/project/src/config.rs | 27 ++++++++++++++- crates/project/src/docs.rs | 48 +++++++++++++++++++++------ crates/project/templates/_layout.html | 8 ++--- 3 files changed, 67 insertions(+), 16 deletions(-) diff --git a/crates/project/src/config.rs b/crates/project/src/config.rs index 4acc18db..1aaba868 100644 --- a/crates/project/src/config.rs +++ b/crates/project/src/config.rs @@ -1,4 +1,4 @@ -use std::{fs, io, path::PathBuf}; +use std::{fmt::Display, fs, io, path::PathBuf}; use serde::Deserialize; @@ -8,6 +8,31 @@ pub struct Config { pub version: String, #[serde(default)] pub description: String, + pub repository: Option, +} + +#[derive(Deserialize)] +pub struct Repository { + pub user: String, + pub project: String, + pub platform: Platform, +} + +#[derive(Deserialize)] +pub enum Platform { + Github, + Gitlab, + Bitbucket, +} + +impl Display for Platform { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> { + match *self { + Platform::Github => f.write_str("github"), + Platform::Gitlab => f.write_str("gitlab"), + Platform::Bitbucket => f.write_str("bitbucket"), + } + } } impl Config { diff --git a/crates/project/src/docs.rs b/crates/project/src/docs.rs index eecf87be..3ab8c46b 100644 --- a/crates/project/src/docs.rs +++ b/crates/project/src/docs.rs @@ -1,4 +1,7 @@ -use crate::{config::Config, module::CheckedModule}; +use crate::{ + config::{Config, Repository}, + module::CheckedModule, +}; use aiken_lang::{ ast::{Definition, RecordConstructor, RecordConstructorArg, TypedDefinition}, format, @@ -33,13 +36,13 @@ struct ModuleTemplate<'a> { module_name: String, project_name: &'a str, project_version: &'a str, - links: &'a Vec, modules_prefix: String, modules: &'a Vec, functions: Vec, types: Vec, constants: Vec, documentation: String, + source: &'a DocLink, timestamp: String, } @@ -51,10 +54,10 @@ struct PageTemplate<'a> { page_title: &'a str, project_name: &'a str, project_version: &'a str, - links: &'a Vec, modules_prefix: String, modules: &'a Vec, content: String, + source: &'a DocLink, timestamp: &'a str, } @@ -64,6 +67,12 @@ struct DocLink { path: String, } +impl DocLink { + pub fn is_empty(&self) -> bool { + self.name.is_empty() + } +} + /// Generate documentation files for a given project. /// /// The documentation is built using template files located at the root of this crate. @@ -73,6 +82,21 @@ pub fn generate_all(root: &Path, config: &Config, modules: Vec<&CheckedModule>) let timestamp = new_timestamp(); let (modules_prefix, modules_links) = generate_modules_links(&modules); + let source = match &config.repository { + None => DocLink { + name: String::new(), + path: String::new(), + }, + Some(Repository { + user, + project, + platform, + }) => DocLink { + name: format!("{user}/{project}"), + path: format!("https://{platform}.com/{user}/{project}"), + }, + }; + let mut output_files: Vec = vec![]; let mut search_indexes: Vec = vec![]; @@ -81,6 +105,7 @@ pub fn generate_all(root: &Path, config: &Config, modules: Vec<&CheckedModule>) config, module, (&modules_prefix, &modules_links), + &source, ×tamp, ); search_indexes.extend(indexes); @@ -92,6 +117,7 @@ pub fn generate_all(root: &Path, config: &Config, modules: Vec<&CheckedModule>) root, config, (&modules_prefix, &modules_links), + &source, ×tamp, )); @@ -102,6 +128,7 @@ fn generate_module( config: &Config, module: &CheckedModule, (modules_prefix, modules): (&str, &Vec), + source: &DocLink, timestamp: &Duration, ) -> (Vec, DocFile) { let mut search_indexes = vec![]; @@ -147,7 +174,6 @@ fn generate_module( let module = ModuleTemplate { aiken_version: VERSION, breadcrumbs: to_breadcrumbs(&module.name), - links: &vec![], documentation: render_markdown(&module.ast.docs.iter().join("\n")), modules_prefix: modules_prefix.to_string(), modules, @@ -158,6 +184,7 @@ fn generate_module( functions, types, constants, + source, timestamp: timestamp.as_secs().to_string(), }; @@ -231,6 +258,7 @@ fn generate_readme( root: &Path, config: &Config, (modules_prefix, modules): (&str, &Vec), + source: &DocLink, timestamp: &Duration, ) -> DocFile { let path = PathBuf::from("index.html"); @@ -240,13 +268,13 @@ fn generate_readme( let template = PageTemplate { aiken_version: VERSION, breadcrumbs: ".", - links: &vec![], modules_prefix: modules_prefix.to_string(), modules, project_name: &config.name, page_title: &config.name, project_version: &config.version.to_string(), content: render_markdown(&content), + source, timestamp: ×tamp.as_secs().to_string(), }; @@ -559,10 +587,10 @@ fn find_modules_prefix(modules: &[DocLink]) -> String { #[test] fn find_modules_prefix_test() { - assert_eq!(find_modules_prefix(&vec![]), "".to_string()); + assert_eq!(find_modules_prefix(&[]), "".to_string()); assert_eq!( - find_modules_prefix(&vec![DocLink { + find_modules_prefix(&[DocLink { name: "aiken/list".to_string(), path: String::new() }]), @@ -570,7 +598,7 @@ fn find_modules_prefix_test() { ); assert_eq!( - find_modules_prefix(&vec![DocLink { + find_modules_prefix(&[DocLink { name: "my_module".to_string(), path: String::new() }]), @@ -578,7 +606,7 @@ fn find_modules_prefix_test() { ); assert_eq!( - find_modules_prefix(&vec![ + find_modules_prefix(&[ DocLink { name: "aiken/list".to_string(), path: String::new() @@ -592,7 +620,7 @@ fn find_modules_prefix_test() { ); assert_eq!( - find_modules_prefix(&vec![ + find_modules_prefix(&[ DocLink { name: "aiken/list".to_string(), path: String::new() diff --git a/crates/project/templates/_layout.html b/crates/project/templates/_layout.html index 355d7162..8f243315 100644 --- a/crates/project/templates/_layout.html +++ b/crates/project/templates/_layout.html @@ -174,12 +174,10 @@ - {% if !links.is_empty() %} -

Links

+ {% if !source.is_empty() %} +

Source code

{% endif %}