~hristoast/hristoast

ref: 6c6b610011fd9b58d5c2baf47345899ba5ef360d hristoast/plugins/insert-if.lua -rw-r--r-- 1.1 KiB
6c6b6100Hristos N. Triantafillou Proofreading edits 4 months ago
                                                                                
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
-- Inserts an HTML snippet iff the page has a certain element
--
-- To run it, you need to add something like this to soupault.conf:
-- [plugins.conditional-insert]
--   file = "plugins/conditional-insert.lua"
--
-- [widgets.blink-warning]
--   widget = "conditional-insert"
--   html = "<div><strong>Warning: blink elements are obsolete!</strong></div>"
--   selector = "body"
--   check_selector = "blink"
--
-- Minimum soupault version: 1.3
-- Author: Daniil Baturin
-- License: MIT


-- Configuration
snippet = config["html"]
selector = config["selector"]
check_selector = config["check_selector"]

-- Plugin code

if not snippet then
  Log.warning("snippet is not configured, using an empty string")
  snippet = ""
end

if (not selector) or (not check_selector) then
  Log.warning("selector and check_selector options must be configured")
else
  elem = HTML.select_one(page, check_selector)
  if elem then
    target = HTML.select_one(page, selector)
    if not target then
      Log.info("page has no element matching selector " .. selector)
    else
      snippet_html = HTML.parse(snippet)
      HTML.append_child(target, snippet_html)
    end
  end
end