src/page2html

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    
    #!/bin/sh
    
    usage()
    {
        cat <<EOF > /dev/stderr
    usage: page2html -url URL -ext EXT -type TYPE FILE
    
      If -url is not specified it is set to $BASE_URL (if defined) or to
      http://basilisk.fr
    
      The EXT extension (typically .html) is added to page links.
    
      TYPE is the file type, either "page" or the pandoc language hightlight type
      (as listed by 'pandoc --list-highlight-languages').
    EOF
        exit 1
    }
    
    ext=""
    while test $# -gt 0; do
        case $1 in
    	-url)
    	    shift
    	    BASE_URL="$1"
    	    ;;
    	-ext)
    	    shift
    	    ext="$1"
    	    ;;
    	-type)
    	    shift
    	    TYPE="$1"
    	    ;;
    	-*)
    	    usage
    	    ;;
    	*.[cm])
    	    page="$1";
    	    javascripts="<script src=\"/js/status.js\" type=\"text/javascript\"></script>";;
    	*)
    	    page="$1";
    	    ;;
        esac
        shift
    done
    
    if test -z "$page"; then
        usage
    fi
    
    if test ! -f $page; then
        echo "page2html: cannot access '$page'" >&2
        exit 1
    fi
    
    darcsroot()
    {
        d=`pwd`
        while ! test -d _darcs; do
    	cd ..
        done
        pwd
        cd "$d"
    }
    
    basename="$page"
    title=""
    if darcs show repo > /dev/null 2>&1; then
        ROOT=`darcsroot`
        DIR=`echo $PWD | sed -e "s|$ROOT||" -e "s|^/||"`
        if test -n "$DIR"; then
    	title="$DIR/"
        fi
    fi
    case $basename in
        /*)
    	basename=`basename $basename`
    	page=`basename $page`
    	url=$basename
    	title=`echo $url | sed 's/%20/ /g'`
    	;;
        *)
    	url=/$title$basename
    	title=$title`echo $basename | sed 's/%20/ /g'`
    	;;
    esac
    
    if test -z "$BASE_URL" -a -z "$DOCUMENT_ROOT"; then
        BASE_URL="http://basilisk.fr"
    fi
    if test -z "$CODEBLOCK_URL"; then
        CODEBLOCK_URL="$BASE_URL"
    fi
    
    cpreproc()
    {
        echo ~~~ {$2 .numberLines .lineAnchors}
        cat $1
        echo
        echo ~~~
    }
    
    cpostproc()
    {
        awk -v tags="$1.tags" -f $BASILISK/darcsit/decl_anchors.awk
    }
    
    pagepreproc()
    {
        $BASILISK/darcsit/literate-c $1 $2 | sed 's/~~~literatec/~~~c/g'
    }
    
    pagepostproc()
    {
        $BASILISK/darcsit/codeblock "$CODEBLOCK_URL" $1 $2
    }
    
    pagetitle()
    {
        awk '{
        	if ($1 == "---")
    	  inheader = 1;
    	else if (inheader && $1 == "title:") {
    	  sub("title: *", "");
    	  print $0;
              exit(0);
    	}
    	else if ($1 == "...")
    	  exit (0);
        }'
    }
    
    if test -n "$TYPE"; then
        case $TYPE in
    	page-magic)
    	    PREPROC="pagepreproc $basename 1"
    	    POSTPROC="pagepostproc $basename $ext"
    	    ;;
    	page)
    	    PREPROC="pagepreproc $basename 0"
    	    POSTPROC="pagepostproc $basename $ext"
    	    headertitle=`pagetitle < $page`
    	    if test -n "$headertitle"; then
    		title=$headertitle
    	    fi
    	    ;;
    	*)
    	    PREPROC="cpreproc $page .$TYPE"
    	    POSTPROC="cpostproc $page"
    	    ;;
        esac
    else
        case $page in
    	*.py | *.[chm])
    	    if $BASILISK/darcsit/pagemagic $page; then
    		PREPROC="pagepreproc $page 1"
    		POSTPROC="pagepostproc $page $ext"
    	    else
    		case $page in
    		    *.py)   PREPROC="cpreproc $page .python" ;;
    		    *.[ch]) PREPROC="cpreproc $page .c" ;;
    		    *.m)    PREPROC="cpreproc $page .octave" ;;
    		esac
    		POSTPROC="cpostproc $page"
    	    fi
    	    ;;
    	*)
    	    if $BASILISK/darcsit/pagemagic $page; then
    		PREPROC="pagepreproc $basename 1"
    	    else
    		PREPROC="pagepreproc $basename 0"
    	    fi
    	    POSTPROC="pagepostproc $basename $ext"
    	    headertitle=`pagetitle < $page`
    	    if test -n "$headertitle"; then
    		title=$headertitle
    	    fi
    	    ;;
        esac
    fi
    
    # Test whether pandoc supports the --smart option
    if echo "" | pandoc --smart > /dev/null 2>&1; then
        PANDOC="pandoc -f markdown --smart"
    else 
        # assumes it works like this (i.e. it is a recent version)
        PANDOC="pandoc -f markdown+smart"
    fi
    
    tabs="<li class=selected><a href=\"$url\">view</a></li>"
    if test -n "$USERS"; then
        tabs="$tabs<li><a href=\"/_edit$url\">edit</a></li>"
    fi
    tabs="$tabs<li><a href=\"$url?history\">history</a></li>"
    
    if test -n "$DOCUMENT_ROOT" -a -r "$DOCUMENT_ROOT/static/templates/page.static"; then
        template="$DOCUMENT_ROOT/static/templates/page.static"
    else
        template="$BASILISK/darcsit/templates/page.static"
    fi
    
    if test -z "WIKI_TITLE"; then
        WIKI_TITLE=Basilisk
    fi
    
    $PREPROC | $PANDOC -s --katex --toc --preserve-tabs            \
    	   -V wikititle="$WIKI_TITLE"				\
    	   -V base="$BASE_URL"				        \
    	   -V pageUrl="$url"			                \
    	   -V pagetitle="$title" 			        \
    	   -V wikiupload=true					\
    	   -V sitenav=true					\
    	   -V pagetools=true					\
    	   -V tabs="$tabs"                                      \
    	   -V javascripts="$javascripts"                        \
    	   -V users="$USERS"                                    \
    	   --template="$template"	                        \
        | sed 's/$/$/g'			        \
        | $POSTPROC