all repos — nasg @ e18582840e17598ab739402d4e8bbbc2dd808328

templates/Search.j2.php (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
{% extends "base.j2.html" %}
{% block lang %}{% endblock %}
{% block title %}Search results for: <?php echo($_GET['q']); ?>{% endblock %}
{% block content %}
<?php
function relurl($from, $to) {
    $from = explode('/', $from);
    $to = explode('/', $to);
    $relpath = '';

    $i = 0;
    while (isset($from[$i]) && isset($to[$i])) {
        if ($from[$i] != $to[$i]) break;
        $i++;
    }
    $j = count( $from ) - 1;
    while ( $i <= $j ) {
        if ( !empty($from[$j]) ) $relpath .= '../';
        $j--;
    }
    while ( isset($to[$i]) ) {
        if ( !empty($to[$i]) ) $relpath .= $to[$i].'/';
        $i++;
    }
    return substr($relpath, 0, -1);
}
?>
<?php const baseurl = '{{ site.url }}'; ?>
<main>
    <header>
        <h1>Search results for: <?php echo($_GET['q']); ?></h1>
    </header>
<?php
$db = new SQLite3('./search.sqlite', SQLITE3_OPEN_READONLY);
$q = str_replace('-', '+', $_GET['q']);
$sql = $db->prepare("
    SELECT
        url, category, title, snippet(data, '', '', '[...]', 5, 24)
    FROM
        data
    WHERE
        data MATCH :q
    ORDER BY
        category
");
$sql->bindValue(':q', $q);
$results = $sql->execute();

printf("<dl>");
while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
    printf('<dt><a href="%s">%s</a></dt><dd>%s</dd>', relurl(baseurl, $row['url']), $row['title'], $row["snippet(data, '', '', '[...]', 5, 24)"]);

}
printf("</dl>");
?>
</main>
{% endblock %}