wok view sokojs/stuff/slc2htm.sh @ rev 24130

Add tnylpo
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Oct 18 14:47:15 2021 +0000 (2021-10-18)
parents 2ed3de6597f5
children db9d9cb3c164
line source
1 #!/bin/sh
3 swap() {
4 awk 'BEGIN { n=0 } { l[n++]=$0 } END {
5 for (i = 0; i < length(l[0]); i++) {
6 for (j = 0; j < n; j++) printf "%s", substr(l[j],i+1,1)
7 print ""
8 }}'
9 }
11 filter() {
12 sed 's| _| |;s|_ | |;s|^_| |;s|_$| |'
13 }
15 check() {
16 grep -Eq ' _|_ |^_|_$'
17 }
19 [ -z "$1" ] && echo "$0 set1.slc ..." && exit 1
20 [ ! -s "main.htm" ] && echo "$PWD/main.htm not found" && exit 2
22 while [ "$1" ]; do
23 file="$1"
24 set="$(basename $1 .slc)"
25 shift
26 [ -d "$set" ] && continue
27 echo "$set"
28 grep -q "<option>$set</option>" main.htm || sed -i "s|</select>|<option>$set</option>\");\n document.write(\"&|" main.htm
29 mkdir "$set"
30 comment=""; state=""; level=0
31 while read line; do
32 case "$line" in
33 *\<Url\>*) echo "$line" | sed 's|.*<Url>||;s|</Url>.*||' >> "$set/description.txt" ;;
34 *\<Title\>*) echo "$line" | sed 's|.*<Title>||;s|</Title>.*||' >> "$set/description.txt" ;;
35 *\<Description\>) state="Description"; continue ;;
36 *\</Description\>) state="" ;;
37 *\<LevelCollection*)
38 maxwidth="$(echo "$line" | sed 's|.*MaxWidth="||;s|".*||')"
39 col=16; [ $maxwidth -gt $col ] && col=$maxwidth
40 maxheight="$(echo "$line" | sed 's|.*MaxHeight="||;s|".*||')"
41 row=16; [ $maxheight -gt $row ] && row=$maxheight
42 echo "$line" | sed '/Copyright=/!d;s|.*Copyright="|copyright |;s|".*||' >> "$set/description.txt" ;;
43 *\<Level*) state="Level"
44 id="$(echo "$line" | sed 's|.*Id="||;s|".*||')"
45 width="$(echo "$line" | sed 's|.*Width="||;s|".*||')"
46 fmt=" "
47 fmt="${fmt:0:$((($col-$width)/2))}%-$(($col-($col-$width)/2))s"
48 height="$(echo "$line" | sed 's|.*Height="||;s|".*||')"
49 height=$((($row-$height)/2))
50 l=0
51 echo -n "" > $set/tmp0
52 while [ $height -gt $l ]; do
53 printf "$fmt\n" "" >> "$set/tmp0"
54 l=$(($l+1))
55 done
56 cat > "$set/level$level.htm" <<EOT
57 <html>
58 <head>
59 </head>
60 <body>
61 <form action="">
62 <script type="text/javascript">
63 <!--
64 if (parent.frames[0] == null) { document.location = "../sokojs.htm" }
65 l = new Array(
66 // $id ${maxwidth}x$maxheight+$((($col-$width)/2))+$height
67 EOT
68 continue ;;
69 *\</Level\>) state=""
70 while [ $row -gt $l ]; do
71 printf "$fmt\n" "" >> "$set/tmp0"
72 l=$(($l+1))
73 done
74 while swap < $set/tmp0 | check || check < $set/tmp0 ; do
75 swap < $set/tmp0 | filter | swap | filter > $set/tmp1
76 mv -f $set/tmp1 $set/tmp0
77 done
78 x=$(($(sed '/[@+]/{s|[@+].*||;q}' $set/tmp0 |\
79 wc -cl | sed 's|^|-|;s|......$|+&|')))
80 sed 's|.*|"&",|;$s|,$|);|' $set/tmp0 >> "$set/level$level.htm"
81 cat >> "$set/level$level.htm" <<EOT
83 Row=$row
84 Col=$col
85 for (x = 0; x < Row; x++)
86 for (y = 0; y < Col; y++)
87 document.write("<INPUT TYPE=\"button\" value=\"",
88 ' _#*$.'.indexOf(l[x].substring(y, y + 1).replace('+','.').replace('@','_')), "\">")
89 document.write("<INPUT TYPE=\"button\" value=\"$x\">",
90 "<INPUT TYPE=\"button\" value=\"$level\"><\/FORM>")
92 document.write("<FORM ACTION=\"\">")
93 document.write("<INPUT TYPE=\"button\" value=\""+Row+"\">",
94 "<INPUT TYPE=\"button\" value=\""+Col+"\">",
95 "<INPUT TYPE=\"button\" value=\"$set\">",
96 "<INPUT TYPE=\"button\" value=\"COUNT\"><\/FORM>")
97 parent.frames[1].location = "../main.htm"
98 //-->
99 </script></form>
100 </body>
101 </html>
102 EOT
103 rm -f $set/tmp*
104 level=$(($level+1))
105 continue ;;
106 esac
107 case "$state" in
108 Description)
109 echo "$line" >> "$set/description.txt" ;;
110 Level)
111 printf "$fmt\n" "$(echo "$line" | sed 's|.*<L>||;s|</L>||;:a;s|\([#.+@$\*]_*\) |\1_|;ta')" >> "$set/tmp0"
112 l=$(($l+1))
113 esac
114 done < "$file"
115 sed -i "s|COUNT|$(($level-1))|" "$set"/level*.htm
116 done