1 # -*- coding: utf-8 -*-
9 from invoke import task
10 from invoke.main import program
11 from invoke.util import cd
12 from pelican import main as pelican_main
13 from pelican.server import ComplexHTTPRequestHandler, RootedHTTPServer
14 from pelican.settings import DEFAULT_CONFIG, get_settings_from_file
16 SETTINGS_FILE_BASE = 'pelicanconf.py'
18 SETTINGS.update(DEFAULT_CONFIG)
19 LOCAL_SETTINGS = get_settings_from_file(SETTINGS_FILE_BASE)
20 SETTINGS.update(LOCAL_SETTINGS)
23 'settings_base': SETTINGS_FILE_BASE,
24 'settings_publish': 'publishconf.py',
25 # Output path. Can be absolute or relative to tasks.py. Default: 'output'
26 'deploy_path': SETTINGS['OUTPUT_PATH'],
27 # Host and port for `serve`
34 """Remove generated files"""
35 if os.path.isdir(CONFIG['deploy_path']):
36 shutil.rmtree(CONFIG['deploy_path'])
37 os.makedirs(CONFIG['deploy_path'])
41 """Build local version of site"""
42 pelican_run('-s {settings_base}'.format(**CONFIG))
46 """`build` with the delete switch"""
47 pelican_run('-d -s {settings_base}'.format(**CONFIG))
51 """Automatically regenerate site upon file modification"""
52 pelican_run('-r -s {settings_base}'.format(**CONFIG))
56 """Serve site at http://$HOST:$PORT/ (default is localhost:8000)"""
58 class AddressReuseTCPServer(RootedHTTPServer):
59 allow_reuse_address = True
61 server = AddressReuseTCPServer(
62 CONFIG['deploy_path'],
63 (CONFIG['host'], CONFIG['port']),
64 ComplexHTTPRequestHandler)
66 sys.stderr.write('Serving at {host}:{port} ...\n'.format(**CONFIG))
67 server.serve_forever()
71 """`build`, then `serve`"""
77 """Build production version of site"""
78 pelican_run('-s {settings_publish}'.format(**CONFIG))
82 """Automatically reload browser tab upon file modification."""
83 from livereload import Server
86 cmd = '-s {settings_base} -e CACHE_CONTENT=True LOAD_CONTENT_CACHE=True'
87 pelican_run(cmd.format(**CONFIG))
91 theme_path = SETTINGS['THEME']
93 CONFIG['settings_base'],
94 '{}/templates/**/*.html'.format(theme_path),
97 content_file_extensions = ['.md', '.rst']
98 for extension in content_file_extensions:
99 content_glob = '{0}/**/*{1}'.format(SETTINGS['PATH'], extension)
100 watched_globs.append(content_glob)
102 static_file_extensions = ['.css', '.js']
103 for extension in static_file_extensions:
104 static_file_glob = '{0}/static/**/*{1}'.format(theme_path, extension)
105 watched_globs.append(static_file_glob)
107 for glob in watched_globs:
108 server.watch(glob, cached_build)
109 server.serve(host=CONFIG['host'], port=CONFIG['port'], root=CONFIG['deploy_path'])
114 """Publish to production via rsync"""
115 pelican_run('-s {settings_publish}'.format(**CONFIG))
117 'rsync --delete --exclude ".DS_Store" -pthrvz -c '
118 '-e "ssh -p {ssh_port}" '
119 '{} {ssh_user}@{ssh_host}:{ssh_path}'.format(
120 CONFIG['deploy_path'].rstrip('/') + '/',
124 def pelican_run(cmd):
125 cmd += ' ' + program.core.remainder # allows to pass-through args to pelican
126 pelican_main(shlex.split(cmd))