Skip to content

Commit

Permalink
Merge pull request asam-ev#107 from ClemensLinnhoff/106-material-para…
Browse files Browse the repository at this point in the history
…m-docu

Add autogen for material properties and tables
  • Loading branch information
ClemensLinnhoff authored Sep 17, 2024
2 parents 96b5eaa + ef268d3 commit 89d2146
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 23 deletions.
5 changes: 2 additions & 3 deletions .antora/modules/specification/nav_openmaterial.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@
** xref:material/Introduction.adoc[]
** xref:material/Fileformat.adoc[]
** xref:material/metadata.adoc[]
** xref:material/Material-parameters.adoc[]
** xref:material/material-properties.adoc[Material properties]
** xref:material/lookup-tables.adoc[]
4 changes: 4 additions & 0 deletions .github/workflows/antora-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ jobs:
mv metadata.adoc ../content/geometry/
python3 json2asciidoc.py ../schemas/material_schema.json metadata
mv metadata.adoc ../content/material/
python3 json2asciidoc.py ../schemas/material_schema.json material_properties
mv material_properties.adoc ../content/material/material-properties.adoc
python3 json2asciidoc.py ../schemas/material_emp_schema.json electromagnetic_properties
mv electromagnetic_properties.adoc ../content/material/electromagnetic-properties.adoc
- name: Configure Pages
uses: actions/configure-pages@v4
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

# Autogenerated files
content/geometry/metadata.adoc
content/material/metadata.adoc
content/material/metadata.adoc
content/material/material-properties.adoc
content/material/electromagnetic-properties.adoc
1 change: 0 additions & 1 deletion content/material/Material-parameters.adoc

This file was deleted.

3 changes: 3 additions & 0 deletions content/material/lookup-tables.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
= Lookup tables

include::electromagnetic-properties.adoc[leveloffset=+1]
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
include::Introduction.adoc[leveloffset=+1]
include::Fileformat.adoc[leveloffset=+1]
include::metadata.adoc[leveloffset=+1]
include::Material-parameters.adoc[leveloffset=+1]
include::material-properties.adoc[leveloffset=+1]
include::lookup-tables.adoc[leveloffset=+1]
2 changes: 1 addition & 1 deletion examples/example_material.xomp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"name": "aluminum",
"description": "aluminum material"
},
"materials_properties": {
"material_properties": {
"surface_roughness": {
"surface_height_rms": 0.8,
"surface_correlation_length": 1.0
Expand Down
4 changes: 2 additions & 2 deletions schemas/material_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"description"
]
},
"materials_properties": {
"material_properties": {
"type": "object",
"description": "Properties related to the material.",
"properties": {
Expand Down Expand Up @@ -139,6 +139,6 @@
},
"required": [
"metadata",
"materials_properties"
"material_properties"
]
}
104 changes: 90 additions & 14 deletions scripts/json2asciidoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
def escape_special_chars(pattern):
"""
Escape special characters in the pattern string for AsciiDoc compatibility.
Args:
pattern (str): The pattern string to be escaped.
Returns:
str: The escaped pattern string.
"""
Expand All @@ -17,25 +17,60 @@ def escape_special_chars(pattern):
return pattern


def generate_asciidoc(field_name, schema, required_fields):
def generate_asciidoc_array_of_arrays(items, description):
"""
Generate AsciiDoc content for the specified field based on the JSON schema.
Generate AsciiDoc content for an array of arrays, listing each item as a column.
Args:
field_name (str): The name of the field to generate documentation for.
schema (dict): The JSON schema dictionary.
required_fields (list): List of required fields for the specified field.
items (list): The list of item schemas in the array.
description (str): The description of the array.
Returns:
str: The generated AsciiDoc content describing the columns.
"""
content = ""
if description:
content += f"{description}\n"

content += "\nColumns of the table:\n\n"

for idx, item in enumerate(items, start=1):
item_description = item.get('description', 'No description')
content += f"- Column {idx}: {item_description}\n"

return content


def generate_asciidoc_properties(properties, required_fields, level=2):
"""
Recursively generate AsciiDoc content for a dictionary of properties.
Args:
properties (dict): The dictionary of properties from the JSON schema.
required_fields (list): The list of required fields.
level (int): The current heading level in the AsciiDoc file.
Returns:
str: The generated AsciiDoc content.
str: The generated AsciiDoc content for the properties.
"""
asciidoc_content = f"= {field_name.capitalize()}\n\n"
field_data = schema['properties'][field_name]
asciidoc_content += field_data.get("description", "") + "\n\n"
asciidoc_content = ""

for prop_name, prop_data in field_data['properties'].items():
asciidoc_content += f"== {prop_name}\n"
for prop_name, prop_data in properties.items():
heading_prefix = "=" * level # Create heading based on level
asciidoc_content += f"{heading_prefix} {prop_name}\n"
asciidoc_content += f"{prop_data.get('description', 'No description')}\n"

# Handle array types and generate description for array of arrays
if prop_data.get('type') == "array":
if isinstance(prop_data['items'], dict) and 'items' in prop_data['items']:
# Generate list for array of arrays
asciidoc_content += generate_asciidoc_array_of_arrays(prop_data['items']['items'], prop_data['items'].get('description', '')) + "\n"
elif isinstance(prop_data['items'], list):
# If it's a list of items, generate columns description directly
asciidoc_content += generate_asciidoc_array_of_arrays(prop_data['items'], prop_data.get('description', '')) + "\n"
else:
# Simple array, include the description of the array
asciidoc_content += f"\n{prop_data['items'].get('description', 'No description')}\n"

# Add pattern inline and handle escaping of backslashes and curly braces
if "pattern" in prop_data:
Expand All @@ -49,7 +84,48 @@ def generate_asciidoc(field_name, schema, required_fields):
asciidoc_content += "\n*Required:* No\n"

asciidoc_content += "\n"

# If there are nested properties, recursively generate content for them
if "properties" in prop_data:
nested_required_fields = prop_data.get('required', [])
asciidoc_content += generate_asciidoc_properties(
prop_data['properties'], nested_required_fields, level + 1
)

return asciidoc_content


def generate_asciidoc(field_name, schema, required_fields):
"""
Generate AsciiDoc content for the specified field based on the JSON schema.
Args:
field_name (str): The name of the field to generate documentation for.
schema (dict): The JSON schema dictionary.
required_fields (list): List of required fields for the specified field.
Returns:
str: The generated AsciiDoc content.
"""
asciidoc_content = f"= {field_name.capitalize()}\n\n"
field_data = schema['properties'][field_name]
asciidoc_content += field_data.get("description", "") + "\n\n"

# Generate the content for the properties, recursively handling nested properties
if 'properties' in field_data:
asciidoc_content += generate_asciidoc_properties(field_data['properties'], required_fields, level=2)
elif field_data.get('type') == 'array':
# Handle array fields directly
if 'items' in field_data and isinstance(field_data['items'], dict) and 'items' in field_data['items']:
# Array of arrays, generate list of columns
asciidoc_content += generate_asciidoc_array_of_arrays(field_data['items']['items'], field_data['items'].get('description', '')) + "\n"
elif isinstance(field_data['items'], list):
# Array of simple types, generate columns description
asciidoc_content += generate_asciidoc_array_of_arrays(field_data['items'], field_data.get('description', '')) + "\n"
else:
# Handle single item in array
asciidoc_content += f"\n{field_data['items'].get('description', 'No description')}\n"

return asciidoc_content


Expand Down

0 comments on commit 89d2146

Please sign in to comment.