Skip to content

Commit

Permalink
Merge pull request #57 from ChillerDragon/pr_check_filename
Browse files Browse the repository at this point in the history
Check inkscape filename matching actual filename
  • Loading branch information
Jupeyy committed Mar 26, 2024
2 parents d5a7190 + 200a58b commit a065be5
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 19 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/meta_data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup
run: |
sudo wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/bin/yq
sudo chmod +x /usr/bin/yq
- name: Check svg
run: ./scripts/check_svg_meta.sh
2 changes: 1 addition & 1 deletion mapres/desert_main.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion mapres/grass_doodads_0.7.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion mapres/grass_main.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
103 changes: 88 additions & 15 deletions scripts/check_svg_meta.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
#!/bin/bash

err=0
num_errors=0
num_warnings=0
arg_fix=0

error() {
printf '[-] ERROR: %s\n' "$1" 1>&2
num_errors="$((num_errors+1))"
}

warning() {
printf '[-] WARNING: %s\n' "$1" 1>&2
num_warnings="$((num_warnings+1))"
}

for arg in "$@"
do
if [ "$arg" == "--help" ] || [ "$arg" == "-h" ] || [ "$arg" == "help" ]
Expand All @@ -21,42 +32,104 @@ do
fi
done

while IFS= read -r file
do
# prefix your keys with dot
# this is a wrapper around jq
xml_get_key() {
local key="$1"
local file="$2"
if yq --version | grep -qF 'https://github.com/mikefarah/yq'
then
yq -r -p xml -o json "$key" "$file"
else
local potential_keys
[[ "$key" =~ ^\.svg\[[\"\']\+@(.*)[\"\']\]$ ]] && key="${BASH_REMATCH[1]}"
potential_keys="$(grep -F "$key=" "$file")"
local num_matches
num_matches="$(printf '%s' "$potential_keys" | wc -l)"
if [ "$num_matches" -lt 2 ] && [ "$potential_keys" != "" ]
then
printf '%s' "$potential_keys" | cut -d'"' -f2
return
fi

local grep_safe_key
grep_safe_key="$(printf '%s' "$key" | grep -o '[a-ZA-Z0-9]')"
potential_keys="$(printf '%s' "$potential_keys" | grep "^[[:space:]]*$grep_safe_key")"
num_matches="$(printf '%s' "$potential_keys" | wc -l)"
if [ "$num_matches" = 0 ]
then
printf null
return
fi
printf '%s' "$potential_keys" | head -n1 | cut -d'"' -f2
fi
}

check_meta_filename_match() {
local file="$1"
local expected_filename
expected_filename="$(basename "$file")"
local actual_filename
if ! actual_filename="$(xml_get_key '.svg["+@sodipodi:docname"]' "$file")"
then
error "failed to get meta filename of $file"
exit 1
fi
[ "$actual_filename" = null ] && return

if [ "$actual_filename" != "$expected_filename" ]
then
warning "svg docname does not match filename in $file"
printf '\n expected: "%s"' "$expected_filename" 1>&2
printf '\n got: "%s"\n\n' "$actual_filename" 1>&2
fi
}

check_absolute_path() {
local file="$1"
if [ "$arg_fix" == "1" ]
then
sed -i -r '/export-filename="([A-Z]:|\/)/d' "$file"
continue
return
fi
while IFS= read -r line
do
printf "[-] ERROR: absolute export path found in %s:%d\\n" \
"$file" \
"$(echo "$line" | cut -d':' -f1)"
err="$((err+1))"
error "absolute export path found in $file:$(echo "$line" | cut -d':' -f1)"
done < <(grep -nE 'export-filename="([A-Z]:\\|/)' "$file")
}

# check if there is width and height parameter
check_width_height() {
local file="$1"
width_found=$(grep -Eiwzo "<svg[^>]*>" "$file" | tr '\0' '\n' | grep -Eo "width=\"([0-9.]|px)*\"")
height_found=$(grep -Eiwzo "<svg[^>]*>" "$file" | tr '\0' '\n' | grep -Eo "height=\"([0-9.]|px)*\"")

if [[ "$width_found" == "" || "$height_found" == "" ]]
then
printf "[-] ERROR: no width or height parameter found in %s\\n" "$file"
err="$((err+1))"
error "no width or height parameter found in $file"
fi
if grep -qF 'xlink:href="data:image/png;base64,' "$file"
then
printf "[-] ERROR: embedded image found %s\\n" "$file"
err="$((err+1))"
error "embedded image found $file"
fi
}

while IFS= read -r file
do
check_absolute_path "$file"
check_meta_filename_match "$file"
check_width_height "$file"
done < <(find . -type f -name "*.svg")

if [ "$err" -ne "0" ]
if [ "$num_errors" -ne "0" ]
then
echo "[-] failed ($err errors)"
echo "[-] failed ($num_errors errors)"
exit 1
fi
if [ "$num_warnings" -ne "0" ]
then
echo "[!] finished ($num_warnings warnings)"
exit 0
fi

echo "[+] done"

2 changes: 1 addition & 1 deletion skins/cammostripes.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a065be5

Please sign in to comment.