-
Notifications
You must be signed in to change notification settings - Fork 2
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
Fix navbar behaviour when <body>
isn't on its own line; add tests for this
#10
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# This workflow runs the insert_navbar script on several test HTML inputs, | ||
# making sure that the output is as expected. | ||
|
||
name: Test Navbar Script | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
|
||
jobs: | ||
test-html-diff: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Copy script to GHA scratch directory | ||
run: cp DocsNav/scripts/insert_navbar.sh .github/workflows/test_navbar_script | ||
|
||
- name: Compare all test inputs against out.html | ||
working-directory: .github/workflows/test_navbar_script | ||
run: | | ||
# Make script executable | ||
chmod +x ./insert_navbar.sh | ||
# Run script on all files in `ins_html` directory | ||
./insert_navbar.sh ./ins_html ./test_navbar.html | ||
# Expected output | ||
OUT="expected_out.html" | ||
# Check if all files in `ins_html` match the expected output | ||
for file in ins_html/*; do | ||
if diff <(tr -d '[:space:]' < "$file") <(tr -d '[:space:]' < $OUT); then | ||
echo "✅ $file matches $OUT (ignoring whitespace)" | ||
else | ||
echo "❌ $file differs from $OUT" | ||
exit 1 # Fail the job if any file is different | ||
fi | ||
done |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<html> | ||
<body> | ||
<p>This is a navbar!</p> | ||
<h1>Test 1</h1> | ||
<p>Test 1</p> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<html> | ||
<body> | ||
<h1>Test 1</h1> | ||
<p>Test 1</p> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<html> | ||
<body><h1>Test 1</h1><p>Test 1</p> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<html><body> | ||
<h1>Test 1</h1> | ||
<p>Test 1</p> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<html><body><h1>Test 1</h1><p>Test 1</p> | ||
</body></html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p>This is a navbar!</p> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,10 +83,8 @@ find "$HTML_DIR" -type f -name "*.html" | while read -r file; do | |
|
||
# Insert the new navbar right after the first <body> tag using awk | ||
awk -v nav="$NAVBAR_HTML" ' | ||
/<body>/ { | ||
print $0 | ||
print nav | ||
next | ||
/<body[^>]*>/ { | ||
sub(/(<body[^>]*>)/, "&\n" nav); | ||
Comment on lines
+86
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new regex is more flexible in that it would picks up cases where the body tag has some attribute |
||
} | ||
{ print } | ||
' "$file" > temp && mv temp "$file" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is also not technically correct, because HTML is not totally whitespace-insignificant - for example tag names and attributes must be separated - but I suppose it's not really possible to test this without using a proper HTML parser, and for the limited test examples I reckon this should be fine.