OILS / build / wiki.ysh View on Github | oils.pub

133 lines, 64 significant
1#!/usr/bin/env ysh
2#
3# Script to build and deploy the wiki to pages.oils.pub
4# (oils-for-unix.github.io). This is used for the dev guide.
5#
6# # Usage:
7#
8# build/wiki.ysh render
9# build/wiki.ysh commit-push
10#
11# # Action: render
12#
13# This will clone the oils-for-unix/oils wiki, and render all of the markdown
14# files using doc tools. The docs are saved to _tmp/wiki/out.
15#
16# # Action: commit-push
17#
18# This will get the latest oils-for-unix/oils-for-unix.github.io, copy the wiki
19# build into the clone and then commit + push it.
20#
21# This requires an environment variable to be set (usually in GitHub Actions
22# secrets): `OILS_PAGES_GITHUB_TOKEN`. This is a PAT with `content: write`
23# permissions to the oils-for-unix.github.io repo.
24
25const WIKI_OUT_DIR = '_tmp/wiki/out'
26const WIKI_DIR = '_tmp/wiki/git'
27const PAGES_DIR = '_tmp/wiki/oils-for-unix.github.io'
28
29proc pull-latest(dir, repo) {
30 ## Pull the latest commit from $repo to $dir. Will clone if necessary
31
32 if test -d $dir {
33 echo "$repo already cloned to $dir, pulling latest"
34
35 cd $dir {
36 git pull
37 }
38
39 return
40 }
41
42 echo "Cloning $repo to $dir"
43 mkdir -p $dir
44 git clone $repo $dir
45}
46
47
48func slugify(s) {
49 return (s.replace(" ", "-").replace(",", " "))
50}
51
52proc pre-render-wikilinks() {
53 ## GitHub wikis have a unique [[link syntax]] which references topic within
54 ## the wiki.
55 ##
56 ## This function converts that syntax to the traditional
57 ## [link syntax](./link-syntax.html) which will render correctly once fed to
58 ## doctools.
59 for line in (io.stdin) {
60 var mdlink = line.replace(/ '[[' <capture ![']']* as link> ']]' /,
61 ^"[$link]($[slugify(link)].html)")
62 write -- $[mdlink]
63 }
64}
65
66proc render-one(path) {
67 mkdir -p $WIKI_OUT_DIR
68
69 var name = path.replace(/ %start dot* '/' <capture dot* as name> '.md' /, ^"$[slugify(name)]")
70 var title = name.replace('-', ' ')
71 var dest = "$WIKI_OUT_DIR/$name.html"
72
73 fopen >$dest {
74 echo """
75 <!DOCTYPE html>
76 <html>
77 <head>
78 <meta name="viewport" content="width=device-width, initial-scale=1">
79 <title>$title</title>
80 <link rel="stylesheet" type="text/css" href="../web/base.css" />
81 </head>
82 <body class="width40">
83 """
84
85 pre-render-wikilinks <$path | cmark
86
87 echo """
88 </body>
89 </html>
90 """
91 }
92}
93
94
95proc render() {
96 ## Pull the latest version of the wiki and render it
97
98 pull-latest $WIKI_DIR https://github.com/oils-for-unix/oils.wiki.git
99 find $WIKI_DIR -name '*.md' -print0 | xargs -I {} -0 -- $0 render-one {}
100}
101
102
103proc commit-push() {
104 ## Commit and push the most recent build to oils-for-unix.github.io
105
106 var PAT = ENV.OILS_PAGES_GITHUB_TOKEN
107 pull-latest $PAGES_DIR "https://x-access-token:$PAT@github.com/oils-for-unix/oils-for-unix.github.io.git"
108
109 # Stash and pending changes (there should be none)
110 cd $PAGES_DIR {
111 git add .
112 git stash
113 }
114
115 # Update the wiki
116 rm -rf $PAGES_DIR/wiki
117 cp -r $WIKI_OUT_DIR $PAGES_DIR/wiki
118
119 cd $PAGES_DIR {
120 git add wiki
121
122 if git diff --quiet --staged {
123 echo 'No changes, will not commit'
124 } else {
125 var date = $(date --rfc-email)
126 git commit -m "Bump wiki $date"
127 git push
128 }
129 }
130}
131
132
133runproc @ARGV