summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/generic/pgf/extract.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/doc/generic/pgf/extract.lua')
-rw-r--r--Master/texmf-dist/doc/generic/pgf/extract.lua148
1 files changed, 90 insertions, 58 deletions
diff --git a/Master/texmf-dist/doc/generic/pgf/extract.lua b/Master/texmf-dist/doc/generic/pgf/extract.lua
index c7ea0669d8f..89141b6da62 100644
--- a/Master/texmf-dist/doc/generic/pgf/extract.lua
+++ b/Master/texmf-dist/doc/generic/pgf/extract.lua
@@ -54,7 +54,7 @@ local extractor = lpeg.P{"document",
C((1 - S",]=")^1),
pair =
- Cg(V"name" * (lit"=" * V"braces")^0) * lit","^-1,
+ Cg(V"name" * (lit"=" * (V"braces" + V"name"))^0) * lit","^-1,
list =
Cf(Ct"" * V"pair"^0, set),
@@ -88,72 +88,104 @@ local extractor = lpeg.P{"document",
}
-- get the basename and extension of a file
-local basename = function(file)
+local function basename(file)
local basename, ext = string.match(file, "^(.+)%.([^.]+)$")
return basename or "", ext or file
end
--- Main loop
-if #arg ~= 2 then
- print("Usage: " .. arg[-1] .. " " .. arg[0] .. " <source-dir> <target-dir>")
- os.exit(1)
-end
local pathsep = package.config:sub(1,1)
-sourcedir = arg[1] .. pathsep
-targetdir = arg[2] .. pathsep
-assert(lfs.attributes(sourcedir, "mode") == "directory", sourcedir .. " is not a directory")
-assert(lfs.attributes(targetdir, "mode") == "directory", targetdir .. " is not a directory")
-
-for file in lfs.dir(sourcedir) do
- if lfs.attributes(sourcedir .. file, "mode") == "file" then
- print("Processing " .. file)
-
- -- Read file into memory
- local f = io.open(sourcedir .. file)
- local text = f:read("*all")
- f:close()
- local name, ext = basename(file)
-
- -- preprocess, strip all commented lines
- text = text:gsub("\n%%[^\n]*\n","")
-
- -- extract all code examples
- local matches = extractor:match(text) or {}
-
- -- write code examples to separate files
- local setup_code = ""
- for n, e in ipairs(matches) do
- local options = e[1]
- local content = e[2]
-
- -- If the snippet is marked as setup code, we have to put it before
- -- every other snippet in the same file
- if options["setup code"] then
- setup_code = setup_code .. strip(content) .. "\n"
- end
- -- Skip those that say "code only"
- if not options["code only"] then
- local newname = name .. "-" .. n .. "." .. ext
- local examplefile = io.open(targetdir .. newname, "w")
-
- examplefile:write"\\documentclass{article}\n"
- examplefile:write"\\usepackage{fp,pgf,tikz,xcolor}\n"
- examplefile:write(preamble)
- examplefile:write"\\begin{document}\n"
- examplefile:write"\\makeatletter\n" -- TODO: this has to go
- examplefile:write(setup_code)
- examplefile:write(options["pre"] and options["pre"] .. "\n" or "")
- if options["render instead"] then
- examplefile:write(options["render instead"] .. "\n")
- else
- examplefile:write(strip(content) .. "\n")
+-- Walk the file tree
+local function walk(sourcedir, targetdir)
+ -- Make sure the arguments are directories
+ assert(lfs.attributes(sourcedir, "mode") == "directory", sourcedir .. " is not a directory")
+ assert(lfs.attributes(targetdir, "mode") == "directory", targetdir .. " is not a directory")
+
+ -- Append the path separator if necessary
+ if sourcedir:sub(-1, -1) ~= pathsep then
+ sourcedir = sourcedir .. pathsep
+ end
+ if targetdir:sub(-1, -1) ~= pathsep then
+ targetdir = targetdir .. pathsep
+ end
+
+ -- Process all items in the directory
+ for file in lfs.dir(sourcedir) do
+ if file == "." or file == ".." then
+ -- Ignore these two special ones
+ elseif lfs.attributes(sourcedir .. file, "mode") == "directory" then
+ -- Recurse into subdirectories
+ lfs.mkdir(targetdir .. file)
+ walk(sourcedir .. file .. pathsep, targetdir .. file .. pathsep)
+ elseif lfs.attributes(sourcedir .. file, "mode") == "file" then
+ print("Processing " .. sourcedir .. file)
+
+ -- Read file into memory
+ local f = io.open(sourcedir .. file)
+ local text = f:read("*all")
+ f:close()
+ local name, ext = basename(file)
+
+ -- preprocess, strip all commented lines
+ text = text:gsub("\n%%[^\n]*","")
+
+ -- extract all code examples
+ local matches = extractor:match(text) or {}
+
+ -- write code examples to separate files
+ local setup_code = ""
+ for n, e in ipairs(matches) do
+ local options = e[1]
+ local content = e[2]
+
+ if content:match("remember picture") then
+ goto continue
end
- examplefile:write(options["post"] and options["post"] .. "\n" or "")
- examplefile:write"\\end{document}\n"
- examplefile:close()
+ -- If the snippet is marked as setup code, we have to put it before
+ -- every other snippet in the same file
+ if options["setup code"] then
+ setup_code = setup_code .. strip(content) .. "\n"
+ goto continue
+ end
+
+ -- Skip those that say "code only"
+ if not options["code only"] then
+ local newname = name .. "-" .. n .. ".tex"
+ local examplefile = io.open(targetdir .. newname, "w")
+
+ examplefile:write"\\documentclass{article}\n"
+ examplefile:write"\\usepackage{fp,pgf,tikz,xcolor}\n"
+ examplefile:write(preamble) -- TODO: this has to go
+ examplefile:write(options["preamble"] and options["preamble"] .. "\n" or "")
+ examplefile:write"\\begin{document}\n"
+ examplefile:write"\\makeatletter\n" -- TODO: this has to go
+ examplefile:write(setup_code)
+ local pre = options["pre"] or ""
+ pre = pre:gsub("##", "#")
+ examplefile:write(pre .. "\n")
+ if options["render instead"] then
+ examplefile:write(options["render instead"] .. "\n")
+ else
+ examplefile:write(strip(content) .. "\n")
+ end
+ examplefile:write(options["post"] and options["post"] .. "\n" or "")
+ examplefile:write"\\end{document}\n"
+
+ examplefile:close()
+ end
+
+ ::continue::
end
end
end
end
+
+-- Main loop
+if #arg < 2 then
+ print("Usage: " .. arg[-1] .. " " .. arg[0] .. " <source-dirs...> <target-dir>")
+ os.exit(1)
+end
+for n = 1, #arg - 1 do
+ walk(arg[n], arg[#arg])
+end