summaryrefslogtreecommitdiff
path: root/search.php
blob: 12c1719c76d8fcfbb63dceaed089b9653eccaef6 (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
    /*
     * Copyright (C) 2024 dwlr <dweller@cabin.digital>
     *
     * BSD 3-Clause License (BSD-3-Clause)
     * See LICENSE for details
     */

    require 'common.php';
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>

        <title>Manup &emsp; &emsp; &emsp; &emsp; &emsp; &emsp; &emsp; &emsp; &emsp; &emsp; Lonely MAN near You</title>

        <link rel="icon" type="image/png"    sizes="128x128" href="imgs/logo-goog.png"/>
        <link rel="icon" type="image/png"    sizes="32x32"   href="imgs/logo-goog32.png"/>
        <link rel="icon" type="image/png"    sizes="16x16"   href="imgs/logo-goog16.png"/>
        <!--<link rel="icon" type="image/x-icon" sizes="16x16"   href="imgs/logo-goog16.ico"/>-->

        <link rel="stylesheet" href="style/normalize.css"/>
        <link rel="stylesheet" href="style/main.css"/>
        <link rel="stylesheet" href="style/search.css"/>
        <link rel="stylesheet" href="style/man.css"/>
        <noscript>
            <style>.jsonly{ display: none; }</style>
        </noscript>
    </head>
    <body>
        <div id="main">
            <header>
                <a id="logo" href="/">
                    <h1>
                        <span id="logo-let">L</span>
                        <span id="logo-me">m</span>
                        <code id="logo-man">m</code>
                        <span id="logo-that">t</span>
                        <span id="logo-for">F</span>
                        <span id="logo-you">y</span>
                    </h1>
                    <div id="logo-ver_wrap">
                        <span id="logo-ver">BETA!</span>
                    </div>
                </a>
                <nav id="search_pane">
                    <form>
                        <div id="query_wrap">
                            <input  id="query"   type='search' value='<?=$query;?>' name='query' autocorrect="off" autocapitalize="none" required/>
                            <button id="btn_def" type='submit' value='man' name='action' class="btn-def" aria-hidden="true" tabindex="-1"></button>
                            <input type="checkbox" id="query_opts_toggle"/>
                            <div id="query_opts">
                                <span class="query_opt" title="section">-s</span>
                                <select id="sect"  name='section' title="section" autocomplete="off" tabindex="-1">
                                    <option value=""></option>
                                    <?php
                                        for($i = 1; $i <= 9; $i++)
                                        {
                                            $sel = "";
                                            if((strlen($section) > 0) && ($section[0] == $i))
                                                $sel = 'selected="selected"';
                                            echo "<option value='$i' $sel>$i</option>";
                                        }
                                    ?>
                                </select>
                                <span class="query_opt" title="architecture">-S</span>
                                <select id="arch"  name='arch' title="architecture" autocomplete="off" tabindex="-1">
                                    <option value=""></option>
                                    <?php
                                        foreach($archs as $a)
                                        {
                                            $sel = "";
                                            if((strlen($arch) > 0) && ($arch == $a))
                                                $sel = 'selected="selected"';
                                            echo "<option value='$a' $sel>$a</option>";
                                        }
                                    ?>
                                </select>
                            </div>
                            <button id="btn_mank" type='submit' value='apropos' name='action' title="apropos(1)">-k</button>
                            <button id="btn_man"  type='submit' value='man'     name='action' title="Feeling lucky, punk?" class="btn-acc">
                                <div id="query_icon"></div>
                            </button>
                        </div>
                    </form>
                </nav>
                <nav id="topnav">
                    <a href="https://cabin.digital">Cabin</a>
                    <a href="#"><span id="topnav_icon"></span></a>
                    <button class="btn-acc">Man up</button>
                </nav>
            </header>
            <section id="results">
<?php
    $arg_section = "";
    if(!empty($section))
        $arg_section .= " -s ". escapeshellarg($section);
    if(!empty($arch))
        $arg_section .= " -S ". escapeshellarg($arch);

    $found_man = false;
    if($action === "man")
    {
        $res = exec("$manbin -M $manpath -T html -O fragment,toc $arg_section "
             . escapeshellarg($query), $lines, $ret);
        if($ret === 0)
        {
            /* TODO: maybe hardcoding a line isn't such a good idea, but I don't wanna regex the
             * whole thing */
            preg_match('/.+<span class="head-ltitle">.+\((.).*\)<\/span>/iu', $lines[1], $sect);

            $url = "$root/?query=". urlencode($query) ."&ampsection=". urlencode($sect[1])
                ."&arch=". urlencode($arch) ."&action=lmmtfy";

            /* HACK: fucking mandoc just generates tons of useless <br>s that make <code> stuff
             * look horrible. So I just remove it here.
             * As you can see, they also decided to pepper the HTML with inline style coz they
             * just absolutely hate me, so I strip it too.
             */
            for($i = 0; $i < count($lines); $i++)
            {
                $line = preg_replace('/\<br.\>/i', '', $lines[$i]);
                $line = preg_replace('/style=".*"/i', '', $line);
                if(!empty(trim($line))) $lines2[$i] = $line;
            }

            echo "<nav id='lmmtfy'><div><div>";
            echo " <label for='lmmtfy_url'>LmmtFy URL</label></div>";
            echo " <textarea id='lmmtfy_url' wrap='off' rows='1' readonly>".$url."</textarea>";
            echo " <button id='lmmtfy_copy' class='jsonly'>Copy</button></div></nav>";
            echo "<article class='manpage'>". implode(PHP_EOL, $lines2) ."</article>";
            echo "<script src='/js/copy.js'></script>";

            $found_man = true;
        }
    }

    if(($action === "apropos") || !$found_man)
    {
        $res = exec("$manbin -M $manpath $arg_section -k " . escapeshellarg($query), $lines, $ret);
        if($ret === 0)
        {
            $n = count($lines);
            $s = $n > 1 ? "s" : "";
            echo "<p class='hint'> Found $n result$s</p>";

            foreach($lines as $line)
            {
                preg_match('/^((.+)(?:,.+)*)\((.+)\)\s?-\s?(.*)$/Uu', $line, $matches);
                $fname = $matches[1];
                $name  = $matches[2];
                $fsect = $matches[3];
                $desc  = $matches[4];
                $sect  = explode(',', $fsect);
                $arch  = explode('/', end($sect));
                $sect  = trim($arch[0]);
                $arch  = trim(end($arch));

                $descl = strlen($desc);
                if($descl > 320) $desc = substr($desc, 0, 320) . "...";
                elseif($descl === 0) $desc = "No synopsis.";

                $url = "$root/search.php?query=". urlencode($name) ."&section=". urlencode($sect)
                       ."&arch=". urlencode($arch) ."&action=man";

                echo '<dl>';
                echo "  <dt><a href='".$url."'>$fname($fsect)</a></dt>";
                echo "  <dd>$desc</dd>";
                echo '</dl>';
            }
        }
        else echo "<p class='hint'> No results.</p>";
    }
?>
            </section>
            <footer>
                <p class="center">
                    Copyleft <span class="copyleft">&copy;</span> 2024
                    <a href="mailto:manup@cabin.digital">dweller</a> from
                    <a href="https://cabin.digital">cabin.digital</a>.
                    All Wrongs Reserved.
                </p>
            </footer>
        </div>
        <div id="js_tmp"></div>
    </body>
    <script src="js/query_opts.js"></script>
</html>