Skip to content

Commit

Permalink
Add source repository to config & docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Dec 17, 2022
1 parent 0b2e2ca commit 8e634bc
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 16 deletions.
27 changes: 26 additions & 1 deletion crates/project/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs, io, path::PathBuf};
use std::{fmt::Display, fs, io, path::PathBuf};

use serde::Deserialize;

Expand All @@ -8,6 +8,31 @@ pub struct Config {
pub version: String,
#[serde(default)]
pub description: String,
pub repository: Option<Repository>,
}

#[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 {
Expand Down
48 changes: 38 additions & 10 deletions crates/project/src/docs.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -33,13 +36,13 @@ struct ModuleTemplate<'a> {
module_name: String,
project_name: &'a str,
project_version: &'a str,
links: &'a Vec<DocLink>,
modules_prefix: String,
modules: &'a Vec<DocLink>,
functions: Vec<DocFunction>,
types: Vec<DocType>,
constants: Vec<DocConstant>,
documentation: String,
source: &'a DocLink,
timestamp: String,
}

Expand All @@ -51,10 +54,10 @@ struct PageTemplate<'a> {
page_title: &'a str,
project_name: &'a str,
project_version: &'a str,
links: &'a Vec<DocLink>,
modules_prefix: String,
modules: &'a Vec<DocLink>,
content: String,
source: &'a DocLink,
timestamp: &'a str,
}

Expand All @@ -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.
Expand All @@ -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<DocFile> = vec![];
let mut search_indexes: Vec<SearchIndex> = vec![];

Expand All @@ -81,6 +105,7 @@ pub fn generate_all(root: &Path, config: &Config, modules: Vec<&CheckedModule>)
config,
module,
(&modules_prefix, &modules_links),
&source,
&timestamp,
);
search_indexes.extend(indexes);
Expand All @@ -92,6 +117,7 @@ pub fn generate_all(root: &Path, config: &Config, modules: Vec<&CheckedModule>)
root,
config,
(&modules_prefix, &modules_links),
&source,
&timestamp,
));

Expand All @@ -102,6 +128,7 @@ fn generate_module(
config: &Config,
module: &CheckedModule,
(modules_prefix, modules): (&str, &Vec<DocLink>),
source: &DocLink,
timestamp: &Duration,
) -> (Vec<SearchIndex>, DocFile) {
let mut search_indexes = vec![];
Expand Down Expand Up @@ -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,
Expand All @@ -158,6 +184,7 @@ fn generate_module(
functions,
types,
constants,
source,
timestamp: timestamp.as_secs().to_string(),
};

Expand Down Expand Up @@ -231,6 +258,7 @@ fn generate_readme(
root: &Path,
config: &Config,
(modules_prefix, modules): (&str, &Vec<DocLink>),
source: &DocLink,
timestamp: &Duration,
) -> DocFile {
let path = PathBuf::from("index.html");
Expand All @@ -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: &timestamp.as_secs().to_string(),
};

Expand Down Expand Up @@ -559,26 +587,26 @@ 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()
}]),
"aiken".to_string()
);

assert_eq!(
find_modules_prefix(&vec![DocLink {
find_modules_prefix(&[DocLink {
name: "my_module".to_string(),
path: String::new()
}]),
"".to_string()
);

assert_eq!(
find_modules_prefix(&vec![
find_modules_prefix(&[
DocLink {
name: "aiken/list".to_string(),
path: String::new()
Expand All @@ -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()
Expand Down
8 changes: 3 additions & 5 deletions crates/project/templates/_layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,10 @@ <h2>
<svg class="label icon icon-x-circle" alt="Close Menu" title="Close Menu"><use xlink:href="#icon-x-circle"></use></svg>
</button>

{% if !links.is_empty() %}
<h2>Links</h2>
{% if !source.is_empty() %}
<h2>Source code</h2>
<ul>
{% for link in links %}
<li><a href="{{ link.path }}">{{ link.name }}</a></li>
{% endfor %}
<li><a href="{{ source.path }}">{{ source.name }}</a></li>
</ul>
{% endif %}

Expand Down

0 comments on commit 8e634bc

Please sign in to comment.