shithub: werc

ref: e48d20254f76737de1cd75c32e2fd2efc64fb26c
dir: /bin/controller.rc/

View raw version
#!/usr/local/plan9/bin/rc

path=(. ./bin $PLAN9/bin /bin/ /usr/bin)
ifs='/' { args = `{ echo -n $REQUEST_URI | sed -e 's/[^a-zA-Z_\-\/]//g' -e 's/\?.*//' } } 
args=`{echo $args | tr -d '
'} 
cd ..


# config
body=index
siteTitle=''
siteSubTitle=''
title=Title
template=default
sidebar=sidebar

. etc/initrc

if (! ~ $#args 0 && ! ~ $args '') {
    title=$args($#args)
    title=`{echo $title | sed 's/_/ /' }
    body=`{ echo -n $"args |sed 's, ,/,g' }
}

l=tpl
for ( i in $args ) {
    l = $l'/'$i
    if ( test -f $l/_config ) {
        . $l/_config
    }
} 

template=tpl/$template.tpl
if (! ~ $sidebar 0) { sidebar=tpl/_inc/$sidebar.tpl }
if (test -d tpl/$body) {
    body=$body/index
}
body=`{echo tpl/^$"body^.md | sed 's, ,/,' }


# Sidebar 
fn menu {
    ls -F $1 | sed -e 's,^./,,' -e 's,\.md$,,' | grep -v '^_'| awk '
    BEGIN { print "<ul class=\"sidebar\">" }
    END { print "</ul>" }
    /^([a-zA-Z0-9_\-]+[\/*]?)+$/ && ! /index$/ {
        isdir = match($0, "/$")
        sub("[*/]$", "") # The '*' makes no sense to me
        
        if(isdir)
            d = "/"
        bname = $0
        sub("^(.*/)?([0-9]+_)?", "", bname)
        gsub("_", " ", bname)

        bname = bname d

        if(index(ENVIRON["REQUEST_URI"], "/" $0) == 1) {
            if(isdir) {
                print "<li><a href=\"/" $0 d "\">&raquo;<i> " bname "</i></a>"
                system("rc -c ''menu " $0 "''")
            } else {
                print "<li><a href=\"/" $0 d "\" class=\"thisPage\">&raquo;<i> " bname "</i></a>"
            }
        } else 
            print "<li><a href=\"/" $0 d "\">&rsaquo; " bname "</a>"

        print "</li>"

    }'

}

fn gensidebar {
    echo '<p class="sideBarTitle">Considered harmful:</p>'
    cd tpl
    menu .
    cd ..
}


# Body

template.awk $template | rc 


# Debug junk
#echo '<pre>'
#env