m-pstricks.lua /size: 2221 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['m-pstricks'] = {
2    version   = 1.001,
3    comment   = "companion to m-pstricks.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9-- The following will be done when I need ps tricks figures
10-- in large quantities:
11--
12-- + hash graphics and only process them once
13-- + save md5 checksums in tuc file
14--
15-- It's no big deal but has a low priority.
16
17local format, lower, concat, gmatch = string.format, string.lower, table.concat, string.gmatch
18local variables = interfaces.variables
19
20moduledata.pstricks = moduledata.pstricks or { }
21
22local report_pstricks = logs.reporter("pstricks")
23
24local template = [[
25\starttext
26    \pushcatcodetable
27    \setcatcodetable\texcatcodes
28    \usemodule[pstric]
29    %s
30    \popcatcodetable
31    \startTEXpage
32        \hbox\bgroup
33            \ignorespaces
34            %s
35            \removeunwantedspaces
36        \egroup
37        \obeydepth %% temp hack as we need to figure this out
38    \stopTEXpage
39\stoptext
40]]
41
42local loaded   = { }
43local graphics = 0
44
45function moduledata.pstricks.usemodule(names)
46    for name in gmatch(names,"([^%s,]+)") do
47        loaded[#loaded+1] = format([[\readfile{%s}{}{}]],name)
48    end
49end
50
51function moduledata.pstricks.process(n)
52    graphics = graphics + 1
53    local name = format("%s-pstricks-%04i",tex.jobname,graphics)
54    local data = buffers.collectcontent("def-"..n)
55    local tmpfile = name .. ".tmp"
56    local epsfile = name .. ".ps"
57    local pdffile = name .. ".pdf"
58    local loaded = concat(loaded,"\n")
59    os.remove(epsfile)
60    os.remove(pdffile)
61    io.savedata(tmpfile,format(template,loaded,data))
62    os.execute(format("mtxrun --script texexec %s --once --dvips",tmpfile))
63    if lfs.isfile(epsfile) then
64        os.execute(format("ps2pdf %s %s",epsfile,pdffile))
65        -- todo: direct call but not now
66        if lfs.isfile(pdffile) then
67            context.externalfigure( { pdffile }, { object = variables.no } )
68        else
69            report_pstricks("run failed, no pdf file")
70        end
71    else
72        report_pstricks("run failed, no ps file")
73    end
74end
75