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

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