diff --git a/.gitignore b/.gitignore index 4ce844a..488afc1 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ __pycache__ Makefile *.pid plugins +cache diff --git a/pelicanconf.py b/pelicanconf.py index f73a1e3..d2867c6 100644 --- a/pelicanconf.py +++ b/pelicanconf.py @@ -59,5 +59,5 @@ CATEGORY_SAVE_AS = '{slug}/index.html' CATEGORY_PAGE_PATH= 'categories' # Uncomment following line if you want document-relative URLs when developing -#RELATIVE_URLS = True +# RELATIVE_URLS = True diff --git a/poetry.lock b/poetry.lock index 010cb5d..c46c651 100644 --- a/poetry.lock +++ b/poetry.lock @@ -70,6 +70,17 @@ files = [ [package.extras] all = ["flake8 (>=7.1.1)", "mypy (>=1.11.2)", "pytest (>=8.3.2)", "ruff (>=0.6.2)"] +[[package]] +name = "invoke" +version = "2.2.0" +description = "Pythonic task execution" +optional = false +python-versions = ">=3.6" +files = [ + {file = "invoke-2.2.0-py3-none-any.whl", hash = "sha256:6ea924cc53d4f78e3d98bc436b08069a03077e6f85ad1ddaa8a116d7dad15820"}, + {file = "invoke-2.2.0.tar.gz", hash = "sha256:ee6cbb101af1a859c7fe84f2a264c059020b0cb7fe3535f9424300ab568f6bd5"}, +] + [[package]] name = "jinja2" version = "3.1.4" @@ -87,6 +98,20 @@ MarkupSafe = ">=2.0" [package.extras] i18n = ["Babel (>=2.7)"] +[[package]] +name = "livereload" +version = "2.7.0" +description = "Python LiveReload is an awesome tool for web developers" +optional = false +python-versions = ">=3.7" +files = [ + {file = "livereload-2.7.0-py3-none-any.whl", hash = "sha256:19bee55aff51d5ade6ede0dc709189a0f904d3b906d3ea71641ed548acff3246"}, + {file = "livereload-2.7.0.tar.gz", hash = "sha256:f4ba199ef93248902841e298670eebfe1aa9e148e19b343bc57dbf1b74de0513"}, +] + +[package.dependencies] +tornado = "*" + [[package]] name = "markdown" version = "3.7" @@ -327,6 +352,26 @@ files = [ {file = "sniffio-1.3.1.tar.gz", hash = "sha256:f4324edc670a0f49750a81b895f35c3adb843cca46f0530f79fc1babb23789dc"}, ] +[[package]] +name = "tornado" +version = "6.4.1" +description = "Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed." +optional = false +python-versions = ">=3.8" +files = [ + {file = "tornado-6.4.1-cp38-abi3-macosx_10_9_universal2.whl", hash = "sha256:163b0aafc8e23d8cdc3c9dfb24c5368af84a81e3364745ccb4427669bf84aec8"}, + {file = "tornado-6.4.1-cp38-abi3-macosx_10_9_x86_64.whl", hash = "sha256:6d5ce3437e18a2b66fbadb183c1d3364fb03f2be71299e7d10dbeeb69f4b2a14"}, + {file = "tornado-6.4.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e2e20b9113cd7293f164dc46fffb13535266e713cdb87bd2d15ddb336e96cfc4"}, + {file = "tornado-6.4.1-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:8ae50a504a740365267b2a8d1a90c9fbc86b780a39170feca9bcc1787ff80842"}, + {file = "tornado-6.4.1-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:613bf4ddf5c7a95509218b149b555621497a6cc0d46ac341b30bd9ec19eac7f3"}, + {file = "tornado-6.4.1-cp38-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:25486eb223babe3eed4b8aecbac33b37e3dd6d776bc730ca14e1bf93888b979f"}, + {file = "tornado-6.4.1-cp38-abi3-musllinux_1_2_i686.whl", hash = "sha256:454db8a7ecfcf2ff6042dde58404164d969b6f5d58b926da15e6b23817950fc4"}, + {file = "tornado-6.4.1-cp38-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:a02a08cc7a9314b006f653ce40483b9b3c12cda222d6a46d4ac63bb6c9057698"}, + {file = "tornado-6.4.1-cp38-abi3-win32.whl", hash = "sha256:d9a566c40b89757c9aa8e6f032bcdb8ca8795d7c1a9762910c722b1635c9de4d"}, + {file = "tornado-6.4.1-cp38-abi3-win_amd64.whl", hash = "sha256:b24b8982ed444378d7f21d563f4180a2de31ced9d8d84443907a0a64da2072e7"}, + {file = "tornado-6.4.1.tar.gz", hash = "sha256:92d3ab53183d8c50f8204a51e6f91d18a15d5ef261e84d452800d4ff6fc504e9"}, +] + [[package]] name = "tzdata" version = "2024.2" @@ -447,4 +492,4 @@ anyio = ">=3.0.0" [metadata] lock-version = "2.0" python-versions = "^3.12" -content-hash = "bb71e5d5103303fd6104fa3185366796e36d2d49e7212f143f9b1ec64676aed0" +content-hash = "1dd2466f5493bade2c6c6e541782c0c03c5da0a09d254a79b75b457e474af451" diff --git a/pyproject.toml b/pyproject.toml index 733d666..032fe80 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,6 +10,8 @@ readme = "README.md" python = "^3.12" pelican = "^4.10.1" markdown = "^3.7" +invoke = "^2.2.0" +livereload = "^2.7.0" [build-system] requires = ["poetry-core"] diff --git a/tasks.py b/tasks.py new file mode 100644 index 0000000..b0b04ef --- /dev/null +++ b/tasks.py @@ -0,0 +1,150 @@ +import os +import shlex +import shutil +import sys + +from invoke import task +from invoke.main import program +from pelican import main as pelican_main +from pelican.server import ComplexHTTPRequestHandler, RootedHTTPServer +from pelican.settings import DEFAULT_CONFIG, get_settings_from_file + +OPEN_BROWSER_ON_SERVE = False +SETTINGS_FILE_BASE = "pelicanconf.py" +SETTINGS = {} +SETTINGS.update(DEFAULT_CONFIG) +LOCAL_SETTINGS = get_settings_from_file(SETTINGS_FILE_BASE) +SETTINGS.update(LOCAL_SETTINGS) + +CONFIG = { + "settings_base": SETTINGS_FILE_BASE, + "settings_publish": "publishconf.py", + # Output path. Can be absolute or relative to tasks.py. Default: 'output' + "deploy_path": SETTINGS["OUTPUT_PATH"], + # Host and port for `serve` + "host": "localhost", + "port": 8000, + # for publication + "ssh_user": "fmouhart", + "ssh_host": "blog.epheme.re", + "ssh_port": 22, + "ssh_path": "/srv/http/blog", +} + + +@task +def clean(c): + """Remove generated files""" + if os.path.isdir(CONFIG["deploy_path"]): + shutil.rmtree(CONFIG["deploy_path"]) + os.makedirs(CONFIG["deploy_path"]) + + +@task +def build(c): + """Build local version of site""" + pelican_run("-s {settings_base}".format(**CONFIG)) + + +@task +def rebuild(c): + """`build` with the delete switch""" + pelican_run("-d -s {settings_base}".format(**CONFIG)) + + +@task +def regenerate(c): + """Automatically regenerate site upon file modification""" + pelican_run("-r -s {settings_base}".format(**CONFIG)) + + +@task +def serve(c): + """Serve site at http://$HOST:$PORT/ (default is localhost:8000)""" + + class AddressReuseTCPServer(RootedHTTPServer): + allow_reuse_address = True + + server = AddressReuseTCPServer( + CONFIG["deploy_path"], + (CONFIG["host"], CONFIG["port"]), + ComplexHTTPRequestHandler, + ) + + if OPEN_BROWSER_ON_SERVE: + # Open site in default browser + import webbrowser + + webbrowser.open("http://{host}:{port}".format(**CONFIG)) + + sys.stderr.write("Serving at {host}:{port} ...\n".format(**CONFIG)) + server.serve_forever() + + +@task +def reserve(c): + """`build`, then `serve`""" + build(c) + serve(c) + + +@task +def preview(c): + """Build production version of site""" + pelican_run("-s {settings_publish}".format(**CONFIG)) + +@task +def livereload(c): + """Automatically reload browser tab upon file modification.""" + from livereload import Server + + def cached_build(): + cmd = "-s {settings_base} -e CACHE_CONTENT=true LOAD_CONTENT_CACHE=true" + pelican_run(cmd.format(**CONFIG)) + + cached_build() + server = Server() + theme_path = SETTINGS["THEME"] + watched_globs = [ + CONFIG["settings_base"], + f"{theme_path}/templates/**/*.html", + ] + + content_file_extensions = [".md", ".rst"] + for extension in content_file_extensions: + content_glob = "{}/**/*{}".format(SETTINGS["PATH"], extension) + watched_globs.append(content_glob) + + static_file_extensions = [".css", ".js"] + for extension in static_file_extensions: + static_file_glob = f"{theme_path}/static/**/*{extension}" + watched_globs.append(static_file_glob) + + for glob in watched_globs: + server.watch(glob, cached_build) + + if OPEN_BROWSER_ON_SERVE: + # Open site in default browser + import webbrowser + + webbrowser.open("http://{host}:{port}".format(**CONFIG)) + + server.serve(host=CONFIG["host"], port=CONFIG["port"], root=CONFIG["deploy_path"]) + + +@task +def publish(c): + """Publish to production via rsync""" + pelican_run("-s {settings_publish}".format(**CONFIG)) + c.run( + 'rsync --delete --exclude ".DS_Store" -pthrvz -c ' + '-e "ssh -p {ssh_port}" ' + "{} {ssh_user}@{ssh_host}:{ssh_path}".format( + CONFIG["deploy_path"].rstrip("/") + "/", **CONFIG + ) + ) + + +def pelican_run(cmd): + cmd += " " + program.core.remainder # allows to pass-through args to pelican + pelican_main(shlex.split(cmd))