Skip to content

Commit

Permalink
Change HTML (#415)
Browse files Browse the repository at this point in the history
* change HTML template

* split generation in module.html and index.html

* generate list content

* generate singlepage Html

* cards open by default

* delete .pol from title and list

* list body

* fmt changes

* generate modules externaly

* solve problem with pol clean

* solve problem with empty contructor

* change presentation

* change doc color

* add show content button

* enclose body in code element

* minor css changes

* ad href

* change to multifile

* cargo fmt

* minor design changes

* change uppercase types to lowercase
  • Loading branch information
Phil-Ebsworth authored Jan 2, 2025
1 parent 9ab5133 commit ddc83a1
Show file tree
Hide file tree
Showing 13 changed files with 341 additions and 91 deletions.
8 changes: 4 additions & 4 deletions app/src/cli/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ pub struct Args {
}

pub async fn exec(cmd: Args) -> miette::Result<()> {
let htmlpath = get_path(&cmd);
let filepath = &cmd.filepath;
write_html(filepath, &htmlpath).await;
let htmlpath = get_path(filepath);
write_html().await;
if cmd.open {
open(&htmlpath);
}
Ok(())
}

fn get_path(cmd: &Args) -> PathBuf {
fn get_path(filepath: &Path) -> PathBuf {
let mut path =
Path::new(DOCS_PATH).join(cmd.filepath.file_name().unwrap().to_string_lossy().as_ref());
Path::new(DOCS_PATH).join(filepath.file_name().unwrap().to_string_lossy().as_ref());
path.set_extension("html");
path
}
93 changes: 76 additions & 17 deletions lang/docs/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,103 @@ use std::path::{Path, PathBuf};
use askama::Template;
use opener;

use driver::paths::{CSS_PATH, CSS_TEMPLATE_PATH};
use driver::paths::{CSS_PATH, CSS_TEMPLATE_PATH, DOCS_PATH, EXAMPLE_PATH};
use driver::Database;

use crate::generate_docs::GenerateDocs;

pub async fn write_html(filepath: &PathBuf, htmlpath: &PathBuf) {
let mut db = Database::from_path(filepath);
let uri = db.resolve_path(filepath).expect("Failed to resolve path");
let prg = db.ust(&uri).await.expect("Failed to get UST");

pub async fn write_html() {
if !Path::new(CSS_PATH).exists() {
fs::create_dir_all(Path::new(CSS_PATH).parent().unwrap())
.expect("Failed to create CSS directory");
fs::write(CSS_PATH, CSS_TEMPLATE_PATH).expect("Failed to create CSS file");
}

let mut stream = fs::File::create(htmlpath).expect("Failed to create file");

let code = prg.generate_docs();
let title = filepath.file_name().unwrap().to_str().unwrap();
let output = generate_html(title, &code);

stream.write_all(output.as_bytes()).expect("Failed to write to file");
write_modules().await;
}

pub fn open(filepath: &PathBuf) {
let absolute_path = fs::canonicalize(filepath).expect("Failed to get absolute path");
opener::open(&absolute_path).unwrap();
}

#[derive(Template)]
#[template(path = "index.html", escape = "none")]
struct IndexTemplate<'a> {
title: &'a str,
list: &'a str,
code: &'a str,
start: &'a str,
}

fn generate_html(title: &str, list: &str, code: &str) -> String {
let template = IndexTemplate { title, list, code, start: title };
template.render().unwrap()
}

#[derive(Template)]
#[template(path = "module.html", escape = "none")]
struct ModuleTemplate<'a> {
title: &'a str,
code: &'a str,
content: &'a str,
}

fn generate_html(title: &str, code: &str) -> String {
let template = ModuleTemplate { title, code };
fn generate_module(title: &str, content: &str) -> String {
let template = ModuleTemplate { title, content };
template.render().unwrap()
}

async fn write_modules() {
let example_path = Path::new(EXAMPLE_PATH);
let mut all_modules = String::new();
let list = file_list(get_files(Path::new(EXAMPLE_PATH)));
for file in get_files(example_path) {
let mut db = Database::from_path(&file);
let uri = db.resolve_path(&file).expect("Failed to resolve path");
let prg = db.ust(&uri).await.expect("Failed to get UST");

let title = file.file_stem().unwrap().to_str().unwrap();
let title = title.replace('_', " ");
let code = prg.generate_docs();
let content = generate_module(&title, &code);
let output_file = generate_html(&title, &list, &content);

let htmlpath = get_path(&file);
let mut stream = fs::File::create(htmlpath).expect("Failed to create file");

stream.write_all(output_file.as_bytes()).expect("Failed to write to file");

all_modules.push_str(&output_file);
}
}

fn get_files(path: &Path) -> Vec<PathBuf> {
let mut files = Vec::new();
if path.is_dir() {
for entry in fs::read_dir(path).expect("Failed to read directory") {
let entry = entry.expect("Failed to read entry");
let path = entry.path();
if path.is_file() && path.extension().and_then(|s| s.to_str()) == Some("pol") {
files.push(path);
}
}
}
files
}

fn file_list(files: Vec<PathBuf>) -> String {
let mut list = String::new();
for file in files {
let name = file.file_stem().unwrap().to_str().unwrap();
let path = name.to_string() + ".html";
let name = name.replace('_', " ");
list.push_str(&format!("<li><a href={}>{}</a></li>", &path, name));
}
list
}

fn get_path(filepath: &Path) -> PathBuf {
let mut path =
Path::new(DOCS_PATH).join(filepath.file_name().unwrap().to_string_lossy().as_ref());
path.set_extension("html");
path
}
33 changes: 28 additions & 5 deletions lang/docs/src/generate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ast::{Ctor, DocComment, Dtor};
use ast::{Case, Ctor, DocComment, Dtor};
use printer::{Print, PrintCfg};

pub trait Generate {
Expand All @@ -15,7 +15,7 @@ impl Generate for Ctor {

let head = if typ.is_simple() { head } else { format!("{}: {}", head, typs) };

format!("{}{}", doc_str, head)
format!("<li>{}{}</li>", doc_str, head)
}
}

Expand All @@ -30,14 +30,14 @@ impl Generate for Dtor {
let head =
if self_param.is_simple() { ".".to_owned() } else { format!("{}.", self_parameter) };

format!("{}{}{}{}: {}", doc_str, head, name.id, parmeter, ret_typ)
format!("<li>{}{}{}{}: {}</li>", doc_str, head, name.id, parmeter, ret_typ)
}
}

impl Generate for DocComment {
fn generate(&self) -> String {
let DocComment { docs } = self;
let prefix = "<span class=\"comment\"> -- |";
let prefix = "<span class=\"comment\">";
let postfix = "</span>";
docs.iter()
.map(|doc| {
Expand All @@ -63,12 +63,35 @@ impl<T: Generate> Generate for Option<T> {
}
}

impl<T: Generate> Generate for Vec<T> {
impl Generate for Vec<DocComment> {
fn generate(&self) -> String {
self.iter().map(|value| value.generate()).collect::<Vec<String>>().join(",<br>")
}
}

impl Generate for Vec<Case> {
fn generate(&self) -> String {
self.iter()
.map(|value| {
format!("<li>{}</li>", value.print_html_to_string(Some(&PrintCfg::default())))
})
.collect::<Vec<String>>()
.join("")
}
}

impl Generate for Vec<Ctor> {
fn generate(&self) -> String {
self.iter().map(|value| value.generate()).collect::<Vec<String>>().join("")
}
}

impl Generate for Vec<Dtor> {
fn generate(&self) -> String {
self.iter().map(|value| value.generate()).collect::<Vec<String>>().join("")
}
}

impl<T: Generate> Generate for Box<T> {
fn generate(&self) -> String {
self.as_ref().generate()
Expand Down
36 changes: 25 additions & 11 deletions lang/docs/src/generate_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ impl GenerateDocs for Decl {
impl GenerateDocs for Data {
fn generate_docs(&self) -> String {
let Data { span: _, doc, name, attr, typ, ctors } = self;
let doc = doc.generate();
let doc = if doc.is_none() { "".to_string() } else { format!("{}<br>", doc.generate()) };
let name = &name.id;
let attr: String = attr.print_html_to_string(Some(&PrintCfg::default()));
let typ: String = typ.print_html_to_string(Some(&PrintCfg::default()));

let body = if ctors.is_empty() { "{}".to_string() } else { ctors.generate() };
let body = if ctors.is_empty() {
"".to_string()
} else {
format!("<ul>{}</ul>", ctors.generate())
};

let data = DataTemplate { doc: &doc, name, attr: &attr, typ: &typ, body: &body };
data.render().unwrap()
Expand All @@ -44,12 +48,16 @@ impl GenerateDocs for Codata {
fn generate_docs(&self) -> String {
let Codata { span: _, doc, name, attr, typ, dtors } = self;

let doc = doc.generate();
let doc = if doc.is_none() { "".to_string() } else { format!("{}<br>", doc.generate()) };
let name = &name.id;
let attr: String = attr.print_html_to_string(Some(&PrintCfg::default()));
let typ: String = typ.print_html_to_string(Some(&PrintCfg::default()));

let body = if dtors.is_empty() { "{}".to_string() } else { dtors.generate() };
let body = if dtors.is_empty() {
"".to_string()
} else {
format!("<ul>{}</ul>", dtors.generate())
};

let codata = CodataTemplate { doc: &doc, name, attr: &attr, typ: &typ, body: &body };
codata.render().unwrap()
Expand All @@ -60,14 +68,17 @@ impl GenerateDocs for Def {
fn generate_docs(&self) -> String {
let Def { span: _, doc, name, attr: _, params, self_param, ret_typ, cases } = self;

let doc = doc.generate();
let doc = if doc.is_none() { "".to_string() } else { format!("{}<br>", doc.generate()) };
let name = &name.id;
let params: String = params.print_html_to_string(Some(&PrintCfg::default()));
let self_param: String = self_param.print_html_to_string(Some(&PrintCfg::default()));
let ret_typ: String = ret_typ.print_html_to_string(Some(&PrintCfg::default()));
let cases: String = cases.print_html_to_string(Some(&PrintCfg::default()));

let body = if cases.is_empty() { "{}".to_string() } else { cases };
let body = if cases.is_empty() {
"{}".to_string()
} else {
format!("<ul>{}</ul>", cases.generate())
};

let def = DefTemplate {
doc: &doc,
Expand All @@ -85,13 +96,16 @@ impl GenerateDocs for Codef {
fn generate_docs(&self) -> String {
let Codef { span: _, doc, name, attr: _, params, typ, cases } = self;

let doc = doc.generate();
let doc = if doc.is_none() { "".to_string() } else { format!("{}<br>", doc.generate()) };
let name = &name.id;
let params: String = params.print_html_to_string(Some(&PrintCfg::default()));
let typ: String = typ.print_html_to_string(Some(&PrintCfg::default()));
let cases: String = cases.print_html_to_string(Some(&PrintCfg::default()));

let body = if cases.is_empty() { "{}".to_string() } else { cases };
let body = if cases.is_empty() {
"{}".to_string()
} else {
format!("<ul>{}</ul>", cases.generate())
};

let codef = CodefTemplate { doc: &doc, name, params: &params, typ: &typ, body: &body };
codef.render().unwrap()
Expand All @@ -102,7 +116,7 @@ impl GenerateDocs for Let {
fn generate_docs(&self) -> String {
let Let { span: _, doc, name, attr: _, params, typ, body } = self;

let doc = doc.generate();
let doc = if doc.is_none() { "".to_string() } else { format!("{}<br>", doc.generate()) };
let name = &name.id;
let params: String = params.print_html_to_string(Some(&PrintCfg::default()));
let typ: String = typ.print_html_to_string(Some(&PrintCfg::default()));
Expand Down
15 changes: 11 additions & 4 deletions lang/docs/templates/codata.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<span>{{doc}}</span>
<span><span class="keyword">Codata</span> {{name}}{{attr}} {{typ}} {</span> <br>
<span>{{body}}</span> <br>
<span>}</span> <br>
<div class="card">
<div class="card-header">
<span><span class="keyword">codata</span> {{name}}{{attr}} {{typ}}</span>
</div>
<div class="card-content" style="display: block;">
<span>{{doc}}</span>
<code>
<span>{{body}}</span>
</code>
</div>
</div>
18 changes: 14 additions & 4 deletions lang/docs/templates/codef.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
<span>{{doc}}</span>
<span><span class="keyword">Codef</span> {{name}}{{params}}: {{typ}} {</span> <br>
<span>{{body}}</span> <br>
<span>}</span> <br>
<div class="card">
<div class="card-header" onclick="toggleCard(this)">
<span><span class="keyword">codef</span> {{name}}{{params}}: {{typ}}</span>
<button class="button">show code</button>
</div>
<div class="doc" style="display: block;">
<span>{{doc}}</span>
</div>
<div class="card-content" style="display: none;">
<code>
<span>{{body}}</span>
</code>
</div>
</div>
15 changes: 11 additions & 4 deletions lang/docs/templates/data.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<span>{{doc}}</span>
<span><span class="keyword">Data</span> {{name}}{{attr}} {{typ}} {</span> <br>
<span>{{body}}</span> <br>
<span>}</span> <br>
<div class="card">
<div class="card-header">
<span> <span class="keyword">data</span> {{name}}{{attr}}{{typ}}</span>
</div>
<div class="card-content" style="display: block;">
<span>{{doc}}</span>
<code>
<span>{{body}}</span>
</code>
</div>
</div>
18 changes: 14 additions & 4 deletions lang/docs/templates/def.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
<span>{{doc}}</span>
<span><span class="keyword">Def</span> {{self_param}}.{{name}}{{params}}: {{typ}} {</span> <br>
<span>{{body}}</span> <br>
<span>}</span> <br>
<div class="card">
<div class="card-header" onclick="toggleCard(this)">
<span><span class="keyword">def</span> {{self_param}}.{{name}}{{params}}: {{typ}}</span>
<button class="button">show code</button>
</div>
<div class="doc" style="display: block;">
<span>{{doc}}</span>
</div>
<div class="card-content" style="display: none;">
<code>
<span>{{body}}</span>
</code>
</div>
</div>
Loading

0 comments on commit ddc83a1

Please sign in to comment.