33This is done once the duplicate src directory is processed.
44"""
55import os
6- import pathlib
76import subprocess
7+ import yaml
8+ from pathlib import Path
9+
10+ HERE = Path (__file__ ).absolute ()
11+
12+
13+ def _find (path , filename ):
14+ return next (
15+ parent / filename
16+ for parent in path .parents
17+ if Path .is_file (parent / filename )
18+ )
819
920
1021def build_pdf (filename = "bids-spec.pdf" , logfile = "bids-spec_pandoc_log.json" ):
@@ -17,19 +28,26 @@ def build_pdf(filename="bids-spec.pdf", logfile="bids-spec_pandoc_log.json"):
1728 logfile : str
1829 Name of the log file. Defaults to "bids-spec_pandoc_log.json".
1930 """
20- # Files that are not supposed to be built into the PDF
21- EXCLUDE = ["./index.md" , "./schema/README.md" , "./pregh-changes.md" ]
22-
23- # Get all input files
24- markdown_list = []
25- for root , dirs , files in os .walk ("." ):
26- for file in files :
27- fpath = os .path .join (root , file )
28- if fpath .endswith (".md" ) and fpath not in EXCLUDE :
29- markdown_list .append (fpath )
30- elif fpath .endswith ("index.md" ):
31- # Special role for index.md
32- index_page = fpath
31+
32+ def _flatten_values (lst ):
33+ """Flatten a list of dicts of lists to a list of values."""
34+ for obj in lst :
35+ for val in obj .values ():
36+ if isinstance (val , str ):
37+ yield val
38+ else :
39+ yield from _flatten_values (val )
40+
41+ fname_mkdocs_yml = _find (HERE , "mkdocs.yml" )
42+
43+ with open (fname_mkdocs_yml , "r" ) as stream :
44+ mkdocs_yml = yaml .safe_load (stream )
45+
46+ sections = mkdocs_yml ["nav" ][0 ]["The BIDS Specification" ]
47+
48+ # special files
49+ index_page = "./index.md"
50+ pandoc_metadata = _find (HERE , "metadata.yml" )
3351
3452 # Prepare the command options
3553 cmd = [
@@ -43,26 +61,23 @@ def build_pdf(filename="bids-spec.pdf", logfile="bids-spec_pandoc_log.json"):
4361 f"--output={ filename } " ,
4462 ]
4563
46- # location of this file: This is also the working directory when
47- # the pdf is being built using `cd build_pdf_src` and then
48- # `bash build_pdf.sh`
49- root = pathlib .Path (__file__ ).parent .absolute ()
50-
5164 # Resources are searched relative to the working directory, but
5265 # we can add additional search paths using <path>:<another path>, ...
5366 # When in one of the appendices/ files there is a reference to
5467 # "../04-modality-specific-files/images/...", then we need to use
5568 # appendices/ as a resource-path so that the relative files can
5669 # be found.
57- cmd += [f'--resource-path=.:{ str (root / "appendices" )} ' ]
70+ build_root = HERE .parent
71+ cmd += [f'--resource-path=.:{ build_root / "appendices" } ' ]
5872
5973 # Add input files to command
6074 # The filenames in `markdown_list` will ensure correct order when sorted
61- cmd += [str (root / index_page )]
62- cmd += [str (root / i ) for i in ["../../metadata.yml" ] + sorted (markdown_list )]
75+ cmd += [str (build_root / index_page )]
76+ cmd += [str (pandoc_metadata )]
77+ cmd += [str (build_root / md ) for md in _flatten_values (sections )]
6378
6479 # print and run
65- print ("running : \n \n " + "\n " .join (cmd ))
80+ print ("pandoc command being run : \n \n " + "\n " .join (cmd ))
6681 subprocess .run (cmd )
6782
6883
0 commit comments