1 -- Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
2 -- License: GPL-2.0+ <https://www.gnu.org/licenses/gpl-2.0.txt>
4 -- This filter accesses the PublicInbox::WwwHighlight PSGI endpoint
5 -- (see examples/highlight.psgi)
7 -- Dependencies: lua-http
9 -- disclaimer: written by someone who does not know Lua.
11 -- This requires cgit linked with Lua
12 -- Usage (in your cgitrc(5) config file):
14 -- source-filter=lua:/path/to/this/script.lua
15 -- about-filter=lua:/path/to/this/script.lua
17 local wwwhighlight_url
= 'http://127.0.0.1:9090/'
18 local req_timeout
= 10
21 -- match $PublicInbox::HTTP::MAX_REQUEST_BUFFER
22 local max_len
= 10 * 1024 * 1024
24 -- about-filter needs surrounding <pre> tags if all we do is
25 -- highlight and linkify
28 function filter_open(...)
31 -- detect when we're used in an about-filter
32 local repo_url
= os
.getenv('CGIT_REPO_URL')
34 local path_info
= os
.getenv('PATH_INFO')
35 rurl
= path_info
:match("^/(.+)/about/?$")
36 pre
= rurl
== repo_url
39 -- hand filename off for language detection
40 local fn
= select(1, ...)
42 local http_util
= require
'http.util'
43 wwwhighlight_url
= wwwhighlight_url
.. http_util
.encodeURI(fn
)
47 -- try to buffer the entire source in memory
48 function filter_write(str
)
51 elseif (req_body
:len() + str
:len()) > max_len
then
57 req_body
= req_body
.. str
62 io
.stderr
:write(tostring(err
), "\n")
73 function filter_close()
77 local request
= require
'http.request'
78 local req
= request
.new_from_uri(wwwhighlight_url
)
79 req
.headers
:upsert(':method', 'PUT')
80 req
:set_body(req_body
)
82 -- don't wait for 100-Continue message from the PSGI app
83 req
.headers
:delete('expect')
85 local headers
, stream
= req
:go(req_timeout
)
86 if headers
== nil then
89 local status
= headers
:get(':status')
90 if status
~= '200' then
91 return fail('status ' .. status
)
93 local body
, err
= stream
:get_body_as_string()
94 if not body
and err
then