|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
1 | 3 | import os |
| 4 | +import shutil |
| 5 | +import sys |
| 6 | +import datetime |
2 | 7 | from pathlib import Path |
3 | 8 |
|
4 | 9 | from invoke import task |
| 10 | +from invoke.util import cd |
| 11 | +from pelican.server import ComplexHTTPRequestHandler, RootedHTTPServer |
| 12 | + |
| 13 | +BASEDIR = (Path(__file__) / '..').resolve() |
| 14 | +INPUTDIR = BASEDIR / 'content' |
| 15 | +OUTPUTDIR = BASEDIR / 'output' |
| 16 | +CONFFILE = BASEDIR / 'pelicanconf.py' |
| 17 | +PUBLISHCONF = BASEDIR / 'publishconf.py' |
| 18 | + |
| 19 | +PORT = 8000 |
| 20 | + |
| 21 | +PELICANOPTS = '--fatal=warnings' |
| 22 | + |
| 23 | + |
| 24 | +@task |
| 25 | +def clean(c): |
| 26 | + """Remove generated files""" |
| 27 | + if OUTPUTDIR.is_dir(): |
| 28 | + shutil.rmtree(OUTPUTDIR) |
| 29 | + OUTPUTDIR.mkdir(parents=True, exist_ok=True) |
5 | 30 |
|
6 | 31 | @task |
7 | 32 | def build(c): |
8 | | - """Builds the local Pelican blog.""" |
9 | | - c.run('echo "Publishing your Pelican website"') |
10 | | - c.run(f'pelican content -s pelicanconf.py --fatal=warnings') |
| 33 | + """Builds the local Pelican blog""" |
| 34 | + c.run('echo "Building your Pelican website"') |
| 35 | + c.run(f'pelican {INPUTDIR} -o {OUTPUTDIR} -s {CONFFILE} {PELICANOPTS}') |
| 36 | + |
| 37 | +@task |
| 38 | +def rebuild(c): |
| 39 | + """`build` with the delete switch""" |
| 40 | + c.run('echo "Re-building your Pelican website"') |
| 41 | + c.run(f'pelican -d -s {CONFFILE} {PELICANOPTS}') |
11 | 42 |
|
12 | 43 | @task |
13 | 44 | def publish(c): |
14 | | - """Builds the Pelican blog with deployment settings.""" |
15 | | - c.run('echo "Building your Pelican website"') |
16 | | - c.run(f'pelican content -s publishconf.py --fatal=warnings') |
| 45 | + """Builds the Pelican blog with deployment settings""" |
| 46 | + c.run('echo "Publishing your Pelican website"') |
| 47 | + c.run(f'pelican {INPUTDIR} -o {OUTPUTDIR} -s {PUBLISHCONF} {PELICANOPTS}') |
17 | 48 |
|
18 | 49 | @task |
19 | 50 | def autoreload(c): |
20 | | - """Starts the autoreload server to help during writing of blog articles.""" |
| 51 | + """Starts the autoreload server to help during writing of blog articles""" |
21 | 52 | c.run('echo "Running autoreload server. Press CTRL+C to stop"') |
22 | | - c.run(f'pelican -r content -s pelicanconf.py') |
| 53 | + c.run(f'pelican -r {INPUTDIR} -o {OUTPUTDIR} -s {CONFFILE} {PELICANOPTS}') |
| 54 | + |
| 55 | +@task |
| 56 | +def regenerate(c): |
| 57 | + """Starts the autoreload server to help during writing of blog articles""" |
| 58 | + autoreload(c) |
| 59 | + |
| 60 | +@task |
| 61 | +def serve(c): |
| 62 | + """Serve site at http://localhost:8000/""" |
| 63 | + |
| 64 | + class AddressReuseTCPServer(RootedHTTPServer): |
| 65 | + allow_reuse_address = True |
| 66 | + |
| 67 | + server = AddressReuseTCPServer( |
| 68 | + OUTPUTDIR, |
| 69 | + ('', PORT), |
| 70 | + ComplexHTTPRequestHandler) |
| 71 | + |
| 72 | + sys.stderr.write('Serving on port {port} ...\n'.format(port=PORT)) |
| 73 | + server.serve_forever() |
23 | 74 |
|
24 | 75 | @task |
25 | 76 | def runserver(c): |
26 | | - """Starts the dev server to visualize the articles locally in a web browser |
27 | | - at url http://localhost:8000. |
28 | | - """ |
29 | | - c.run('echo "Running development server. Press CTRL+C to stop"') |
30 | | - c.run(f'python -m http.server -d output') |
| 77 | + """Serve site at http://localhost:8000/""" |
| 78 | + serve(c) |
| 79 | + |
| 80 | +@task |
| 81 | +def devserver(c): |
| 82 | + """Starts the devserver""" |
| 83 | + c.run('echo "Running Pelican DevServer. Press CTRL+C to stop"') |
| 84 | + c.run(f'pelican -lr {INPUTDIR} -o {OUTPUTDIR} -s {CONFFILE} {PELICANOPTS} -p {PORT}') |
| 85 | + |
| 86 | +@task |
| 87 | +def reserve(c): |
| 88 | + """`build`, then `serve`""" |
| 89 | + build(c) |
| 90 | + serve(c) |
| 91 | + |
| 92 | +@task |
| 93 | +def preview(c): |
| 94 | + """Build production version of site""" |
| 95 | + c.run('pelican -s publishconf.py') |
31 | 96 |
|
32 | 97 | @task |
33 | 98 | def revert(c): |
|
0 commit comments