Applied sphinx-inline-tabs to remaining examples, refs #1153

This commit is contained in:
Simon Willison 2023-07-08 11:00:08 -07:00
commit c076fb65e0
8 changed files with 1019 additions and 152 deletions

View file

@ -1,13 +1,25 @@
import json
import textwrap
import yaml
from yaml import safe_dump
from ruamel.yaml import round_trip_load
def metadata_example(cog, example):
def metadata_example(cog, data=None, yaml=None):
assert data or yaml, "Must provide data= or yaml="
assert not (data and yaml), "Cannot use data= and yaml="
output_yaml = None
if yaml:
# dedent it first
yaml = textwrap.dedent(yaml).strip()
# round_trip_load to preserve key order:
data = round_trip_load(yaml)
output_yaml = yaml
else:
output_yaml = safe_dump(data, sort_keys=False)
cog.out("\n.. tab:: YAML\n\n")
cog.out(" .. code-block:: yaml\n\n")
cog.out(textwrap.indent(yaml.safe_dump(example, sort_keys=False), " "))
cog.out(textwrap.indent(output_yaml, " "))
cog.out("\n\n.. tab:: JSON\n\n")
cog.out(" .. code-block:: json\n\n")
cog.out(textwrap.indent(json.dumps(example, indent=2), " "))
cog.out(textwrap.indent(json.dumps(data, indent=2), " "))
cog.out("\n")