summaryrefslogtreecommitdiff
path: root/scripts/setup.sh
blob: d77d23e74fcf6912c32b64ad411f322f1dc9117b (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env bash
# setup-docsify.sh — Bootstrap a self-hosted Docsify site on Apache 2.
#
# Run this script from your Apache document root:
#   cd /path/to/docroot
#   bash setup-docsify.sh
#
# Downloads Docsify core, the search plugin, the Vue theme, the copy-to-clipboard
# plugin, and extra Prism languages (bash, yaml, python, r) — all
# served locally, no external CDN dependencies at runtime.
#
# Re-running in an existing site is safe: it refreshes the assets and regenerates
# index.html, but never overwrites Markdown files (README.md, _sidebar.md, and any
# pages you have added) that already exist.

set -euo pipefail

# ── Configuration ───────────────────────────────────────────────────────────────
# Use "4" for the latest 4.x release, or pin to a specific version e.g. "4.13.1"
DOCSIFY_VERSION="4"

# ── Argument check ──────────────────────────────────────────────────────────────
if [[ $# -gt 0 ]]; then
  echo "This script takes no options (got: $*)." >&2
  echo "Usage: bash setup-docsify.sh" >&2
  exit 1
fi

# ── Output helpers ──────────────────────────────────────────────────────────────
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[0;33m'
RED='\033[0;31m'
BOLD='\033[1m'
NC='\033[0m'

step() { echo -e "\n${BLUE}${BOLD}==> $1${NC}"; }
ok()   { echo -e "  ${GREEN}✓${NC}  $1"; }
skip() { echo -e "  ${YELLOW}•${NC}  $1"; }
die()  { echo -e "  ${RED}✗${NC}  $1" >&2; exit 1; }

# ── Preflight checks ────────────────────────────────────────────────────────────
step "Preflight checks"

command -v curl &>/dev/null \
  || die "curl is required but not found. Install curl and try again."
ok "curl $(curl --version | head -1 | awk '{print $2}')"

if [[ -f index.html ]]; then
  echo -e "  ${RED}!${NC}  index.html already exists in: $(pwd)"
  echo -e "       This re-downloads assets and regenerates index.html (edits to"
  echo -e "       index.html will be lost). Existing Markdown files are left untouched."
  read -r -p "       Continue? [y/N] " reply
  [[ "${reply,,}" == "y" ]] || { echo "Aborted."; exit 0; }
fi

echo -e "  Working in: $(pwd)"

# ── Step 1: Directories ─────────────────────────────────────────────────────────
step "Step 1 — Creating directory structure"

mkdir -p assets/js assets/css
ok "assets/js/"
ok "assets/css/"

# ── Step 2: Download Docsify assets ─────────────────────────────────────────────
step "Step 2 — Downloading Docsify assets"

CDN="https://cdn.jsdelivr.net/npm"

fetch() {
  local url="$1" dest="$2"
  curl -sL --fail "$url" -o "$dest" || die "Failed to download: $url"
  ok "$dest"
}

# Core Docsify, search plugin, and theme
fetch "${CDN}/docsify@${DOCSIFY_VERSION}/lib/docsify.min.js"         assets/js/docsify.min.js
fetch "${CDN}/docsify@${DOCSIFY_VERSION}/lib/plugins/search.min.js"  assets/js/search.min.js
fetch "${CDN}/docsify@${DOCSIFY_VERSION}/themes/vue.css"             assets/css/vue.css

# Copy-to-clipboard plugin (injects its own CSS — no separate stylesheet needed)
fetch "${CDN}/docsify-copy-code@3/dist/docsify-copy-code.min.js"    assets/js/docsify-copy-code.min.js

# Extra Prism languages. Docsify already bundles Prism core plus html/css/clike/
# javascript, so we only add the languages it does NOT ship. Never download
# prism-core here — a standalone core replaces Docsify's Prism and silently breaks
# highlighting for every language loaded after it.
fetch "${CDN}/prismjs@1/components/prism-bash.min.js"    assets/js/prism-bash.min.js
fetch "${CDN}/prismjs@1/components/prism-yaml.min.js"    assets/js/prism-yaml.min.js
fetch "${CDN}/prismjs@1/components/prism-python.min.js"  assets/js/prism-python.min.js
fetch "${CDN}/prismjs@1/components/prism-r.min.js"       assets/js/prism-r.min.js

# ── Step 3: index.html ──────────────────────────────────────────────────────────
step "Step 3 — Creating index.html"

# The heredoc delimiter is single-quoted ('HTML') so the shell does not expand
# $docsify inside the template.
cat > index.html << 'HTML'
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>My Docs</title>
  <link rel="stylesheet" href="assets/css/vue.css">
</head>
<body>
  <div id="app"></div>
  <script>
    window.$docsify = {
      name: 'My Docs',
      loadSidebar: true,   // enables _sidebar.md
      subMaxLevel: 2,      // auto-generate H2 entries in sidebar
      search: 'auto'       // enables the search plugin
    }
  </script>
  <script src="assets/js/docsify.min.js"></script>
  <script src="assets/js/search.min.js"></script>
  <script src="assets/js/docsify-copy-code.min.js"></script>

  <!-- Extra Prism languages — Docsify bundles Prism core + html/css/js. Load after docsify. -->
  <script src="assets/js/prism-bash.min.js"></script>
  <script src="assets/js/prism-yaml.min.js"></script>
  <script src="assets/js/prism-python.min.js"></script>
  <script src="assets/js/prism-r.min.js"></script>
</body>
</html>
HTML

ok "index.html"

# ── Step 4: Boilerplate content ─────────────────────────────────────────────────
step "Step 4 — Boilerplate content (created only if missing)"

# Write the heredoc on stdin to $1, but never clobber an existing file — so
# re-running refreshes the assets and index.html without touching your content.
write_boilerplate() {
  local dest="$1"
  local content
  content="$(cat)"
  if [[ -e "$dest" ]]; then
    skip "$dest (exists — left unchanged)"
  else
    printf '%s\n' "$content" > "$dest"
    ok "$dest"
  fi
}

write_boilerplate README.md << 'EOF'
# Welcome

This is the home page of your documentation site.
Edit `README.md` to replace this content.

## Quick Links

- [Getting Started](getting-started.md)
EOF

write_boilerplate _sidebar.md << 'EOF'
- [Home](/)
- [Getting Started](getting-started.md)
EOF

write_boilerplate getting-started.md << 'EOF'
# Getting Started

This is an example page. Replace this content with your own documentation.

## Section One

Write your content here using standard Markdown.

## Section Two

Docsify will automatically generate sidebar anchors for H2 headings
when `subMaxLevel: 2` is set in `index.html`.
EOF

# ── Step 5: Permissions ─────────────────────────────────────────────────────────
step "Step 5 — Setting file permissions"

find . -type f -exec chmod 644 {} \;
find . -type d -exec chmod 755 {} \;
ok "Files: 644 | Directories: 755"

# ── Verify ───────────────────────────────────────────────────────────────────────
step "Verifying file layout"

find . -not -path '*/\.*' | sort

# ── Done ─────────────────────────────────────────────────────────────────────────
echo ""
echo -e "${GREEN}${BOLD}Setup complete.${NC}"
echo -e "Open your site in a browser to verify."
echo ""
echo -e "Syntax highlighting: html, css, and javascript are built into Docsify;"
echo -e "this setup adds bash, yaml, python, and r. To add more languages, download"
echo -e "additional prism-<lang>.min.js files into assets/js/ and add their <script>"
echo -e "tags to index.html. Copy-to-clipboard buttons are enabled on all code blocks."