<?php

    // A simple search engine for HTML version of Vim manuals.
    // Author: David Blanchet <davidDOTblanchetATfreeDOTfr>
    // Last changes: 2005/03/12
    // Requires: Vim manual tag file (":helptags $VIMRUNTIME/doc").

    if (isset($_REQUEST['tag']) and strlen($_REQUEST['tag']) > 0) {
        $tag = $_REQUEST['tag'];

        $taglist = file_get_contents("tags");
        // tag file items structure : ^tag\tfile\t/*tag*$

        // Exact match.
        $pattern = "/\n" . addslashes(urldecode($tag)) . "\\t(.+)\\t\/\*/";
        if (preg_match($pattern, $taglist, $line)) {
            $URL = $line[1] . ".html#" . $tag;
            // Found it ! Go there immediatly.
            header ("Location: $URL");
        } else {
            // Partial match.
            $pattern = "/\n(.*?"
                    . addslashes(urldecode($tag))
                    . ".*?)\\t(.+)\\t\/\*/";
            if (preg_match_all ($pattern, $taglist, $matches)) {
                // Found partial matches, format them to HTML.
                $list = "";
                for ($i = 0; $i < count($matches[0]); $i++) {
                    // matches[0] gives the whole line,
                    // matches[1] gives the tag and
                    // matches[2] gives the manual filename.
                    $list = $list . "<li><a href=\"" . $matches[2][$i]
                            . ".html#" . $matches[1][$i] . "\">"
                            . $matches[1][$i] . "</a></li>";
                }
            }
            // No match !
            unset($line);
        }
    }
?>
<!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" lang="fr" xml:lang="fr">
    <head>
        <title>Manuels de Vim - Accueil</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
    </head>
    <body>
        <h1>Acc&egrave;s aux manuels Vim traduits</h1>
        <p>Cette page vous offre deux possibilit&eacute;s&nbsp;:</p>
        <ul>
            <li>Acc&eacute;der directement au
                <a href="help.txt.html">fichier principal de l'aide</a>.</li>
            <li>Rechercher un sujet pour y acc&eacute;der directement&nbsp;:
<?php
    // Give information about previous request.
    if (isset($tag) && !isset($line)) {
        // Asked tag not found, tell it.
        echo "<p>Le sujet '<b>", $tag,
                "</b>' n'a pas &eacute;t&eacute; trouv&eacute;";
        if (isset($list)) {
            // Partial matches found, show them.
            echo ", mais les sujets suivants contiennent '" . $tag
                    .  "'&nbsp;:</p> <ul>" . $list . "</ul>";
        } else {
            echo ".</p>";
        }
    }
?>
                <form method="get" action="./index.php">
                    <p><input type="text" 
                        value="<?php if (isset($tag)) { echo $tag; } ?>"
                        name="tag"/>
                    <input type="submit" value="Rechercher"/></p>
                </form> </li>
        </ul>
            <hr/>
    </body>
</html>