def type_info_table(self, schema: SchemaNode) -> List[List]: """ Schema type info table : - type, - additional properties, - default value, - definitions links ready to be rendered by generate_table filter """ type_info = [] schema_type = schema.type_name default_value = schema.default_value type_info.append(["Type", "`combining`" if jinja_filters.is_combining(schema) else f"`{schema_type}`"]) if jinja_filters.deprecated(self.config, schema): type_info.append(["**Deprecated**", self.badge("Deprecated", "red")]) type_info.append(["**Additional properties**", self.additional_properties(schema)]) if schema.default_value: type_info.append(["**Default**", f"`{default_value}`"]) if schema.should_be_a_link(self.config): schema_link_name = schema.links_to.link_name html_id = schema.links_to.html_id type_info.append(["**Same definition as**", f"[{ schema_link_name }](#{ html_id })"]) elif schema.refers_to: type_info.append(["**Defined in**", schema.ref_path]) return type_info
def _get_jinja_template(self) -> Template: loader = FileSystemLoader(self.config.template_path.parent) env = jinja2.Environment( loader=loader, extensions=[loopcontrols], trim_blocks=(self.config.template_is_markdown), lstrip_blocks=(self.config.template_is_markdown), ) env.globals["jsfh_config"] = self.config env.globals["jsfh_md"] = markdown2.Markdown( extras=self.config.markdown_options) if self.config.template_is_markdown: md_template = MarkdownTemplate(self.config) md_template.register_jinja(env) env.filters["python_to_json"] = jinja_filters.python_to_json env.filters["get_default"] = ( jinja_filters.get_default_look_in_description if self.config.default_from_description else jinja_filters.get_default) env.filters["get_type_name"] = templating_utils.get_type_name env.filters["get_description"] = jinja_filters.get_description env.filters[ "get_numeric_restrictions_text"] = jinja_filters.get_numeric_restrictions_text env.filters[ "get_required_properties"] = jinja_filters.get_required_properties env.filters["get_first_property"] = jinja_filters.get_first_property env.filters[ "get_undocumented_required_properties"] = jinja_filters.get_undocumented_required_properties env.filters[ "highlight_json_example"] = jinja_filters.highlight_json_example env.filters[ "highlight_yaml_example"] = jinja_filters.highlight_yaml_example env.filters["yaml_example"] = jinja_filters.yaml_example env.filters["first_line"] = jinja_filters.first_line env.tests["combining"] = jinja_filters.is_combining env.tests["description_short"] = jinja_filters.is_text_short env.tests["deprecated"] = lambda schema: jinja_filters.deprecated( self.config, schema) env.globals["examples_as_yaml"] = self.config.examples_as_yaml env.globals["get_local_time"] = jinja_filters.get_local_time with open(self.config.template_path, "r") as template_fp: template = env.from_string(template_fp.read()) return template
def properties_table(self, schema: SchemaNode) -> List[List]: """ Generate list of properties ready to be rendered by generate_table filter """ properties = [] for sub_property in schema.iterate_properties: line = [] # property name property_name = "+ " if sub_property.is_required_property else "- " property_name += self.format_link(self.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 self.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(self.escape_for_table(self.first_line_fixed(description, const.LINE_WIDTH))) properties.append(line) if properties: # add header properties.insert(0, ["Property", "Pattern", "Type", "Deprecated", "Definition", "Title/Description"]) return properties
def generate_from_schema( schema_file: Union[str, Path, TextIO], loaded_schemas: Optional[Dict[str, Any]] = None, minify: bool = True, deprecated_from_description: bool = False, default_from_description: bool = False, expand_buttons: bool = False, link_to_reused_ref: bool = True, config: GenerationConfiguration = None, ) -> str: config = config or _get_final_config( minify=minify, deprecated_from_description=deprecated_from_description, default_from_description=default_from_description, expand_buttons=expand_buttons, copy_css=False, copy_js=False, link_to_reused_ref=link_to_reused_ref, ) templates_directory = os.path.join(config.templates_directory, config.template_name) base_template_path = os.path.join(templates_directory, TEMPLATE_FILE_NAME) md = markdown2.Markdown(extras=config.markdown_options) loader = FileSystemLoader(templates_directory) env = jinja2.Environment( loader=loader, extensions=[loopcontrols], trim_blocks=(config.template_name in ("md", "md_nested")), lstrip_blocks=(config.template_name in ("md", "md_nested")), ) if config.template_name in ("md", "md_nested"): md_template = MarkdownTemplate(config) md_template.register_jinja(env) env.filters["markdown"] = ( lambda text: jinja2.Markup(md.convert(text)) if config.description_is_markdown else lambda t: t ) env.filters["python_to_json"] = jinja_filters.python_to_json env.filters["get_default"] = ( jinja_filters.get_default_look_in_description if config.default_from_description else jinja_filters.get_default ) env.filters["get_type_name"] = templating_utils.get_type_name env.filters["get_description"] = ( jinja_filters.get_description_remove_default if config.default_from_description else jinja_filters.get_description ) env.filters["get_numeric_restrictions_text"] = jinja_filters.get_numeric_restrictions_text env.filters["get_required_properties"] = jinja_filters.get_required_properties env.filters["get_first_property"] = jinja_filters.get_first_property env.filters["get_undocumented_required_properties"] = jinja_filters.get_undocumented_required_properties env.filters["highlight_json_example"] = jinja_filters.highlight_json_example env.filters["first_line"] = jinja_filters.first_line env.tests["combining"] = jinja_filters.is_combining env.tests["description_short"] = jinja_filters.is_text_short env.tests["deprecated"] = lambda schema: jinja_filters.deprecated(config, schema) env.globals["get_local_time"] = jinja_filters.get_local_time with open(base_template_path, "r") as template_fp: template = env.from_string(template_fp.read()) if isinstance(schema_file, list): # Backward compatibility schema_file = os.path.sep.join(schema_file) intermediate_schema = build_intermediate_representation(schema_file, config, loaded_schemas) rendered = template.render(schema=intermediate_schema, config=config) if minify: if config.template_name in ("md", "md_nested"): # remove multiple contiguous empty lines rendered = re.sub(r"\n\s*\n", "\n\n", rendered) else: rendered = htmlmin.minify(rendered) return rendered