Repository navigation
Expand file tree
/
Copy pathpreview.sh
More file actions
executable file
·51 lines (46 loc) · 2.06 KB
/
Copy pathpreview.sh
File metadata and controls
executable file
·51 lines (46 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/sh
# Build the whole published site locally and serve it: the edition being
# worked on, every archived edition built from its tag, and the landing page.
#
# `hugo server` on its own only builds content/, which is the current edition -
# it never shows the landing page or the archived editions. Use this when you
# want to see the site as published; use `hugo server` while working on decks,
# since it has live reload and this does not.
#
# ./preview.sh -> http://localhost:1313/
# PORT=8000 ./preview.sh -> http://localhost:8000/
#
# Includes /draft/, the unlinked copy that has `draft: true` pages in it, so
# that what you see here is what the workflow publishes.
#
# Archived editions load their images from raw.githubusercontent.com pinned to
# their tag, so they need network access and the tags need to be pushed.
set -eu
cd "$(dirname "$0")"
PORT=${PORT:-1313}
OUT=${OUT:-site}
EDITION=${EDITION:-$(tools/current-edition.sh)}
tools/build-site.sh "$OUT" "http://localhost:$PORT" --with-current --with-draft
# The landing page does not link the edition being worked on while it is
# unpublished, which would leave no way into it here. Add a banner that only
# ever exists in this preview - the workflow copies landing/ untouched.
python3 - "$OUT/index.html" "$EDITION" <<'BANNER'
import pathlib, sys
page, edition = pathlib.Path(sys.argv[1]), sys.argv[2]
html = page.read_text()
if f'href="{edition}/' not in html:
banner = (
'<div style="position:fixed;left:0;right:0;bottom:0;z-index:99;'
'background:#1c1c1e;color:#fff;padding:12px 16px;text-align:center;'
'font:14px/1.4 Urbanist,Helvetica,Arial,sans-serif">'
'Local preview · the ' + edition + ' edition is not published yet · '
'<a href="' + edition + '/" style="color:#ff8a6f;font-weight:600">'
'open /' + edition + '/</a></div>'
)
page.write_text(html.replace('</body>', banner + '\n</body>', 1))
BANNER
echo
echo "Serving at http://localhost:$PORT/"
echo "Press Ctrl-C to stop."
echo
cd "$OUT" && exec python3 -m http.server "$PORT" --bind 127.0.0.1