Skip to content

Commit f778ec1

Browse files
committed
replace bookdown build script w/ quarto build script
1 parent 11d6e6e commit f778ec1

File tree

2 files changed

+83
-212
lines changed

2 files changed

+83
-212
lines changed

build.py

100755100644
+83-76
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
#!/usr/bin/env python
22
"""
3-
build docs for specified MAJOR, MINOR version
4-
and specified document (optional), format (optional)
5-
when document and format not specified, builds all documents in all formats.
6-
7-
arg 1: MAJOR version number
8-
arg 2: MINOR version number
9-
optional arg 3: specified document or "all"
10-
optional arg 4: specified format or "all"
3+
Build docs website and/or pdfs.
4+
5+
Command line arguments:
6+
7+
arg 1: MAJOR version number
8+
arg 2: MINOR version number
9+
optional arg 3: format, either "pdf" or "website"; defaults to both
10+
optional arg 4: pdf doc, defaults to "all", ignored if arg3 is "website"
11+
12+
Quarto builds in subdirectory of `src`, and then
13+
resulting files are moved to directory named `docs/MAJOR_MINOR`
14+
Document pdf has subtitle "Version MAJOR dot MINOR".
15+
Directory and filenames have version string "MAJOR underscore MINOR".
1116
"""
1217

1318
import glob
@@ -18,8 +23,13 @@
1823
import sys
1924
import contextlib
2025

21-
all_docs = ("functions-reference", "reference-manual", "stan-users-guide", "cmdstan-guide")
22-
all_formats = ("html", "pdf")
26+
all_docs = (
27+
"functions-reference",
28+
"reference-manual",
29+
"stan-users-guide",
30+
"cmdstan-guide",
31+
)
32+
2333

2434
@contextlib.contextmanager
2535
def pushd(new_dir):
@@ -28,101 +38,98 @@ def pushd(new_dir):
2838
yield
2939
os.chdir(previous_dir)
3040

