Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(md): Support customising property table columns #185

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,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"
]
}
}
}
},
Expand Down
10 changes: 10 additions & 0 deletions json_schema_for_humans/generation_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
OFFLINE_JS_FILE_NAMES,
)

DEFAULT_PROPERTIES_TABLE_COLUMNS = [
"Property",
"Pattern",
"Type",
"Deprecated",
"Definition",
"Title/Description",
]


@dataclass_json
@dataclass
Expand Down Expand Up @@ -62,6 +71,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
Expand Down
72 changes: 41 additions & 31 deletions json_schema_for_humans/md_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,42 +324,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

Expand Down
8 changes: 8 additions & 0 deletions tests/generation_configuration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading