File: help_chrome.lua

package info (click to toggle)
luakit 1%3A2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,844 kB
  • sloc: ansic: 12,519; makefile: 140; ruby: 79; sh: 48
file content (351 lines) | stat: -rw-r--r-- 13,965 bytes parent folder | download | duplicates (2)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
--- Provides luakit://help/ page.
--
-- This module provides the <luakit://help/> page and all of its sub-pages,
-- including the built-in documentation browser.
--
-- @module help_chrome
-- @copyright 2016 Aidan Holm <aidanholm@gmail.com>
-- @copyright 2012 Mason Larobina <mason.larobina@gmail.com>

local lousy = require("lousy")
local chrome = require("chrome")
local history = require("history")
local add_cmds = require("modes").add_cmds
local error_page = require("error_page")
local get_modes = require("modes").get_modes
local markdown = require("markdown")

local _M = {}

local index_html_template = [==[
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Luakit Help</title>
    <style type="text/css">{style}
    </style>
</head>
<body>
    <header id="page-header">
        <h1>Luakit Help</h1>
        <div class="rhs">version {version} / webkit {webkitversion}</div>
    </header>
    <div class=content-margin>
        <h2>About Luakit</h2>
            <p>Luakit is a highly configurable, browser framework based on the <a
            href="http://webkit.org/" target="_blank">WebKit</a> web content engine and the <a
            href="http://gtk.org/" target="_blank">GTK+</a> toolkit. It is very fast, extensible with <a
            href="http://lua.org/" target="_blank">Lua</a> and licensed under the <a
            href="https://raw.github.com/luakit/luakit/develop/COPYING.GPLv3" target="_blank">GNU GPLv3</a>
            license.  It is primarily targeted at power users, developers and any people with too much time
            on their hands who want to have fine-grained control over their web browser&rsquo;s behaviour and
            interface.</p>
            <p>
            Useful (though outdated) documentation can be found here:
            <ul>
            <li><a href="doc/pages/01-authors.html">Authors</a></li>
            <li><a href="doc/pages/02-faq.html">Frequently Asked Questions</a></li>
            <li><a href="doc/pages/03-quick-start-guide.html">Quick-start Guide</a></li>
            <li><a href="doc/pages/04-migration-guide.html">Migration Guide</a></li>
            <li><a href="doc/pages/05-configuration.html">Files and Directories</a></li>
            <li><a href="doc/pages/06-tests.html">Running the Test Suite</a></li>
            <li><a href="doc/pages/07-build-debian-package.html">Building a Debian Package</a></li>
            </ul>
            </p>
        <h2>Configuration</h2>
        <h3>Settings</h3>
        <p>The available settings are displayed at:</p>
        <ul>
            <li><a href="luakit://settings/">Settings</a></li>
        </ul>
        <h3>Key bindings</h3>
        <p>Currently active bindings are listed in the following page.</p>
        <ul>
            <li><a href="luakit://binds/">Bindings</a></li>
        </ul>
        {chromepageshtml}
        <h2>API Documentation</h2>
        <ul>
            <li><a href="luakit://help/doc/index.html">API Index</a></li>
        </ul>
        <h2>Questions, Bugs, and Contributions</h2>

        <p>Please report any bugs or issues you find at the GitHub
        <a href="https://github.com/luakit/luakit/issues" target="_blank">issue tracker</a>.</p>
        <p>If you have any feature requests or questions, feel free to open an
        issue for those as well. Pull requests and patches are both welcome,
        and there are plenty of areas that could be improved, especially tests
        and documentation.</p>

        <h2>License</h2>
        <p>Luakit is licensed under the GNU General Public License version 3 or later.
        The abbreviated text of the license is as follows:</p>
        <div class=license>
            <p>This program is free software: you can redistribute it and/or modify
            it under the terms of the GNU General Public License as published by
            the Free Software Foundation, either version 3 of the License, or
            (at your option) any later version.</p>

            <p>This program is distributed in the hope that it will be useful,
            but WITHOUT ANY WARRANTY; without even the implied warranty of
            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
            GNU General Public License for more details.</p>

            <p>You should have received a copy of the GNU General Public License
            along with this program.  If not, see
            <a href="https://www.gnu.org/licenses/">https://www.gnu.org/licenses/</a>.</p>
        </div>
    </div>
</body>
]==]

