From 28383a6355c6c238a86a469b4a410139a3ce3d7a Mon Sep 17 00:00:00 2001 From: John Franey Date: Sat, 2 Feb 2019 16:24:11 -0400 Subject: [PATCH 1/2] Add livereload invoke task. Fixes #1326 Adds a `livereload` invoke task that builds the project and reloads the browser window when content files are updated. Usage: ```console $ invoke livereload [I 190202 16:28:30 server:298] Serving on http://127.0.0.1:5500 [I 190202 16:28:30 handlers:59] Start watching changes [I 190202 16:28:30 handlers:61] Start detecting changes [I 190202 16:28:32 handlers:132] Browser Connected: http://127.0.0.1:5500/ ``` See: https://livereload.readthedocs.io/en/latest/ --- pelican/tools/templates/tasks.py.jinja2 | 20 ++++++++++++++++++++ setup.py | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pelican/tools/templates/tasks.py.jinja2 b/pelican/tools/templates/tasks.py.jinja2 index f022bfc0..5b27e2b5 100644 --- a/pelican/tools/templates/tasks.py.jinja2 +++ b/pelican/tools/templates/tasks.py.jinja2 @@ -7,7 +7,14 @@ import datetime from invoke import task from invoke.util import cd +from livereload import Server from pelican.server import ComplexHTTPRequestHandler, RootedHTTPServer +from pelican.settings import DEFAULT_CONFIG, get_settings_from_file + +SETTINGS = {} +SETTINGS.update(DEFAULT_CONFIG) +LOCAL_SETTINGS = get_settings_from_file('pelicanconf.py') +SETTINGS.update(LOCAL_SETTINGS) CONFIG = { # Local path configuration (can be absolute or relative to tasks.py) @@ -80,6 +87,19 @@ def preview(c): """Build production version of site""" c.run('pelican -s publishconf.py') +@task +def livereload(c): + """Automatically reload browser tab upon file modification.""" + build(c) + server = Server() + deploy_path = CONFIG['deploy_path'] + content_path = SETTINGS['PATH'] + content_file_extensions = ['.md', '.rst'] + for file_extension in content_file_extensions: + content_blob = '{0}/**/*{1}'.format(content_path, file_extension) + server.watch(content_blob, lambda: build(c)) + server.serve(root=deploy_path) + {% if cloudfiles %} @task def cf_upload(c): diff --git a/setup.py b/setup.py index e8918941..25e109c4 100755 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ from setuptools import setup requires = ['feedgenerator >= 1.9', 'jinja2 >= 2.7', 'pygments', 'docutils', 'pytz >= 0a', 'blinker', 'unidecode', 'six >= 1.4', - 'python-dateutil'] + 'python-dateutil', 'livereload'] entry_points = { 'console_scripts': [ From ca012bd288d388c45fa060caddfdd1e48bca8d8a Mon Sep 17 00:00:00 2001 From: John Franey Date: Tue, 18 Jun 2019 22:56:18 -0400 Subject: [PATCH 2/2] Update livereload Invoke task and add docs Removes the `livereload` dependency from `setup.py`. Updates the `invoke livereload` task by moving the `livereload` import into the task function since it is now an optional dependency. Updates the Invoke section of the documentaion with instructions on using the `livereload` Invoke task. --- docs/publish.rst | 6 ++++++ pelican/tools/templates/tasks.py.jinja2 | 2 +- setup.py | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/docs/publish.rst b/docs/publish.rst index 197aa779..96d67d58 100644 --- a/docs/publish.rst +++ b/docs/publish.rst @@ -146,6 +146,12 @@ http://localhost:8000/:: invoke serve +To serve the generated site with automatic browser reloading every time a +change is detected, first ``pip install livereload``, then use the +following command:: + + invoke livereload + If during the ``pelican-quickstart`` process you answered "yes" when asked whether you want to upload your site via SSH, you can use the following command to publish your site via rsync over SSH:: diff --git a/pelican/tools/templates/tasks.py.jinja2 b/pelican/tools/templates/tasks.py.jinja2 index 5b27e2b5..ef48e968 100644 --- a/pelican/tools/templates/tasks.py.jinja2 +++ b/pelican/tools/templates/tasks.py.jinja2 @@ -7,7 +7,6 @@ import datetime from invoke import task from invoke.util import cd -from livereload import Server from pelican.server import ComplexHTTPRequestHandler, RootedHTTPServer from pelican.settings import DEFAULT_CONFIG, get_settings_from_file @@ -90,6 +89,7 @@ def preview(c): @task def livereload(c): """Automatically reload browser tab upon file modification.""" + from livereload import Server build(c) server = Server() deploy_path = CONFIG['deploy_path'] diff --git a/setup.py b/setup.py index 25e109c4..e8918941 100755 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ from setuptools import setup requires = ['feedgenerator >= 1.9', 'jinja2 >= 2.7', 'pygments', 'docutils', 'pytz >= 0a', 'blinker', 'unidecode', 'six >= 1.4', - 'python-dateutil', 'livereload'] + 'python-dateutil'] entry_points = { 'console_scripts': [