|
| 1 | +import os |
| 2 | +import shlex |
| 3 | +import shutil |
| 4 | +import sys |
| 5 | +import datetime |
| 6 | + |
| 7 | +from invoke import task |
| 8 | +from invoke.main import program |
| 9 | +from pelican import main as pelican_main |
| 10 | +from pelican.server import ComplexHTTPRequestHandler, RootedHTTPServer |
| 11 | +from pelican.settings import DEFAULT_CONFIG, get_settings_from_file |
| 12 | + |
| 13 | +OPEN_BROWSER_ON_SERVE = True |
| 14 | +SETTINGS_FILE_BASE = "pelicanconf.py" |
| 15 | +SETTINGS = {} |
| 16 | +SETTINGS.update(DEFAULT_CONFIG) |
| 17 | +LOCAL_SETTINGS = get_settings_from_file(SETTINGS_FILE_BASE) |
| 18 | +SETTINGS.update(LOCAL_SETTINGS) |
| 19 | + |
| 20 | +CONFIG = { |
| 21 | + "settings_base": SETTINGS_FILE_BASE, |
| 22 | + "settings_publish": "publishconf.py", |
| 23 | + # Output path. Can be absolute or relative to tasks.py. Default: 'output' |
| 24 | + "deploy_path": SETTINGS["OUTPUT_PATH"], |
| 25 | + # Github Pages configuration |
| 26 | + "github_pages_branch": "main", |
| 27 | + "commit_message": f"'Publish site on {datetime.date.today().isoformat()}'", |
| 28 | + # Host and port for `serve` |
| 29 | + "host": "localhost", |
| 30 | + "port": 8000, |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +@task |
| 35 | +def clean(c): |
| 36 | + """Remove generated files""" |
| 37 | + if os.path.isdir(CONFIG["deploy_path"]): |
| 38 | + shutil.rmtree(CONFIG["deploy_path"]) |
| 39 | + os.makedirs(CONFIG["deploy_path"]) |
| 40 | + |
| 41 | + |
| 42 | +@task |
| 43 | +def build(c): |
| 44 | + """Build local version of site""" |
| 45 | + pelican_run("-s {settings_base}".format(**CONFIG)) |
| 46 | + |
| 47 | + |
| 48 | +@task |
| 49 | +def rebuild(c): |
| 50 | + """`build` with the delete switch""" |
| 51 | + pelican_run("-d -s {settings_base}".format(**CONFIG)) |
| 52 | + |
| 53 | + |
| 54 | +@task |
| 55 | +def regenerate(c): |
| 56 | + """Automatically regenerate site upon file modification""" |
| 57 | + pelican_run("-r -s {settings_base}".format(**CONFIG)) |
| 58 | + |
| 59 | + |
| 60 | +@task |
| 61 | +def serve(c): |
| 62 | + """Serve site at http://$HOST:$PORT/ (default is localhost:8000)""" |
| 63 | + |
| 64 | + class AddressReuseTCPServer(RootedHTTPServer): |
| 65 | + allow_reuse_address = True |
| 66 | + |
| 67 | + server = AddressReuseTCPServer( |
| 68 | + CONFIG["deploy_path"], |
| 69 | + (CONFIG["host"], CONFIG["port"]), |
| 70 | + ComplexHTTPRequestHandler, |
| 71 | + ) |
| 72 | + |
| 73 | + if OPEN_BROWSER_ON_SERVE: |
| 74 | + # Open site in default browser |
| 75 | + import webbrowser |
| 76 | + |
| 77 | + webbrowser.open("http://{host}:{port}".format(**CONFIG)) |
| 78 | + |
| 79 | + sys.stderr.write("Serving at {host}:{port} ...\n".format(**CONFIG)) |
| 80 | + server.serve_forever() |
| 81 | + |
| 82 | + |
| 83 | +@task |
| 84 | +def reserve(c): |
| 85 | + """`build`, then `serve`""" |
| 86 | + build(c) |
| 87 | + serve(c) |
| 88 | + |
| 89 | + |
| 90 | +@task |
| 91 | +def preview(c): |
| 92 | + """Build production version of site""" |
| 93 | + pelican_run("-s {settings_publish}".format(**CONFIG)) |
| 94 | + |
| 95 | +@task |
| 96 | +def livereload(c): |
| 97 | + """Automatically reload browser tab upon file modification.""" |
| 98 | + from livereload import Server |
| 99 | + |
| 100 | + def cached_build(): |
| 101 | + cmd = "-s {settings_base} -e CACHE_CONTENT=true LOAD_CONTENT_CACHE=true" |
| 102 | + pelican_run(cmd.format(**CONFIG)) |
| 103 | + |
| 104 | + cached_build() |
| 105 | + server = Server() |
| 106 | + theme_path = SETTINGS["THEME"] |
| 107 | + watched_globs = [ |
| 108 | + CONFIG["settings_base"], |
| 109 | + f"{theme_path}/templates/**/*.html", |
| 110 | + ] |
| 111 | + |
| 112 | + content_file_extensions = [".md", ".rst"] |
| 113 | + for extension in content_file_extensions: |
| 114 | + content_glob = "{}/**/*{}".format(SETTINGS["PATH"], extension) |
| 115 | + watched_globs.append(content_glob) |
| 116 | + |
| 117 | + static_file_extensions = [".css", ".js"] |
| 118 | + for extension in static_file_extensions: |
| 119 | + static_file_glob = f"{theme_path}/static/**/*{extension}" |
| 120 | + watched_globs.append(static_file_glob) |
| 121 | + |
| 122 | + for glob in watched_globs: |
| 123 | + server.watch(glob, cached_build) |
| 124 | + |
| 125 | + if OPEN_BROWSER_ON_SERVE: |
| 126 | + # Open site in default browser |
| 127 | + import webbrowser |
| 128 | + |
| 129 | + webbrowser.open("http://{host}:{port}".format(**CONFIG)) |
| 130 | + |
| 131 | + server.serve(host=CONFIG["host"], port=CONFIG["port"], root=CONFIG["deploy_path"]) |
| 132 | + |
| 133 | + |
| 134 | +@task |
| 135 | +def publish(c): |
| 136 | + """Publish to production via rsync""" |
| 137 | + pelican_run("-s {settings_publish}".format(**CONFIG)) |
| 138 | + c.run( |
| 139 | + 'rsync --delete --exclude ".DS_Store" -pthrvz -c ' |
| 140 | + '-e "ssh -p {ssh_port}" ' |
| 141 | + "{} {ssh_user}@{ssh_host}:{ssh_path}".format( |
| 142 | + CONFIG["deploy_path"].rstrip("/") + "/", **CONFIG |
| 143 | + ) |
| 144 | + ) |
| 145 | + |
| 146 | +@task |
| 147 | +def gh_pages(c): |
| 148 | + """Publish to GitHub Pages""" |
| 149 | + preview(c) |
| 150 | + c.run( |
| 151 | + "ghp-import -b {github_pages_branch} " |
| 152 | + "-m {commit_message} " |
| 153 | + "{deploy_path} -p".format(**CONFIG) |
| 154 | + ) |
| 155 | + |
| 156 | +def pelican_run(cmd): |
| 157 | + cmd += " " + program.core.remainder # allows to pass-through args to pelican |
| 158 | + pelican_main(shlex.split(cmd)) |
0 commit comments