| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- import subprocess
- from pathlib import Path
- import urllib.parse
- import os
- SCRIPT_DIR = Path(__file__).parent.resolve()
- def generate_example(
- filename, title, strip_asserts=True, insert_run_link=True, footer=None
- ):
- path_in = SCRIPT_DIR.parent / "examples" / f"{filename}.nbt"
- path_out = SCRIPT_DIR / "src" / f"example-{filename}.md"
- print(path_in)
- print(path_out)
- code = []
- with open(path_in, "r", encoding="utf-8") as fin:
- for line in fin:
- if not (strip_asserts and "assert_eq" in line):
- code.append(line)
- url = f"https://numbat.dev/?q={urllib.parse.quote_plus(''.join(code))}"
- with open(path_out, "w", encoding="utf-8") as fout:
- fout.write("<!-- This file is autogenerated! Do not modify it -->\n")
- fout.write("\n")
- fout.write(f"# {title}\n")
- if insert_run_link:
- fout.write(
- f'<a href="{url}"><i class="fa fa-play"></i> Run this example</a>\n'
- )
- fout.write("\n")
- fout.write("``` numbat\n")
- fout.writelines(code)
- fout.write("```\n")
- if footer:
- fout.write("\n")
- fout.write(footer)
- fout.write("\n")
- def xkcd_footer(number, img_name):
- footer = '<p align="center" style="margin-top: 2em">'
- footer += f'<a href="https://xkcd.com/{number}/"><img src="https://imgs.xkcd.com/comics/{img_name}.png" alt="XKCD {number}" style="max-width: 100%"></a>'
- footer += f'<br>Source: <a href="https://xkcd.com/{number}/">https://xkcd.com/{number}/</a>'
- footer += "</p>"
- return footer
- generate_example("acidity", "Acidity")
- generate_example("barometric_formula", "Barometric formula")
- generate_example("body_mass_index", "Body mass index")
- generate_example("factorial", "Factorial", strip_asserts=False)
- generate_example("medication_dosage", "Medication dosage")
- generate_example("molarity", "Molarity")
- generate_example("musical_note_frequency", "Musical note frequency")
- generate_example("paper_size", "Paper sizes")
- generate_example("pipe_flow_rate", "Flow rate in a pipe")
- generate_example("population_growth", "Population growth")
- generate_example("recipe", "Recipe")
- generate_example("voyager", "Voyager")
- generate_example(
- "xkcd_681",
- "XKCD 681",
- footer=xkcd_footer(681, "gravity_wells"),
- )
- generate_example(
- "xkcd_687", "XKCD 687", footer=xkcd_footer(687, "dimensional_analysis")
- )
- generate_example("xkcd_2585", "XKCD 2585", footer=xkcd_footer(2585, "rounding"))
- generate_example(
- "xkcd_2812", "XKCD 2812", footer=xkcd_footer(2812, "solar_panel_placement")
- )
- generate_example(
- "numbat_syntax", "Syntax overview", strip_asserts=False, insert_run_link=False
- )
- path_units = SCRIPT_DIR / "src" / "list-units.md"
- with open(path_units, "w", encoding="utf-8") as f:
- print("Generating list of units...", flush=True)
- subprocess.run(
- ["cargo", "run", "--release", "--quiet", "--example=inspect", "units"],
- stdout=f,
- text=True,
- )
- def list_of_functions(file_name, document):
- path = SCRIPT_DIR / "src" / f"list-functions-{file_name}.md"
- with open(path, "w", encoding="utf-8") as f:
- print(f"# {document['title']}\n", file=f, flush=True)
- if introduction := document.get("introduction"):
- print(introduction + "\n", file=f, flush=True)
- sections = document["sections"]
- if len(sections) >= 3:
- links = []
- for section in sections:
- if title := section.get("title"):
- link = title.lower().replace(" ", "-").replace(",", "")
- links.append(f"[{title}](#{link})")
- print(f"{' · '.join(links)}\n", file=f, flush=True)
- for section in sections:
- modules = section["modules"]
- if title := section.get("title"):
- print(f"## {title}\n", file=f, flush=True)
- print(f"Defined in: `{'`, `'.join(modules)}`\n", file=f, flush=True)
- for module in modules:
- print(
- f"Generating list of functions for module '{module}'...", flush=True
- )
- env = os.environ.copy()
- env["TZ"] = "UTC"
- subprocess.run(
- [
- "cargo",
- "run",
- "--release",
- "--quiet",
- "--example=inspect",
- "--",
- "functions",
- module,
- ],
- stdout=f,
- text=True,
- env=env,
- )
- list_of_functions(
- "math",
- {
- "title": "Mathematical functions",
- "sections": [
- {
- "title": "Basics",
- "modules": ["core::functions"],
- },
- {
- "title": "Transcendental functions",
- "modules": ["math::transcendental"],
- },
- {
- "title": "Trigonometry",
- "modules": ["math::trigonometry"],
- },
- {
- "title": "Statistics",
- "modules": ["math::statistics"],
- },
- {
- "title": "Random sampling, distributions",
- "modules": ["core::random", "math::distributions"],
- },
- {
- "title": "Number theory",
- "modules": ["math::number_theory"],
- },
- {
- "title": "Numerical methods",
- "modules": [
- "numerics::diff",
- "numerics::solve",
- "numerics::fixed_point",
- ],
- },
- {
- "title": "Geometry",
- "modules": ["math::geometry"],
- },
- {
- "title": "Algebra",
- "modules": ["extra::algebra"],
- },
- {
- "title": "Trigonometry (extra)",
- "modules": ["math::trigonometry_extra"],
- },
- ],
- },
- )
- list_of_functions(
- "lists",
- {
- "title": "List-related functions",
- "sections": [
- {
- "modules": ["core::lists"],
- },
- ],
- },
- )
- list_of_functions(
- "strings",
- {
- "title": "String-related functions",
- "sections": [
- {
- "modules": ["core::strings"],
- },
- ],
- },
- )
- list_of_functions(
- "datetime",
- {
- "title": "Date and time",
- "introduction": "See [this page](./date-and-time.md) for a general introduction to date and time handling in Numbat.",
- "sections": [
- {
- "modules": ["datetime::functions", "datetime::human"],
- },
- ],
- },
- )
- list_of_functions(
- "other",
- {
- "title": "Other functions",
- "sections": [
- {
- "title": "Error handling",
- "modules": ["core::error"],
- },
- {
- "title": "Floating point",
- "modules": ["core::numbers"],
- },
- {
- "title": "Quantities",
- "modules": ["core::quantities"],
- },
- {
- "title": "Chemical elements",
- "modules": ["chemistry::elements"],
- },
- {
- "title": "Mixed unit conversion",
- "modules": ["units::mixed"],
- },
- {
- "title": "Temperature conversion",
- "modules": ["physics::temperature_conversion"],
- },
- {
- "title": "Color format conversion",
- "modules": ["extra::color"],
- },
- ],
- },
- )
- subprocess.run(["mdbook", "build"], text=True)
|