Skip to content

Commit

Permalink
add mkdocs exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-1991 committed Jun 6, 2024
1 parent 40b4767 commit d3c4ac0
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/exporters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub enum Templates {
JsonSchemaAll,
Shex,
PythonSdrdm,
MkDocs,
}

/// Converts string representation of a template to a `Templates` enum.
Expand All @@ -60,6 +61,7 @@ impl FromStr for Templates {
"json-schema-all" => Ok(Templates::JsonSchemaAll),
"shex" => Ok(Templates::Shex),
"python-sdrdm" => Ok(Templates::PythonSdrdm),
"mk-docs" => Ok(Templates::MkDocs),
_ => {
let err = format!("Invalid template type: {}", s);
Err(err.into())
Expand Down Expand Up @@ -111,6 +113,7 @@ pub fn render_jinja_template(
Templates::Shacl => env.get_template("shacl.jinja")?,
Templates::Shex => env.get_template("shex.jinja")?,
Templates::PythonSdrdm => env.get_template("python-sdrdm.jinja")?,
Templates::MkDocs => env.get_template("mkdocs.jinja")?,
_ => {
panic!(
"The template is not available as a Jinja Template and should not be used using the jinja exporter.
Expand Down Expand Up @@ -279,4 +282,15 @@ mod tests {
.expect("Could not read expected file");
assert_eq!(rendered, expected);
}

#[test]
fn test_convert_to_mkdocs() {
// Arrange
let rendered = build_and_convert(Templates::MkDocs);

// Assert
let expected = fs::read_to_string("tests/data/expected_mkdocs.md")
.expect("Could not read expected file");
assert_eq!(rendered, expected);
}
}
4 changes: 4 additions & 0 deletions src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ pub fn process_pipeline(path: &PathBuf) -> Result<(), Box<dyn std::error::Error>
let content = model.clone().convert_to(&template)?;
save_to_file(&specs.out, content.as_str())?;
}
Templates::MkDocs => {
let content = model.clone().convert_to(&template)?;
save_to_file(&specs.out, content.as_str())?;
}
}

println!(
Expand Down
89 changes: 89 additions & 0 deletions templates/mkdocs.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{##### Macros ####}

{# Get the type of an attribute #}
{%- macro get_dtype(attribute) %}
{%- for dtype in attribute.dtypes %}
{%- set dtype_string -%}
`{{ wrap_multiple(wrap_multiple(dtype, attribute.multiple)) }}`
{%- endset -%}
{{ linkify(dtype, dtype_string) }}
{%- endfor %}
{%- endmacro %}

{# Wrap a multiple in a list statement if it is the case #}
{%- macro wrap_multiple(dtype, multiple) -%}

{%- if multiple is true -%}
list[{{ dtype }}]
{%- else -%}
{{ dtype }}
{%- endif -%}

{%- endmacro -%}

{# Turn in-document types into links #}
{%- macro linkify(dtype, dtype_string) %}

{%- if dtype in object_names or dtype in enum_names -%}
[{{ dtype_string }}](#{{ dtype | lower }})
{%- else -%}
{{ dtype_string }}
{%- endif -%}

{%- endmacro %}
{#################}

---
hide:
- navigation
---

# Model Reference

This page provides comprehensive information about the structure and components of the data model, including detailed descriptions of the types and their properties, information on enumerations, and an overview of the ontologies used and their associated prefixes. Below, you will find a graph that visually represents the overall structure of the data model.

??? quote "Graph"
``` mermaid
flowchart TB
{%- for object in objects %}
{%- for attribute in object.attributes %}
{%- for dtype in attribute.dtypes %}
{%- if dtype in object_names or dtype in enum_names %}
{{ object.name | lower}}({{ object.name }}) --> {{ dtype | lower }}({{ dtype }})
{%- endif %}
{%- endfor %}
{%- endfor %}
{%- endfor %}
```

{% if prefixes | length > 0 %}
## Ontologies

{%- for prefix, value in prefixes %}
- [{{ prefix }}]({{ value }})
{%- endfor %}
{% endif %}

## Types

{% for object in objects %}
### {{ object.name }}
{{ object.docstring }}
{%- for attribute in object.attributes %}
{%- set required %}
{%- if attribute.required %}*{% endif %}
{%- endset %}

__{{ attribute.name }}__{{ required }} {{ get_dtype(attribute) }}
{% if attribute.docstring %}
- {{ attribute.docstring }}
{%- endif %}
{%- for option in attribute.options -%}
{%- if option.key != 'description' %}
- `{{ option.key | capitalize }}`: {{ option.value }}
{%- endif %}
{%- endfor %}
{%- endfor %}

------
{% endfor %}
53 changes: 53 additions & 0 deletions tests/data/expected_mkdocs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
hide:
- navigation
---

# Model Reference

This page provides comprehensive information about the structure and components of the data model, including detailed descriptions of the types and their properties, information on enumerations, and an overview of the ontologies used and their associated prefixes. Below, you will find a graph that visually represents the overall structure of the data model.

??? quote "Graph"
``` mermaid
flowchart TB
test(Test) --> test2(Test2)
test(Test) --> ontology(Ontology)
```


## Ontologies
- [schema](http://schema.org/)


## Types


### Test


__name__* `string`

- The name of the test.

__number__ `float`


__test2__ [`list[Test2]`](#test2)


__ontology__ [`Ontology`](#ontology)


------

### Test2


__names__ `list[string]`


__number__ `float`

- `Minimum`: 0

------

0 comments on commit d3c4ac0

Please sign in to comment.