11"""unc_website.source.make_spec_version_toc - module containing function to build TOC page for different specification versions"""
2-
3- from os import listdir
4- from os .path import isdir
2+ import os . path
3+ from os import listdir , makedirs
4+ from os .path import isdir , dirname , abspath
55from os .path import join as pjoin
6-
6+ import subprocess
7+ import shutil
8+ from pathlib import Path
79
810__author__ = "Sam Hunt <sam.hunt@npl.co.uk>"
911__all__ = ["make_spec_version_toc" ]
1012
13+ # Directories
14+ SOURCE_DIR = abspath (dirname (__file__ ))
1115
1216# Spec TOC filename
1317SPEC_FNAME = "spec_toc.rst"
1418
1519# URL of folder where PDFs are stored
16- PDF_URL = "https://comet-toolkit.github.io/unc_website/_static"
20+ PDF_PATH = "build/html/_static/spec_pdfs"
21+ PDF_URL = "https://comet-toolkit.github.io/unc_website/_static/spec_pdfs"
1722
1823# Static page content elements
1924HEADER = ("======================================\n "
@@ -36,7 +41,7 @@ def make_spec_version_toc(spec_dir: str):
3641 :param spec_dir: directory containing specification versions
3742 """
3843
39- toc_path = pjoin (spec_dir , SPEC_FNAME )
44+ toc_path = pjoin (SOURCE_DIR , spec_dir , SPEC_FNAME )
4045 page_content = ""
4146
4247 # add header
@@ -45,7 +50,7 @@ def make_spec_version_toc(spec_dir: str):
4550 # each version of the spec is a separate folder in the spec_dir
4651 spec_vers = [f for f in listdir (spec_dir ) if isdir (pjoin (spec_dir , f ))]
4752 spec_vers_toc_paths = [f"{ f } /unc_specification" for f in spec_vers ]
48- spec_vers_urls = [f"{ PDF_URL } /{ f } /unc_specification .pdf" for f in spec_vers ]
53+ spec_vers_urls = [f"{ PDF_URL } /{ f } /unc_specification_ { f } .pdf" for f in spec_vers ]
4954
5055 # add web version TOC
5156 page_content += WEB_VERS_SUBHEADER
@@ -64,5 +69,36 @@ def make_spec_version_toc(spec_dir: str):
6469 f .write (page_content )
6570
6671
72+ def build_spec_pdfs (spec_dir : str ):
73+
74+ abs_spec_dir = pjoin (SOURCE_DIR , spec_dir )
75+
76+ spec_vers = [f for f in listdir (abs_spec_dir ) if isdir (pjoin (abs_spec_dir , f ))]
77+ spec_ver_dirs = [pjoin (abs_spec_dir , f ) for f in spec_vers if isdir (pjoin (abs_spec_dir , f ))]
78+
79+ conf_file_path = pjoin (SOURCE_DIR , "conf.py" )
80+
81+ for spec_ver , spec_ver_dir in zip (spec_vers , spec_ver_dirs ):
82+
83+ ver_conf_file_path = pjoin (spec_ver_dir , "conf.py" )
84+ shutil .copyfile (conf_file_path , ver_conf_file_path )
85+
86+ tmp_index_path = pjoin (spec_ver_dir , "index.rst" )
87+
88+ Path (tmp_index_path ).touch ()
89+
90+ with open (ver_conf_file_path , "a" ) as f :
91+
92+ f .write (f"version = '{ spec_ver } '\n " )
93+ f .write ("pdf_documents = [('unc_specification', f'unc_specification_{version}', f'{project_title}', f'{author}'),]" )
94+
95+ makedirs (pjoin (PDF_PATH , spec_ver ), exist_ok = True )
96+
97+ subprocess .run (["sphinx-build" , "-b" , "pdf" , spec_ver_dir , pjoin (PDF_PATH , spec_ver )])
98+
99+ os .remove (tmp_index_path )
100+ os .remove (ver_conf_file_path )
101+
102+
67103if __name__ == "__main__" :
68- make_spec_version_toc ( "source/ specification" )
104+ build_spec_pdfs ( " specification" )
0 commit comments