luatex-gadgets.lua /size: 2951 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['luatex-gadgets'] = {
2    version   = 1.001,
3    comment   = "companion to luatex-gadgets.tex",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9if context then return end
10
11-- This module contains some maybe useful gadgets. More might show up here
12-- as side effect of tutorials or articles. Don't use this file in ConTeXt
13-- because we often have similar mechanisms already.
14
15gadgets = gadgets or { } -- global namespace
16
17-- marking content for optional removal
18
19local marking        = { }
20gadgets.marking      = marking
21
22local marksignal     = 5001 -- will be set in the tex module
23local lastmarked     = 0
24local marked         = { }
25local local_par_code = 9
26
27function marking.setsignal(n)
28    marksignal = tonumber(n) or marksignal
29end
30
31function marking.mark(str)
32    local currentmarked = marked[str]
33    if not currentmarked then
34        lastmarked    = lastmarked + 1
35        currentmarked = lastmarked
36        marked[str]   = currentmarked
37    end
38    tex.setattribute(marksignal,currentmarked)
39end
40
41function marking.remove(str)
42    local attr = marked[str]
43    if not attr then
44        return
45    end
46    local list = tex.nest[tex.nest.ptr]
47    if list then
48        local head = list.head
49        local tail = list.tail
50        local last = tail
51        if last[marksignal] == attr then
52            local first = last
53            while true do
54                local prev = first.prev
55                if not prev or prev[marksignal] ~= attr or prev.id == local_par_code then
56                    break
57                else
58                    first = prev
59                end
60            end
61            if first == head then
62                list.head = nil
63                list.tail = nil
64            else
65                local prev = first.prev
66                list.tail  = prev
67                prev.next  = nil
68            end
69            node.flush_list(first)
70        end
71    end
72end
73
74-- local imgscan = img.scan
75--
76-- local valid = {
77--     ["png"] = "^" .. string.char(0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A),
78--     ["jpg"] = "^" .. string.char(0xFF,0xD8,0xFF),
79--     ["jp2"] = "^" .. string.char(0x00,0x00,0x00,0x0C,0x6A,0x50,0x20,0x20,0x0D,0x0A),
80--     ["pdf"] = "^" .. ".-%%PDF",
81-- }
82--
83-- function img.scan(t)
84--     if t and t.filename then
85--         local f = io.open(t.filename,"rb")
86--         if f then
87--             local d = f:read(4096)
88--             for k, v in next,valid do
89--                 if string.find(d,v) then
90--                     f:close() -- be nice
91--                     return imgscan(t)
92--                 end
93--             end
94--             f:close() -- be nice
95--         end
96--     end
97-- end
98--
99-- print(img.scan({filename = "hacker1b.tif"}))
100-- print(img.scan({filename = "cow.pdf"}))
101-- print(img.scan({filename = "mill.png"}))
102