Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add action to generate pdf diff #1

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# The roar-irb-maintainers team will own all markdown files.
*.md @yeatmanlab/roar-irb-maintainers
71 changes: 71 additions & 0 deletions .github/workflows/markdown-diff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: Generate Diff PDFs

on:
pull_request:
types: [opened, synchronize, reopened]
branches:
- main

jobs:
generate_pdfs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0 # Fetch full history to compare branches
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y wkhtmltopdf
npm install -g diff2html-cli
- name: Fetch main branch
run: |
git fetch origin main
- name: Generate diffs and PDFs
run: |
# Get list of modified markdown files
MODIFIED_FILES=$(git diff --name-only origin/main...HEAD -- '*.md')
echo "Modified markdown files:"
echo "$MODIFIED_FILES"
mkdir -p pdfs
# Generate individual PDFs
for FILE in $MODIFIED_FILES; do
echo "Processing $FILE"
# Generate diff and convert to HTML
git diff origin/main...HEAD -- "$FILE" | diff2html -i stdin -s line -o stdout > diff.html
# Convert HTML to PDF
OUTPUT_FILE="pdfs/${FILE//\//_}.pdf"
wkhtmltopdf diff.html "$OUTPUT_FILE"
done
# Generate combined PDF
COMBINED_HTML="combined_diff.html"
echo "<html><body>" > $COMBINED_HTML
for FILE in $MODIFIED_FILES; do
echo "<h1>Diff for $FILE</h1><hr>" >> $COMBINED_HTML
git diff origin/main...HEAD -- "$FILE" | diff2html -i stdin -s line -o stdout >> $COMBINED_HTML
echo "<div style='page-break-after: always;'></div>" >> $COMBINED_HTML
done
echo "</body></html>" >> $COMBINED_HTML
# Convert combined HTML to PDF
wkhtmltopdf $COMBINED_HTML "pdfs/combined_diff.pdf"
- name: Upload PDFs
uses: actions/upload-artifact@v3
with:
name: diff_pdfs
path: pdfs/
- name: Comment on PR with download link
uses: actions/github-script@v6
with:
script: |
const artifactUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `📄 Diff PDFs have been generated and uploaded as artifacts for this workflow run. You can download them from the [Artifacts section of the workflow run](${artifactUrl}).`
});
97 changes: 97 additions & 0 deletions .github/workflows/pdf-diff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
name: Generate Rendered Diff PDFs

on:
pull_request:
types: [opened, synchronize, reopened]
branches:
- main

jobs:
generate_pdfs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0 # Fetch full history to compare branches

- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y pandoc texlive-latex-base texlive-latex-extra texlive-fonts-recommended texlive-xetex texlive-fonts-extra latexdiff latexmk ghostscript
mkdir -p old_version new_version diffs pdfs

- name: Fetch main branch
run: |
git fetch origin main

- name: Generate rendered diffs and PDFs
run: |
# Create a LaTeX preamble with necessary packages
echo "\usepackage{fontspec}
\setmainfont{DejaVu Serif}
\setsansfont{DejaVu Sans}
\setmonofont{DejaVu Sans Mono}" > preamble.tex

# Get list of modified markdown files
MODIFIED_FILES=$(git diff --name-only origin/main...HEAD -- '*.md')
echo "Modified markdown files:"
echo "$MODIFIED_FILES"
mkdir -p pdfs
COMBINED_DIFFS=""
for FILE in $MODIFIED_FILES; do
echo "Processing $FILE"

# Extract old version from main branch
git show origin/main:"$FILE" > old_version/"$(basename "$FILE")"

# Copy new version
cp "$FILE" new_version/

# Convert both versions to LaTeX with preamble
pandoc old_version/"$(basename "$FILE")" --include-in-header=preamble.tex -o old_version/"$(basename "$FILE" .md)".tex
pandoc new_version/"$(basename "$FILE")" --include-in-header=preamble.tex -o new_version/"$(basename "$FILE" .md)".tex

# Run latexdiff to get the diffed LaTeX file, including preamble
latexdiff --encoding=utf8 --preamble "\input{preamble.tex}" old_version/"$(basename "$FILE" .md)".tex new_version/"$(basename "$FILE" .md)".tex > diffs/"$(basename "$FILE" .md)"_diff.tex

# Compile the diffed LaTeX file to PDF using xelatex
cd diffs
latexmk -pdf -xelatex -interaction=nonstopmode -quiet "$(basename "$FILE" .md)"_diff.tex
cd ..

# Move the generated PDF to the pdfs directory
mv diffs/"$(basename "$FILE" .md)"_diff.pdf pdfs/

# Append to combined diffs
COMBINED_DIFFS="$COMBINED_DIFFS \includepdf[pages=-]{pdfs/$(basename "$FILE" .md)_diff.pdf}"
done

# Generate combined PDF if there are diffs
if [ -n "$COMBINED_DIFFS" ]; then
echo "\documentclass{article}
\usepackage{pdfpages}
\begin{document}
$COMBINED_DIFFS
\end{document}" > combined_diff.tex
latexmk -pdf -xelatex -interaction=nonstopmode -quiet combined_diff.tex
mv combined_diff.pdf pdfs/
fi

- name: Upload PDFs
uses: actions/upload-artifact@v3
with:
name: rendered_diff_pdfs
path: pdfs/

- name: Comment on PR with download link
uses: actions/github-script@v6
with:
script: |
const artifactUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `📄 Rendered diff PDFs have been generated and uploaded as artifacts for this workflow run. You can download them from the [Artifacts section of the workflow run](${artifactUrl}).`
});
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# roar-legal-documents

A repository that hosts all relevant legal documents for ROAR research. This repository currently includes:

- Consolidated Assent
- Terms of Service
- District 2 Consent Form
- Web-Based Behavioral Consent
- Web-Based Behavioral Consent (Eye-Tracking)

## Deployment

Insert steps here for how to modify a markdown file.
Loading