forked from martinthomson/i-d-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-index.sh
executable file
·125 lines (109 loc) · 3.7 KB
/
build-index.sh
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env bash
# Usage: $0 [dir] [gh-user] [gh-repo] > index.html
hash realpath 2>/dev/null || function realpath() { cd "$1"; pwd -P; }
master=${GITHUB_MASTER:-master}
root=$(realpath "${1:-.}")
branch="${2:-$master}"
user="${3:-<user>}"
repo="${4:-<repo>}"
gh="https://github.com/${user}/${repo}"
indent=''
function p() {
echo -n "$indent";echo "$@"
}
function pi() {
p "$@"
indent="$indent "
}
function po() {
indent="${indent# }"
p "$@"
}
p '<!DOCTYPE html>'
pi '<html>'
pi '<head>'
p '<title>'"$user/$repo $branch"' preview</title>'
p '<meta name="viewport" content="initial-scale=1.0">'
pi '<style type="text/css">/*<![CDATA[*/'
p 'body { font-family: "Helvetica Neue","Open Sans",Helvetica,Calibri,sans-serif; }'
p 'h1, h2, td { font-family: "Helvetica Neue","Roboto Condensed","Open Sans",Helvetica,Calibri,sans-serif; }'
p 'h1 { font-size: 20px; } h2 { font-size: 16px; }'
p 'table { margin: 5px 10px; border-collapse: collapse; }'
p 'th, td { font-weight: normal; text-align: left; padding: 2px 5px; }'
p 'a:link { color: #000; } a:visited { color: #00a; }'
po '/*]]>*/</style>'
po '</head>'
pi '<body>'
function rfcdiff() {
echo "https://tools.ietf.org/rfcdiff?url1=${1}&url2=${2}"
}
function reldot() {
[ "$1" = "$root" ] && echo '.' || echo "${1/$root\//}"
}
function githubio() {
d="${1%/}/"
echo "https://${user}.github.io/${repo}/${d#master/}${2}.txt"
}
function list_dir() {
pi '<table id="branch-'"$2"'">'
for file in "$1"/*.txt; do
dir=$(dirname "$file")
file=$(basename "$file" .txt)
pi '<tr>'
p '<th>'"${file}"'</th>'
p '<td><a href="'"$(reldot "$dir")/${file}"'.html"'
p ' class="html '"$file"'">html</a></td>'
p '<td><a href="'"$(reldot "$dir")/${file}"'.txt"'
p ' class="txt '"$file"'">plain text</a></td>'
this_githubio=$(githubio "$branch${dir#$root}" "$file")
if [ "$2" != "$master" ]; then
diff=$(rfcdiff $(githubio "$master/" "$file") "$this_githubio")
p '<td><a href='"$diff"'>diff with '"$master"'</a></td>'
fi
diff=$(rfcdiff "https://tools.ietf.org/id/${file}.txt" "$this_githubio")
p '<td><a href="'"$diff"'" class="diff '"$file"'">'
p ' diff with last submission</a></td>'
po '</tr>'
done
po '</table>'
}
branchlink="$gh"
[ "$branch" = "$master" ] || branchlink="${branchlink}/tree/${branch}"
p "<h1>Editor's drafts for ${branch} branch of <a href=\"${branchlink}\">${user}/${repo}</a></h1>"
p "<p>View <a href=\"issues.html\">saved issues</a>,"
p " or the latest GitHub issues <a href=\"${gh}/issues\">issues</a>"
p " and <a href=\"${gh}/pulls\">pull requests</a>.</p>"
list_dir "${root}" $branch
for dir in $(find "${root}" -mindepth 1 -type d \( -name '.*' -prune -o -print \)); do
dirbranch="${dir#$root/}"
p '<h2>Preview for branch <a href="'"$dirbranch"'">'"$dirbranch"'</a></h2>'
list_dir "$dir" "$dirbranch"
done
pi '<script>'
cat <<EOJS
// @licstart
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/
// @licend
window.onload = function() {
var referrer_branch = 'master';
// e.g., "https://github.com/user/repo/tree/master"
var chunks = document.referrer.split("/");
if (chunks[2] === 'github.com' && chunks[5] === 'tree') {
referrer_branch = chunks[6];
}
let branch = document.querySelector('#branch-' + referrer_branch);
let h = document.location.hash.substring(1);
if (h === 'show') {
document.location.hash = '#' + branch.id;
} else if (branch && h.startsWith('go')) {
let e = branch.querySelector(h.substring(2));
if (e && e.href) {
document.location = e.href;
}
}
};
EOJS
po '</script>'
po '</body>'
po '</html>'