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

Choose columns to be displayed in properties table in md template #220

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
14 changes: 11 additions & 3 deletions json_schema_for_humans/generation_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
from dataclasses import dataclass
from json import JSONDecodeError
from pathlib import Path
from typing import Any, Union, Dict, List, Optional
from typing import Any, Dict, List, Optional, Union, Tuple

import yaml
from dataclasses_json import dataclass_json

from json_schema_for_humans.const import (
DocumentationTemplate,
FileLikeType,
DEFAULT_CSS_FILE_NAME,
DEFAULT_JS_FILE_NAME,
OFFLINE_CSS_FILE_NAMES,
OFFLINE_FONT_FILE_NAMES,
OFFLINE_JS_FILE_NAMES,
DocumentationTemplate,
FileLikeType,
)


Expand Down Expand Up @@ -48,6 +48,14 @@ class GenerationConfiguration:
template_md_options: Optional[Dict[str, Any]] = None
with_footer: bool = True
footer_show_time: bool = True
properties_table_columns: Tuple[str] = (
"Property",
"Pattern",
"Type",
"Deprecated",
"Definition",
"Title/Description",
)

def __post_init__(self) -> None:
default_markdown_options = {
Expand Down
88 changes: 55 additions & 33 deletions json_schema_for_humans/md_template.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Dict, List, Union, Optional
from urllib.parse import quote_plus, quote
import logging
from typing import Dict, List, Optional, Union
from urllib.parse import quote, quote_plus

import jinja2

Expand Down Expand Up @@ -245,14 +246,14 @@ def heading(self, title: str, depth: int, html_id: Union[bool, str] = False, nes
if nested:
menu = "<strong>"

# generate markdown title with anchor (except if depth 0)
# generate markdown title
if depth == 0:
menu += f" {title}"
else:
if self.config.template_md_options.get("show_heading_numbers"):
menu += f' <a name="{html_id}"></a>{heading_numbers} {title}'
menu += f" {heading_numbers} {title}"
else:
menu += f' <a name="{html_id}"></a>{title}'
menu += f" {title}"

# store current heading in toc
toc_menu = f"[{title}](#{html_id})"
Expand All @@ -262,7 +263,12 @@ def heading(self, title: str, depth: int, html_id: Union[bool, str] = False, nes

if nested:
menu += "</strong>"
return menu

if depth == 0:
return menu
else:
# Add anchor link
return f'<a name="{html_id}"></a>\n{menu}'

def get_toc(self) -> str:
"""
Expand Down Expand Up @@ -290,7 +296,10 @@ def get_toc(self) -> str:
@staticmethod
def format_link(title: str, link: str, tooltip: str = "") -> str:
"""Format a Markdown link"""
return f"[{title}](#{link} {tooltip})"
if tooltip:
return f"[{title}](#{link} {tooltip})"
else:
return f"[{title}](#{link})"

def badge(self, name: str, color: str, value: str = "", show_text: bool = False, fallback: bool = True) -> str:
"""
Expand Down Expand Up @@ -322,44 +331,57 @@ def properties_table(self, schema: SchemaNode) -> List[List]:
Generate list of properties ready to be rendered by generate_table filter
"""
properties = []
header = {}
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"
)

# title or description
description = sub_property.description or "-"
if sub_property.title:
description = sub_property.title

# 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)
definition_info = "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)
definition_info = "In " + sub_property.ref_path
else:
line.append("-")
definition_info = "-"

# title or description
description = sub_property.description or "-"
if sub_property.title:
description = sub_property.title
if sub_property.property_name is None or not sub_property.property_name.strip():
continue

line.append(escape_for_table(description))
header2value = {
"property": self.format_link(escape_for_table(sub_property.property_name), sub_property.html_id),
"required": "Yes" if sub_property.is_required_property else "No",
"pattern": "Yes" if sub_property.is_pattern_property else "No",
"type": "Combination"
if jinja_filters.is_combining(sub_property)
else escape_for_table(sub_property.type_name),
"deprecated": self.badge("Deprecated", "red")
if jinja_filters.deprecated(self.config, sub_property)
else "No",
"definition": definition_info,
"title/description": escape_for_table(description),
"default": sub_property.default_value if sub_property.default_value is not None else "-",
}

for colname in self.config.properties_table_columns:
try:
line.append(header2value[colname.lower()])
header[colname.title()] = ""
except KeyError:
logging.warning(
"No value found for column '%s', passed in config 'properties_table_columns'. Ignoring.",
colname,
)

properties.append(line)

if properties:
# add header
properties.insert(0, ["Property", "Pattern", "Type", "Deprecated", "Definition", "Title/Description"])
if header:
properties.insert(0, list(header))

return properties

Expand Down