local gen_html_chrome_pages = function()
    local links = ""
    for _, v in ipairs(chrome.available_handlers()) do
        links = links .. "<li><a href=\"luakit://" .. v .. "\">" .. v .. "</a></li>\n"
    end
    return [==[
        <h3>luakit:// pages</h3>
        <p>These are all the available <code>luakit://</code> pages:</p>
        <ul>
]==] .. links .. "</ul>"
end

local help_index_page = function ()
    local html_subs = {
        style = chrome.stylesheet,
        version = luakit.version,
        webkitversion = luakit.webkit_version,
        chromepageshtml = gen_html_chrome_pages(),
    }
    local html = string.gsub(index_html_template, "{(%w+)}", html_subs)
    return html
end

local builtin_module_set = {
    extension = true,
    ipc = true,
    luakit = true,
    msg = true,
    soup = true,
    utf8 = true,
}

local help_doc_index_page_preprocess = function (inner, style)
    -- Mark each list with the section heading just above it
    inner = inner:gsub("<h2>(%S+)</h2>%s*<ul>", "<h2>%1</h2><ul class=%1>")
    -- Customize each module link bullet
    inner = inner:gsub('<li><a href="modules/(%S+).html">', function (pkg)
        local class = package.loaded[pkg] and "enabled" or "disabled"
        if builtin_module_set[pkg] then class = "builtin" end
        return '<li class=' .. class .. '><a title="' .. pkg .. ": " .. class .. '" href="modules/' .. pkg .. '.html">'
    end)
    style = style .. [===[
        div#wrap { padding-top: 0; }
        h2 { margin: 1em 0 0.75em; }
        h2 + ul { margin: 0.5em 0; }
        ul {
            display: flex;
            flex-wrap: wrap;
            padding-left: 1rem;
            list-style-type: none;
        }
        ul > li {
            flex: 1 0 14rem;
            padding: 0.2em 0.2rem 0.2rem 1.5rem;
            margin: 0px !important;
            position: relative;
        }
        ul > li:not(.dummy):before {
            font-weight: bold;
            width: 1.5rem;
            text-align: center;
            left: 0;
            position: absolute;
        }
        ul > li:not(.dummy):before { content: "●"; transform: translate(1px, -1px); z-index: 0; }
        ul.Modules > li.enabled:before { content: "\2713 "; color: darkgreen; }
        ul.Modules > li.disabled:before { content: "\2717 "; color: darkred; }
        ul.Modules > li.enabled:before, ul.Modules > li.disabled:before {
            transform: none;
        }
        #page-header { z-index: 100; }
    ]===]
    return inner, style
end

