# // preview.awk # // Display the post list # // 30.03.2015 # // # // Copyright (C) 2015 Balthasar SzczepaƄski # // # // This program is free software: you can redistribute it and/or modify # // it under the terms of the GNU Affero General Public License as # // published by the Free Software Foundation, either version 3 of the # // License, or (at your option) any later version. # // # // This program is distributed in the hope that it will be useful, # // but WITHOUT ANY WARRANTY; without even the implied warranty of # // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # // GNU Affero General Public License for more details. # // # // You should have received a copy of the GNU Affero General Public License # // along with this program. If not, see . BEGIN{ posttop="
\n
%s
\n
%s • %s
\n
\n" postbottom="

\n" for(i=0;i<256;++i) { ch=sprintf("%c",i) hex=sprintf("%02X",i) hexl=sprintf("%02x",i) dec=sprintf("%u",i) ch2hex[ch]=hex ch2hexl[ch]=hexl ch2dec[ch]=dec } ln=0 print "" print "" print "the Silent Chronotransponder preview • Bicycles on the Moon" print "" print "" print "

the Silent Chronotransponder preview

" } { gsub(/[\r\n]/,"",$0) if (FNR==1) { name=$0 next } if (FNR==2) { # pass=$0 next } if($0=="") { if(ln!=0) { post() ln=0 nobb="" nosmil="" nourl="" sig="" notify="" silent="" quot="" h="" d="" m="" y="" subject="" content="" options="" postid="" } next } ++ln if(ln==1) { y=substr($0,1,4) m=substr($0,6,2) d=substr($0,9,2) h=substr($0,12,2) } else if(ln==2) { options=tolower($0) if(options ~ /b/) nobb=1 if(options ~ /s/) nosmil=1 if(options ~ /u/) nourl=1 if(options ~ /a/) sig=1 if(options ~ /n/) notify=1 if(options ~ /v/) silent=1 } else if(ln==3) { subject=$0 } else { if(ln!=4) content=content "[br]" $0 else content=$0 } } END{ if(ln!=0) post() print "" } function post() { postid=sprintf("%04d%02d%02d%02d_%s",y,m,d,h,hexencode(name)) printf(posttop, postid, htmlescape(subject), htmlescape(name), d"."m"."y", "h":00 GMT") if(nobb=="") print bb2html(content) else print htmlescape(content) print postbottom # print "" debug="" } function bb2html(text, depth,count,bbtree,html,tag,tagstart,taglength,elm,tagv,tagname,tagvalue,matchname) { depth=0 count[0]=1 bbtree["0"]="" bbtree["0.n"]=0 bbtree["0.t"]="post" bbtree["0.v"]=argtab["subject"] html="" gsub(/=/,"=",text) debug = debug "\n\nBuild BBcode tree:" while(length(text)!=0) { tag=match(text, /\[\/?[a-z0-9\*]+(=(([^\[<>\"\]]*)|(\"[^\"<>]*\")))?\]/) #"#\"" if (tag==0) { debug = debug "\ntext: " text bbtree[ind(count,depth)]=text bbtree[indt(count,depth)".n"]=count[depth] ++count[depth] text="" break; } tagstart=RSTART taglength=RLENGTH if(tagstart>1) { elm=substr(text,1,tagstart-1) debug = debug "\ntext: " elm bbtree[ind(count,depth)]=elm bbtree[indt(count,depth)".n"]=count[depth] ++count[depth] } tag=substr(text,tagstart,taglength) text=substr(text,tagstart+taglength) debug = debug "\ntag: " tag if(tag!~/^\[\//) { tagv=index(tag,"=") if(tagv<2) { tagname=substr(tag,2,length(tag)-2) tagvalue="" } else { tagname=substr(tag,2,tagv-2) tagvalue=substr(tag,tagv+1,length(tag)-tagv-1) } if(tagname !~ /^((quote)|b|i|u|(code)|(img)|(url)|(size)|(color)|(center)|(right)|(sub)|(sup)|(s)|(spoiler)|(list)|(br)|(\*))$/) { debug = debug "\nunknown tag" bbtree[ind(count,depth)]=tag bbtree[indt(count,depth)".n"]=count[depth] ++count[depth] continue } 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")) { debug = debug "\nthis tag forbidden here" bbtree[ind(count,depth)]=tag bbtree[indt(count,depth)".n"]=count[depth] ++count[depth] continue } if(tagname=="*") { if(bbtree[indt(count,depth)".t"]==tagname) { debug = debug "\nimplied tag: [/"tagname"]" debug = debug "\nmatched" bbtree[ind(count,depth)]="[/"tagname"]" bbtree[ind(count,depth)".k"]="e" bbtree[ind(count,depth)".m"]=1 bbtree[ind(count,depth)".t"]=tagname bbtree[ind(count,depth)".v"]=bbtree[indt(count,depth)".v"] bbtree[indt(count,depth)".n"]=count[depth] bbtree[indt(count,depth)".m"]=1 count[depth]="" --depth ++count[depth] } else if(bbtree[indt(count,depth)".t"]!="list") { debug = debug "\nthis tag forbidden here" bbtree[ind(count,depth)]=tag bbtree[indt(count,depth)".n"]=count[depth] ++count[depth] continue } } debug = debug "\nname: " tagname " value: " tagvalue bbtree[ind(count,depth)]=tagname" "tagvalue bbtree[ind(count,depth)".k"]="s" bbtree[indt(count,depth)".n"]=count[depth] ++depth count[depth]=1 bbtree[indt(count,depth)]=tag bbtree[indt(count,depth)".n"]=0 bbtree[indt(count,depth)".t"]=tagname bbtree[indt(count,depth)".v"]=tagvalue if(tagname=="br") #or other single tags in the future { debug = debug "\nmatched" bbtree[ind(count,depth)]=tag bbtree[ind(count,depth)".k"]="e" bbtree[ind(count,depth)".m"]=1 bbtree[ind(count,depth)".t"]=tagname bbtree[ind(count,depth)".v"]=bbtree[indt(count,depth)".v"] bbtree[indt(count,depth)".n"]=count[depth] bbtree[indt(count,depth)".m"]=1 count[depth]="" --depth ++count[depth] } } else { if(depth<=0) { debug = debug "\n""unmatched" bbtree[ind(count,depth)]=tag bbtree[indt(count,depth)".n"]=count[depth] ++count[depth] continue } tagname=substr(tag,3,length(tag)-3) if(tagname=="list"&&bbtree[indt(count,depth)".t"]=="*") { debug = debug "\nimplied tag: [/*]" debug = debug "\nmatched" bbtree[ind(count,depth)]="[/*]" bbtree[ind(count,depth)".k"]="e" bbtree[ind(count,depth)".m"]=1 bbtree[ind(count,depth)".t"]="*" bbtree[ind(count,depth)".v"]=bbtree[indt(count,depth)".v"] bbtree[indt(count,depth)".n"]=count[depth] bbtree[indt(count,depth)".m"]=1 count[depth]="" --depth ++count[depth] } debug = debug "\nname: " tagname matchname=bbtree[indt(count,depth)".t"] if((bbtree[indt(count,depth)".t"] == "code" && tagname!="code")||(bbtree[indt(count,depth)".t"] == "img" && tagname != "img")) { debug = debug "\nthis tag forbidden here" bbtree[ind(count,depth)]=tag bbtree[indt(count,depth)".n"]=count[depth] ++count[depth] continue } if(tagname!=matchname) { debug = debug "\nmismatched: " matchname # bbtree[ind(count,depth)]=tag # bbtree[indt(count,depth)".n"]=count[depth] # ++count[depth] # continue bbtree[ind(count,depth)]=tag bbtree[ind(count,depth)".k"]="e" bbtree[ind(count,depth)".t"]=bbtree[indt(count,depth)".t"] bbtree[ind(count,depth)".v"]=bbtree[indt(count,depth)".v"] bbtree[indt(count,depth)".n"]=count[depth] count[depth]="" --depth ++count[depth] continue } debug = debug "\nmatched" bbtree[ind(count,depth)]=tag bbtree[ind(count,depth)".k"]="e" bbtree[ind(count,depth)".m"]=1 bbtree[ind(count,depth)".t"]=tagname bbtree[ind(count,depth)".v"]=bbtree[indt(count,depth)".v"] bbtree[indt(count,depth)".n"]=count[depth] bbtree[indt(count,depth)".m"]=1 count[depth]="" --depth ++count[depth] } } if(depth>0) debug = debug "\nunclosed tags: " depth debug = debug "\n\n""generate HTML:" depth=0 count[0]=0 while(count[0]<=bbtree["0.n"]&&depth>=0) { if(count[depth]==0) { tag=bbtree[indt(count,depth)] tagname=bbtree[indt(count,depth)".t"] tagvalue=bbtree[indt(count,depth)".v"] debug = debug "\nname: "tagname" value: "tagvalue" elements: "bbtree[indt(count,depth)".n"] if(tagname == "post") { } else if(bbtree[indt(count,depth)".m"]=="") { debug = debug "\nunmatched" html = html htmlescape(tag) } else if(tagname == "quote") { if (tagvalue=="") { html = html "
" } else { if(tagvalue !~ /^\".*\"$/) #"#\"" tagvalue="You forgot about the quote marks, "htmlescape(tagvalue) else tagvalue=bb2html(substr(tagvalue,2,length(tagvalue)-2)) html = html "
"tagvalue" wrote:
" } } else if (tagname == "b") { html = html"" if(tagvalue!="") html=html"This tag shouldn't have any value. " } else if (tagname == "i") { html = html"" if(tagvalue!="") html=html"This tag shouldn't have any value. " } else if (tagname == "u") { html = html"" if(tagvalue!="") html=html"This tag shouldn't have any value. " } else if (tagname == "code") { if(bbtree[indtu(count,depth)".t"] == "code") { html=html htmlescape(tag) debug = debug "\nnot a tag, actually" } else { html=html"
Code: Select all
" if(tagvalue!="") html=html"This tag shouldn't have any value. " } } else if (tagname == "img") html=html"" } else if (tagname == "size") { if (tagvalue==""||(tagvalue !~ /^[0-9]+$/)||(tagvalue+0)>200) html=html" This tag should have a correct value. " else html=html"" } else if (tagname == "color") { 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. html=html" This tag should have a correct value. " else html=html"" } else if (tagname ~ /^((center)|(right))$/) { html=html"
" if(tagvalue!="") html=html"This tag shouldn't have any value. " } else if (tagname =="s") { html=html"" if(tagvalue!="") html=html"This tag shouldn't have any value. " } else if (tagname =="sub") { html=html"" if(tagvalue!="") html=html"This tag shouldn't have any value. " } else if (tagname =="sup") { html=html"" if(tagvalue!="") html=html"This tag shouldn't have any value. " } else if (tagname=="spoiler") { html=html"
Spoiler:
" if(tagvalue!="") html=html"This tag shouldn't have any value. " } else if (tagname=="list") { if(tagvalue=="") html=html"
    " else { if (tagvalue=="1") tagvalue="decimal" else if (tagvalue=="i") tagvalue="lower-roman" else if (tagvalue=="I") tagvalue="upper-roman" else if (tagvalue=="a") tagvalue="lower-alpha" else if (tagvalue=="A") tagvalue="upper-alpha" else { html=html"This tag should have a correct value. " tagvalue="decimal" } html=html"
      " } } else if (tagname=="*") { html=html"
    1. " if(tagvalue!="") html=html"This tag shouldn't have any value. " } else if (tagname == "br") { html = html"
      " if(tagvalue!="") html=html"This tag shouldn't have any value. " } else { debug = debug "\nunknown tag" html = html htmlescape(tag) } ++count[depth] } else if(bbtree[ind(count,depth)".k"]=="s") { debug = debug "\nentering tag:" ++depth count[depth]=0 } else if(bbtree[ind(count,depth)".k"]=="e") { debug = debug "\nleaving tag:" tag=bbtree[ind(count,depth)] tagname=bbtree[ind(count,depth)".t"] tagvalue=bbtree[ind(count,depth)".v"] debug = debug "\nname: "tagname" value: "tagvalue if (tagname == "post") { } else if(bbtree[ind(count,depth)".m"]=="") { debug = debug "\nunmatched" html = html htmlescape(tag) } else if(tagname == "quote") html = html "
