From 142c8436fbb0fad0f47d34201dca60670a88d7cc Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 16 Mar 2023 19:16:37 +0800 Subject: [PATCH] feat: use node container to build static web pages --- src/release/{demo.py => release.py} | 8 +++---- src/release/utils/common.py | 1 - src/release/utils/release.py | 33 ++++++++++++++++++++++++----- 3 files changed, 32 insertions(+), 10 deletions(-) rename src/release/{demo.py => release.py} (69%) mode change 100644 => 100755 diff --git a/src/release/demo.py b/src/release/release.py old mode 100644 new mode 100755 similarity index 69% rename from src/release/demo.py rename to src/release/release.py index 3ae4223..93962a2 --- a/src/release/demo.py +++ b/src/release/release.py @@ -11,8 +11,8 @@ from utils import gitbookRelease metadata, content = loadBook('rc-5') -txtRelease(metadata, content) -jsonRelease(metadata, content) -htmlRelease(metadata, content) +# txtRelease(metadata, content) +# jsonRelease(metadata, content) +# htmlRelease(metadata, content) staticRelease(metadata, content) -gitbookRelease(metadata, content) +# gitbookRelease(metadata, content) diff --git a/src/release/utils/common.py b/src/release/utils/common.py index b3a11a2..192b97d 100644 --- a/src/release/utils/common.py +++ b/src/release/utils/common.py @@ -22,7 +22,6 @@ releaseInfo = { # release file name 'static': 'XXRS.tar.xz', 'calibre': 'xxrs.html', 'gitbook': 'xxrs-online/', - 'temp': 'xxrs-static/', } releaseInfo = { # convert to absolute path key: os.path.join(releasePath, file) for (key, file) in releaseInfo.items() diff --git a/src/release/utils/release.py b/src/release/utils/release.py index f112282..96672ff 100644 --- a/src/release/utils/release.py +++ b/src/release/utils/release.py @@ -4,6 +4,8 @@ import os import json import shutil +import tempfile +import subprocess from .common import rootPath from .common import saveFile @@ -71,7 +73,7 @@ def staticDepends(workDir: str, metadata: dict, content: dict) -> None: 'description': projectDesc, "language": "zh-hans", 'plugins': [ - '-search', '-sharing', 'hints', 'github', + '-lunr', '-search', '-sharing', 'hints', 'github', 'fontsettings', 'image-captions', 'back-to-top-button' ], 'pluginsConfig': { @@ -89,8 +91,29 @@ def staticDepends(workDir: str, metadata: dict, content: dict) -> None: ) +def staticBuild(workDir: str) -> None: + buildDir = '/xxrs/' + nodeImage = 'node:10-alpine' + buildCommand = 'docker run --rm -v %s:%s --entrypoint sh %s -c "%s"' % ( + workDir, buildDir, nodeImage, + 'npm install gitbook-cli -g && gitbook install %s && gitbook build %s --log=debug' % ( + buildDir, buildDir + ) + ) + print('Gitbook Build -> %s' % workDir) + subprocess.Popen(buildCommand, shell = True).wait() # blocking wait + os.chdir(os.path.join(workDir, './_book')) + os.popen('tar cJf %s *' % releaseInfo['static']) + + def staticRelease(metadata: dict, content: dict) -> None: - createFolder(releaseInfo['temp']) - # TODO: using temp folder module - staticDepends(releaseInfo['temp'], metadata, content) - # TODO: building static html by docker `node:10-alpine` container + + # TODO: confirm running under root + + tempDir = tempfile.TemporaryDirectory() # access temporary directory + + content = {x: content[x] for x in list(content)[:20]} # TODO: just for test + + staticDepends(tempDir.name, metadata, content) + staticBuild(tempDir.name) + tempDir.cleanup()