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

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