site

My personal website.

generate (6296B)

     1 #!/bin/sh
     2 
     3 end=""
     4 preformatted=""
     5 output="$1"
     6 dir="$2"
     7 
     8 [ $output = "html" ] && extension="html"
     9 [ $output = "gopher" ] && extension="gph"
    10 
    11 program="$(printf "%s" '
    12 BEGIN {
    13     html = --HTML--;
    14     if (html)
    15         print "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<title>--TITLE--</title>\n<meta charset=\"utf-8\" />\n<link rel=\"icon\" href=\"/favicon.ico\" type=\"image/x-icon\" />\n<link rel=\"stylesheet\" href=\"/style.css\" type=\"text/css\" />\n</head>\n<body>";
    16 }
    17 END {
    18     if (html) {
    19         if (list || link_list)
    20             print "</ul>";
    21         print "</body>\n</html>";
    22     }
    23 }
    24 function wrap(text,   q, y, z) {
    25     while (text) {
    26         q = match(text, / |$/);
    27         y += q
    28         if (y > 72) {
    29             if (html)
    30                 z = z "\n";
    31             else
    32                 z = z "\nt";
    33             y = q - 1;
    34         } else if (z) {
    35             z = z FS;
    36         }
    37         z = z substr(text, 1, q - 1);
    38         text = substr(text, q + 1);
    39     }
    40     return z;
    41 }
    42 function join(array, start, end, sep,    result, i) {
    43     if (sep == "")
    44        sep = " ";
    45     else if (sep == SUBSEP) # magic value
    46        sep = "";
    47     result = array[start];
    48     for (i = start + 1; i <= end; i++)
    49         result = result sep array[i];
    50     return result;
    51 }
    52 /^=>/ && !preformatted {
    53     if (html && list) {
    54         print "</ul>";
    55         list = 0;
    56     }
    57     $0 = substr($0, 3, length($0));
    58     sub(/^ */, "");
    59     url = $1;
    60     text = substr($0, length($1) + 1, length($0));
    61     sub(/^ */, "", text);
    62     if (text == "")
    63         text = url;
    64     if (html) {
    65         if (!link_list) {
    66             link_list = 1;
    67             print "<ul class=\"link-list\">";
    68         }
    69         sub(/\.gmi$/, ".html", url);
    70         print "<li><a href=\"" url "\">" text "</a></li>";
    71     } else {
    72         split(url, segments, "/");
    73         host = segments[3];
    74         if (match(url, /^gopher:\/\//)) {
    75             itemtype = "1";
    76             for (i = 1; i in segments; i++);
    77             path = "/" join(segments, 4, i - 1, "/");
    78             port = "70";
    79         } else if (match(url, /^http:\/\//)) {
    80             itemtype = "h";
    81             path = "URL:" url;
    82             port = "80";
    83         } else if (match(url, /^https:\/\//)) {
    84             itemtype = "h";
    85             path = "URL:" url;
    86             port = "443";
    87         } else if (match(url, /^gemini:\/\//)) {
    88             itemtype = "h";
    89             path = "URL:" url;
    90             port = "1965";
    91         } else if (match(url, /\.gmi$/)) {
    92             host = "milotier.net";
    93             itemtype = "1";
    94             path = url;
    95             sub(/\.gmi$/, ".gph", path);
    96             port = "70";
    97         } else if (match(url, /(\.jpg|\.jpeg|\.png)$/)) {
    98             host = "milotier.net";
    99             itemtype = "I";
   100             path = url;
   101             port = "70";
   102         } else {
   103             host = "milotier.net";
   104             itemtype = "0";
   105             path = url;
   106             port = "70";
   107         }
   108         print "[" itemtype "|" text "|" path "|" host "|" port "]";
   109     }
   110     next;
   111 }
   112 html {
   113     gsub(/&/, "\\&amp;");
   114     gsub(/</, "\\&lt;");
   115     gsub(/>/, "\\&gt;");
   116 }
   117 /^```/ {
   118     preformatted = !preformatted;
   119     if (html) {
   120         if (preformatted) {
   121             if (list) {
   122                 print "</ul>";
   123                 list = 0;
   124             } else if (link_list) {
   125                 print "</ul>";
   126                 link_list = 0;
   127             }
   128             printf "<pre><code>";
   129         } else {
   130             print "</code></pre>";
   131         }
   132     }
   133     next;
   134 }
   135 preformatted {
   136     if (html)
   137         print $0;
   138     else
   139         print "t  " $0;
   140     next;
   141 }
   142 /^# / {
   143     if (html) {
   144         if (list) {
   145             print "</ul>";
   146             list = 0;
   147         } else if (link_list) {
   148             print "</ul>";
   149             link_list = 0;
   150         }
   151     }
   152     $1 = "";
   153     sub(/^ /, "");
   154     if (html)
   155         print "<h1>" $0 "</h1>";
   156     else
   157         print "t# " $0;
   158     next;
   159 }
   160 /^## / {
   161     if (html) {
   162         if (list) {
   163             print "</ul>";
   164             list = 0;
   165         } else if (link_list) {
   166             print "</ul>";
   167             link_list = 0;
   168         }
   169     }
   170     $1 = "";
   171     sub(/^ /, "");
   172     if (html)
   173         print "<h2>" $0 "</h2>";
   174     else
   175         print "t## " $0;
   176     next;
   177 }
   178 /^### / {
   179     if (html) {
   180         if (list) {
   181             print "</ul>";
   182             list = 0;
   183         } else if (link_list) {
   184             print "</ul>";
   185             link_list = 0;
   186         }
   187     }
   188     $1 = "";
   189     sub(/^ /, "");
   190     if (html)
   191         print "<h3>" $0 "</h3>";
   192     else
   193         print "t### " $0;
   194     next;
   195 }
   196 /^> / {
   197     if (html) {
   198         if (list) {
   199             print "</ul>";
   200             list = 0;
   201         } else if (link_list) {
   202             print "</ul>";
   203             link_list = 0;
   204         }
   205     }
   206     $1 = "";
   207     if (html)
   208         print "<blockquote>" $0 "</blockquote>";
   209     else
   210         print "t    " $0;
   211     next;
   212 }
   213 /^\* / {
   214     if (html) {
   215         if (link_list) {
   216             print "</ul>";
   217             link_list = 0;
   218         }
   219         list = 1;
   220         print "<ul>";
   221     }
   222     $1 = "";
   223     print;
   224     next;
   225 }
   226 {
   227     if (html) {
   228         if (list) {
   229             print "</ul>";
   230             list = 0;
   231         } else if (link_list) {
   232             print "</ul>";
   233             link_list = 0;
   234         }
   235         # if ($0 == "")
   236         #     print "<br />";
   237         # else
   238         if ($0 != "")
   239             print "<p>" $0 "</p>";
   240     } else {
   241         $0 = wrap($0);
   242         print "t" $0;
   243     }
   244 }
   245 ' | sed "s/--HTML--/$([ $output = "html" ] && printf 1 || printf 0)/")"
   246 
   247 escape() {
   248     [ $output = "html" ] && printf "%s" "$1" | sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
   249     [ $output = "gopher" ] && printf "%s" "$1"
   250 }
   251 
   252 mkdir -p "$dir"
   253 cp -rf ./* "$dir"
   254 files="$(find "$dir" -name '*.gmi' -type f)"
   255 
   256 while read -r file <&4; do
   257     title="$(grep "^# " "$file" | cut -c 3-)"
   258     awk "$(printf "%s" "$program" | sed "s|--TITLE--|$title|")" "$file" > "$(printf "%s" "$file" | sed "s/\.gmi$/.$extension/")"
   259 done 4<< EOF
   260 $files
   261 EOF
   262 
   263 links="$(find "$dir" -name '*.gmi' -type l)"
   264 while read -r link <&4; do
   265     [ -z "$link" ] && continue;
   266     path="$(readlink "$link")"
   267     unlink "$link"
   268     ln -sf "$(printf "%s" "$path" | sed "s/\.gmi$/.$extension/")" "$(printf "%s" "$link" | sed "s/\.gmi$/.$extension/")"
   269 done 4<< EOF
   270 $links
   271 EOF
   272 
   273 rm -f $files