]> bicyclesonthemoon.info Git - ott/enhance/blob - ong-bsta.awk
487d0498916c399d457d226c8ce0bce461464263
[ott/enhance] / ong-bsta.awk
1 #// ong-bsta.awk
2 #// The bot to ketchup frames from the bsta viewer
3 #//  6.09.2019
4 #// 
5 #// Copyright (C) 2019  Balthasar SzczepaƄski
6 #// 
7 #// This program is free software: you can redistribute it and/or modify
8 #// it under the terms of the GNU Affero General Public License as
9 #// published by the Free Software Foundation, either version 3 of the
10 #// License, or (at your option) any later version.
11 #// 
12 #// This program is distributed in the hope that it will be useful,
13 #// but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #// GNU Affero General Public License for more details.
16 #// 
17 #// You should have received a copy of the GNU Affero General Public License
18 #// along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #//
20
21 BEGIN{
22         FS=""
23         framepath="/eizm/www/time/"
24         datapath="/eizm/data/aftertime/"
25         temppath="/eizm/tmp/aftertime/"
26         passpath="/eizm/mem/aftertime/pass/"
27         
28         bstapath="/eizm/data/bsta/"
29         bstaviewpath="/eizm/www/time/bsta/"
30         
31         timecmd="date -u +%Y%m%d%H%s"
32         timecmd2="date -u +\"%Y-%m-%d %H:%M:%S\""
33         ongtimecmd1="date -u -d@"
34         ongtimecmd2=" +\"%Y-%m-%d %H:%M:%S\""
35         
36         botcastleurl="http://1190.bicyclesonthemoon.info"
37         
38         cp="/bin/cp"
39         mawk="/usr/bin/mawk"
40         chmod="/bin/chmod"
41         
42         # wget="/usr/bin/wget"
43         # remap="/eizm/pro/aftertime/remapt-1"
44         npb="/eizm/pro/ong1/npb"
45         # bot4awk="/eizm/pro/aftertime/bot4.awk"
46         # uploadawk="/eizm/pro/aftertime/upload.awk"
47         
48         timecmd2 | getline thistime2
49                 close(timecmd2)
50         
51         for(i=0;i<256;++i)
52         {
53                 ch=sprintf("%c",i)
54                 hex=sprintf("%02X",i)
55                 dec=sprintf("%u",i)
56                 ch2hex[ch]=hex
57                 ch2dec[ch]=dec
58         }
59 }
60 {
61         file=1
62         gsub(/[\r\n]/, "", $0)
63         eq=index($0, "=")
64         
65         argtab[substr($0, 1, eq-1)]=substr($0, eq+1)
66 }
67 END{
68         print ""
69         print thistime2" "argtab ["story"]
70         
71         if (file=="")
72         {
73                 print "No data."
74                 exit 1
75         }
76         
77         if(argtab["ongoing"]!="2")
78         {
79                 print "Story is not externally onged."
80                 exit 1
81         }
82         
83         framedir = "aftertime/"argtab["story"]"/"
84         
85         temp=""
86         statefile= datapath argtab["story"]"/ongstate"
87         getline temp < statefile
88         getline alt < statefile
89         close(statefile)
90         
91         split(temp, temptab, " ")
92         state=int(temptab[1])
93         last=int(temptab[2])
94         nextong=int(temptab[3])
95         ongfailcount=int(temptab[4])
96         
97         if(state==4)
98         {
99                 print "Time to start onging."
100                 f=int(argtab["first"])
101         }
102         else if (state==2 || state==1)
103                 f=last+1
104         else
105         {
106                 print "Onging not avtive."
107                 exit 1
108         }
109         
110         bstasettingsfile = bstapath "settings"
111         bstastatefile = bstapath "state"
112         bstadefaultfile = bstapath "default"
113         
114         bstaargtab[0]=""
115         specialtab[0]=""
116         attachtab[0]=""
117         
118         timefile= datapath argtab["story"]"/ongtime"
119         
120         while(1)
121         {
122                 bstaspecialfile = bstapath f
123                 specialfile = datapath argtab["story"]"/"sprintf(argtab["special"],f)
124                 
125                 for (i in bstaargtab)
126                         delete bstaargtab[i]
127                 
128                 readdatafile(bstasettingsfile,bstaargtab)
129                 readdatafile(bstastatefile,bstaargtab)
130                 readdatafile(bstadefaultfile,bstaargtab)
131                 readdatafile(bstaspecialfile,bstaargtab)
132                 
133                 # for (i in bstaargtab)
134                         # print i": "bstaargtab[i]
135                 
136                 if (f > int(bstaargtab["last"])) {
137                         print "Wait for frame "f"."
138                         exit 0
139                 }
140                 
141                 print "ONG frame "f"."
142                 
143                 for (i in specialtab)
144                         delete specialtab[i]
145                 
146                 #skip NPB frame
147                 
148                 specialtab["title"]=bstaargtab["title"]
149                 # specialtab["alt"]=bstaargtab["title"]
150                 specialtab["frame"]=sprintf(bstaargtab["frame"],f,bstaargtab["ext"])
151                 specialtab["text"]=bstaargtab["content"]
152                 
153                 inframe = bstaviewpath specialtab["frame"]
154                 frame= framepath framedir specialtab["frame"]
155                 
156                 
157                 text_HTML=""
158                 text_BB=""
159                 text=specialtab["text"]
160                 
161                 while (text!="")
162                 {
163                         eq=match(text,/(###([^#;]*);)/)
164                         
165                         if(eq)
166                         {
167                                 text_HTML=text_HTML substr(text, 1, RSTART - 1)
168                                 text_BB = text_BB substr(text, 1, RSTART - 1)
169                                 tag = substr(text, RSTART +3, RLENGTH -4)
170                                 text= substr(text, RSTART + RLENGTH)
171                                 
172                                 if (match(tag,/^fr&([0-9]+)$/))
173                                 {
174                                         id=int(substr(tag,4))
175                                         
176                                         tag="/aftertime/frame?story="arrtab["story"]"&f="id
177                                         text_HTML=text_HTML tag
178                                         text_BB=text_BB botcastleurl tag
179                                 }
180                                 else if (match(tag,/^vw&([0-9]+)$/))
181                                 {
182                                         id=int(substr(tag,4))
183                                         
184                                         tag="/aftertime/viewer?story="arrtab["story"]"&f="id
185                                         text_HTML=text_HTML tag
186                                         text_BB=text_BB botcastleurl tag
187                                         
188                                 }
189                                 else if (match(tag,/^att&([0-9]+)$/))
190                                 {
191                                         id=int(substr(tag,5))
192                                         
193                                         print "ONG attachment "id"."
194                                         
195                                         attachfile=bstapath"a"id
196                                         for (i in attachtab)
197                                                 delete attachtab[i]
198                                         readdatafile(attachfile, attachtab)
199
200                                         outfile=framepath framedir attachtab["filename"]
201                                         infile= bstapath attachtab["filename"]
202                                         
203                                         cpcmd = cp" "infile" "outfile
204                                         chmodcmd = chmod" +r "outfile
205                                         
206                                         if (attachtab["content"]!="")
207                                         {
208                                                 printf("%s",attachtab["content"]) > outfile
209                                                 close(outfile)
210                                         }
211                                         else if(system(cpcmd)!=0)
212                                         {
213                                                 print "Attachment upload failed."
214                                                 exit 1
215                                         }
216                                         
217                                         if(system(chmodcmd)!=0)
218                                         {
219                                                 print "Attachment chmod failed."
220                                                 exit 1
221                                         }
222                                         
223                                         tag="/aftertime/"argtab["story"]"/"attachtab["filename"]
224                                         text_HTML=text_HTML tag
225                                         text_BB=text_BB botcastleurl tag
226                                 }
227                                 else
228                                 {
229                                         
230                                 }
231                                 
232                         }
233                         
234                         else
235                         {
236                                 text_HTML = text_HTML text
237                                 text_BB = text_BB text
238                                 text = ""
239                         }
240                         
241                 }
242                 
243                 # print text_HTML
244                 # print text_BB
245                 
246                 debug=""
247                 specialtab["HTML"]=bb2html(text_HTML)
248                 specialtab["BBHTML"]=bb2bb(text_BB)
249                 
250                 cpcmd = cp" "inframe" "frame
251                 chmodcmd = chmod" +r "frame
252                 
253                 ongtimecmd= ongtimecmd1 bstaargtab["ongtime"] ongtimecmd2
254                 ongtimecmd | getline ongtime
255                 close(ongtimecmd)
256                 
257                 print ongtime
258                 
259                 if (state==4)
260                         print sprintf("%04u ",f)ongtime > timefile
261                 else
262                         print sprintf("%04u ",f)ongtime >> timefile
263                 close(timefile)
264                 
265                 if(system(cpcmd)!=0)
266                 {
267                         print "Frame upload failed."
268                         exit 1
269                 }
270                 
271                 if(system(chmodcmd)!=0)
272                 {
273                         print "Frame chmod failed."
274                         exit 1
275                 }
276                 
277                 # print debug
278                 
279                 for (i in specialtab)
280                 {
281                         if (i!="text")
282                                 print i"="specialtab[i] > specialfile
283                 }
284                 print "" > specialfile
285                 if (specialtab["text"]!="")
286                         printf("%s",specialtab["text"]) > specialfile
287                 close (specialfile)
288                 
289                 
290                 print sprintf("%u %u %u %u",2,f,0,0) > statefile
291                 close (statefile)
292                 
293                 state=2
294                 ++f
295                 
296                 # exit 0
297         }
298 }
299
300 #limited compatibility with bsta data file format
301 function readdatafile (datafile, argtab,   line,undertext,eq)
302 {
303         line=""
304         while((getline line < datafile)>0)
305         {
306                 \r               gsub(/[\r\n]/, "", line)
307                 
308                 if (line == "" && !undertext)
309                 {
310                         undertext=1
311                         argtab["content"]=""
312                 }
313                 else if (undertext)
314                 {
315                         argtab["content"] = argtab["content"] line "\n"
316                 }
317                 else
318                 {
319                         eq = match(line, /(: |=)/)
320                         
321                         if (eq!=0) {
322                                 argtab[substr(line, 1, RSTART - 1)]=substr(line, RSTART + RLENGTH)
323                         }
324                 }
325         }
326         close (datafile)
327         
328         # return bstaargtab
329 }
330
331 function bb2bb (text)
332 {
333         gsub(/\[(fq|tq)\]/, "[quote]", text)
334         gsub(/\[\/(fq|tq)\]/, "[/quote]", text)
335         gsub(/\[br\]/, "[color=#BB6622]", text)
336         gsub(/\[ni\]/, "[color=#0057AF]", text)
337         gsub(/\[po\]/, "[color=#FF8800]", text)
338         gsub(/\[\/(br|ni|po)\]/, "[/color]", text)
339         
340         return htmlescape(text,"","br")
341 }
342
343 function bb2html(text,   depth,count,bbtree,html,tag,tagstart,taglength,elm,tagv,tagname,tagvalue,matchname)
344 {
345         depth=0
346         count[0]=1
347         bbtree["0"]=""
348         bbtree["0.n"]=0
349         bbtree["0.t"]="post"
350         bbtree["0.v"]=argtab["subject"]
351         html=""
352         # gsub(/&#61;/,"=",text)
353         debug = debug "\n\nBuild BBcode tree:"
354         while(length(text)!=0)
355         {
356                 tag=match(text, /\[\/?[a-z0-9\*]+(=(([^\[<>\"\]]*)|(\"[^\"<>]*\")))?\]/)
357                 #"#\""
358                 if (tag==0)
359                 {
360                         debug = debug "\ntext: " text
361                         bbtree[ind(count,depth)]=text
362                         bbtree[indt(count,depth)".n"]=count[depth]
363                         ++count[depth]
364                         text=""
365                         break;
366                 }
367                 tagstart=RSTART
368                 taglength=RLENGTH
369                 if(tagstart>1)
370                 {
371                         elm=substr(text,1,tagstart-1)
372                         debug = debug "\ntext: " elm
373                         bbtree[ind(count,depth)]=elm
374                         bbtree[indt(count,depth)".n"]=count[depth]
375                         ++count[depth]
376                 }
377                 tag=substr(text,tagstart,taglength)
378                 text=substr(text,tagstart+taglength)
379                 debug = debug "\ntag: " tag
380                 if(tag!~/^\[\//)
381                 {
382                         tagv=index(tag,"=")
383                         if(tagv<2)
384                         {
385                                 tagname=substr(tag,2,length(tag)-2)
386                                 tagvalue=""
387                         }
388                         else
389                         {
390                                 tagname=substr(tag,2,tagv-2)
391                                 tagvalue=substr(tag,tagv+1,length(tag)-tagv-1)
392                         }
393                         if(tagname !~ /^((fq)|(tq)|(ni)|(po)|(quote)|b|i|u|(code)|(img)|(url)|(size)|(color)|(center)|(right)|(sub)|(sup)|(s)|(spoiler)|(list)|(br)|(\*))$/)
394                         {
395                                 debug = debug "\nunknown tag"
396                                 bbtree[ind(count,depth)]=tag
397                                 bbtree[indt(count,depth)".n"]=count[depth]
398                                 ++count[depth]
399                                 continue
400                         }
401                         if((bbtree[indt(count,depth)".t"] == "code" && tagname!="code")||bbtree[indt(count,depth)".t"] == "img"||(bbtree[indt(count,depth)".t"] == "spoiler" && tagname=="spoiler")||(bbtree[indt(count,depth)".t"] == "size" && tagname=="size"))
402                         {
403                                 debug = debug "\nthis tag forbidden here"
404                                 bbtree[ind(count,depth)]=tag
405                                 bbtree[indt(count,depth)".n"]=count[depth]
406                                 ++count[depth]
407                                 continue
408                         }
409                         if(tagname=="*")
410                         {
411                                 if(bbtree[indt(count,depth)".t"]==tagname)
412                                 {
413                                         debug = debug "\nimplied tag: [/"tagname"]"
414                                         debug = debug "\nmatched"
415                                         bbtree[ind(count,depth)]="[/"tagname"]"
416                                         bbtree[ind(count,depth)".k"]="e"
417                                         bbtree[ind(count,depth)".m"]=1
418                                         bbtree[ind(count,depth)".t"]=tagname
419                                         bbtree[ind(count,depth)".v"]=bbtree[indt(count,depth)".v"]
420                                         bbtree[indt(count,depth)".n"]=count[depth]
421                                         bbtree[indt(count,depth)".m"]=1
422                                         count[depth]=""
423                                         --depth
424                                         ++count[depth]
425                                 }
426                                 else if(bbtree[indt(count,depth)".t"]!="list")
427                                 {
428                                         debug = debug "\nthis tag forbidden here"
429                                         bbtree[ind(count,depth)]=tag
430                                         bbtree[indt(count,depth)".n"]=count[depth]
431                                         ++count[depth]
432                                         continue
433                                 }
434                         }
435                         debug = debug "\nname: " tagname " value: " tagvalue
436                         bbtree[ind(count,depth)]=tagname" "tagvalue
437                         bbtree[ind(count,depth)".k"]="s"
438                         bbtree[indt(count,depth)".n"]=count[depth]
439                         ++depth
440                         count[depth]=1
441                         bbtree[indt(count,depth)]=tag
442                         bbtree[indt(count,depth)".n"]=0
443                         bbtree[indt(count,depth)".t"]=tagname
444                         bbtree[indt(count,depth)".v"]=tagvalue
445                         
446                         # if(tagname=="br") #or other single tags in the future
447                         # {
448                                 # debug = debug "\nmatched"
449                                 # bbtree[ind(count,depth)]=tag
450                                 # bbtree[ind(count,depth)".k"]="e"
451                                 # bbtree[ind(count,depth)".m"]=1
452                                 # bbtree[ind(count,depth)".t"]=tagname
453                                 # bbtree[ind(count,depth)".v"]=bbtree[indt(count,depth)".v"]
454                                 # bbtree[indt(count,depth)".n"]=count[depth]
455                                 # bbtree[indt(count,depth)".m"]=1
456                                 # count[depth]=""
457                                 # --depth
458                                 # ++count[depth]
459                         # }
460                 }
461                 else
462                 {
463                         if(depth<=0)
464                         {
465                                 debug = debug "\n""unmatched"
466                                 bbtree[ind(count,depth)]=tag
467                                 bbtree[indt(count,depth)".n"]=count[depth]
468                                 ++count[depth]
469                                 continue
470                         }
471                         tagname=substr(tag,3,length(tag)-3)
472                         if(tagname=="list"&&bbtree[indt(count,depth)".t"]=="*")
473                         {
474                                 debug = debug "\nimplied tag: [/*]"
475                                 debug = debug "\nmatched"
476                                 bbtree[ind(count,depth)]="[/*]"
477                                 bbtree[ind(count,depth)".k"]="e"
478                                 bbtree[ind(count,depth)".m"]=1
479                                 bbtree[ind(count,depth)".t"]="*"
480                                 bbtree[ind(count,depth)".v"]=bbtree[indt(count,depth)".v"]
481                                 bbtree[indt(count,depth)".n"]=count[depth]
482                                 bbtree[indt(count,depth)".m"]=1
483                                 count[depth]=""
484                                 --depth
485                                 ++count[depth]
486                         }
487                         debug = debug "\nname: " tagname
488                         matchname=bbtree[indt(count,depth)".t"]
489                         if((bbtree[indt(count,depth)".t"] == "code" && tagname!="code")||(bbtree[indt(count,depth)".t"] == "img" && tagname != "img"))
490                         {
491                                 debug = debug "\nthis tag forbidden here"
492                                 bbtree[ind(count,depth)]=tag
493                                 bbtree[indt(count,depth)".n"]=count[depth]
494                                 ++count[depth]
495                                 continue
496                         }
497                         if(tagname!=matchname)
498                         {
499                                 debug = debug "\nmismatched: " matchname
500                                 # bbtree[ind(count,depth)]=tag
501                                 # bbtree[indt(count,depth)".n"]=count[depth]
502                                 # ++count[depth]
503                                 # continue
504                                 bbtree[ind(count,depth)]=tag
505                                 bbtree[ind(count,depth)".k"]="e"
506                                 bbtree[ind(count,depth)".t"]=bbtree[indt(count,depth)".t"]
507                                 bbtree[ind(count,depth)".v"]=bbtree[indt(count,depth)".v"]
508                                 bbtree[indt(count,depth)".n"]=count[depth]
509                                 count[depth]=""
510                                 --depth
511                                 ++count[depth]
512                                 continue
513                         }
514                         debug = debug "\nmatched"
515                         bbtree[ind(count,depth)]=tag
516                         bbtree[ind(count,depth)".k"]="e"
517                         bbtree[ind(count,depth)".m"]=1
518                         bbtree[ind(count,depth)".t"]=tagname
519                         bbtree[ind(count,depth)".v"]=bbtree[indt(count,depth)".v"]
520                         bbtree[indt(count,depth)".n"]=count[depth]
521                         bbtree[indt(count,depth)".m"]=1
522                         count[depth]=""
523                         --depth
524                         ++count[depth]
525                 }
526         }
527         if(depth>0)
528                 debug = debug "\nunclosed tags: " depth
529         
530         debug = debug "\n\n""generate HTML:"
531         depth=0
532         count[0]=0
533         while(count[0]<=bbtree["0.n"]&&depth>=0)
534         {
535                 if(count[depth]==0)
536                 {
537                         tag=bbtree[indt(count,depth)]
538                         tagname=bbtree[indt(count,depth)".t"]
539                         tagvalue=bbtree[indt(count,depth)".v"]
540                         debug = debug "\nname: "tagname" value: "tagvalue" elements: "bbtree[indt(count,depth)".n"]
541                         if(tagname == "post")
542                         {
543                         }
544                         else if(bbtree[indt(count,depth)".m"]=="")
545                         {
546                                 debug = debug "\nunmatched"
547                                 html = html htmlescape(tag)
548                         }
549                         else if(tagname == "quote")
550                         {
551                                 if (tagvalue=="")
552                                 {
553                                         html = html "<blockquote class=\"pq\"><div>"
554                                 }
555                                 else
556                                 {
557                                         if(tagvalue !~ /^\".*\"$/)
558                                         #"#\""
559                                                 tagvalue="You forgot about the quote marks, "htmlescape(tagvalue)
560                                         else
561                                                 tagvalue=bb2html(substr(tagvalue,2,length(tagvalue)-2))
562                                         html = html "<blockquote class=\"pq\"><div><cite class=\"pq\"><b class=\"br\">"tagvalue"</b> wrote:</cite><br>"
563                                 }
564                         }
565                         else if (tagname == "fq")
566                         {
567                                 html = html"<div class=\"fq\">"
568                                 if(tagvalue!="")
569                                         html=html"This tag shouldn't have any value. " 
570                         }
571                         else if (tagname == "tq")
572                         {
573                                 html = html"<div class=\"tq\">"
574                                 if(tagvalue!="")
575                                         html=html"This tag shouldn't have any value. " 
576                         }
577                         else if (tagname == "br")
578                         {
579                                 html = html"<span class=\"br\">"
580                                 if(tagvalue!="")
581                                         html=html"This tag shouldn't have any value. " 
582                         }
583                         else if (tagname == "ni")
584                         {
585                                 html = html"<span class=\"ni\">"
586                                 if(tagvalue!="")
587                                         html=html"This tag shouldn't have any value. " 
588                         }
589                         else if (tagname == "po")
590                         {
591                                 html = html"<span class=\"po\">"
592                                 if(tagvalue!="")
593                                         html=html"This tag shouldn't have any value. " 
594                         }
595                         else if (tagname == "b")
596                         {
597                                 html = html"<span style=\"font-weight: bold\">"
598                                 if(tagvalue!="")
599                                         html=html"This tag shouldn't have any value. " 
600                         }
601                         else if (tagname == "i")
602                         {
603                                 html = html"<span style=\"font-style: italic\">"
604                                 if(tagvalue!="")
605                                         html=html"This tag shouldn't have any value. " 
606                         }
607                         else if (tagname == "u")
608                         {
609                                 html = html"<span style=\"text-decoration: underline\">"
610                                 if(tagvalue!="")
611                                         html=html"This tag shouldn't have any value. " 
612                         }
613                         else if (tagname == "code")
614                         {
615                                 if(bbtree[indtu(count,depth)".t"] == "code")
616                                 {
617                                         html=html htmlescape(tag)
618                                         debug = debug "\nnot a tag, actually"
619                                 }
620                                 else
621                                 {
622                                         html=html"<dl class=\"pq\"><dt class=\"pq\">Code: <a href=\"#\" onclick=\"selectCode(this); return false;\">Select all</a></dt><dd><code class=\"pq\">"
623                                         if(tagvalue!="")
624                                                 html=html"This tag shouldn't have any value. "
625                                 }
626                         }
627                         else if (tagname == "img")
628                                 html=html"<img src=\""
629                         else if (tagname == "url")
630                         {
631                                 if(tagvalue=="")
632                                 {
633                                         ++count[depth]
634                                         tagvalue=bbtree[ind(count,depth)]
635                                         --count[depth]
636                                         debug = debug "\nimplied value: " tagvalue
637                                 }
638                                 html=html"<a href=\""htmlescape(tagvalue)"\" class=\"postlink\">"
639                         }
640                         else if (tagname == "size")
641                         {
642                                 if (tagvalue==""||(tagvalue !~ /^[0-9]+$/)||(tagvalue+0)>200)
643                                         html=html"<span style=\"font-size: 130%; line-height: 116%;\"> This tag should have a correct value. "
644                                 else
645                                         html=html"<span style=\"font-size: "htmlescape(tagvalue)"%; line-height: 116%;\">"
646                         }
647                         else if (tagname == "color")
648                         {
649                                 if (tagvalue==""||tagvalue !~ /^#[0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F]$/) #for some reason /^([0-9A-F]{6})$/ doesn't work.
650                                         html=html"<span style=\"color: #EE0000\"> This tag should have a correct value. "
651                                 else
652                                         html=html"<span style=\"color: "htmlescape(tagvalue)"\">"
653                         }
654                         else if (tagname ~ /^((center)|(right))$/)
655                         {
656                                 html=html"<div align=\""tagname"\">"
657                                 if(tagvalue!="")
658                                         html=html"This tag shouldn't have any value. "
659                         }
660                         else if (tagname =="s")
661                         {
662                                 html=html"<strike>"
663                                 if(tagvalue!="")
664                                         html=html"This tag shouldn't have any value. "
665                         }
666                         else if (tagname =="sub")
667                         {
668                                 html=html"<sub>"
669                                 if(tagvalue!="")
670                                         html=html"This tag shouldn't have any value. "
671                         }
672                         else if (tagname =="sup")
673                         {
674                                 html=html"<sup>"
675                                 if(tagvalue!="")
676                                         html=html"This tag shouldn't have any value. "
677                         }
678                         else if (tagname=="spoiler")
679                         {
680                                 html=html"<div style=\"margin:20px; margin-top:5px\"><div class=\"quotetitle\"><b>Spoiler:</b> <input type=\"button\" class=\"pk\" value=\"Show\" style=\"width:45px;font-size:10px;margin:0px;padding:0px;\" onclick=\"if (this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display != '') { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = '';        this.innerText = ''; this.value = 'Hide'; } else { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = 'none'; this.innerText = ''; this.value = 'Show'; }\" /></div><div class=\"quotecontent\"><div style=\"display: none;\">"
681                                 if(tagvalue!="")
682                                         html=html"This tag shouldn't have any value. "
683                         }
684                         else if (tagname=="list")
685                         {
686                                 if(tagvalue=="")
687                                         html=html"<ul>"
688                                 else
689                                 {
690                                         if (tagvalue=="1")
691                                                 tagvalue="decimal"
692                                         else if (tagvalue=="i")
693                                                 tagvalue="lower-roman"
694                                         else if (tagvalue=="I")
695                                                 tagvalue="upper-roman"
696                                         else if (tagvalue=="a")
697                                                 tagvalue="lower-alpha"
698                                         else if (tagvalue=="A")
699                                                 tagvalue="upper-alpha"
700                                         else
701                                         {
702                                                 html=html"This tag should have a correct value. "
703                                                 tagvalue="decimal"
704                                         }
705                                         html=html"<ol style=\"list-style-type: "tagvalue"\">"
706                                 }
707                         }
708                         else if (tagname=="*")
709                         {
710                                 html=html"<li>"
711                                 if(tagvalue!="")
712                                         html=html"This tag shouldn't have any value. "
713                         }
714                         # else if (tagname == "br")
715                         # {
716                                 # html = html"<br>"
717                                 # if(tagvalue!="")
718                                         # html=html"This tag shouldn't have any value. " 
719                         # }
720                         else
721                         {
722                                 debug = debug "\nunknown tag"
723                                 html = html htmlescape(tag)
724                         }
725                         ++count[depth]
726                 }
727                 else if(bbtree[ind(count,depth)".k"]=="s")
728                 {
729                         debug = debug "\nentering tag:"
730                         ++depth
731                         count[depth]=0
732                 }
733                 else if(bbtree[ind(count,depth)".k"]=="e")
734                 {
735                         debug = debug "\nleaving tag:"
736                         tag=bbtree[ind(count,depth)]
737                         tagname=bbtree[ind(count,depth)".t"]
738                         tagvalue=bbtree[ind(count,depth)".v"]
739                         debug = debug "\nname: "tagname" value: "tagvalue
740                         if (tagname == "post")
741                         {
742                         }
743                         else if(bbtree[ind(count,depth)".m"]=="")
744                         {
745                                 debug = debug "\nunmatched"
746                                 html = html htmlescape(tag)
747                         }
748                         else if(tagname == "quote")
749                                 html = html "</div></blockquote>"
750                         else if (tagname ~/^(b|i|u|(size)|(color)|(br)|(ni)|(po))$/)
751                                 html = html"</span>"
752                         else if (tagname == "code")
753                         {
754                                 if(bbtree[indtu(count,depth)".t"] == "code")
755                                 {
756                                         html=html htmlescape(tag)
757                                         debug = debug "\nnot a tag, actually"
758                                 }
759                                 else
760                                         html = html"</code></dd></dl>"
761                         }
762                         else if (tagname == "img")
763                         {
764                                 html = html"\" alt=\"Image\" />"
765                                 if(tagvalue!="")
766                                         html=html"This tag shouldn't have any value. "
767                         }
768                         else if(tagname=="url")
769                                 html=html"</a>"
770                         else if (tagname ~ /^((center)|(right)|(fq)|(tq))$/)
771                                 html=html"</div>"
772                         else if (tagname =="s")
773                                 html=html"</strike>"
774                         else if (tagname =="sub")
775                                 html=html"</sub>"
776                         else if (tagname =="sup")
777                                 html=html"</sup>"
778                         else if (tagname=="spoiler")
779                                 html=html"</div></div></div>"
780                         else if (tagname=="list")
781                         {
782                                 if (tagvalue=="")
783                                         html=html"</ul>"
784                                 else
785                                         html=html"</ol>"
786                         }
787                         else if (tagname=="*")
788                                 html=html"</li>"
789                         # else if (tagname == "br") #
790                         # {
791                                 # #
792                         # }
793                         else
794                         {
795                                 debug = debug "\nunknown tag"
796                                 html = html htmlescape(tag)
797                         }
798                         count[depth]=""
799                         --depth
800                         ++count[depth]
801                 }
802                 else if(count[depth]>bbtree[indt(count,depth)".n"])
803                 {
804                         debug = debug "\nrun out of elements in this tag; leaving"
805                         count[depth]=""
806                         --depth
807                         ++count[depth]
808                 }
809                 else
810                 {
811                         tag=bbtree[ind(count,depth)]
812                         debug = debug "\ntext: "tag
813                         
814                         tag=htmlescape(tag,"","br")
815                         
816                         if(bbtree[indt(count,depth)".t"] == "code")
817                         {
818                                 gsub(/ /,"\\&nbsp;",tag)
819                                 gsub(/\t/,"\\&nbsp;\\&nbsp;",tag)
820                         }
821                         else{
822                                 gsub(/  /,"\\&nbsp; ",tag)
823                                 gsub(/  /," \\&nbsp;",tag)
824                                 # gsub(/<br> /,"<br>\\&nbsp;",tag)
825                         }
826                         
827                         
828                         if(nourl==""&& bbtree[indt(count,depth)".t"] !~ /^((code)|(img)|(url))$/)
829                                 gsub(/https?:\/\/[^\n\r\t<> ]+/,"<a href=\"&\" class=\"postlink\">&</a>",tag)
830                         # if(nosmil=="" && bbtree[indt(count,depth)".t"] !~ /^((code)|(img))$/)
831                         # {
832                                 # gsub(/(^|[\n\r\t ]):D($|[\n\r\t ])/," <img title=\"Very Happy\" alt=\":D\" src=\"http://forums.xkcd.com/images/smilies/icon_biggrin.gif\" /> ",tag)
833                                 # gsub(/(^|[\n\r\t ]):\)($|[\n\r\t ])/," <img title=\"Smile\" alt=\":)\" src=\"http://forums.xkcd.com/images/smilies/icon_smile.gif\" /> ",tag)
834                                 # gsub(/(^|[\n\r\t ]):\(($|[\n\r\t ])/," <img title=\"Sad\" alt=\":(\" src=\"http://forums.xkcd.com/images/smilies/icon_sad.gif\" /> ",tag)
835                                 # gsub(/(^|[\n\r\t ]):o($|[\n\r\t ])/," <img title=\"Surprised\" alt=\":o\" src=\"http://forums.xkcd.com/images/smilies/icon_surprised.gif\" /> ",tag)
836                                 # gsub(/(^|[\n\r\t ]):shock:($|[\n\r\t ])/," <img title=\"Shocked\" alt=\":shock:\" src=\"http://forums.xkcd.com/images/smilies/icon_eek.gif\" /> ",tag)
837                                 # gsub(/(^|[\n\r\t ]):\?($|[\n\r\t ])/," <img title=\"Confused\" alt=\":?\" src=\"http://forums.xkcd.com/images/smilies/icon_confused.gif\" /> ",tag)
838                                 # gsub(/(^|[\n\r\t ])8-\)($|[\n\r\t ])/," <img title=\"Cool\" alt=\"8-)\" src=\"http://forums.xkcd.com/images/smilies/icon_cool.gif\" /> ",tag)
839                                 # gsub(/(^|[\n\r\t ]):lol:($|[\n\r\t ])/," <img title=\"Laughing\" alt=\":lol:\" src=\"http://forums.xkcd.com/images/smilies/icon_lol.gif\" /> ",tag)
840                                 # gsub(/(^|[\n\r\t ]):x($|[\n\r\t ])/," <img title=\"Mad\" alt=\":x\" src=\"http://forums.xkcd.com/images/smilies/icon_mad.gif\" /> ",tag)
841                                 # gsub(/(^|[\n\r\t ]):P($|[\n\r\t ])/," <img title=\"Razz\" alt=\":P\" src=\"http://forums.xkcd.com/images/smilies/icon_razz.gif\" /> ",tag)
842                                 # gsub(/(^|[\n\r\t ]):oops:($|[\n\r\t ])/," <img title=\"Embarassed\" alt=\":oops:\" src=\"http://forums.xkcd.com/images/smilies/icon_redface.gif\" /> ",tag)
843                                 # gsub(/(^|[\n\r\t ]):cry:($|[\n\r\t ])/," <img title=\"Crying or Very Sad\" alt=\":cry:\" src=\"http://forums.xkcd.com/images/smilies/icon_cry.gif\" /> ",tag)
844                                 # gsub(/(^|[\n\r\t ]):evil:($|[\n\r\t ])/," <img title=\"Evil or Very Mad\" alt=\":evil:\" src=\"http://forums.xkcd.com/images/smilies/icon_evil.gif\" /> ",tag)
845                                 # gsub(/(^|[\n\r\t ]):twisted:($|[\n\r\t ])/," <img title=\"Twisted Evil\" alt=\":twisted:\" src=\"http://forums.xkcd.com/images/smilies/icon_twisted.gif\" /> ",tag)
846                                 # gsub(/(^|[\n\r\t ]):roll:($|[\n\r\t ])/," <img title=\"Rolling Eyes\" alt=\":roll:\" src=\"http://forums.xkcd.com/images/smilies/icon_rolleyes.gif\" /> ",tag)
847                                 # gsub(/(^|[\n\r\t ]):wink:($|[\n\r\t ])/," <img title=\"Wink\" alt=\":wink:\" src=\"http://forums.xkcd.com/images/smilies/icon_wink.gif\" /> ",tag)
848                                 # gsub(/(^|[\n\r\t ]);\)($|[\n\r\t ])/," <img title=\"Wink\" alt=\":wink:\" src=\"http://forums.xkcd.com/images/smilies/icon_wink.gif\" /> ",tag)
849                                 # gsub(/(^|[\n\r\t ]):!:($|[\n\r\t ])/," <img title=\"Exclamation\" alt=\":!:\" src=\"http://forums.xkcd.com/images/smilies/icon_exclaim.gif\" /> ",tag)
850                                 # gsub(/(^|[\n\r\t ]):\?:($|[\n\r\t ])/," <img title=\"Question\" alt=\":?:\" src=\"http://forums.xkcd.com/images/smilies/icon_question.gif\" /> ",tag)
851                                 # gsub(/(^|[\n\r\t ]):idea:($|[\n\r\t ])/," <img title=\"Idea\" alt=\":idea:\" src=\"http://forums.xkcd.com/images/smilies/icon_idea.gif\" /> ",tag)
852                                 # gsub(/(^|[\n\r\t ]):arrow:($|[\n\r\t ])/," <img title=\"Arrow\" alt=\":arrow:\" src=\"http://forums.xkcd.com/images/smilies/icon_arrow.gif\" /> ",tag)
853                                 # gsub(/(^|[\n\r\t ]):\|($|[\n\r\t ])/," <img title=\"Neutral\" alt=\":|\" src=\"http://forums.xkcd.com/images/smilies/icon_neutral.gif\" /> ",tag)
854                                 # gsub(/(^|[\n\r\t ]):mrgreen:($|[\n\r\t ])/," <img title=\"Mr. Green\" alt=\":mrgreen:\" src=\"http://forums.xkcd.com/images/smilies/icon_mrgreen.gif\" /> ",tag)
855                         # }
856                         
857                         html = html tag
858                         ++count[depth]
859                 }
860         }
861         debug = debug "\ndone\n"
862         return html
863 }
864
865 function ind(count,depth,   ii,iindex)
866 {
867         iindex = ""
868         for(ii=0;ii<=depth;++ii)
869         {
870                 if(ii!=0)
871                         iindex = iindex "."
872                 iindex = iindex count[ii]
873         }
874         # debug = debug "\n" iindex
875         return iindex
876 }
877 function indt(count,depth,   ii,iindex)
878 {
879         iindex = ""
880         for(ii=0;ii<depth;++ii)
881         {
882                 if(ii!=0)
883                         iindex = iindex "."
884                 iindex = iindex count[ii]
885         }
886         if(depth>0)
887                 iindex=iindex"."
888         iindex = iindex "0"
889         # debug = debug "\n" iindex
890         return iindex
891 }
892 function indtu(count,depth,   ii,iindex)
893 {
894         iindex = ""
895         for(ii=0;ii<depth-1;++ii)
896         {
897                 if(ii!=0)
898                         iindex = iindex "."
899                 iindex = iindex count[ii]
900         }
901         if(depth>0)
902                 iindex=iindex"."
903         iindex = iindex "0"
904         # debug = debug "\n" iindex
905         return iindex
906 }
907 function htmlescape(name,less,br,   len,iii,escaped,ch)
908 {
909         len=length(name)
910         escaped=""
911         for(iii=1;iii<=len;++iii)
912         {
913                 ch=substr(name,iii,1);
914                 if(ch == "\n")
915                         escaped = escaped ((br!="")?"<br>":"&#"ch2dec[ch]";")
916                 else if(ch =="\r")
917                         escaped = escaped ((br!="")?"":"&#"ch2dec[ch]";")
918                 else if(ch == "\"" && less=="")
919                         escaped = escaped "&#"ch2dec[ch]";"
920                 else if(ch ~ /[=<>&]/)
921                         escaped = escaped "&#"ch2dec[ch]";"
922                 else
923                         escaped = escaped ch
924         }
925         return escaped
926 }
927 function hexencode(name,   len,iii,escaped,ch)
928 {
929         len=length(name)
930         escaped=""
931         for(iii=1;iii<=len;++iii)
932         {
933                 ch=substr(name,iii,1)
934                 escaped = escaped ch2hex[ch]
935         }
936         return escaped
937 }
938