<?php //~Public domain
/*
    // DEFAULTS
    $USE_CACHE    = false;
    $CREATE_CACHE = false;

    // GO AWAY FROM DEFAULTS?


    // WHAT TO DO WITH PROGRAM DIRECTIONS
    if ($CREATE_CACHE)
        ob_start();
*/
    // FUNCTIONS
    function get_html_tag_content($html_string, $tag) {
        /* Finds the content of the first tag if present */
        $tag_s = "<$tag>";
        $tag_e = "</$tag>";
        $tag_s_pos = strpos($html_string, $tag_s);
        $tag_e_pos = strpos($html_string, $tag_e);
        if ($tag_s_pos === false || $tag_e_pos == false || $tag_e_pos < $tag_s_pos)
            return "";

        $start = $tag_s_pos + strlen($tag_s);
        $end = $tag_e_pos;
        $title = substr($html_string, $start, $end - $start);
        return $title;
    }

    // THE ACTUAL WEBSITE CODE
    $pages = array();
    $grouped_pages = array();

    $scanned_dir = scandir('.');
    foreach ($scanned_dir as $filename) {
        if (
         is_file($filename) &&
         !in_array($filename, array("index.php", ".", "..")) &&
         preg_match("/^[^\.]+\.(htm|html|xhtml|php|py)$/", $filename) == 1
        ) {
            $content = file_get_contents($filename);
            $title = get_html_tag_content($content, "title");
            array_push($pages, array($title, $filename));
        }
    }

    foreach ($pages as $page) {
        $title = $page[0];
        $filename = $page[1];

        $group = "General";

        if ($title == "") {
            $group = "Untitled";
            $title = $filename;
        }
        else if (preg_match("/^[^\{\}]+\{.+\}\s*$/", $title) == 1) {
            $spl = explode("{", $title);
            $group = $spl[0];
            $title = $spl[1];
            $group = trim($group);
            $title = trim(substr($title, 0, strlen($title) - 1));
        }

        if (!array_key_exists($group, $grouped_pages))
            $grouped_pages[$group] = array();
        array_push($grouped_pages[$group], array($title, $filename));
    }
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns='http://www.w3.org/1999/xhtml'>
    <head>
        <title>nohiX {L@B}</title>
        <style type='text/css'>
            body {
                margin: 10px;
                font-family: 'dejavu serif', serif;
                font-size: 16px;
                font-weight: normal;
                color #000;
                background: #fff;
            }

            div {
                width: 465px;
            }

            h1 {
                font-size: 32px;
                display: none;
            }

            h2 {
                font-size: 26px;
            }

            h3 {
                font-size: 20px;
            }
        </style>
    </head>

    <body>
    <div>
        <img src="media/nohix-logo.png" alt="nohiX {L@B}" />
        <h1>nohiX</h1>
        <?php
            $slogans = file("data/slogans");
            $num = mt_rand(0, sizeof($slogans)-1);
            $slogan = $slogans[$num];
            echo "<p style='font-style: italic; margin-left: 20px;'>$slogan</p>\n";
        ?>

        <h2>Experiments</h2>
        <ul>
        <?php
            foreach ($grouped_pages as $group=>$pages) {
                echo "
            <li>
                <h3>$group</h3>
                <ul>";
                foreach ($pages as $page) {
                    $title = $page[0];
                    $filename = $page[1];
                    echo "
                    <li>
                        <p><a href='$filename'>$title</a></p>
                    </li>";
                }
                echo "
                </ul>
            </li>
";
            }
        ?>
        </ul>

        <h2>About</h2>
        <ul>
            <li><p>This website is <a href="http://metanohi.org/about/niels"><em>Niels</em></a>' playground.</p></li>
            <li><p>It contains mainly JavaScript tests.</p></li>
            <li><p><em>Niels</em> is not that fond of JavaScript, but he can't help but play with it. He thinks it's a disease.</p></li>
            <li><p>The logo (<a href="media/nohix-logo.svg">also available in SVG<a/>) is licensed under the <a href="http://creativecommons.org/licenses/by-sa/3.0/">Attribution-Share Alike 3.0 Unported license</a>. Niels is the author.</p></li>
            <li><p>The PHP source code is available <a href="http://metanohi.org/:nohix/index.php">here<a/>.</p></li>
            <li><p>You might not also want to check out <a href="http://projects.metanohi.name/x/">this<a/>, which was the inspiration for nohiX.</p></li>
        </ul>

    <div>
    </body>
</html><?php
/*  if ($CREATE_CACHE) {
        $content = ob_get_contents();
        ob_end_clean();
        file_put_contents(".cache", $content);
        echo $content;
    }*/
?>
