Skip to content

Commit 710bd7f

Browse files
committedApr 25, 2019
Produce compilable examples from extract.lua #640
1 parent 0104be3 commit 710bd7f

File tree

1 file changed

+60
-16
lines changed

1 file changed

+60
-16
lines changed
 

‎doc/generic/pgf/extract.lua

+60-16
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
1+
-- TODO: this has to go
2+
local preamble = [[
3+
\usetikzlibrary{3d,arrows,arrows.spaced,arrows.meta,bending,babel,calc,
4+
fit,patterns,plotmarks,shapes.geometric,shapes.misc,shapes.symbols,
5+
shapes.arrows,shapes.callouts,shapes.multipart,shapes.gates.logic.US,
6+
shapes.gates.logic.IEC,circuits.logic.US,circuits.logic.IEC,
7+
circuits.logic.CDH,circuits.ee.IEC,datavisualization,
8+
datavisualization.polar,datavisualization.formats.functions,er,automata,
9+
backgrounds,chains,topaths,trees,petri,mindmap,matrix,calendar,folding,
10+
fadings,shadings,spy,through,turtle,positioning,scopes,
11+
decorations.fractals,decorations.shapes,decorations.text,
12+
decorations.pathmorphing,decorations.pathreplacing,decorations.footprints,
13+
decorations.markings,shadows,lindenmayersystems,intersections,
14+
fixedpointarithmetic,fpu,svg.path,external,graphs,graphs.standard,quotes,
15+
math,angles,views,animations,rdf,perspective}
16+
\usetikzlibrary{graphdrawing}
17+
\usegdlibrary{trees,circular,layered,examples,force,phylogenetics,routing}
18+
]]
19+
120
local lfs = require("lfs")
2-
local lpeg = require"lpeg"
21+
local lpeg = require("lpeg")
322
local C, Cf, Cg, Ct, P, S, V = lpeg.C, lpeg.Cf, lpeg.Cg, lpeg.Ct, lpeg.P, lpeg.S, lpeg.V
423

524
-- strip leading and trailing whitespace
625
local function strip(str)
726
return str:match"^%s*(.-)%s*$"
827
end
28+
-- strip braces
29+
local function strip_braces(str)
30+
return str:match"^{?(.-)}?$"
31+
end
932

1033
-- optional whitespace
1134
local ws = S" \t\n\r"^0
@@ -21,14 +44,14 @@ local function set(t,k,v)
2144
-- strip whitespace from keys
2245
k = strip(k)
2346
-- if the value is empty, set it to invalid character
24-
v = v or invalid
47+
v = v and strip_braces(v) or invalid
2548
return rawset(t,k,v)
2649
end
2750

2851
-- Grammar to extract code examples
2952
local extractor = lpeg.P{"document",
3053
name =
31-
C((1 - S"]=")^1),
54+
C((1 - S",]=")^1),
3255

3356
pair =
3457
Cg(V"name" * (lit"=" * V"braces")^0) * lit","^-1,
@@ -78,42 +101,63 @@ local basename = function(file)
78101
end
79102

80103
-- Main loop
81-
sourcedir = "text-en/"
82-
targetdir = "/tmp/"
104+
if #arg ~= 2 then
105+
print("Usage: " .. arg[-1] .. " " .. arg[0] .. " <source-dir> <target-dir>")
106+
os.exit(1)
107+
end
108+
local pathsep = package.config:sub(1,1)
109+
sourcedir = arg[1] .. pathsep
110+
targetdir = arg[2] .. pathsep
111+
assert(lfs.attributes(sourcedir, "mode") == "directory", sourcedir .. " is not a directory")
112+
assert(lfs.attributes(targetdir, "mode") == "directory", targetdir .. " is not a directory")
83113

84114
for file in lfs.dir(sourcedir) do
85115
if lfs.attributes(sourcedir .. file, "mode") == "file" then
116+
print("Processing " .. file)
117+
86118
-- Read file into memory
87119
local f = io.open(sourcedir .. file)
88120
local text = f:read("*all")
89121
f:close()
90122
local name, ext = basename(file)
91123

124+
-- preprocess, strip all commented lines
125+
text = text:gsub("\n%%[^\n]*\n","")
126+
92127
-- extract all code examples
93128
local matches = extractor:match(text) or {}
94129

95130
-- write code examples to separate files
131+
local setup_code = ""
96132
for n, e in ipairs(matches) do
97133
local options = e[1]
98134
local content = e[2]
99135

136+
-- If the snippet is marked as setup code, we have to put it before
137+
-- every other snippet in the same file
138+
if options["setup code"] then
139+
setup_code = setup_code .. strip(content) .. "\n"
140+
end
141+
100142
-- Skip those that say "code only"
101143
if not options["code only"] then
102144
local newname = name .. "-" .. n .. "." .. ext
103-
104145
local examplefile = io.open(targetdir .. newname, "w")
105146

106-
-- TODO: Use options to convert to MWE
107-
examplefile:write("===Options===\n")
108-
for key, value in pairs(options) do
109-
examplefile:write(key)
110-
if value ~= invalid then
111-
examplefile:write("=" .. value)
112-
end
113-
examplefile:write("\n")
147+
examplefile:write"\\documentclass{article}\n"
148+
examplefile:write"\\usepackage{fp,pgf,tikz,xcolor}\n"
149+
examplefile:write(preamble)
150+
examplefile:write"\\begin{document}\n"
151+
examplefile:write"\\makeatletter\n" -- TODO: this has to go
152+
examplefile:write(setup_code)
153+
examplefile:write(options["pre"] and options["pre"] .. "\n" or "")
154+
if options["render instead"] then
155+
examplefile:write(options["render instead"] .. "\n")
156+
else
157+
examplefile:write(strip(content) .. "\n")
114158
end
115-
examplefile:write("===Content===\n")
116-
examplefile:write(strip(content))
159+
examplefile:write(options["post"] and options["post"] .. "\n" or "")
160+
examplefile:write"\\end{document}\n"
117161

118162
examplefile:close()
119163
end

0 commit comments

Comments
 (0)
Please sign in to comment.