<?php

// Return commands array(
//   [0] => array(
//     [type] => 0
//     [commands] => <commands_str>
//     [syntax] => <syntax_str>
//     [description] => <description_text>
//   ),
//   [1] => ...
// )
function get_commands($file)
{
    
$commands = array();


    while (!
feof($file)) {
        
$line fgets($file); // next line

        // skip comment
        
if (preg_match('/^\s*#/'$line))
            continue;

        
start_cmd_parser:
        
// start cmd
        
if (preg_match('/^\|(?<type>\d+)\|\|(?<commands>.*)$/'$line$matches)) {
            
$command = array();

            
$command["type"] = $matches["type"];
            
$command["commands"] = split("\|"$matches["commands"]);

            
// Syntax
            
$line fgets($file); // next line
            
if (preg_match('/^Синтаксис:(.*)$/'$line$matches)) {
                
$command["syntax"] = trim($matches[1]);
                
$line fgets($file);
            }

            
// Description
            
$cmd_end false// end current command?
            
$descr "";
            while (
$cmd_end != true and !feof($file)) {

                if (
preg_match('/^\s*$/'$line) or preg_match('/^\|/'$line)) {
                    
// this is end of command
                    
$cmd_end true;
                } else {
                    
// append
                    
$descr .= $line;
                    
$line fgets($file);
                }
            }
            
$command["description"] = $descr;
            
$commands[] = $command;

            
// if current line is command start
            
if (preg_match('/^\|/'$line)) {
                
// skip "get next line"
                // FIXME: if possible
                
goto start_cmd_parser;
            }
        }
    }

    return 
$commands;
}

$file fopen("commands.txt""r") or die("Commands file not found.\n");
$commands get_commands($file);

// [b] -> <b> and [u] -> <u>
for ($i 0$i sizeof($commands); $i++) {
    if (isset(
$commands[$i]["syntax"])) {
        
$commands[$i]["syntax"] = preg_replace('/\[b\](.*?)\[b\]/''<b>${1}</b>'$commands[$i]["syntax"]);
        
$commands[$i]["syntax"] = preg_replace('/\[u\](.*?)\[u\]/''<u>${1}</u>'$commands[$i]["syntax"]);
    }
    
$commands[$i]["description"] = preg_replace('/\[b\](.*?)\[b\]/''<b>${1}</b>'$commands[$i]["description"]);
    
$commands[$i]["description"] = preg_replace('/\[u\](.*?)\[u\]/''<u>${1}</u>'$commands[$i]["description"]);
}

?>
<!DOCTYPE HTML>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <title>Команды бота для канала #linux@RusNet</title>
    <link rel='stylesheet' href='/style.css' type='text/css'>
</head>
<body>
    <h1 align="center">Команды бота для канала #linux@RusNet</h1>

<?php foreach ($commands as $cmd) : ?>
    <div class="quote">
        <h2>
<?php foreach ($cmd["commands"] as $c) : ?>
        <?=$c?>
<?php 
endforeach; ?>
        </h2>
<?php if (isset($cmd["syntax"])) : ?>
        <p class="syntax"><?=$cmd["syntax"]?></p>
<?php endif; ?>
        <p class="description">
            <?=$cmd["description"]?>
        </p>
    </div>
<?php endforeach; ?>
    <hr>
    <p align='center'>
        Bot commands v. 1.1 <a href='http://gordio.pp.ua/'>Gordio</a>. 2013
    </p>
</body>
</html>