mtx-context-module.tex /size: 5946 b    last modification: 2020-07-01 14:35
1%D \module
2%D   [       file=mtx-context-modules,
3%D        version=2018.02.24, % very old stuff, now also as extra
4%D          title=\CONTEXT\ Extra Trickry,
5%D       subtitle=Module TYpesetting,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14%D This is a \TEXEXEC\ features that has been moved to \MKIV.
15
16% begin help
17%
18% usage: context --extra=listing [options] list-of-files
19%
20% end help
21
22\input mtx-context-common.tex
23
24\usemodule[scite]
25\usemodule[module-basic]
26
27\dontcomplain
28
29\starttext
30
31\startluacode
32    local find, gsub, match, sub = string.find, string.gsub, string.match, string.sub
33    local formatters, strip, splitlines, is_empty = string.formatters, string.strip, string.splitlines, string.is_empty
34
35    local types = {
36            mkiv = "tex",
37            mkii = "tex",
38            cld  = "lua",
39            lfg  = "lua",
40            mpiv = "mp",
41            mpii = "mp",
42        }
43
44    local function process(inpname,filetype)
45        local data = io.loaddata(inpname)
46        if data and data ~= "" then
47            local result       = { }
48            local skiplevel    = 0
49            local indocument   = false
50            local indefinition = false
51            local started      = false
52            local settings     = formatters["type=%s"](filetype)
53            local preamble, n  = lpeg.match(lpeg.Cs((1-lpeg.patterns.newline^2)^1) * lpeg.Cp(),data)
54            local r            = 0
55            if preamble then
56                preamble = match(preamble,"\\module.-%[(.-)%]")
57                if preamble then
58                    preamble = gsub(preamble,"%%D *","")
59                    preamble = gsub(preamble,"%%(.-)[\n\r]","")
60                    preamble = gsub(preamble,"[\n\r]","")
61                    preamble = strip(preamble)
62                    settings = formatters["%s,%s"](settings,preamble)
63                    data = sub(data,n,#data)
64                end
65            end
66            local lines = splitlines(data)
67            r = r + 1 ; result[r] = formatters["\\startmoduledocumentation[%s]"](settings)
68            for i=1,#lines do
69                local line = lines[i]
70                if find(line,"^%%D ") or find(line,"^%%D$") then
71                    if skiplevel == 0 then
72                        local someline = #line < 3 and "" or sub(line,4,#line)
73                        if indocument then
74                            r = r + 1 ; result[r] = someline
75                        else
76                            if indefinition then
77                                r = r + 1 ; result[r] = "\\stopdefinition"
78                                indefinition = false
79                            end
80                            if not indocument then
81                                r = r + 1 ; result[r] = "\\startdocumentation"
82                            end
83                            r = r + 1 ; result[r] = someline
84                            indocument = true
85                        end
86                    end
87                elseif find(line,"^%%M ") or find(line,"^%%M$") then
88                    if skiplevel == 0 then
89                        local someline = (#line < 3 and "") or sub(line,4,#line)
90                        r = r + 1 ; result[r] = someline
91                    end
92                elseif find(line,"^%%S B") then
93                    skiplevel = skiplevel + 1
94                elseif find(line,"^%%S E") then
95                    skiplevel = skiplevel - 1
96                elseif find(line,"^%%") then
97                    -- nothing
98                elseif skiplevel == 0 then
99                    inlocaldocument = indocument
100                    inlocaldocument = false
101                    local someline = line
102                    if indocument then
103                        r = r + 1 ; result[r] = "\\stopdocumentation"
104                        indocument = false
105                    end
106                    if indefinition then
107                        if is_empty(someline) then
108                            r = r + 1 ; result[r] = "\\stopdefinition"
109                            indefinition = false
110                        else
111                            r = r + 1 ; result[r] = someline
112                        end
113                    elseif not is_empty(someline) then
114                        r = r + 1 ; result[r] = "\\startdefinition"
115                        indefinition = true
116                        if inlocaldocument then
117                            -- nothing
118                        else
119                            r = r + 1 ; result[r] = someline
120                        end
121                    end
122                end
123            end
124            if indocument then
125                r = r + 1 ; result[r] = "\\stopdocumentation"
126            end
127            if indefinition then
128                r = r + 1 ; result[r] = "\\stopdefinition"
129            end
130            r = r + 1 ; result[r] = "\\stopmoduledocumentation"
131            result = table.concat(result,"\r")
132            buffers.assign("module",result)
133            context.getbuffer { "module" }
134        end
135    end
136
137    local pattern = document.arguments.pattern
138
139    if pattern then
140        document.files = dir.glob(pattern)
141    end
142
143    local done  = false
144    local files = document.files
145
146    if #files > 0 then
147        if document.arguments.sort then
148            table.sort(files)
149        end
150        for i=1,#files do
151            local filename = files[i]
152            if not find(filename,"^mtx%-context%-") then
153                local suffix = file.extname(filename) or ""
154                process(filename,types[suffix] or suffix)
155                done = true
156            end
157        end
158    end
159
160    if not done then
161        context("no files given")
162    end
163
164\stopluacode
165
166\stoptext
167