<?php

# oneliners2html.php
# ed <irc.rizon.net>, MIT-licensed
# https://ocv.me/doc/unix/oneliners/?dat=sauce

header('Content-Type: text/html; charset=UTF-8');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Sat, 09 Jan 2009 09:09:09 GMT');
date_default_timezone_set('UTC');

if (isset($_GET['dat']) && $_GET['dat']=='sauce')
    die(highlight_file('index.php') && '');

?>
<!DOCTYPE html><html lang="en"><head>
    <meta charset="utf-8">
    <title>oneliners</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=0.8">
    <style>
html, body, #wrap {
    color: #333;
    background: #f7f7f7;
    font-family: sans-serif;
}
* {
    line-height: 1.5em;
}
#wrap {
    margin: 2em auto;
    padding: 0 1em 3em 1em;
}
p>a {
    color: #fff;
    background: #079;
    border-radius: .2em;
    padding: .2em .4em;
    text-decoration: none;
}
p>a:hover,
p>a:active,
p>a:focus {
    color: #333;
    background: #fff;
    box-shadow: .05em .05em .4em #999 inset;
}
pre {
    font-family: monospace, monospace;
    white-space: pre-wrap;
    font-size: .9em;
    margin: 0;
    padding: .25em 0;
    min-height: 1.3em;
    line-height: 1.3em;
    border: 1px solid #f0f;
    border-color: #e9e9e9 transparent #fdfdfd transparent;
}
pre:hover {
    border-color: #ccc;
    background: #fff;
}
h1, h2 {
    white-space: pre-wrap;
    margin: .5em 0 0 0;
}
h1 {
    /* b94 db6 */
    background: #ec7;
    padding: 0 .4em;
    margin: 2em 0 1em 0;
    border-radius: .2em;
    border-bottom: .2em solid #b94;
    font-weight: normal;
    font-size: 2em;
    color: #642;
}
h2 {
    font-family: monospace, monospace;
    font-size: 1em;
    color: #693;
    border-bottom: .1em solid #9b7; /*bd9*/
}
h2 a {
    color: inherit;
    text-decoration: none;
    padding: 1.7em .6em 0 1.5em;
    margin-left: -1.5em;
}
#head {
    padding: .5em 2.5em;
    font-style: italic;
}
#head a {
    margin-left: .25em;
}
.cmt {
    color: #b94;
    font-style: italic;
}
#toc h6 {
    font-size: 1em;
    padding: .1em 1em 0 1em;
    margin: 1.5em 0 .3em 0;
    background: #fff;
    border-radius: .5em;
    box-shadow: .1em .1em .1em #bbb;
    font-weight: normal;
    color: #a80;
}
#toc a {
    color: #079;
    display: block;
    text-decoration: none;
    padding: .25em 1em;
    line-height: 1.2em;
}
#toc a:before {
    content: '';
    margin: 0 .3em 0 -.7em;
    border-left: .4em solid #9bc;
    border-top: .3em solid transparent;
    border-bottom: .3em solid transparent;
    height: 0;
    position: relative;
    display: inline-block;
}
#toc a:hover {
    color: #000;
    background: #fff;
}
#sh>pre::first-letter {
}
</style></head><body><div id="wrap">
<h1>mostly unorganized collection of bash oneliners</h1>
<div id="head">
    <p>this place is not a place of honor</p>
    <p>nix.sh, <a href="//ocv.me/doc/unix/oneliners/">ocv.me/doc/unix/oneliners/</a>
    &nbsp;anno <?php
        $t = filemtime('nix.sh');
        print date('Y-m-d, H:i:s', $t);
    ?></p>
    <p>ctrl-f something and you might get lucky (or use the TOC below)</p>
    <p>you may also be interested in the <a href="nix.sh">plaintext edition</a> or the <a href="?dat=sauce">php</a></p>
</div>
<?php

function esc($txt)
{
    return str_replace(
        array('&','<','>'),
        array('&amp;','&lt;','&gt;'), $txt);
}

function linkify($hit)
{
    return '<a href="'.$hit[0].'">'.$hit[0].'</a>';
}

function parse($print)
{
    $fh = fopen('nix.sh', 'rb');
    if (!$fh) die('EPERM');

    if (fgets($fh) === false) return;
    if (fgets($fh) === false) return;
    if (fgets($fh) === false) return;

    $toc = [];
    $cat = [];
    $buf = '';
    $was_blank = false;
    while (($ln = fgets($fh)) !== false)
    {
        $ln = esc(rtrim($ln, "\r\n"));

        if (substr($ln, 0, 2) === '##')
        {
            $buf .= ltrim(substr($ln, 2), ' ') . "\n";
            continue;
        }
        
        if (substr($ln, 0, 1) === '#' && $was_blank)
        {
            $crc = hash('crc32b', $ln);
            
            if ($print)
            {
                echo "\r\n<h2><a id=\"$crc\" href=\"#$crc\">$ln</a></h2>\r\n";
            }
            else
            {
                $cat[] = [$crc, ltrim(substr($ln, 1))];
            }
            
            $was_blank = false;
            continue;
        }
        
        if ($ln === '' && $buf !== '')
        {
            $buf = trim($buf, "\n");
            
            if ($print)
            {
                echo "\r\n<h1>" . $buf . "</h1>\r\n";
            }
            else
            {
                //$cat = [];
                //$toc[] = [$buf, &$cat];
                $toc[] = [$buf, []];
                $cat = &$toc[count($toc)-1][1];
            }
            
            $was_blank = true;
            $buf = '';
            continue;
        }
        
        $was_blank = ($ln === '');
        
        if (!$print)
            continue;

        // inline comments
        if (preg_match("/(.*)#([^'\"#]+)\$/", $ln, $m) === 1)
            $ln = $m[1] . '<span class="cmt">#' . $m[2] . '</span>';
        
        // full-line comments
        if (preg_match("/^( *#.*)\$/", $ln, $m) === 1)
            $ln = '<span class="cmt">' . $m[1] . '</span>';

        // links
        $ln = preg_replace_callback("/(https?:\/\/[^][\")^\\\\<> ]+)/", 'linkify', $ln);
        
        echo "<pre>" . $ln . "</pre>\r\n";
    }
    fclose($fh);
    return $toc;
}

$toc = parse(false);

echo <<<EOT
    <div id="toc">
    <h6>TOTOC (TOC categories)</h6>
EOT;
foreach ($toc as &$th)
{
    $ca = preg_replace('/[^a-zA-Z0-9]+/', '-', $th[0]);
    echo "<a href=\"#$ca\">${th[0]}</a>\n";
}

foreach ($toc as &$th)
{
    $ca = preg_replace('/[^a-zA-Z0-9]+/', '-', $th[0]);
    echo "<h6 id=\"$ca\">${th[0]}</h6>\n";
    
    foreach ($th[1] as &$te)
    {
        echo "<a href=\"#${te[0]}\">${te[1]}</a>\n";
    }
}

echo <<<EOT
    </div>
    <div id="sh">
EOT;

parse(true);

?>
</div></div></body></html>