<?php
function clean_section_name(string $name): string {
    return 
str_replace('`'''trim($name" \t\n\r\0\x0B#"));
}

function 
create_section_anchor(string $name): string {
    return 
preg_replace('#[^0-9a-zA-Z]#''-'strtolower($name));
}

$validColourSchemes = ['Light' => '''Dark' => 'dark''Mio' => 'mio'];

if(isset(
$_GET['c']) && is_string($_GET['c']) && in_array($_GET['c'], $validColourSchemes)) {
    
setcookie('scd_cs'$_GET['c'], strtotime('+1 year'), '/sockchat');
    
header('Location: ./');
    exit;
}

$colorScheme = isset($_COOKIE['scd_cs']) && in_array($_COOKIE['scd_cs'] ?? null$validColourSchemes) ? $_COOKIE['scd_cs'] : '';

require_once 
'parsedown.php';

$protInfo file_get_contents('Protocol-draft.md');
$protLines explode("\n"$protInfo);
$sections = [];

foreach(
$protLines as $line) {
    if(empty(
$line))
        continue;
    
    if(
$line[0] === '#') {
        
$depth 0;
        
        for(
$i 0;;$i++) {
            if(
$line[$i] === '#')
                
$depth++;
            else
                break;
        }

        
$name clean_section_name($line" \t\n\r\0\x0B#");

        if(
$depth 1) {
            
$anchor create_section_anchor($name);
            
$sections[] = [
                
'name' => $name,
                
'anchor' => $anchor,
                
'depth' => $depth,
            ];
        }
        continue;
    }
}
?>
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Sock Chat Documentation</title>
        <link href="./assets/style.css" rel="stylesheet">
    </head>
    <body<?php if(!empty($colorScheme)) { echo ' class="' $colorScheme '"'; } ?>>
        <div class="navigation">
            <a class="logo" href="#">
                Sock Chat Docs
            </a>
            <div class="options">
            <?php foreach($validColourSchemes as $name => $scheme): ?>
            <a href="?c=<?=$scheme;?>">[<?=$name;?>]</a> 
            <?php endforeach; ?>
            </div>
            <?php foreach($sections as $section): ?>
            <a href="#<?=$section['anchor'];?>" class="nav-link depth-<?=$section['depth'];?>">
                <?=$section['name'];?>
            </a>
            <?php endforeach; ?>
        </div>
        <div class="content">
            <div class="content-wrapper">
                <?php
                    $pd 
= new Parsedown;
                    echo 
$pd->text($protInfo);
                
?>
            </div>
            <div class="attribs">
                <div class="attrib">Sock Chat V1 was created by <a href="http://aroltd.com">MallocNull</a></div>
                <div class="attrib">Markdown parsing by a <a href="./src.php?f=parsedown.php">modified</a> version of <a href="https://github.com/erusev/parsedown">Parsedown</a></div>
                <div class="attrib">Maintained <a href="https://flash.moe">flashwave</a></div>
                <div class="attrib"><a href="./src.php?f=index.php">This page</a> is automatically generated using Protocol.md from the <a href="https://github.com/flashii/sharp-chat">Sharp Chat</a> repository.</div>
            </div>
        </div>
    </body>
</html>