local help_doc_page = function (v, path, request)
    -- Generate HTML documenting the additional bindings added by module `m`
    local generate_mode_doc_html = function (m)
        local fmt = function (str)
            -- Fix < and > being escaped inside code -_- fail
            return markdown(str):gsub("<pre><code>(.-)</code></pre>", lousy.util.unescape)
        end
        local bind_to_html = function (b)
            b = lousy.bind.bind_to_string(b) or "???"
            if b:match("^:.") then
                local cmds = {}
                for _, c in ipairs(lousy.util.string.split(b, ", ")) do
                    c = lousy.util.escape(c)
                    table.insert(cmds, ("<li><span class=cmd>%s</span>"):format(c))
                end
                return "<ul class=triggers>" .. table.concat(cmds, "") .. "</ul>"
            elseif b:match("^^.") then
                b = ("<span class=buf>%s</span>"):format(lousy.util.escape(b))
            else
                b = ("<kbd>%s</kbd>"):format(lousy.util.escape(b))
            end
            return "<ul class=triggers><li>" .. b .. "</ul>"
        end
        local modes, parts = get_modes(), {}
        for name, mode in pairs(modes) do
            local binds = {}
            for _, bm in pairs(mode.binds or {}) do
                local _, a = unpack(bm)
                local src_m = debug.getinfo(a.func, "S").source:match("lib/(.*)%.lua")
                if src_m == m then binds[#binds+1] = bm end
            end
            if #binds > 0 then
                parts[#parts+1] = string.format("<h3><code>%s</code> mode</h3>", name)
                parts[#parts+1] = "<ul class=binds>\n"
                for _, bm in ipairs(binds) do
                    local b, a = unpack(bm)
                    local b_desc = a.desc or "<i>No description</i>"
                    b_desc = fmt(lousy.util.string.dedent(b_desc)):gsub("</?p>", "", 2)
                    parts[#parts+1] = "<li><div class=two-col>" .. bind_to_html(b)
                    parts[#parts+1] = "<div class=desc>" .. b_desc .. "</div></div>"
                end
                parts[#parts+1] = "</ul>"
            end
        end
        return #parts > 0 and "<h2>Binds and Modes</h2>" .. table.concat(parts, "") or ""
    end

    local extract_doc_html = function (file)
        local prefix = luakit.dev_paths and "doc/apidocs/" or (luakit.install_paths.doc_dir .. "/")
        local ok, blob = pcall(lousy.load, prefix .. file)
        if not ok then return nil, prefix .. file end
        local style = blob:match("<style>(.*)</style>")
        local inner = blob:match("(<div id=wrap>.*</div>)%s*</body>")
        if file == "index.html" then
            inner, style = help_doc_index_page_preprocess(inner, style)
        else
            style = style .. [===[
                #wrap { padding: 1rem; }
                header#page-header { position: static; }
                div.content-margin { padding: 0; }

                .status_indicator {
                    position: absolute;
                    top: 0;
                    right: 0;
                    border-radius: .3125em;
                    padding: .3em 0.5em;
                    -webkit-user-select: none;
                    cursor: default;
                    line-height: 1.1rem;
                    font-weight: bold;
                }
                .status_indicator.active {
                    border: 2px solid #008800;
                    color: #008800;
                }
                .status_indicator.inactive {
                    border: 2px solid #880000;
                    color: #880000;
                }
                .status_indicator.builtin {
                    border: 2px solid #444444;
                    color: #444444;
                }
                .status_indicator.active::before { content: "✓ "; }
                .status_indicator.inactive::before { content: "✗ "; }
                .status_indicator.builtin::before { content: "● "; vertical-align: top; line-height: 1.2; }
            ]===]
        end
        local m = file:match("^modules/(.*)%.html$")
        if m then
            local modes_binds_html = generate_mode_doc_html(m)
            local i = inner:find("<!-- modes and binds -->", 1, true)
            inner = inner:sub(1, i-1) .. modes_binds_html .. inner:sub(i)

            local m_status = package.loaded[m] and "active" or "inactive"
            if builtin_module_set[m] then m_status = "builtin" end
            local tooltip = ({
                active = "This module is active and currently running.",
                inactive = "This module is inactive.",
                builtin =  "This module is builtin.",
            })[m_status]
            local status_indicator_html = ([==[
                <span class="status_indicator %s" title="%s">%s</span>
            ]==]):format(m_status, tooltip, m_status:gsub("^%l", string.upper))
            i = inner:find("<!-- status indicator -->", 1, true)
            inner = inner:sub(1, i-1) .. status_indicator_html .. inner:sub(i)
        end
        return inner, style
    end

    local doc_html_template = [==[
    <!doctype html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>Luakit API Documentation</title>
        <style type="text/css">
        {style}
        </style>
    </head>
    <body>
        <header id="page-header">
            <h1>Luakit API Documentation</h1>
        </header>
        <div class="content-margin">
        {doc_html}
        </div>
    </body>
    ]==]

    local doc_html, doc_style = extract_doc_html(path:gsub("[?#].*", ""))
    if not doc_html then
        local file = doc_style
        error_page.show_error_page(v, {
            heading = "Documentation not found",
            content = "Opening <code>" .. file .. "</code> failed",
            buttons = { path ~= "index.html" and {
                label = "Return to API Index",
                callback = function (vv) vv.uri = "luakit://help/doc/index.html" end
            } or nil },
            request = request,
        })
        return
    end
    local html_subs = {
        style = chrome.stylesheet .. doc_style,
        doc_html = doc_html,
    }
    local html = string.gsub(doc_html_template, "{([%w_]+)}", html_subs)
    return html
end

chrome.add("help", function (v, meta)
    if meta.path:match("^/?$") then
        return help_index_page()
    elseif meta.path:match("^doc/?") then
        return help_doc_page(v, ({meta.path:match("^doc/?(.*)$")})[1], meta.request)
    end
end, nil, {})

add_cmds({
    { ":help", "Open <luakit://help/> in a new tab.",
        function (w) w:new_tab("luakit://help/") end },
})

-- Prevent history items from turning up in history
history.add_signal("add", function (uri)
    if string.match(uri, "^luakit://help/") then return false end
end)

return _M

-- vim: et:sw=4:ts=8:sts=4:tw=80