build.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import subprocess
  2. from pathlib import Path
  3. SCRIPT_DIR = Path(__file__).parent
  4. def generate_example(filename, title, strip_assets):
  5. path_in = SCRIPT_DIR.parent / "examples" / f"{filename}.nbt"
  6. path_out = SCRIPT_DIR / "src" / f"example-{filename}.md"
  7. code = []
  8. with open(path_in, "r") as fin:
  9. for line in fin:
  10. if not strip_assets or "assert_eq" not in line:
  11. code.append(line)
  12. with open(path_out, "w") as fout:
  13. fout.write("<!-- This file is autogenerated! Do not modify it -->\n")
  14. fout.write("\n")
  15. fout.write(f"# {title}\n")
  16. fout.write("\n")
  17. fout.write("``` numbat\n")
  18. fout.writelines(code)
  19. fout.write("```\n")
  20. generate_example("acidity", "Acidity", True)
  21. generate_example("barometric_formula", "Barometric formula", True)
  22. generate_example("body_mass_index", "Body mass index", True)
  23. generate_example("factorial", "Factorial", False)
  24. generate_example("medication_dosage", "Medication dosage", True)
  25. generate_example("molarity", "Molarity", True)
  26. generate_example("musical_note_frequency", "Musical note frequency", True)
  27. generate_example("paper_size", "Paper sizes", False)
  28. generate_example("pipe_flow_rate", "Flow rate in a pipe", True)
  29. generate_example("population_growth", "Population growth", True)
  30. generate_example("recipe", "Recipe", True)
  31. generate_example("voyager", "Voyager", True)
  32. generate_example("xkcd_687", "XKCD 687", True)
  33. generate_example("xkcd_2585", "XKCD 2585", True)
  34. generate_example("xkcd_2812", "XKCD 2812", True)
  35. generate_example("workhours", "Workhours", True)
  36. generate_example("numbat_syntax", "Syntax overview", False)
  37. path_units = SCRIPT_DIR / "src" / "list-units.md"
  38. with open(path_units, "w") as f:
  39. subprocess.run(["cargo", "run", "--example=inspect"], stdout=f, text=True)
  40. subprocess.run(["mdbook", "build"], text=True)