From 89a88885e6f119f61dce7f6a12f83e2e22dec5f0 Mon Sep 17 00:00:00 2001 From: Alex Jurkiewicz Date: Wed, 15 Feb 2023 21:07:47 +1100 Subject: [PATCH] feat(md): Support customising property table columns --- config_schema.json | 15 ++++ .../generation_configuration.py | 10 +++ json_schema_for_humans/md_template.py | 72 +++++++++++-------- tests/generation_configuration_test.py | 8 +++ 4 files changed, 74 insertions(+), 31 deletions(-) diff --git a/config_schema.json b/config_schema.json index 9492d7aa..83ef85aa 100644 --- a/config_schema.json +++ b/config_schema.json @@ -139,6 +139,21 @@ "type": "boolean", "description": "if true generate array restrictions section.\n\n if false, do not generate", "default": true + }, + "properties_table_columns": { + "type": "array", + "description": "array of column names to display in the properties table.\n\n if empty, the default is ['Property','Pattern','Type','Deprecated','Definition','Title/Description']", + "items": { + "type": "string", + "enum": [ + "Property", + "Pattern", + "Type", + "Deprecated", + "Definition", + "Title/Description" + ] + } } } }, diff --git a/json_schema_for_humans/generation_configuration.py b/json_schema_for_humans/generation_configuration.py index 6bd82c0c..f5c9c630 100644 --- a/json_schema_for_humans/generation_configuration.py +++ b/json_schema_for_humans/generation_configuration.py @@ -16,6 +16,15 @@ DEFAULT_JS_FILE_NAME, ) +DEFAULT_PROPERTIES_TABLE_COLUMNS = [ + "Property", + "Pattern", + "Type", + "Deprecated", + "Definition", + "Title/Description", +] + @dataclass_json @dataclass @@ -59,6 +68,7 @@ def __post_init__(self) -> None: "badge_as_image": False, "show_heading_numbers": True, "show_array_restrictions": True, + "properties_table_columns": DEFAULT_PROPERTIES_TABLE_COLUMNS, } default_template_md_options.update(self.template_md_options or {}) self.template_md_options = default_template_md_options diff --git a/json_schema_for_humans/md_template.py b/json_schema_for_humans/md_template.py index 1054f5c6..ad4253db 100644 --- a/json_schema_for_humans/md_template.py +++ b/json_schema_for_humans/md_template.py @@ -325,42 +325,52 @@ def properties_table(self, schema: SchemaNode) -> List[List]: properties = [] for sub_property in schema.iterate_properties: line: List[str] = [] - # property name - property_name = "+ " if sub_property.is_required_property else "- " - property_name += self.format_link(escape_for_table(sub_property.property_name), sub_property.html_id) - line.append(property_name) - # pattern - line.append("Yes" if sub_property.is_pattern_property else "No") - # type - line.append( - "Combination" if jinja_filters.is_combining(sub_property) else escape_for_table(sub_property.type_name) - ) - # Deprecated - line.append( - self.badge("Deprecated", "red") if jinja_filters.deprecated(self.config, sub_property) else "No" - ) - # Link - if sub_property.should_be_a_link(self.config): - line.append( - "Same as " + self.format_link(sub_property.links_to.link_name, sub_property.links_to.html_id) - ) - elif sub_property.refers_to: - line.append("In " + sub_property.ref_path) - else: - line.append("-") - - # title or description - description = sub_property.description or "-" - if sub_property.title: - description = sub_property.title - - line.append(escape_for_table(description)) + for field in self.config.template_md_options.get("properties_table_columns"): + if field == "Property": + # property name + property_name = "+ " if sub_property.is_required_property else "- " + property_name += self.format_link(escape_for_table(sub_property.property_name), sub_property.html_id) + line.append(property_name) + elif field == "Pattern": + # pattern + line.append("Yes" if sub_property.is_pattern_property else "No") + elif field == "Type": + # type + line.append( + "Combination" if jinja_filters.is_combining(sub_property) else escape_for_table(sub_property.type_name) + ) + elif field == "Deprecated": + # Deprecated + line.append( + self.badge("Deprecated", "red") \ + if jinja_filters.deprecated(self.config, sub_property) \ + else "No" + ) + elif field == "Definition": + # Link + if sub_property.should_be_a_link(self.config): + line.append( + "Same as " + self.format_link(sub_property.links_to.link_name, sub_property.links_to.html_id) + ) + elif sub_property.refers_to: + line.append("In " + sub_property.ref_path) + else: + line.append("-") + elif field == "Title/Description": + # title or description + description = sub_property.description or "-" + if sub_property.title: + description = sub_property.title + line.append(escape_for_table(description)) + else: + raise ValueError(f"Unknown field {field} for properties table") properties.append(line) if properties: # add header - properties.insert(0, ["Property", "Pattern", "Type", "Deprecated", "Definition", "Title/Description"]) + headers = self.config.template_md_options.get("properties_table_columns") + properties.insert(0, headers) return properties diff --git a/tests/generation_configuration_test.py b/tests/generation_configuration_test.py index 5da2a10b..75be68c2 100644 --- a/tests/generation_configuration_test.py +++ b/tests/generation_configuration_test.py @@ -67,6 +67,14 @@ def test_override_template_md_options() -> None: "badge_as_image": False, "show_heading_numbers": True, "show_array_restrictions": True, + "properties_table_columns": [ + "Property", + "Pattern", + "Type", + "Deprecated", + "Definition", + "Title/Definition", + ], } # override badge_as_image key