2023-05-05 15:04:16 +03:00
|
|
|
#!/bin/bash
|
2023-05-11 13:13:21 +03:00
|
|
|
# Change directory to the root of the repository.
|
|
|
|
script_path=$(cd "$(dirname "${0}")" && pwd)
|
|
|
|
cd "${script_path}/.."
|
|
|
|
# Parse parameters.
|
|
|
|
if test ${#} -gt 0; then
|
|
|
|
files="${*}"
|
|
|
|
else
|
|
|
|
files='doc src'
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Get the files.
|
2023-05-11 17:45:13 +03:00
|
|
|
files=$(find ${files} -type f \( -name '*.rst' -or -name '*.json' \) -and -not -path '*/_build/*' -and -not -path '*/man/*' | sort)
|
2023-05-11 13:13:21 +03:00
|
|
|
work_file=$(mktemp)
|
|
|
|
for file in $files; do
|
2023-05-05 15:04:16 +03:00
|
|
|
json=0
|
|
|
|
comment=0
|
|
|
|
line_num=0
|
|
|
|
echo "processing: $file"
|
2023-05-11 13:13:21 +03:00
|
|
|
while IFS= read -r line; do
|
2023-05-05 15:04:16 +03:00
|
|
|
line_num=$((line_num+1))
|
2023-05-11 13:13:21 +03:00
|
|
|
if [ $comment -eq 0 -a $json -eq 0 -a $(echo "$line" | grep "^[A-Za-z]+\|^\s*\`" | wc -l) -eq 1 ]; then
|
2023-05-10 22:35:25 +03:00
|
|
|
# ignore line if it starts with 'A-Za-z' or spaces followed by '`'
|
2023-05-05 15:04:16 +03:00
|
|
|
continue
|
2023-05-11 13:13:21 +03:00
|
|
|
elif [ $comment -eq 0 -a $(echo "$line" | grep "/\*" | grep -v "\*/" | wc -l) -eq 1 ]; then
|
2023-05-10 22:35:25 +03:00
|
|
|
# if the line contains /* and it does not contain */ on the same line
|
2023-05-05 15:04:16 +03:00
|
|
|
comment=1
|
|
|
|
echo "" >> $work_file
|
|
|
|
continue
|
2023-05-11 13:13:21 +03:00
|
|
|
elif [ $comment -eq 1 -a $(echo "$line" | grep "\*/" | wc -l) -eq 1 ]; then
|
2023-05-10 22:35:25 +03:00
|
|
|
# if the line contains */
|
2023-05-05 15:04:16 +03:00
|
|
|
comment=0
|
|
|
|
echo "" >> $work_file
|
|
|
|
continue
|
2023-05-12 14:30:47 +03:00
|
|
|
elif [ $comment -eq 0 -a $json -eq 0 -a $(echo "$line" | grep "^\s*{\|^\s*\".*{\|^\s*\[\s*$" | grep -v "}" | wc -l) -eq 1 ]; then
|
2023-05-11 13:13:21 +03:00
|
|
|
# if this is not a comment and the line starts with spaces followed by '{' or by '"' followed by "{"
|
2023-05-05 15:04:16 +03:00
|
|
|
json=1
|
|
|
|
# ignore any map name before top level map
|
2023-05-12 14:30:47 +03:00
|
|
|
line=$(echo "$line" | sed 's/.*{/{/g')
|
2023-05-05 15:04:16 +03:00
|
|
|
echo "" > $work_file
|
2023-05-11 13:13:21 +03:00
|
|
|
elif [ $comment -eq 0 -a $json -eq 1 -a $(echo "$line" | grep "^\s*[A-Za-z]\|^\s*\`" | wc -l) -eq 1 ]; then
|
2023-05-10 22:35:25 +03:00
|
|
|
# if the line is not a comment and the line starts with spaces followed by 'A-Za-z' or followed by "`" and the parser is processing a json structure
|
2023-05-05 15:04:16 +03:00
|
|
|
json=0
|
|
|
|
cat $work_file | jq . > /dev/null
|
|
|
|
if [ $? -ne 0 ]; then
|
2023-05-10 22:35:25 +03:00
|
|
|
# if the jq tool returned error
|
2023-05-05 15:04:16 +03:00
|
|
|
echo "file $file contains invalid JSON near line $line_num"
|
|
|
|
echo "===start of JSON block==="
|
|
|
|
cat $work_file
|
|
|
|
echo "====end of JSON block===="
|
|
|
|
fi
|
|
|
|
fi
|
2023-05-10 22:35:25 +03:00
|
|
|
if [ $comment -eq 0 -a $json -eq 1 ]; then
|
2023-05-11 13:13:21 +03:00
|
|
|
if [ $(echo "$line" | grep "^\s*\.\.\s" | wc -l) -eq 1 ]; then
|
2023-05-05 15:04:16 +03:00
|
|
|
echo "" >> $work_file
|
|
|
|
else
|
2023-05-10 22:35:25 +03:00
|
|
|
# if file is .json the following replace in line are done:
|
|
|
|
# 1. delete everything after '#'
|
|
|
|
# 2. delete everything after //
|
|
|
|
# 3. ignore <?include?>
|
|
|
|
# 4. replace all '[ <DATA> ]' with '[ "<DATA>"]' where DATA contains: '-' and 'A-Za-z0-9' and ' '
|
|
|
|
# 5. replace all ' <DATA>:' with ' "<DATA>":'
|
|
|
|
# 6. replace all ': <DATA>' with ': "<DATA>"'
|
|
|
|
# 7. replace ' ...' with ' "placeholder": "value"
|
|
|
|
# 8. replace ', ... ' with ' '
|
|
|
|
# 9. replace ' <DATA>' with ' "placeholder": "value"'
|
|
|
|
# 10. replace ' <DATA>' with ' "placeholder"'
|
2023-05-11 13:13:21 +03:00
|
|
|
if [ $(echo "$file" | grep "\.json" | wc -l) -eq 0 ]; then
|
2023-05-12 14:30:47 +03:00
|
|
|
echo "$line" | cut -d "#" -f 1 | sed 's/\/\/ .*//g' | sed 's/<?.*?>//g' | sed 's/\[ <\([-A-Za-z0-9 ]*\)> \]/\[ \"<\1>\" \]/g' | sed 's/ <\(.*\)>:/ \"<\1>\":/g' | sed 's/: <\(.*\)>/: \"<\1>\"/g' | sed 's/ \.\.\./ \"placeholder\": \"value\"/g' | sed 's/, \.\.\. / /g' | sed 's/ <\(.*\)>/ \"placeholder\": \"value\"/g' | sed 's/ <\(.*\)>/ \"placeholder\"/g' >> $work_file
|
2023-05-05 15:04:16 +03:00
|
|
|
else
|
2023-05-10 22:35:25 +03:00
|
|
|
# if file is .rst the following replace in line are done:
|
|
|
|
# 1. delete everything after '#'
|
|
|
|
# 2. delete everything after //
|
|
|
|
# 3. ignore <?include?>
|
2023-05-12 14:30:47 +03:00
|
|
|
echo "$line" | cut -d "#" -f 1 | sed 's/\/\/ .*//g' | sed 's/<?.*?>//g' >> $work_file
|
2023-05-05 15:04:16 +03:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
2023-05-12 14:30:47 +03:00
|
|
|
done <<< $(cat $file | sed ':a;N;$!ba;s/,\s*\n\s*\.\.\.//g' | sed ':a;N;$!ba;s/\s\\\n//g')
|
2023-05-05 15:04:16 +03:00
|
|
|
if [ $comment -eq 0 -a $json -eq 1 ]; then
|
2023-05-10 22:35:25 +03:00
|
|
|
# if the file ended but the parser is processing a json structure
|
2023-05-05 15:04:16 +03:00
|
|
|
cat $work_file | jq . > /dev/null
|
|
|
|
if [ $? -ne 0 ]; then
|
2023-05-10 22:35:25 +03:00
|
|
|
# if the jq tool returned error
|
2023-05-05 15:04:16 +03:00
|
|
|
echo "file $file contains invalid JSON near line $line_num"
|
|
|
|
echo "===start of JSON block==="
|
|
|
|
cat $work_file
|
|
|
|
echo "====end of JSON block===="
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
rm $work_file
|