summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/mtx-context-module.tex
blob: f8b6d0a54ada266fa326b1dbaf99d3d7d0f7616f (plain)
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
%D \module
%D   [       file=mtx-context-modules,
%D        version=2018.02.24, % very old stuff, now also as extra
%D          title=\CONTEXT\ Extra Trickry,
%D       subtitle=Module TYpesetting,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D This is a \TEXEXEC\ features that has been moved to \MKIV.

% begin help
%
% usage: context --extra=listing [options] list-of-files
%
% end help

\input mtx-context-common.tex

\usemodule[scite]
\usemodule[module-basic]

\dontcomplain

\starttext

\startluacode
    local find, gsub, match, sub = string.find, string.gsub, string.match, string.sub
    local formatters, strip, splitlines, is_empty = string.formatters, string.strip, string.splitlines, string.is_empty

    local types = {
            mkiv = "tex",
            mkii = "tex",
            cld  = "lua",
            lfg  = "lua",
            mpiv = "mp",
            mpii = "mp",
        }

    local function process(inpname,filetype)
        local data = io.loaddata(inpname)
        if data and data ~= "" then
            local result       = { }
            local skiplevel    = 0
            local indocument   = false
            local indefinition = false
            local started      = false
            local settings     = formatters["type=%s"](filetype)
            local preamble, n  = lpeg.match(lpeg.Cs((1-lpeg.patterns.newline^2)^1) * lpeg.Cp(),data)
            local r            = 0
            if preamble then
                preamble = match(preamble,"\\module.-%[(.-)%]")
                if preamble then
                    preamble = gsub(preamble,"%%D *","")
                    preamble = gsub(preamble,"%%(.-)[\n\r]","")
                    preamble = gsub(preamble,"[\n\r]","")
                    preamble = strip(preamble)
                    settings = formatters["%s,%s"](settings,preamble)
                    data = sub(data,n,#data)
                end
            end
            local lines = splitlines(data)
            r = r + 1 ; result[r] = formatters["\\startmoduledocumentation[%s]"](settings)
            for i=1,#lines do
                local line = lines[i]
                if find(line,"^%%D ") or find(line,"^%%D$") then
                    if skiplevel == 0 then
                        local someline = #line < 3 and "" or sub(line,4,#line)
                        if indocument then
                            r = r + 1 ; result[r] = someline
                        else
                            if indefinition then
                                r = r + 1 ; result[r] = "\\stopdefinition"
                                indefinition = false
                            end
                            if not indocument then
                                r = r + 1 ; result[r] = "\\startdocumentation"
                            end
                            r = r + 1 ; result[r] = someline
                            indocument = true
                        end
                    end
                elseif find(line,"^%%M ") or find(line,"^%%M$") then
                    if skiplevel == 0 then
                        local someline = (#line < 3 and "") or sub(line,4,#line)
                        r = r + 1 ; result[r] = someline
                    end
                elseif find(line,"^%%S B") then
                    skiplevel = skiplevel + 1
                elseif find(line,"^%%S E") then
                    skiplevel = skiplevel - 1
                elseif find(line,"^%%") then
                    -- nothing
                elseif skiplevel == 0 then
                    inlocaldocument = indocument
                    inlocaldocument = false
                    local someline = line
                    if indocument then
                        r = r + 1 ; result[r] = "\\stopdocumentation"
                        indocument = false
                    end
                    if indefinition then
                        if is_empty(someline) then
                            r = r + 1 ; result[r] = "\\stopdefinition"
                            indefinition = false
                        else
                            r = r + 1 ; result[r] = someline
                        end
                    elseif not is_empty(someline) then
                        r = r + 1 ; result[r] = "\\startdefinition"
                        indefinition = true
                        if inlocaldocument then
                            -- nothing
                        else
                            r = r + 1 ; result[r] = someline
                        end
                    end
                end
            end
            if indocument then
                r = r + 1 ; result[r] = "\\stopdocumentation"
            end
            if indefinition then
                r = r + 1 ; result[r] = "\\stopdefinition"
            end
            r = r + 1 ; result[r] = "\\stopmoduledocumentation"
            result = table.concat(result,"\r")
            buffers.assign("module",result)
            context.getbuffer { "module" }
        end
    end

    local pattern = document.arguments.pattern

    if pattern then
        document.files = dir.glob(pattern)
    end

    local done  = false
    local files = document.files

    if #files > 0 then
        if document.arguments.sort then
            table.sort(files)
        end
        for i=1,#files do
            local filename = files[i]
            if not find(filename,"^mtx%-context%-") then
                local suffix = file.extname(filename) or ""
                process(filename,types[suffix] or suffix)
                done = true
            end
        end
    end

    if not done then
        context("no files given")
    end

\stopluacode

\stoptext