" else if (tagname ~/^(b|i|u|(size)|(color))$/) html = html"" else if (tagname == "code") { if(bbtree[indtu(count,depth)".t"] == "code") { html=html htmlescape(tag) debug = debug "\nnot a tag, actually" } else html = html"" } else if (tagname == "img") { html = html"\" alt=\"Image\" />" if(tagvalue!="") html=html"This tag shouldn't have any value. " } else if(tagname=="url") html=html"" else if (tagname ~ /^((center)|(right))$/) html=html"
" else if (tagname =="s") html=html"" else if (tagname =="sub") html=html"" else if (tagname =="sup") html=html"" else if (tagname=="spoiler") html=html"" else if (tagname=="list") { if (tagvalue=="") html=html"" else html=html"" } else if (tagname=="*") html=html"" else if (tagname == "br") # { # } else { debug = debug "\nunknown tag" html = html htmlescape(tag) } count[depth]="" --depth ++count[depth] } else if(count[depth]>bbtree[indt(count,depth)".n"]) { debug = debug "\nrun out of elements in this tag; leaving" count[depth]="" --depth ++count[depth] } else { tag=bbtree[ind(count,depth)] debug = debug "\ntext: "tag tag=htmlescape(tag) if(bbtree[indt(count,depth)".t"] == "code") { gsub(/ /,"\\ ",tag) gsub(/\t/,"\\ \\ ",tag) } if(nourl==""&& bbtree[indt(count,depth)".t"] !~ /^((code)|(img)|(url))$/) gsub(/https?:\/\/[^\n\r\t<> ]+/,"&",tag) if(nosmil=="" && bbtree[indt(count,depth)".t"] !~ /^((code)|(img))$/) { gsub(/(^|[\n\r\t ]):D($|[\n\r\t ])/," \":D\" ",tag) gsub(/(^|[\n\r\t ]):\)($|[\n\r\t ])/," \":)\" ",tag) gsub(/(^|[\n\r\t ]):\(($|[\n\r\t ])/," \":(\" ",tag) gsub(/(^|[\n\r\t ]):o($|[\n\r\t ])/," \":o\" ",tag) gsub(/(^|[\n\r\t ]):shock:($|[\n\r\t ])/," \":shock:\" ",tag) gsub(/(^|[\n\r\t ]):\?($|[\n\r\t ])/," \":?\" ",tag) gsub(/(^|[\n\r\t ])8-\)($|[\n\r\t ])/," \"8-)\" ",tag) gsub(/(^|[\n\r\t ]):lol:($|[\n\r\t ])/," \":lol:\" ",tag) gsub(/(^|[\n\r\t ]):x($|[\n\r\t ])/," \":x\" ",tag) gsub(/(^|[\n\r\t ]):P($|[\n\r\t ])/," \":P\" ",tag) gsub(/(^|[\n\r\t ]):oops:($|[\n\r\t ])/," \":oops:\" ",tag) gsub(/(^|[\n\r\t ]):cry:($|[\n\r\t ])/," \":cry:\" ",tag) gsub(/(^|[\n\r\t ]):evil:($|[\n\r\t ])/," \":evil:\" ",tag) gsub(/(^|[\n\r\t ]):twisted:($|[\n\r\t ])/," \":twisted:\" ",tag) gsub(/(^|[\n\r\t ]):roll:($|[\n\r\t ])/," \":roll:\" ",tag) gsub(/(^|[\n\r\t ]):wink:($|[\n\r\t ])/," \":wink:\" ",tag) gsub(/(^|[\n\r\t ]);\)($|[\n\r\t ])/," \":wink:\" ",tag) gsub(/(^|[\n\r\t ]):!:($|[\n\r\t ])/," \":!:\" ",tag) gsub(/(^|[\n\r\t ]):\?:($|[\n\r\t ])/," \":?:\" ",tag) gsub(/(^|[\n\r\t ]):idea:($|[\n\r\t ])/," \":idea:\" ",tag) gsub(/(^|[\n\r\t ]):arrow:($|[\n\r\t ])/," \":arrow:\" ",tag) gsub(/(^|[\n\r\t ]):\|($|[\n\r\t ])/," \":|\" ",tag) gsub(/(^|[\n\r\t ]):mrgreen:($|[\n\r\t ])/," \":mrgreen:\" ",tag) } html = html tag ++count[depth] } } debug = debug "\ndone\n" return html } function ind(count,depth, ii,iindex) { iindex = "" for(ii=0;ii<=depth;++ii) { if(ii!=0) iindex = iindex "." iindex = iindex count[ii] } # debug = debug "\n" iindex return iindex } function indt(count,depth, ii,iindex) { iindex = "" for(ii=0;ii0) iindex=iindex"." iindex = iindex "0" # debug = debug "\n" iindex return iindex } function indtu(count,depth, ii,iindex) { iindex = "" for(ii=0;ii0) iindex=iindex"." iindex = iindex "0" # debug = debug "\n" iindex return iindex } function htmlescape(name,less,br, len,iii,escaped,ch) { len=length(name) escaped="" for(iii=1;iii<=len;++iii) { ch=substr(name,iii,1); if(ch == "\n") escaped = escaped ((br!="")?"
":"&#"ch2dec[ch]";") else if(ch =="\r") escaped = escaped ((br!="")?"":"&#"ch2dec[ch]";") else if(ch == "\"" && less=="") escaped = escaped "&#"ch2dec[ch]";" else if(ch ~ /[=<>&]/) escaped = escaped "&#"ch2dec[ch]";" else escaped = escaped ch } return escaped } function hexencode(name, len,iii,escaped,ch) { len=length(name) escaped="" for(iii=1;iii<=len;++iii) { ch=substr(name,iii,1) escaped = escaped ch2hex[ch] } return escaped }