|
| 1 | +name: Deploy to GitHub Pages |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main ] |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + pages: write |
| 11 | + id-token: write |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: "pages" |
| 15 | + cancel-in-progress: true |
| 16 | + |
| 17 | +jobs: |
| 18 | + build: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + steps: |
| 21 | + - name: Checkout this repo |
| 22 | + uses: actions/checkout@v4 |
| 23 | + with: |
| 24 | + fetch-depth: 1 |
| 25 | + |
| 26 | + - name: Install tools |
| 27 | + run: | |
| 28 | + sudo apt-get update |
| 29 | + sudo apt-get install -y pandoc jq |
| 30 | +
|
| 31 | + # Prefer an existing docs/index.html; only generate from README if missing. |
| 32 | + - id: build_page |
| 33 | + name: Build docs/index.html (prefer existing; fallback to README) |
| 34 | + env: |
| 35 | + REPO_HTML_TITLE: ${{ github.repository }} |
| 36 | + run: | |
| 37 | + set -e |
| 38 | + mkdir -p docs |
| 39 | + GENERATED=0 |
| 40 | +
|
| 41 | + if [ -f docs/index.html ]; then |
| 42 | + echo "Keeping existing docs/index.html (not regenerating)." |
| 43 | + elif [ -f README.md ]; then |
| 44 | + echo "Generating docs/index.html from README.md..." |
| 45 | + pandoc README.md -f markdown -t html -s -o docs/index.html --metadata title="$REPO_HTML_TITLE" |
| 46 | + GENERATED=1 |
| 47 | + else |
| 48 | + echo "No docs/index.html or README.md found; writing minimal page." |
| 49 | + printf '%s\n' \ |
| 50 | + '<!doctype html><html><head><meta charset="utf-8"><title>Site</title></head><body>' \ |
| 51 | + '<h1>Site</h1>' \ |
| 52 | + '<p>No README.md found. Add one and push to regenerate this page.</p>' \ |
| 53 | + '</body></html>' \ |
| 54 | + > docs/index.html |
| 55 | + GENERATED=1 |
| 56 | + fi |
| 57 | +
|
| 58 | + # Ensure Arial font is applied, even on a custom index.html |
| 59 | + if grep -qi '</head>' docs/index.html; then |
| 60 | + sed -i 's|</head>|<style>body{font-family:Arial, Helvetica, sans-serif;} h1,h2,h3,h4,h5,h6{font-family:Arial, Helvetica, sans-serif;}</style></head>|' docs/index.html |
| 61 | + else |
| 62 | + # Wrap bare content with a head + Arial |
| 63 | + TMP=$(mktemp) |
| 64 | + printf '%s\n' \ |
| 65 | + '<!doctype html><html><head><meta charset="utf-8">' \ |
| 66 | + '<style>body{font-family:Arial, Helvetica, sans-serif;} h1,h2,h3,h4,h5,h6{font-family:Arial, Helvetica, sans-serif;}</style>' \ |
| 67 | + '</head><body>' \ |
| 68 | + > "$TMP" |
| 69 | + cat docs/index.html >> "$TMP" |
| 70 | + printf '%s\n' '</body></html>' >> "$TMP" |
| 71 | + mv "$TMP" docs/index.html |
| 72 | + fi |
| 73 | +
|
| 74 | + # Expose whether we generated from README/minimal (1) or kept existing (0) |
| 75 | + echo "generated=$GENERATED" >> "$GITHUB_OUTPUT" |
| 76 | +
|
| 77 | + # Append citation ONLY when we generated the page from README/minimal |
| 78 | + - name: Append suggested citation (written + BibTeX) + repo metadata |
| 79 | + if: steps.build_page.outputs.generated == '1' |
| 80 | + env: |
| 81 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 82 | + REPO: ${{ github.repository }} |
| 83 | + OWNER: ${{ github.repository_owner }} |
| 84 | + run: | |
| 85 | + set -e |
| 86 | +
|
| 87 | + REPO_NAME="${REPO#*/}" |
| 88 | + REPO_URL="https://github.com/$REPO" |
| 89 | + LAST_UPDATE=$(git log -1 --format=%cI || date -u +%Y-%m-%dT%H:%M:%SZ) |
| 90 | + YEAR=$(date -u -d "$LAST_UPDATE" +%Y 2>/dev/null || date -u +%Y) |
| 91 | +
|
| 92 | + # Collect contributor logins (exclude bots/ghost) |
| 93 | + logins=$(curl -s -H "Authorization: Bearer $GH_TOKEN" \ |
| 94 | + "https://api.github.com/repos/$REPO/contributors?per_page=100&anon=false" \ |
| 95 | + | jq -r '.[] | select((.type // "") != "Bot") | select(.login != "ghost") | .login' | sort -fu) |
| 96 | +
|
| 97 | + # Map to display names (fallback to login), skip Steph to keep her first |
| 98 | + names="" |
| 99 | + for u in $logins; do |
| 100 | + name=$(curl -s -H "Authorization: Bearer $GH_TOKEN" "https://api.github.com/users/$u" | jq -r '.name // empty') |
| 101 | + [ -z "$name" ] && name="$u" |
| 102 | + if [ "$name" != "Steph Buongiorno" ] && [ "$u" != "stephbuon" ]; then |
| 103 | + names="$names\n$name" |
| 104 | + fi |
| 105 | + done |
| 106 | + names_sorted=$(printf "%b" "$names" | awk 'NF' | sort -fu) |
| 107 | +
|
| 108 | + # Suggested (written) citation |
| 109 | + citation_written="Steph Buongiorno" |
| 110 | + if [ -n "$names_sorted" ]; then |
| 111 | + citation_written="$citation_written; $(echo "$names_sorted" | paste -sd ', ' -)" |
| 112 | + fi |
| 113 | + citation_written="$citation_written. $REPO_NAME. $YEAR. Available at: $REPO_URL" |
| 114 | +
|
| 115 | + # Proper BibTeX authors: "A and B and C" |
| 116 | + authors_bibtex="Steph Buongiorno" |
| 117 | + if [ -n "$names_sorted" ]; then |
| 118 | + authors_bibtex="$authors_bibtex and $(echo "$names_sorted" | paste -sd ' and ' -)" |
| 119 | + fi |
| 120 | +
|
| 121 | + # BibTeX entry as a shell variable (YAML-safe) |
| 122 | + bibtex="@misc{$REPO_NAME-$YEAR, |
| 123 | + author = {$authors_bibtex}, |
| 124 | + title = {$REPO_NAME}, |
| 125 | + year = {$YEAR}, |
| 126 | + howpublished = {\\url{$REPO_URL}}, |
| 127 | + note = {Last updated: $LAST_UPDATE} |
| 128 | + }" |
| 129 | +
|
| 130 | + # Append both to index.html |
| 131 | + printf '%s\n' \ |
| 132 | + '' \ |
| 133 | + '<hr>' \ |
| 134 | + '<h2>Suggested Citation</h2>' \ |
| 135 | + "<p>$citation_written</p>" \ |
| 136 | + '' \ |
| 137 | + '<h3>BibTeX</h3>' \ |
| 138 | + '<pre><code>' \ |
| 139 | + "$bibtex" \ |
| 140 | + '</code></pre>' \ |
| 141 | + '' \ |
| 142 | + '<h3>Repository</h3>' \ |
| 143 | + "<p><em>$REPO_NAME</em><br>" \ |
| 144 | + "<a href=\"$REPO_URL\">$REPO_URL</a><br>" \ |
| 145 | + "<strong>Last updated:</strong> $LAST_UPDATE</p>" \ |
| 146 | + >> docs/index.html |
| 147 | +
|
| 148 | + - name: Upload site artifact |
| 149 | + uses: actions/upload-pages-artifact@v3 |
| 150 | + with: |
| 151 | + path: docs |
| 152 | + |
| 153 | + deploy: |
| 154 | + needs: build |
| 155 | + runs-on: ubuntu-latest |
| 156 | + environment: |
| 157 | + name: github-pages |
| 158 | + url: ${{ steps.deployment.outputs.page_url }} |
| 159 | + steps: |
| 160 | + - id: deployment |
| 161 | + uses: actions/deploy-pages@v4 |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | + |
0 commit comments