|
| 1 | +# Thymeleaf Preview Generation for GitHub Pages |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This directory contains scripts to generate static HTML previews from Thymeleaf templates for GitHub Pages PR previews. |
| 6 | + |
| 7 | +## Files |
| 8 | + |
| 9 | +- `generate_thymeleaf_previews.py` - Main script that converts Thymeleaf templates to static HTML |
| 10 | +- `test_thymeleaf_generation.sh` - Test script to verify the generation works |
| 11 | +- `update_pr_index.py` - Updates the main GitHub Pages index with PR information |
| 12 | +- `remove_pr_from_index.py` - Removes PR information when PR is closed |
| 13 | + |
| 14 | +## How It Works |
| 15 | + |
| 16 | +### Thymeleaf Template Processing |
| 17 | + |
| 18 | +The `generate_thymeleaf_previews.py` script: |
| 19 | + |
| 20 | +1. **Parses Thymeleaf Templates**: Reads template files from `src/main/resources/templates/` |
| 21 | +2. **Processes Thymeleaf Syntax**: Converts Thymeleaf expressions to static HTML: |
| 22 | + - `th:text="${variable}"` → Static text with mock data |
| 23 | + - `th:each="item : ${items}"` → Generated HTML loops |
| 24 | + - `th:if="${condition}"` → Conditional content |
| 25 | + - `th:href="@{/path}"` → Static links |
| 26 | +3. **Adds Mock Data**: Provides realistic preview data for: |
| 27 | + - Challenge information |
| 28 | + - User stats and configuration |
| 29 | + - Session data |
| 30 | + - Canary token information |
| 31 | +4. **Generates Navigation**: Adds navigation between preview pages |
| 32 | +5. **Outputs Static HTML**: Saves processed templates as standalone HTML files |
| 33 | + |
| 34 | +### Generated Pages |
| 35 | + |
| 36 | +- **welcome.html** - Home page with challenge table (from `welcome.html` template) |
| 37 | +- **about.html** - About page with project information (from `about.html` template) |
| 38 | +- **stats.html** - Stats and configuration page (from `stats.html` template) |
| 39 | +- **challenge-example.html** - Sample challenge page (from `challenge.html` template) |
| 40 | + |
| 41 | +### Integration with GitHub Actions |
| 42 | + |
| 43 | +The script is integrated into the GitHub Pages preview workflow: |
| 44 | + |
| 45 | +1. PR is opened/updated with template changes |
| 46 | +2. GitHub Actions runs the build process |
| 47 | +3. Script generates static HTML from updated templates |
| 48 | +4. Static files are deployed to GitHub Pages |
| 49 | +5. PR comment includes links to preview pages |
| 50 | + |
| 51 | +## Usage |
| 52 | + |
| 53 | +### Manual Testing |
| 54 | + |
| 55 | +```bash |
| 56 | +# Generate previews for PR number 123 |
| 57 | +python3 .github/scripts/generate_thymeleaf_previews.py 123 |
| 58 | + |
| 59 | +# Run tests |
| 60 | +./.github/scripts/test_thymeleaf_generation.sh |
| 61 | +``` |
| 62 | + |
| 63 | +### GitHub Actions Integration |
| 64 | + |
| 65 | +The script runs automatically in the `github-pages-preview.yml` workflow when: |
| 66 | +- PRs are opened, synchronized, or reopened |
| 67 | +- Changes are made to templates or related files |
| 68 | + |
| 69 | +## Mock Data |
| 70 | + |
| 71 | +The script includes realistic mock data: |
| 72 | + |
| 73 | +- **Challenges**: 10 sample challenges with different difficulties and environments |
| 74 | +- **Stats**: Session counters, canary callbacks, configuration settings |
| 75 | +- **User State**: Some challenges marked as completed for preview purposes |
| 76 | +- **Configuration**: Hints, reasons, CTF mode, and other feature flags |
| 77 | + |
| 78 | +## Benefits |
| 79 | + |
| 80 | +- **Better PR Reviews**: Reviewers can see rendered HTML instead of raw templates |
| 81 | +- **Visual Validation**: Changes to styling and layout are immediately visible |
| 82 | +- **No Build Required**: Works without running the full Spring Boot application |
| 83 | +- **Fast Preview**: Generates quickly using mock data |
| 84 | +- **Mobile Friendly**: Responsive design works on all devices |
| 85 | + |
| 86 | +## Limitations |
| 87 | + |
| 88 | +- Uses mock data instead of real application data |
| 89 | +- Some dynamic features (JavaScript interactions) may not work fully |
| 90 | +- Template fragments and includes are simplified |
| 91 | +- Complex Thymeleaf expressions may need manual handling |
| 92 | + |
| 93 | +## Adding New Templates |
| 94 | + |
| 95 | +To add preview support for new templates: |
| 96 | + |
| 97 | +1. Add the template processing logic to `ThymeleafToStaticConverter.generate_*_page()` method |
| 98 | +2. Add mock data for any new variables in `self.mock_data` |
| 99 | +3. Add navigation links in `generate_navigation_html()` |
| 100 | +4. Update the main preview page to link to the new page |
| 101 | +5. Test with the test script |
| 102 | + |
| 103 | +## Troubleshooting |
| 104 | + |
| 105 | +- **Missing templates**: Script will generate fallback content |
| 106 | +- **Parse errors**: Check Thymeleaf syntax in templates |
| 107 | +- **Empty output**: Verify mock data contains expected variables |
| 108 | +- **Broken layout**: Check CSS/JS file paths in static assets |
0 commit comments