This directory contains scripts to generate static HTML previews from Thymeleaf templates for GitHub Pages PR previews.
generate_thymeleaf_previews.py- Main script that converts Thymeleaf templates to static HTMLtest_thymeleaf_generation.sh- Test script to verify the generation worksupdate_pr_index.py- Updates the main GitHub Pages index with PR informationremove_pr_from_index.py- Removes PR information when PR is closed
The generate_thymeleaf_previews.py script:
- Parses Thymeleaf Templates: Reads template files from
src/main/resources/templates/ - Processes Thymeleaf Syntax: Converts Thymeleaf expressions to static HTML:
th:text="${variable}"→ Static text with mock datath:each="item : ${items}"→ Generated HTML loopsth:if="${condition}"→ Conditional contentth:href="@{/path}"→ Static links
- Adds Mock Data: Provides realistic preview data for:
- Challenge information
- User stats and configuration
- Session data
- Canary token information
- Generates Navigation: Adds navigation between preview pages
- Outputs Static HTML: Saves processed templates as standalone HTML files
- welcome.html - Home page with challenge table (from
welcome.htmltemplate) - about.html - About page with project information (from
about.htmltemplate) - stats.html - Stats and configuration page (from
stats.htmltemplate) - challenge-example.html - Sample challenge page (from
challenge.htmltemplate)
The script is integrated into the GitHub Pages preview workflow:
- PR is opened/updated with template changes
- GitHub Actions runs the build process
- Script generates static HTML from updated templates
- Static files are deployed to GitHub Pages
- PR comment includes links to preview pages
# Generate previews for PR number 123
python3 .github/scripts/generate_thymeleaf_previews.py 123
# Run tests
./.github/scripts/test_thymeleaf_generation.shThe script runs automatically in the github-pages-preview.yml workflow when:
- PRs are opened, synchronized, or reopened
- Changes are made to templates or related files
The script includes realistic mock data:
- Challenges: 10 sample challenges with different difficulties and environments
- Stats: Session counters, canary callbacks, configuration settings
- User State: Some challenges marked as completed for preview purposes
- Configuration: Hints, reasons, CTF mode, and other feature flags
- Better PR Reviews: Reviewers can see rendered HTML instead of raw templates
- Visual Validation: Changes to styling and layout are immediately visible
- No Build Required: Works without running the full Spring Boot application
- Fast Preview: Generates quickly using mock data
- Mobile Friendly: Responsive design works on all devices
- Uses mock data instead of real application data
- Some dynamic features (JavaScript interactions) may not work fully
- Template fragments and includes are simplified
- Complex Thymeleaf expressions may need manual handling
To add preview support for new templates:
- Add the template processing logic to
ThymeleafToStaticConverter.generate_*_page()method - Add mock data for any new variables in
self.mock_data - Add navigation links in
generate_navigation_html() - Update the main preview page to link to the new page
- Test with the test script
- Missing templates: Script will generate fallback content
- Parse errors: Check Thymeleaf syntax in templates
- Empty output: Verify mock data contains expected variables
- Broken layout: Check CSS/JS file paths in static assets