diff options
author | KatolaZ <katolaz@freaknet.org> | 2018-09-03 17:12:12 +0100 |
---|---|---|
committer | KatolaZ <katolaz@freaknet.org> | 2018-09-03 17:12:12 +0100 |
commit | 4d152e47955641f954b9b038b969c94c087a0146 (patch) | |
tree | 006e1e7bced7c092493e0a95c80dcc49d7574bb9 |
first commit - PoC
-rw-r--r-- | parse.awk | 97 | ||||
-rw-r--r-- | render.awk | 115 |
2 files changed, 212 insertions, 0 deletions
diff --git a/parse.awk b/parse.awk new file mode 100644 index 0000000..fd6e3e7 --- /dev/null +++ b/parse.awk @@ -0,0 +1,97 @@ +BEGIN{ + lnum=0; + cnum=0; + type=""; + FS=":"; + # category is an array of category data + delete category; + # link is an array of link data + delete link; + + ### configurable variables + BASEDIR="./"; + ROOTFILE="./lawn/index.gph"; + ROOTSEL="/lawn/index.gph"; + HOST="localhost"; + PORT="1500"; +} + +function is_empty(a){ + for (i in a) + return 0; + return 1; +} + +function add_link(c){ + lnum+=1; + for (k in c){ + link[lnum,k]=c[k]; + } +} + +function add_category(c){ + cnum+=1; + for (k in c){ + category[cnum,k]=c[k]; + } +} + + +function get_cur(cur){ + #print "cur[Type]:", cur["Type"]; + switch (cur["Type"]) { + case "": { + print "empty type -- skipping record"; + return; + } + case "link": { + add_link(cur); + return; + } + case "category":{ + add_category(cur); + return; + } + default: { + printf("invalid type: %s -- skipping\n", cur["Type"]) ; + return; + } + } +} + + +/^[A-Z][-a-zA-Z]+:/{ + + #print "Field:",$1,"-","Value:",$2 + gsub(/^ +/,"",$2); + gsub(/ *$/,"",$2); + cur[$1]=$2; +} + +/^[[:blank:]]*$/{ + if (!is_empty(cur)){ + get_cur(cur); + delete cur; + } + else{ + print "cur is empty" > /dev/stderr + } +} + +END{ +## for (i=1;i<=cnum;i++){ +## printf("[1%s|%s|%s|%s]\r\n", category[i,"LinkName"], category[i,"Selector"], \ +## category[i,"Host"], category[i,"Port"]); +## } + + render_init(); + render_categories(category, cnum); + render_post_categories(category, cnum); + render_links(category, cnum, link, lnum); + render_finalise(category, cnum, link, lnum); + ##render_links(link, lnum, cfiles); + #for (k in cnames) + # print k, cnames[k]; + + +} diff --git a/render.awk b/render.awk new file mode 100644 index 0000000..db97f47 --- /dev/null +++ b/render.awk @@ -0,0 +1,115 @@ + +function info_line(name, sel, host, port, dst){ + printf("[i|%s|%s|%s|%s]\r\n", name, sel, host, port) > dst; +} + +function show_link(l, i, dst){ + + if(l[i,"Category"] == "") + print "Uncategorised link:", l[i,"Selector"] > /dev/stderr; + else{ + printf("[1|%s|%s|%s|%s]\r\n", l[i,"LinkName"], l[i,"Selector"], l[i,"Host"], l[i,"Port"] ) > dst; + info_line(l[i,"Description"],"Err", HOST, PORT, dst); + info_line("", "Err", HOST, PORT, dst); + } + +} + + +function show_category_link(category, i, dst){ + printf("[1|%s|%s|%s|%s]\r\n", category[i,"LinkName"], category[i,"Selector"], category[i,"Host"], category[i,"Port"]) > dst; +} + + + +function get_parents(a){ + delete pars; + split(a, pars, /[:blank:]*,[:blank:]*/); + for (k in pars){ + gsub(/ +/, "", pars[k]); + #printf("%d: %s\n", k, pars[k]); + } + +} + +function category_in_parents(category, i){ + + get_parents(category[i,"Parent"]); + for (p in pars){ + #printf("pars[%d]: %s\n", p, pars[p]); + if (pars[p]!="none"){### this is the root page + f = pars[p]; + ##print "cfiles[pars[p]]:", cfiles[f]; + show_category_link(category, i, cfiles[f]); + } + } +} + +function category_header(category, i, dst){ + info_line(category[i,"Title"], "Err", HOST, PORT, dst); + info_line("", "Err", HOST, PORT, dst); + info_line("Categories:", "Err", HOST, PORT, dst); +} + +function get_link_categories(a){ + delete cats; + split(a, cats, /[:blank:]*,[:blank:]*/); + for (k in cats){ + gsub(/ +/, "", cats[k]); + } +} + +######################################## + + +function render_init(){ + +} + + +function render_categories(category, cnum, link, lnum){ + for (i=1; i<=cnum; i++){ + cname=category[i,"Name"]; + #print "'" cname "'"; + cfiles[cname]=BASEDIR category[i,"Selector"]; + #print cname ":" cfiles[cname]; + cmd=sprintf("rm -f %s", cfiles[cname]); + system(cmd); + category_header(category, i, cfiles[cname]); + category_in_parents(category, i); + } +} + +function render_post_categories(category, cnum){ + + for(i=1;i<=cnum; i++){ + info_line("_______________", "Err", HOST, PORT, cfiles[category[i,"Name"]]); + info_line("Links:", "Err", HOST, PORT, cfiles[category[i,"Name"]]); + } + +} + + +function render_links(category, cnum, link, lnum){ + + for (i=1;i<=lnum;i++){ + get_link_categories(link[i,"Category"]); + for (c in cats){ + if (cfiles[cats[c]]!=""){ + show_link(link, i, cfiles[cats[c]]); + } + } + } +} + + +function render_finalise(category, cnum, link, lnum){ + + for(i=1;i<=cnum; i++){ + info_line("_______________", "Err", HOST, PORT, cfiles[category[i,"Name"]]); + printf("[1|%s|%s|%s|%s]\r\n", "Back to the lawn", ROOTSEL, HOST, PORT ) > cfiles[category[i,"Name"]]; + info_line("Links:", "Err", HOST, PORT, cfiles[category[i,"Name"]]); + } + + +} |