Source

smak / app.rc

Full commit
fn conf_enable_smak {
    if (~ $smak_extensions '')
        smak_extensions=(png jpg jpeg bmp gif)
    if (~ $smak_prev_width '')
        smak_prev_width=420
    if (~ $smak_thumb_size '')
        smak_thumb_height=96

    smak_base_uri=$conf_wd
    smak_root=`{pwd}

    conf_enable_app smak
}

fn smak_init {
    p=`{echo $req_path | sed 's!^'^$smak_base_uri^'!!'}
    smak_dir=`{basename -d $"p | sed 's!^\.$!!'}
    smak_img=`{basename $"p}
    smak_file=`{ls $smak_root/$"smak_dir/$"smak_img^.$smak_extensions \
        >[2]/dev/null | sed 1q}
    smak_dir_list=`{ls $smak_root/$"smak_dir/*.$smak_extensions \
        >[2]/dev/null | sed -e 's!.*/([^/]+)\.[^\.]+$!\1!' \
        | sort -n | uniq} # -un did not work, don't ask me why
    smak_comment=`{basename -d $"smak_file}^/_^$"smak_img^.md

    extraHeaders=($extraHeaders '<style>' `{cat \
        apps/smak/style.css} '</style>')

    if(test -f $smak_file)
        handler_body_main=(tpl_handler `{smak_template preview})
    if not if(~ $"smak_img '' && test -d $smak_root/$"smak_dir \
        && ! test -f $smak_root/$"smak_dir^/index.^(md htm html tpl) \
        && ! test -f $werc_root/$local_path)
        handler_body_main=(tpl_handler `{smak_template thumbnails})
}

fn smak_template {
    if(test -f $smak_root/_werc/smak/$1.tpl)
        echo -n $smak_root/_werc/smak/$1.tpl
    if not
        get_lib_file smak/$1.tpl apps/smak/$1.tpl
}

fn smak_resize {
    {png -t9 $3 || jpg -t9 $3 || bmp -t9 $3 || gif -t9 $3} \
        >[2]/dev/null | resample $1 $2 | topng > $4
}

fn smak_preview {
    p=_werc/smak/prev/
    f=$smak_root/$p^$"smak_dir/$"smak_img.png
    t=`{echo $"smak_img | sed 's/_/ /g'}

    mkdir -p `{basename -d $f}
    if(! test $f -nt $smak_file)
        smak_resize -x $smak_prev_width $smak_file $f

    echo '<a href="'`{basename $smak_file}'">' \
        '<img alt="'$"t'" title="'$"t'" ' \
        'src="'$smak_base_uri^$p^$"smak_dir/$"smak_img'.png" ' \
        'width='$smak_prev_width'px></a>'
}

fn smak_thumbnails {
    p=_werc/smak/thumb/
    d=$smak_root/$p^$"smak_dir

    mkdir -p $d
    for(i in $smak_dir_list){
        f=$d/$i.png
        o=`{ls $smak_root/$"smak_dir/$i^.$smak_extensions \
            >[2]/dev/null | sed 1q}
        t=`{echo $i | sed 's/_/ /g'}
        s=''
        if(~ $o $smak_file)
            s='class="thisPage" '

        if(! test $f -nt $o)
            smak_resize -y $smak_thumb_height $o $f

        echo '<a href="'$smak_base_uri^$"smak_dir/$i'">' \
            '<img alt="'$"t'" title="'$"t'" '$s \
            'src="'$smak_base_uri^$p^$"smak_dir/$i'.png" ' \
            'height='$1'px></a>'
    }
}

fn smak_preload {
    t=_werc/smak/prev/$"smak_dir/^`{basename $1}^.png
    o=`{ls $smak_root/$"smak_dir/^`{basename $1}^.$smak_extensions \
        >[2]/dev/null | sed 1q}
    f=$smak_root/$t

    mkdir -p `{basename -d $f}
    if(! test $f -nt $o)
        smak_resize -x $smak_prev_width $o $f

    echo '<img style="display:none;" src="'$smak_base_uri^$t'">'
}

fn smak_navigation {
   b=$smak_base_uri^_werc/smak/prev/$"smak_dir/
   p=''
   for(i in $smak_base_uri$"smak_dir/$smak_dir_list){
        if(~ $req_path $i){
            if(! ~ $"p '')
                echo '<a href="'$p'" title="prev">&larr;</a>'
            echo '<a href="." title="up">::</a>'
        }
        if(~ $req_path $"p){
            smak_next=$i
            echo '<a href="'$i'" title="next">&rarr;</a>'
        }
        p=$i
    }
}