Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Link to filename element definitions in filename templates #1228

Merged
merged 29 commits into from
Aug 23, 2022
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
936202c
Try linking to entities in make_filename_template.
tsalo Aug 17, 2022
cc5cba2
Update tools/schemacode/bidsschematools/render.py
tsalo Aug 18, 2022
6be1de7
Update render.py
tsalo Aug 18, 2022
ae4ee43
Link datatype.
tsalo Aug 18, 2022
100a795
Link to suffix as well.
tsalo Aug 18, 2022
0ec6e24
Extensions.
tsalo Aug 18, 2022
56299b2
Merge branch 'master' into link-in-templates
tsalo Aug 18, 2022
045ddd2
Fix.
tsalo Aug 18, 2022
bea43e6
Update render.py
tsalo Aug 18, 2022
140dae5
Run black.
tsalo Aug 18, 2022
244a0e7
Make things wonderful.
tsalo Aug 18, 2022
4220c71
Update render.py
tsalo Aug 18, 2022
48b30aa
Update test.
tsalo Aug 18, 2022
3f45e9c
Fix PDF build?
tsalo Aug 18, 2022
1cfaa52
Update macros.py
tsalo Aug 18, 2022
4114cae
Okay, fix more stuff.
tsalo Aug 18, 2022
2e3e96a
Le sigh
tsalo Aug 18, 2022
9ec9fab
Merge branch 'master' into link-in-templates
tsalo Aug 18, 2022
5ce5590
Docstring improvements.
tsalo Aug 18, 2022
384d975
Update tools/schemacode/bidsschematools/render.py
tsalo Aug 18, 2022
c829fe7
Update render.py
tsalo Aug 18, 2022
cc5da79
Glossary headings have to be lower case.
tsalo Aug 18, 2022
96b83ff
Merge branch 'master' into link-in-templates
effigies Aug 18, 2022
5f38ece
Merge branch 'master' into link-in-templates
tsalo Aug 18, 2022
7a81694
Merge branch 'master' into link-in-templates
tsalo Aug 19, 2022
82904dc
Merge branch 'master' into link-in-templates
tsalo Aug 19, 2022
7912845
Merge branch 'master' into link-in-templates
tsalo Aug 22, 2022
1f76c9b
Update render.py
tsalo Aug 22, 2022
2f5165a
Update tools/schemacode/bidsschematools/render.py
tsalo Aug 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions tools/schemacode/bidsschematools/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def _add_entity(filename_template, entity_pattern, requirement_level):
return filename_template


def make_filename_template(schema, n_dupes_to_combine=6, **kwargs):
def make_filename_template(schema, n_dupes_to_combine=6, src_path=None, **kwargs):
"""Create codeblocks containing example filename patterns for a given datatype.

Parameters
Expand All @@ -189,6 +189,9 @@ def make_filename_template(schema, n_dupes_to_combine=6, **kwargs):
n_dupes_to_combine : int
The minimum number of suffixes/extensions to combine in the template as
<suffix>/<extension>.
src_path : str | None
The file where this macro is called, which may be explicitly provided
by the "page.file.src_path" variable.
kwargs : dict
Keyword arguments used to filter the schema.
Example kwargs that may be used include: "suffixes", "datatypes",
Expand All @@ -205,11 +208,16 @@ def make_filename_template(schema, n_dupes_to_combine=6, **kwargs):

paragraph = ""
# Parent directories
paragraph += "{}-<{}>/\n\t[{}-<{}>/]\n".format(
schema["objects"]["entities"]["subject"]["name"],
schema["objects"]["entities"]["subject"]["format"],
schema["objects"]["entities"]["session"]["name"],
schema["objects"]["entities"]["session"]["format"],
paragraph += (
'<a href="SPEC_ROOT/99-appendices/09-entities.md#subject">'
tsalo marked this conversation as resolved.
Show resolved Hide resolved
f'{schema["objects"]["entities"]["subject"]["name"]}-&lt;'
f'{schema["objects"]["entities"]["subject"]["format"]}&gt;'
"</a>\n\t"
"["
'<a href="SPEC_ROOT/99-appendices/09-entities.md#session">'
f'{schema["objects"]["entities"]["session"]["name"]}-&lt;'
f'{schema["objects"]["entities"]["session"]["format"]}&gt;'
"</a>]\n"
)

datatypes = schema.rules.datatypes
Expand All @@ -226,22 +234,26 @@ def make_filename_template(schema, n_dupes_to_combine=6, **kwargs):
for ent in entity_order:
if "enum" in schema["objects"]["entities"][ent].keys():
# Entity key-value pattern with specific allowed values
ent_format = "{}-<{}>".format(
schema["objects"]["entities"][ent]["name"],
"|".join(schema["objects"]["entities"][ent]["enum"]),
ent_format = (
f'<a href="SPEC_ROOT/99-appendices/09-entities.md#{ent}">'
f'{schema["objects"]["entities"][ent]["name"]}-&lt;'
f'{"|".join(schema["objects"]["entities"][ent]["enum"])}&gt;'
"</a>"
)
else:
# Standard entity key-value pattern with simple label/index
ent_format = "{}-<{}>".format(
schema["objects"]["entities"][ent]["name"],
schema["objects"]["entities"][ent].get("format", "label"),
ent_format = (
f'<a href="SPEC_ROOT/99-appendices/09-entities.md#{ent}">'
f'{schema["objects"]["entities"][ent]["name"]}-&lt;'
f'{schema["objects"]["entities"][ent].get("format", "label")}&gt;'
"</a>"
)

if ent in group["entities"]:
if isinstance(group["entities"][ent], dict):
if "enum" in group["entities"][ent].keys():
# Overwrite the filename pattern based on the valid values
ent_format = "{}-<{}>".format(
ent_format = "{}-&lt;{}&gt;".format(
schema["objects"]["entities"][ent]["name"],
"|".join(group["entities"][ent]["enum"]),
)
Expand All @@ -257,7 +269,7 @@ def make_filename_template(schema, n_dupes_to_combine=6, **kwargs):
# In cases of large numbers of suffixes,
# we use the "suffix" variable and expect a table later in the spec
if len(group["suffixes"]) >= n_dupes_to_combine:
suffix = "_<suffix>"
suffix = "_&lt;suffix&gt;"
string += suffix
strings = [string]
else:
Expand All @@ -266,14 +278,14 @@ def make_filename_template(schema, n_dupes_to_combine=6, **kwargs):
# Add extensions
full_strings = []
extensions = group["extensions"]
extensions = [ext if ext != "*" else ".<extension>" for ext in extensions]
extensions = [ext if ext != "*" else ".&lt;extension&gt;" for ext in extensions]
extensions = utils.combine_extensions(extensions)
if len(extensions) >= n_dupes_to_combine:
# Combine exts when there are many, but keep JSON separate
if ".json" in extensions:
extensions = [".<extension>", ".json"]
extensions = [".&lt;extension&gt;", ".json"]
else:
extensions = [".<extension>"]
extensions = [".&lt;extension&gt;"]

for extension in extensions:
for string in strings:
Expand All @@ -285,8 +297,9 @@ def make_filename_template(schema, n_dupes_to_combine=6, **kwargs):
paragraph += "\n".join(full_strings) + "\n"

paragraph = paragraph.rstrip()
codeblock = "Template:\n```Text\n" + paragraph + "\n```"
codeblock = 'Template:\n<pre style="overflow-x:scroll;">\n' + paragraph + "\n</pre>"
codeblock = codeblock.expandtabs(4)
codeblock = codeblock.replace("SPEC_ROOT", get_relpath(src_path))
return codeblock


Expand Down