Skip to content

Commit 5ed3687

Browse files
Thierry Chappuisfreezed
authored andcommitted
Added Makefile and rewrote tasks.py to support similar operations
1 parent 51865ca commit 5ed3687

File tree

2 files changed

+151
-13
lines changed

2 files changed

+151
-13
lines changed

Makefile

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
PY?=python3
2+
PELICAN?=pelican
3+
PELICANOPTS=
4+
5+
BASEDIR=$(CURDIR)
6+
INPUTDIR=$(BASEDIR)/content
7+
OUTPUTDIR=$(BASEDIR)/output
8+
CONFFILE=$(BASEDIR)/pelicanconf.py
9+
PUBLISHCONF=$(BASEDIR)/publishconf.py
10+
11+
12+
DEBUG ?= 0
13+
ifeq ($(DEBUG), 1)
14+
PELICANOPTS += -D
15+
endif
16+
17+
RELATIVE ?= 0
18+
ifeq ($(RELATIVE), 1)
19+
PELICANOPTS += --relative-urls
20+
endif
21+
22+
help:
23+
@echo 'Makefile for a pelican Web site '
24+
@echo ' '
25+
@echo 'Usage: '
26+
@echo ' make html (re)generate the web site '
27+
@echo ' make clean remove the generated files '
28+
@echo ' make regenerate regenerate files upon modification '
29+
@echo ' make publish generate using production settings '
30+
@echo ' make serve [PORT=8000] serve site at http://localhost:8000'
31+
@echo ' make serve-global [SERVER=0.0.0.0] serve (as root) to $(SERVER):80 '
32+
@echo ' make devserver [PORT=8000] serve and regenerate together ' '
33+
@echo ' '
34+
@echo 'Set the DEBUG variable to 1 to enable debugging, e.g. make DEBUG=1 html '
35+
@echo 'Set the RELATIVE variable to 1 to enable relative urls '
36+
@echo ' '
37+
38+
html:
39+
$(PELICAN) $(INPUTDIR) -o $(OUTPUTDIR) -s $(CONFFILE) $(PELICANOPTS)
40+
41+
clean:
42+
[ ! -d $(OUTPUTDIR) ] || rm -rf $(OUTPUTDIR)
43+
44+
regenerate:
45+
$(PELICAN) -r $(INPUTDIR) -o $(OUTPUTDIR) -s $(CONFFILE) $(PELICANOPTS)
46+
47+
serve:
48+
ifdef PORT
49+
$(PELICAN) -l $(INPUTDIR) -o $(OUTPUTDIR) -s $(CONFFILE) $(PELICANOPTS) -p $(PORT)
50+
else
51+
$(PELICAN) -l $(INPUTDIR) -o $(OUTPUTDIR) -s $(CONFFILE) $(PELICANOPTS)
52+
endif
53+
54+
serve-global:
55+
ifdef SERVER
56+
$(PELICAN) -l $(INPUTDIR) -o $(OUTPUTDIR) -s $(CONFFILE) $(PELICANOPTS) -p $(PORT) -b $(SERVER)
57+
else
58+
$(PELICAN) -l $(INPUTDIR) -o $(OUTPUTDIR) -s $(CONFFILE) $(PELICANOPTS) -p $(PORT) -b 0.0.0.0
59+
endif
60+
61+
62+
devserver:
63+
ifdef PORT
64+
$(PELICAN) -lr $(INPUTDIR) -o $(OUTPUTDIR) -s $(CONFFILE) $(PELICANOPTS) -p $(PORT)
65+
else
66+
$(PELICAN) -lr $(INPUTDIR) -o $(OUTPUTDIR) -s $(CONFFILE) $(PELICANOPTS)
67+
endif
68+
69+
publish:
70+
$(PELICAN) $(INPUTDIR) -o $(OUTPUTDIR) -s $(PUBLISHCONF) $(PELICANOPTS)
71+
72+
73+
.PHONY: html help clean regenerate serve serve-global devserver stopserver publish ftp_upload

tasks.py

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,98 @@
1+
# -*- coding: utf-8 -*-
2+
13
import os
4+
import shutil
5+
import sys
6+
import datetime
27
from pathlib import Path
38

49
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)
530

631
@task
732
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}')
1142

1243
@task
1344
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}')
1748

1849
@task
1950
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"""
2152
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()
2374

2475
@task
2576
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')
3196

3297
@task
3398
def revert(c):

0 commit comments

Comments
 (0)