From 7be5ec701d4f9405dde7886eda825289b71ee3c6 Mon Sep 17 00:00:00 2001 From: Youen Date: Thu, 18 May 2023 17:17:36 +0200 Subject: [PATCH] Added support for multiversion build (wip) --- src/data/document.py | 47 +++++++++++++++++++++++++++++++++-------- src/web/web_document.py | 2 +- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/data/document.py b/src/data/document.py index ceba343..2acf61b 100644 --- a/src/data/document.py +++ b/src/data/document.py @@ -52,23 +52,46 @@ class Document: self.doc_path = doc_path self.valid = True + + # TODO: actually read document settings + self.settings = { + 'multiversion': False, + 'default_version': 'main' + } def build(self): - #venv_path = os.getenv('VIRTUAL_ENV') + multiversion_build = self.settings['multiversion'] cmd = [] # update source files from git cmd.append(['git', 'pull']) - # build the HTML version - cmd.append(['make', 'html', 'BUILDDIR=../build']) + if multiversion_build: + # also fetch all branches and tags, so that sphinx-multiversion knows what versions exist and can pull them + cmd.append(['bash', '-c', 'for BRANCH in $(git branch -a | grep remotes | grep -v HEAD | grep -v master); do git branch --track "${BRANCH#remotes/origin/}" "${BRANCH}" || echo "(ignored)"; done']) + + # build the HTML version + cmd.append(['make', 'html_versions', 'BUILDDIR=../build']) + + # TODO: build PDF(s) version(s) + + else: + # build the HTML version + cmd.append(['make', 'html', 'BUILDDIR=../build']) + + # build the PDF version + cmd.append(['make', 'pdf', 'BUILDDIR=../build']) - # build the PDF version - cmd.append(['make', 'pdf', 'BUILDDIR=../build']) + # Copy the generated PDF file to the HTML directory, so that it is accessible for download by users + cmd.append(['cp', self.doc_path + '/build/weasyprint/vheliotech.pdf', self.doc_path + '/build/html/' + self.doc_name + '.pdf']) - # Copy the generated PDF file to the HTML directory, so that it is accessible for download by users - cmd.append(['cp', self.doc_path + '/build/weasyprint/vheliotech.pdf', self.doc_path + '/build/html/' + self.doc_name + '.pdf']) + # Now that the build is successfull, move it to the deployment directory (replacing any existing content) + cmd.append(['rm', '-rf', self.doc_path + '/dist']) + if multiversion_build: + cmd.append(['mv', self.doc_path + '/build/html_versions/', self.doc_path + '/dist/']) + else: + cmd.append(['mv', self.doc_path + '/build/html/', self.doc_path + '/dist/']) task = ProcessTask(cmd, cwd = self.doc_path + "/repo") task.start() @@ -91,10 +114,16 @@ class Document: os.rmdir(origin_root) def get_url(self): - return "/doc/" + self.encoded_origin + "/" + sanitize_name(self.doc_name)+'/'+sanitize_name(self.branch) + "/index.html" + if self.settings['multiversion']: + return "/doc/" + self.encoded_origin + "/" + sanitize_name(self.doc_name)+'/'+sanitize_name(self.branch) + "/" + self.settings['default_version'] + "/index.html" + else: + return "/doc/" + self.encoded_origin + "/" + sanitize_name(self.doc_name)+'/'+sanitize_name(self.branch) + "/index.html" def get_pdf_url(self): - return "/doc/" + self.encoded_origin + "/" + sanitize_name(self.doc_name)+'/'+sanitize_name(self.branch) + "/" + self.doc_name + ".pdf" + if self.settings['multiversion']: + return "/doc/" + self.encoded_origin + "/" + sanitize_name(self.doc_name)+'/'+sanitize_name(self.branch) + "/" + self.settings['default_version'] + "/" + self.doc_name + ".pdf" + else: + return "/doc/" + self.encoded_origin + "/" + sanitize_name(self.doc_name)+'/'+sanitize_name(self.branch) + "/" + self.doc_name + ".pdf" def get_api_key(self): with open(self.doc_path + "/apikey") as f: diff --git a/src/web/web_document.py b/src/web/web_document.py index f7aaef4..b63b062 100644 --- a/src/web/web_document.py +++ b/src/web/web_document.py @@ -9,4 +9,4 @@ bp = Blueprint('web_document', __name__, url_prefix='/doc') @bp.route('////') def index(origin, doc_name, branch, path): doc = Document(origin, doc_name, branch) - return send_from_directory(doc.doc_path + "/build/html", path) + return send_from_directory(doc.doc_path + "/dist", path)