41+
3142
def shexec(command):
32-
ret = subprocess.run(command, shell=True, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
43+
ret = subprocess.run(
44+
command,
45+
shell=True,
46+
check=False,
47+
stdout=subprocess.PIPE,
48+
stderr=subprocess.PIPE,
49+
universal_newlines=True,
50+
)
3351
if ret.returncode != 0:
34-
print('Command {} failed'.format(command))
52+
print("Command {} failed".format(command))
3553
print(ret.stderr)
3654
raise Exception(ret.returncode)
3755

38-
def check_bookdown_at_least_0_23():
39-
command = ["Rscript", "-e", "utils::packageVersion(\"bookdown\") > 0.22"]
40-
check = subprocess.run(command, capture_output=True, text=True)
41-
if not 'TRUE' in check.stdout:
42-
print("R Package bookdown version must be at least 0.23, see README for build tools requirements")
43-
sys.exit(1)
4456

4557
def safe_rm(fname):
4658
if os.path.exists(fname):
4759
os.remove(fname)
4860

49-
def make_docs(docspath, version, document, formats):
61+
62+
def make_pdfs(docspath, version, document):
5063
path = os.getcwd()
5164
srcpath = os.path.join(path, "src", document)
5265
with pushd(srcpath):
53-
if ("pdf" in formats):
54-
print('render {} as pdf'.format(document))
55-
command = "Rscript -e \"bookdown::render_book(\'index.Rmd\', output_format=\'bookdown::pdf_book\')\""
56-
shexec(command)
57-
srcpdf = os.path.join(srcpath, "_book", "_main.pdf")
58-
pdfname = ''.join([document,'-',version,".pdf"])
59-
pdfpath = os.path.join(docspath, pdfname)
60-
shutil.move(srcpdf, pdfpath)
61-
print('output file: {}'.format(pdfpath))
62-
if ("html" in formats):
63-
print('render {} as html'.format(document))
64-
command = "Rscript -e \"bookdown::render_book(\'index.Rmd\', output_format=\'bookdown::gitbook\')\""
65-
shexec(command)
66-
[safe_rm(f) for f in ("stan-manual.css", "_main.rds")]
67-
[safe_rm(f) for f in glob.glob(os.path.join("_book","*.md"))]
68-
htmlpath = os.path.join(docspath, document)
69-
shutil.rmtree(htmlpath, ignore_errors=True)
70-
command = ' '.join(["mv _book", htmlpath])
71-
shexec(command)
72-
print('output dir: {}'.format(htmlpath))
73-
else:
74-
[safe_rm(f) for f in ("stan-manual.css", "_main.rds")]
75-
shutil.rmtree("_book", ignore_errors=True)
66+
pdfname = "".join([document, "-", version, ".pdf"])
67+
print("render {} as {}".format(document, pdfname))
68+
command = " ".join(["quarto render", "--output-dir _pdf", "--output", pdfname])
69+
shexec(command)
70+
outpath = os.path.join(srcpath, "_pdf", pdfname)
71+
safe_rm(os.path.join(docspath, pdfname))
72+
shutil.move(outpath, docspath)
73+
7674

7775
def main():
78-
if sys.version_info < (3, 7):
79-
print('requires Python 3.7 or higher, found {}'.format(sys.version))
76+
if sys.version_info < (3, 8): # required by shutil.copytree
77+
print("requires Python 3.8 or higher, found {}".format(sys.version))
8078
sys.exit(1)
81-
check_bookdown_at_least_0_23()
8279
global all_docs
83-
global all_formats
84-
if (len(sys.argv) > 2):
80+
build_web = True
81+
build_pdfs = True
82+
docset = all_docs
83+
84+
if len(sys.argv) > 2:
8585
stan_major = int(sys.argv[1])
8686
stan_minor = int(sys.argv[2])
8787
else:
88-
print("Expecting arguments MAJOR MINOR version numbers")
88+
print("Expecting version number args MAJOR MINOR")
8989
sys.exit(1)
9090

91-
stan_version = '_'.join([str(stan_major), str(stan_minor)])
91+
stan_version = "_".join([str(stan_major), str(stan_minor)])
9292
path = os.getcwd()
9393
docspath = os.path.join(path, "docs", stan_version)
94-
if (not(os.path.exists(docspath))):
94+
if not (os.path.exists(docspath)):
9595
try:
9696
os.makedirs(docspath)
9797
except OSError:
98-
print("Creation of the directory %s failed" % docspath)
98+
print("Failed to create directory %s" % docspath)
99+
sys.exit(1)
99100
else:
100-
print("Successfully created the directory %s " % docspath)
101+
print("Created directory %s " % docspath)
101102

102-
docset = all_docs
103-
if (len(sys.argv) > 3):
104-
if (sys.argv[3] != "all"):
105-
if (sys.argv[3] not in docset):
106-
print("Expecting one of %s" % ' '.join(docset))
107-
sys.exit(1)
108-
docset = (sys.argv[3],)
109-
110-
formats = all_formats
111-
if (len(sys.argv) > 4):
112-
if (sys.argv[4] != "all"):
113-
if (sys.argv[4] not in formats):
114-
print("Expecting one of %s" % ' '.join(formats))
115-
sys.exit(1)
116-
formats = (sys.argv[4],)
117-
118-
if (len(sys.argv) > 5):
119-
print("Unused arguments: %s" % ' '.join(sys.argv[5: ]))
120-
121-
# set environmental variable used in the index.Rmd files
122-
os.environ['STAN_DOCS_VERSION'] = '.'.join([str(stan_major), str(stan_minor)])
123-
124-
for doc in docset:
125-
make_docs(docspath, stan_version, doc, formats)
103+
if len(sys.argv) > 3:
104+
if sys.argv[3] == "pdf":
105+
build_web = False
106+
elif sys.argv[3] == "website":
107+
build_pdf = False
108+
else:
109+
print("Bad arg[3], should be 'pdf' or 'website'".format(sys.argv[3]))
110+
sys.exit(1)
111+
112+
if len(sys.argv) > 4:
113+
if sys.argv[4] not in docset:
114+
print("Bad arg[4], should be one of %s" % " ".join(docset))
115+
sys.exit(1)
116+
docset = (sys.argv[4],)
117+
118+
if len(sys.argv) > 4:
119+
print("Unused arguments: %s" % " ".join(sys.argv[4:]))
120+
121+
if build_web:
122+
print("render website")
123+
with pushd(os.path.join(path, "src")):
124+
command = "quarto render"
125+
shexec(command)
126+
shutil.copytree(
127+
"_website", docspath, copy_function=shutil.move, dirs_exist_ok=True
128+
)
129+
130+
if build_pdfs:
131+
for doc in docset:
132+
make_pdfs(docspath, stan_version, doc)
126133

127134

128135
if __name__ == "__main__":

build_quarto.py

-136
This file was deleted.

0 commit comments

Comments
 (0)