-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuildDocs.sh
More file actions
executable file
·135 lines (115 loc) · 4.22 KB
/
buildDocs.sh
File metadata and controls
executable file
·135 lines (115 loc) · 4.22 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
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
126
127
128
129
130
131
132
133
134
135
#!/bin/bash
set -euo pipefail
OUTPUT_PATH="${DOCC_OUTPUT_PATH:-$HOME/Downloads/couchdbclient}"
OUTPUT_PARENT="$(dirname "$OUTPUT_PATH")"
HOSTING_BASE_PATH="${DOCC_HOSTING_BASE_PATH:-docs/couchdbclient}"
swift package --allow-writing-to-directory "$OUTPUT_PARENT" \
generate-documentation \
--target CouchDBClient \
--disable-indexing \
--output-path "$OUTPUT_PATH" \
--transform-for-static-hosting \
--experimental-transform-for-static-hosting-with-content \
--hosting-base-path "$HOSTING_BASE_PATH" \
--enable-experimental-markdown-output \
--enable-experimental-markdown-output-manifest
python3 - "$OUTPUT_PATH" <<'PY'
import html
import os
import sys
from pathlib import Path
output_path = Path(sys.argv[1])
link_html = (
'<a id="docc-markdown-link" href="#" aria-label="Open Markdown version" '
'style="position:fixed;right:16px;bottom:16px;z-index:2147483647;'
'padding:8px 12px;border-radius:9999px;background:rgba(255,255,255,0.94);'
'border:1px solid rgba(0,0,0,0.08);box-shadow:0 2px 8px rgba(0,0,0,0.18);'
'color:#06c;font:500 13px -apple-system,BlinkMacSystemFont,'
''SF Pro Text','Helvetica Neue',sans-serif;text-decoration:none;'
'backdrop-filter:saturate(180%) blur(20px)" hidden>Markdown</a>'
)
script_html = """
<script>
(function() {
const link = document.getElementById("docc-markdown-link");
if (!link) return;
function trimSlashes(value) {
return value.replace(/^\\/+|\\/+$/g, "");
}
function splitPath(value) {
const trimmed = trimSlashes(value);
return trimmed ? trimmed.split("/") : [];
}
function relativeHref(fromDirectory, toPath) {
const fromParts = splitPath(fromDirectory);
const toParts = splitPath(toPath);
let sharedIndex = 0;
while (
sharedIndex < fromParts.length &&
sharedIndex < toParts.length &&
fromParts[sharedIndex] === toParts[sharedIndex]
) {
sharedIndex += 1;
}
return [
...Array(fromParts.length - sharedIndex).fill(".."),
...toParts.slice(sharedIndex)
].join("/") || ".";
}
function currentRoute() {
const configuredBaseUrl =
typeof baseUrl === "string" && baseUrl.length > 0 ? baseUrl : "/";
const normalizedBaseUrl = trimSlashes(configuredBaseUrl);
const basePrefix = normalizedBaseUrl ? "/" + normalizedBaseUrl + "/" : "/";
let pathname = window.location.pathname;
if (normalizedBaseUrl && pathname.startsWith(basePrefix)) {
pathname = pathname.slice(basePrefix.length);
} else {
pathname = pathname.replace(/^\\/+/, "");
}
pathname = pathname.replace(/index\\.html$/, "");
return trimSlashes(pathname);
}
function updateMarkdownLink() {
const route = currentRoute();
if (!route) {
link.hidden = true;
link.removeAttribute("href");
return;
}
link.href = relativeHref(route, "data/" + route.toLowerCase() + ".md");
link.hidden = false;
}
const originalPushState = history.pushState;
history.pushState = function() {
const result = originalPushState.apply(this, arguments);
queueMicrotask(updateMarkdownLink);
return result;
};
const originalReplaceState = history.replaceState;
history.replaceState = function() {
const result = originalReplaceState.apply(this, arguments);
queueMicrotask(updateMarkdownLink);
return result;
};
window.addEventListener("popstate", updateMarkdownLink);
updateMarkdownLink();
})();
</script>
"""
for html_path in output_path.rglob("index.html"):
contents = html_path.read_text()
if 'id="docc-markdown-link"' in contents:
continue
route = html_path.relative_to(output_path).as_posix()
route = route[:-len("/index.html")] if route.endswith("/index.html") else ""
markdown_path = output_path / "data" / f"{route.lower()}.md" if route else None
relative_markdown_path = (
os.path.relpath(markdown_path, html_path.parent).replace(os.sep, "/")
if markdown_path and markdown_path.exists()
else "#"
)
page_link_html = link_html.replace('href="#"', f'href="{html.escape(relative_markdown_path, quote=True)}"')
updated_contents = contents.replace("</body>", f"{page_link_html}{script_html}</body>")
html_path.write_text(updated_contents)
PY