-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLBeautifier.sh
More file actions
54 lines (50 loc) · 2.22 KB
/
HTMLBeautifier.sh
File metadata and controls
54 lines (50 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
if [ -z "$1" ]; then
echo "Please specify an HTML file."
exit 1
fi
if [[ -f "$1" ]]; then
old_code=$(cat "$1")
else
old_code="$1"
fi
arr=("area" "base" "br" "col" "embed" "hr" "img" "input" "link" "meta" "source" "track" "wbr" "param" "!DOCTYPE" "!--")
old_code=$(echo "$old_code" | sed -e 's/^[ \t]*//')
old_code=$(echo "$old_code" | sed -e 's/[ \t]*$//')
old_code=$(echo "$old_code" | sed -e 's/[[:space:]]\+/ /g')
old_code=$(echo "$old_code" | sed -e ':a;N;$!ba;s/\n//g')
old_code=$(echo "$old_code" | sed 's/>[[:space:]]</></')
nr_of_tabs=0
modified_code=""
while [[ $old_code =~ ^\<[^\>]*\> ]] || [[ $old_code =~ [^\<]* ]]; do #moving on the tags
tag="${BASH_REMATCH[0]}" #saving everything between < and > in "tag"
old_code="${old_code#${BASH_REMATCH[0]}}" #erase the selected tag so I don't reselect it again
modtag=$(echo "$tag" | grep -oP '(?<=\<)[^ /\>]+')
if [[ "$tag" =~ ^\<[^/]*$ ]]; then #if it is an open tag
found=false
for elem in "${arr[@]}"; do #I check if the key-word for my tag is a self-closing tag
if [[ "$elem" == "$modtag" ]]; then
found=true
break
fi
done
if [[ "$found" == true ]]; then
modified_code+="$(printf '%*s' $((nr_of_tabs * 4)) '')$tag "
else
modified_code+="$(printf '%*s' $((nr_of_tabs * 4)) '')$tag "
((nr_of_tabs++))
fi
elif [[ "$tag" =~ ^\</[^\>]*\>$ ]]; then #if it is a closing tag
((nr_of_tabs--))
modified_code+="$(printf '%*s' $((nr_of_tabs * 4)) '')$tag "
else #else it is content and we don't need to modify the number of tabs
modified_code+="$(printf '%*s' $((nr_of_tabs * 4)) '')$tag "
fi
modified_code+="$(printf '%*s')\n"
if [[ -z "$old_code" ]]; then
break
fi
done
#for a proper aspect, if the content is too long, I section it in more lines
modified_code=$(echo "$modified_code" | sed 's/>[[:space:]]*</>\\n</g')
echo -e "$modified_code"