doc: improve readme #968
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build Typst Projects | |
| on: | |
| push: | |
| branches: [ main ] | |
| pull_request: | |
| branches: [ main ] | |
| workflow_dispatch: | |
| jobs: | |
| check_changes: | |
| runs-on: ubuntu-22.04 | |
| outputs: | |
| should_build: ${{ steps.check_changes.outputs.should_build }} | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 # Fetch all history for git log commands | |
| - name: Check for changes in Typst projects or README | |
| id: check_changes | |
| run: | | |
| # Get the hash of the previous commit | |
| if [ "${{ github.event_name }}" == "pull_request" ]; then | |
| # For pull requests, compare against the base commit | |
| PREVIOUS_COMMIT=${{ github.event.pull_request.base.sha }} | |
| else | |
| # For pushes, compare against the previous commit | |
| PREVIOUS_COMMIT=$(git rev-parse HEAD^1 || echo "") | |
| fi | |
| if [ -z "$PREVIOUS_COMMIT" ]; then | |
| # If this is the first commit, we should build | |
| echo "should_build=true" >> $GITHUB_OUTPUT | |
| echo "First commit, building all projects" | |
| exit 0 | |
| fi | |
| # Check if any .typ or .typst files or README.md has changed | |
| CHANGED_FILES=$(git diff --name-only $PREVIOUS_COMMIT HEAD) | |
| TYPST_CHANGES=$(echo "$CHANGED_FILES" | grep -E '\.typ(st)?$' || true) | |
| README_CHANGES=$(echo "$CHANGED_FILES" | grep -E 'README\.md$' || true) | |
| if [ -n "$TYPST_CHANGES" ] || [ -n "$README_CHANGES" ] || [ "${{ github.event_name }}" == "workflow_dispatch" ]; then | |
| echo "should_build=true" >> $GITHUB_OUTPUT | |
| echo "Changes detected in Typst files or README.md, proceeding with build" | |
| else | |
| echo "should_build=false" >> $GITHUB_OUTPUT | |
| echo "No changes detected in Typst files or README.md, skipping build" | |
| fi | |
| build: | |
| needs: check_changes | |
| if: needs.check_changes.outputs.should_build == 'true' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 # Fetch all history for git log commands | |
| - name: Install packages | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y poppler-utils | |
| - name: Setup Typst | |
| uses: typst-community/setup-typst@v3 | |
| with: | |
| typst-version: latest | |
| - name: Find Typst projects with main.typst | |
| id: find_projects | |
| run: | | |
| echo "TYPST_DIRS=$(find . -name "main.typst" -o -name "main.typ" | xargs dirname | tr '\n' ' ')" >> $GITHUB_ENV | |
| mkdir -p build | |
| - name: Build PDFs from Typst projects | |
| run: | | |
| # Create a file to store modification dates | |
| touch modification_dates.txt | |
| for dir in $TYPST_DIRS; do | |
| if [ -f "$dir/main.typst" ]; then | |
| main_file="$dir/main.typst" | |
| else | |
| main_file="$dir/main.typ" | |
| fi | |
| # Get relative path from repository root | |
| rel_dir="${dir#./}" | |
| echo "Building $main_file to build/$rel_dir/main.pdf" | |
| mkdir -p "build/$rel_dir" | |
| # Try to compile, continue if there's an error | |
| typst compile "$main_file" "build/$rel_dir/main.pdf" || echo "Failed to build $main_file, continuing..." | |
| # If build was successful, get the last modified date based on content changes | |
| if [ -f "build/$rel_dir/main.pdf" ]; then | |
| # Get the last commit date that modified ANY file in this directory or subdirectories | |
| # This captures changes to includes, images, data files, etc. | |
| last_modified=$(git log -1 --format="%ad" --date=format:"%Y-%m-%d" -- "$dir") | |
| # If git log fails (e.g., directory not yet committed), use file system date | |
| if [ -z "$last_modified" ]; then | |
| last_modified=$(find "$dir" -type f -printf "%TY-%Tm-%Td\n" | sort -r | head -n1) | |
| fi | |
| # Store the file path and its last modified date | |
| echo "$rel_dir/main.pdf|$last_modified" >> modification_dates.txt | |
| fi | |
| done | |
| - name: Generate Enhanced README | |
| run: | | |
| # Create header with site info | |
| if [ -f "README.md" ]; then | |
| echo "$(head -n 1 README.md)" >> build/README.md | |
| fi | |
| cat > build/README.md << 'EOF' | |
| <div align="center"> | |
| <h3>Automatically Generated PDF Documents</h3> | |
| <p> | |
| <em>This site hosts the compiled PDF files from the Typst project source files of <a href="https://github.com/Huy-DNA/MPiSC/tree/main">MPiSC</a>.</em> | |
| </p> | |
| </div> | |
| --- | |
| ## 📄 Available Documents | |
| <table> | |
| <thead> | |
| <tr> | |
| <th align="left">Document</th> | |
| <th align="left">Last Content Update</th> | |
| <th align="center">View</th> | |
| <th align="center">Download</th> | |
| </tr> | |
| </thead> | |
| <tbody> | |
| EOF | |
| # Add table rows for each PDF | |
| while IFS="|" read -r file_path last_mod_date; do | |
| pdf_name=$(pdfinfo "build/$file_path" | grep "Title") | |
| pdf_name=${pdf_name:6} | |
| echo " <tr>" >> build/README.md | |
| echo " <td><strong>$pdf_name</strong></td>" >> build/README.md | |
| echo " <td>$last_mod_date</td>" >> build/README.md | |
| echo " <td align=\"center\"><a href=\"$file_path\">📕 View</a></td>" >> build/README.md | |
| echo " <td align=\"center\"><a href=\"$file_path\" download>⬇️ PDF</a></td>" >> build/README.md | |
| echo " </tr>" >> build/README.md | |
| done < modification_dates.txt | |
| # Close the table | |
| echo " </tbody>" >> build/README.md | |
| echo " </table>" >> build/README.md | |
| echo "" >> build/README.md | |
| # Add original README content if it exists | |
| echo "## 📝 Project Information" >> build/README.md | |
| echo "" >> build/README.md | |
| if [ -f "README.md" ]; then | |
| echo "" >> build/README.md | |
| cat README.md | tail -n +2 | sed -E 's/^(#+)/\1#/' >> build/README.md | |
| echo "" >> build/README.md | |
| fi | |
| # Add footer with build info | |
| cat >> build/README.md << EOF | |
| --- | |
| <div align="center"> | |
| <p> | |
| <small>Last build: $(date)</small><br> | |
| <small>Generated by GitHub Actions • <a href="https://github.com/${GITHUB_REPOSITORY}/tree/main">View Source</a></small> | |
| </p> | |
| </div> | |
| EOF | |
| - name: Deploy to GitHub Pages | |
| uses: JamesIves/github-pages-deploy-action@v4 | |
| with: | |
| folder: build | |
| branch: gh-pages | |
| clean: true | |
| commit-message: "Deploy Typst PDFs to GitHub Pages [skip ci]" |