Skip to content

Commit 9567067

Browse files
committed
[IPMPROG-1780] Use JSON.sh to prepare properly escaped strings from text files
1 parent 49b3a84 commit 9567067

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "JSON.sh"]
2+
path = JSON.sh
3+
url = https://github.com/42ity/JSON.sh

JSON.sh

Submodule JSON.sh added at d43c050

tools/convert.sh

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/bin/bash
22

3+
SCRIPT_DIR="`dirname "$0"`"
4+
SCRIPT_DIR="`cd "$SCRIPT_DIR" && pwd`"
5+
36
usage() {
47
cat << EOF
58
# Usage: convert.sh <INPUT_DIR> <OUTPUT_FILE>
@@ -27,8 +30,8 @@ convert_md_to_json() {
2730
n=0
2831
echo -n "[" > "$output_file"
2932
# for each file in directory (revert sort with file name without extension)
30-
# caution: 2.3.0-1 must be prior then 2.3.0
31-
for file in `ls $input_dir/*.md | rev | cut -d . -f 2- | rev | sort -r`
33+
# caution: 2.3.0-1 must be prior to 2.3.0
34+
for file in `ls -1 "$input_dir"/*.md | rev | cut -d . -f 2- | rev | sort -r`
3235
do
3336
version="$(basename "$file")"
3437
echo "Find $version ($file)"
@@ -42,12 +45,11 @@ convert_md_to_json() {
4245
echo "${version}\"," >> "$output_file"
4346
echo -n $'\t\t"content": "' >> "$output_file"
4447

45-
# Reading each line and convert CR with "\n"
46-
while read line; do
47-
# Escape '"'
48-
line=$(echo "$line" | sed -e 's/[\"]/\\&/g')
49-
echo -n "$line\\\\n" >> "$output_file"
50-
done < "${file}.md"
48+
# Read each line and convert CR with "\n"
49+
"$SCRIPT_DIR/../JSON.sh/JSON.sh" -Q < "${file}.md" >> "$output_file"
50+
# Add literal "\n" at end of converted text, before the closing quote"
51+
echo -n '\\' >> "$output_file"
52+
echo -n 'n' >> "$output_file"
5153
echo -n $'"\n\t}' >> "$output_file"
5254
n=$((n+1))
5355
done
@@ -60,6 +62,7 @@ convert_md_to_json() {
6062
if [ $# -lt 2 ]; then
6163
echo "Bad parameters"
6264
usage
65+
exit 1
6366
else
6467
input_dir="$1"
6568
output_file="$2"

0 commit comments

Comments
 (0)