def test_generate_from_file_name_with_invalid_output_dir( tmp_path: Path) -> None: """Test generating from file names for input and output where the output directory is absent""" test_case_path = get_test_case_path("basic") result_path = tmp_path / "nonsense" / "result_with_another_name.html" with pytest.raises(FileNotFoundError) as exception_info: generate_from_filename(test_case_path, str(result_path.resolve()), False, False, False, False) assert f"{os.path.dirname(result_path)} not found" in str( exception_info.value)
def test_generate_from_file_name_deprecation_warning( tmp_path: Path, caplog: LogCaptureFixture, minify: bool, expect_warning: bool) -> None: test_case_path = get_test_case_path("basic") result_path = tmp_path / "result_file.html" caplog.set_level(logging.INFO) generate_from_filename(test_case_path, str(result_path.resolve()), minify=minify) _assert_deprecation_message(caplog, expect_warning)
def test_generate_from_file_name(tmp_path: Path) -> None: """Test generating from file names for input and output""" test_case_path = get_test_case_path("basic") result_path = tmp_path / "result_with_another_name.html" generate_from_filename(test_case_path, str(result_path.resolve()), False, False, False, False) with result_path.open(encoding="utf-8") as result_fp: soup = BeautifulSoup(result_fp.read(), "html.parser") assert (tmp_path / "result_with_another_name.html").exists() assert (tmp_path / "schema_doc.min.js").exists()
def generate_docs(schemaName: str, format: str): template_name = "js" if format == "html" else "md_nested" file_name = "html" if format == "html" else "md" outdir = Path(f"output") outdir.mkdir(parents=True, exist_ok=True) doc = outdir.joinpath(f"{schemaName}.{file_name}") schema_file = Path(f"schema/{schemaName}.schema.json") config = GenerationConfiguration( template_name=template_name, show_toc=True, link_to_reused_ref=False, collapse_long_descriptions=False, ) generate_from_filename(schema_file, doc, config=config) print(f"Created {doc}...")
def generate_each_template( examples_md_file, configurations, template_names, case_path, case_url, case_name, examples_dir ) -> None: """ Generate examples from JSON case file for each template selected """ print(f"Generating example {case_name}") examples_md_file.write(f"\n## --{case_name}--") if case_name in CASES_DESCRIPTION_DICT: case_data = CASES_DESCRIPTION_DICT[case_name] case_display_name = case_data["display_name"] case_description = case_data["description"] examples_md_file.write(f"\n### {case_display_name}\n\n") if case_description: examples_md_file.write(f"**Description:** {case_description}\n") else: examples_md_file.write(f"\n### {case_name}\n") examples_md_file.write(MD_EXAMPLE_JSON_TEMPLATE.format(file_url=case_url, title="Json schema")) for config in configurations: template_configuration = config["config"] template_name = template_configuration.template_name example_dir_name = config["dir_name"] example_file_name = case_name + (".md" if template_name in ("md", "md_nested") else ".html") examples_md_file.write( config["md_example_template"].format( file_url=f"examples/{example_dir_name}/{example_file_name}", title=config["title"] ) ) if not (template_name in template_names): continue example_dest_dir = os.path.join(examples_dir, example_dir_name) os.makedirs(example_dest_dir, exist_ok=True) example_file_path = os.path.join(example_dest_dir, example_file_name) generate_from_filename(case_path, example_file_path, config=template_configuration) remove_generated_timestamp(example_file_path)
def generate_each_template(examples_md_file: FileLikeType, case_path: str, case_url: str, case_name: str) -> None: """ Generate examples from JSON case file for each template selected """ print(f"Generating example {case_name}") examples_md_file.write(f"\n## --{case_name}--") if case_name in CASES_DESCRIPTION_DICT: case_data = CASES_DESCRIPTION_DICT[case_name] case_display_name = case_data["display_name"] case_description = case_data["description"] examples_md_file.write(f"\n### {case_display_name}\n\n") if case_description: examples_md_file.write(f"**Description:** {case_description}\n") else: examples_md_file.write(f"\n### {case_name}\n") examples_md_file.write( MD_EXAMPLE_JSON_TEMPLATE.format(file_url=case_url, title="Json schema")) for config in CONFIGURATIONS: template_configuration = config["config"] example_dir_name = config["dir_name"] example_file_name = f"{case_name}.{template_configuration.result_extension}" examples_md_file.write(config["md_example_template"].format( file_url=f"examples/{example_dir_name}/{example_file_name}", title=config["title"])) example_dest_dir = os.path.join(EXAMPLES_DIR, example_dir_name) os.makedirs(example_dest_dir, exist_ok=True) example_file_path = os.path.join(example_dest_dir, example_file_name) generate_from_filename(case_path, example_file_path, config=template_configuration) remove_generated_timestamp(example_file_path)
TEMPLATE_FILE_NAME = "base_nohtml.html" generate.TEMPLATE_FILE_NAME = TEMPLATE_FILE_NAME entries = [ 'object', 'place', 'person', 'group', 'set', 'text', 'image', 'provenance', 'event', 'digital' ] header = """--- title: "Linked Art Schema: {{title}}" up_href: "/api/1.0/" up_label: "Linked Art API 1.0" --- """ for s in entries: print(f"Building documentation for {s}") generate_from_filename(f"schema/{s}.json", f"html/{s}.html", config=config) # Jinja loses the linebreaks, so write markdown header in by hand fh = open(f'html/{s}.html') data = fh.read() fh.close() out = header.replace('{{title}}', s.title()) + "\n" + data fh = open(f'html/{s}.html', 'w') fh.write(out) fh.close() # Can GET info from https://spinup.internal.yale.edu/api/v2/storage/5446 #os.system("aws s3 --profile linked-data.yalespace.org sync html s3://linked-data.yalespace.org/docs/model/") #os.system("aws cloudfront --profile linked-data.yalespace.org create-invalidation --distribution-id E1CKFKVC1UT9DM --paths \"/*\"")
name, _ = os.path.splitext(name) case_source = os.path.abspath(os.path.join(SCHEMA_DIR_PATH, case_name)) if os.path.isfile(case_source) and ext == ".json": # replace @id with DOCS_URL_PATH + basename with open(case_source, "r") as f: obj = json.load(f) obj["$id"] = f"{DOCS_URL_PATH}/{SCHEMA_DIR_PATH}/{obj['$id']}" print("Updated $id: ", obj["$id"]) print("Writing to index") yaml_data_dict = { 'title': obj["title"], 'description': obj["description"] } out.append(yaml_data_dict) print("Writing to: ", os.path.join(DOCS_SCHEMA_PATH, case_name)) with open(os.path.join(DOCS_SCHEMA_PATH, case_name), 'w') as f: json.dump(obj, f, indent=4, sort_keys=False) print(f"Generating example {name}") config = GenerationConfiguration(recursive_detection_depth=10000, expand_buttons=True, deprecated_from_description=True) generate_from_filename(os.path.join(SCHEMA_DIR_PATH, case_name), os.path.join(DOCS_PATH, f"{name}.html"), config=config) # Write site index YAML #pprint.pprint(out) with open(os.path.join(DOCS_PATH, "_data", "index.yml"), 'w') as yamlfile: yaml.safe_dump(out, yamlfile)
json_examples_dir = os.path.join(os.getcwd(), "_includes", "examples") os.makedirs(json_examples_dir, exist_ok=True) asserts_dir = os.path.join(os.getcwd(), "assets") results_example_dir = os.path.join(asserts_dir, "examples") os.makedirs(results_example_dir, exist_ok=True) readme = "README.md" readme_location = os.path.abspath( os.path.join(os.path.dirname(os.getcwd()), readme)) shutil.copyfile(readme_location, os.path.join(includes_dir, readme)) config_schema = "config_schema.json" config_schema_location = os.path.abspath( os.path.join(os.path.dirname(os.getcwd()), config_schema)) generate_from_filename(config_schema_location, os.path.join(asserts_dir, "config_schema.html"), expand_buttons=True) for case_name in os.listdir(cases_source_dir): name, ext = os.path.splitext(case_name) case_source = os.path.abspath(os.path.join(cases_source_dir, case_name)) if not os.path.isfile(case_source) or ext != ".json": continue shutil.copyfile(case_source, os.path.join(json_examples_dir, case_name)) print(f"Generating example {name}") generate_from_filename( case_source, os.path.join(results_example_dir, f"{name}.html"),
cases_source_dir = os.path.abspath( os.path.join(os.path.dirname(os.getcwd()), "tests", "cases")) includes_dir = os.path.join(os.getcwd(), "_includes") json_examples_dir = os.path.join(os.getcwd(), "_includes", "examples") os.makedirs(json_examples_dir, exist_ok=True) results_example_dir = os.path.join(os.getcwd(), "assets", "examples") os.makedirs(results_example_dir, exist_ok=True) readme = "README.md" readme_location = os.path.abspath( os.path.join(os.path.dirname(os.getcwd()), readme)) shutil.copyfile(readme_location, os.path.join(includes_dir, readme)) for case_name in os.listdir(cases_source_dir): name, ext = os.path.splitext(case_name) case_source = os.path.abspath(os.path.join(cases_source_dir, case_name)) if not os.path.isfile(case_source) or ext != ".json": continue shutil.copyfile(case_source, os.path.join(json_examples_dir, case_name)) print(f"Generating example {name}") generate_from_filename( case_source, os.path.join(results_example_dir, f"{name}.html"), deprecated_from_description=True, expand_buttons=True, )
import sys from json_schema_for_humans.generate import generate_from_filename from json_schema_for_humans.generation_configuration import GenerationConfiguration config = GenerationConfiguration(copy_css=True, expand_buttons=True, minify=False, link_to_reused_ref=False, footer_show_time=False) schema = sys.argv[1] source_dir = sys.argv[2] output_dir = sys.argv[3] schema_path = source_dir + "/" + schema + ".json" output_path = output_dir + "/" + schema + ".html" generate_from_filename(schema_path, output_path, config=config)