Skip to content

Update pages.yml

Update pages.yml #36

Workflow file for this run

name: Deploy to GitHub Pages
on:
push:
branches: [ main ]
workflow_dispatch:
permissions:
contents: write
pages: write
id-token: write
concurrency:
group: "pages"
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout this repo
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Install tools
run: |
sudo apt-get update
sudo apt-get install -y pandoc jq curl
- id: ensure_license
name: Ensure MIT License exists (and mark if created)
run: |
if [ ! -f LICENSE ]; then
YEAR=$(date +%Y)
cat > LICENSE <<EOF
MIT License
Copyright (c) $YEAR Steph Buongiorno
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
EOF
echo "MIT License generated."
echo "created=true" >> "$GITHUB_OUTPUT"
else
echo "LICENSE file already exists — skipping generation."
echo "created=false" >> "$GITHUB_OUTPUT"
fi
- name: Commit LICENSE to repo (first time only)
if: steps.ensure_license.outputs.created == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add LICENSE
git commit -m "Add MIT LICENSE (auto-generated)"
git push
- id: build_page
name: Build index.html with custom template + grid sidebar
env:
REPO_HTML_TITLE: ${{ github.repository }}
run: |
set -e
GENERATED=0
OWNER="${GITHUB_REPOSITORY%/*}"
REPO_NAME="${GITHUB_REPOSITORY#*/}"
HOMELINK="https://democracy-lab.github.io/"
REPO_URL="https://github.com/$OWNER/$REPO_NAME"
cat > pandoc_template.html <<'TPL'
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>$title$</title>
</head>
<body>
$for(include-before)$
$include-before$
$endfor$
$body$
$for(include-after)$
$include-after$
$endfor$
</body>
</html>
TPL
if [ -f index.html ]; then
echo "Keeping existing index.html (not regenerating)."
elif [ -f README.md ]; then
echo "Generating index.html from README.md..."
pandoc README.md -f markdown -t html -s --template=pandoc_template.html -o index.html --metadata title="$REPO_HTML_TITLE"
GENERATED=1
else
echo "No index.html or README.md found; writing minimal page."
printf '%s\n' '<!doctype html><html><head><meta charset="utf-8"><title>Site</title></head><body>' '<h1>Site</h1>' '<p>No README.md found. Add one and push to regenerate this page.</p>' '</body></html>' > index.html
GENERATED=1
fi
CSS='<style>
html, body { width:100%; max-width:none; margin:0; padding:0; font-family: Arial, Helvetica, sans-serif; color:#222; }
h1, h2, h3, h4, h5, h6 { font-family: Arial, Helvetica, sans-serif; }
pre code { display:block; padding:.75em; background:#f6f8fa; border-radius:6px; font-size:90%; overflow:auto; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; }
.page { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; background: linear-gradient(to right, #f4f4f4 0, #f4f4f4 220px, transparent 220px); }
.sidebar { padding: 20px; position: sticky; top: 0; height: 100vh; overflow: auto; }
.sidebar a { display:block; margin-bottom:10px; color:#0366d6; text-decoration:none; font-weight:600; }
.sidebar a:hover { text-decoration:underline; }
.content { width: 100%; }
.container { width:100%; max-width: 1400px; margin: 0 auto; padding: 48px; }
.extras { margin-top: 48px; }
.extras h2, .extras h3 { margin-top: 1.2em; }
@media (max-width: 1000px) {
.page { display:block; background:none; }
.sidebar { position: static; height:auto; overflow:visible; padding:16px; background:#f4f4f4; }
.container { max-width:100%; margin:0; padding:24px; }
}
</style>'
if grep -qi '</head>' index.html; then
awk -v css="$CSS" 'BEGIN{IGNORECASE=1} { if (!done && match(tolower($0), /<\/head>/)) { sub(/<\/head>/, css"</head>"); done=1 } print }' index.html > index.html.tmp
mv index.html.tmp index.html
fi
if grep -qi '<div class="page"' index.html; then
echo "Wrapper already present; skipping."
else
awk -v home="$HOMELINK" -v repo="$REPO_URL" 'BEGIN{IGNORECASE=1}
{
if (!done && match(tolower($0), /<body[^>]*>/)) {
sub(/<body[^>]*>/, "&\n<div class=\"page\"><aside class=\"sidebar\"><a href=\"" home "\" aria-label=\"Go to home\">← Home</a>\n<a href=\"" repo "\" aria-label=\"View source on GitHub\">View Repository</a></aside>\n<main class=\"content\"><div class=\"container\">");
done=1
}
print
}' index.html > index.html.tmp && mv index.html.tmp index.html
awk 'BEGIN{IGNORECASE=1} { sub(/<\/body>/, "</div></main></div></body>"); print }' index.html > index.html.tmp && mv index.html.tmp index.html
fi
echo "generated=$GENERATED" >> "$GITHUB_OUTPUT"
- name: Inject License/Citation/BibTeX block BELOW README content (inside container)
if: steps.build_page.outputs.generated == '1'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
OWNER: ${{ github.repository_owner }}
run: |
set -e
REPO_NAME="${REPO#*/}"
REPO_URL="https://github.com/$REPO"
LAST_UPDATE=$(git log -1 --format=%cI || date -u +%Y-%m-%dT%H:%M:%SZ)
YEAR=$(date -u -d "$LAST_UPDATE" +%Y 2>/dev/null || date -u +%Y)
logins=$(curl -s -H "Authorization: Bearer $GH_TOKEN" \
"https://api.github.com/repos/$REPO/contributors?per_page=100&anon=false" \
| jq -r '.[] | select((.type // "") != "Bot") | select(.login != "ghost") | .login' | sort -fu)
names=""
for u in $logins; do
name=$(curl -s -H "Authorization: Bearer $GH_TOKEN" "https://api.github.com/users/$u" | jq -r '.name // empty')
[ -z "$name" ] && name="$u"
if [ "$name" != "Steph Buongiorno" ] && [ "$u" != "stephbuon" ]; then
names="$names\n$name"
fi
done
names_sorted=$(printf "%b" "$names" | awk 'NF' | sort -fu)
citation_written="Steph Buongiorno"
if [ -n "$names_sorted" ]; then
citation_written="$citation_written; $(echo "$names_sorted" | paste -sd ', ' -)"
fi
citation_written="$citation_written. $REPO_NAME. $YEAR. Available at: $REPO_URL"
authors_bibtex="Steph Buongiorno"
if [ -n "$names_sorted" ]; then
authors_bibtex="$authors_bibtex and $(echo "$names_sorted" | paste -sd ' and ' -)"
fi
bibtex="@misc{$REPO_NAME-$YEAR,
author = {$authors_bibtex},
title = {$REPO_NAME},
year = {$YEAR},
howpublished = {\\url{$REPO_URL}},
note = {Last updated: $LAST_UPDATE}
}"
EXTRAS=$(cat <<'HTML'
<section class="extras">
<hr>
<h2>License</h2>
<p>This project is licensed under the <a href="LICENSE">MIT License</a>.</p>
<h2>Suggested Citation</h2>
<p>__CITATION_WRITTEN__</p>
<h3>BibTeX</h3>
<pre><code>__BIBTEX__</code></pre>
<h3>Repository</h3>
<p><em>__REPO_NAME__</em><br>
<a href="__REPO_URL__">__REPO_URL__</a><br>
<strong>Last updated:</strong> __LAST_UPDATE__</p>
</section>
HTML
)
EXTRAS=${EXTRAS/__CITATION_WRITTEN__/$citation_written}
EXTRAS=${EXTRAS/__BIBTEX__/$(printf "%s" "$bibtex" | sed 's/[&/\\]/\\&/g')}
EXTRAS=${EXTRAS/__REPO_NAME__/$REPO_NAME}
EXTRAS=${EXTRAS/__REPO_URL__/$REPO_URL}
EXTRAS=${EXTRAS/__LAST_UPDATE__/$LAST_UPDATE}
awk -v block="$EXTRAS" 'BEGIN{IGNORECASE=1}
{
line=$0
if (!done && match(tolower(line), /<\/div>\s*<\/main>\s*<\/div>\s*<\/body>/)) {
sub(/<\/div>\s*<\/main>\s*<\/div>\s*<\/body>/, block"</div></main></div></body>")
done=1
}
print
}' index.html > index.html.tmp && mv index.html.tmp index.html
- name: Upload site artifact
uses: actions/upload-pages-artifact@v3
with:
path: .
deploy:
needs: build
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- id: deployment
uses: actions/deploy-pages@v4