diff --git a/doc/generic/pgf/extract.lua b/doc/generic/pgf/extract.lua index eca1cb83c..1775a678c 100644 --- a/doc/generic/pgf/extract.lua +++ b/doc/generic/pgf/extract.lua @@ -11,6 +11,7 @@ local documentfinder = require "documentfinder" local examplefinder = require "examplefinder" local exoptfinder = require "examplewithoptionfinder" local examplesfinder = require "examplesfinder" +local examplescasfinder = require "examplescasfinder" local DEBUG = false @@ -53,10 +54,15 @@ for n = 1, #arg - 1 do utils.walk(arg[n], arg[#arg], exoptfinder) end --- Extract code exmples from examples being assigned as a table +-- Extract code exmples from examples being assigned as a table of tables for n = 1, #arg - 1 do utils.walk(arg[n], arg[#arg], examplesfinder) end +-- Extract code exmples from examples being assigned as a table of strings +for n = 1, #arg - 1 do + utils.walk(arg[n], arg[#arg], examplescasfinder) +end + -- utils.walk("/Users/hchar/tmp/from", "/Users/hchar/tmp/mwe", exoptfinder) os.exit(0) diff --git a/doc/generic/pgf/lib/examplescasfinder.lua b/doc/generic/pgf/lib/examplescasfinder.lua index 9432c3a4f..39f1b2939 100644 --- a/doc/generic/pgf/lib/examplescasfinder.lua +++ b/doc/generic/pgf/lib/examplescasfinder.lua @@ -1,13 +1,13 @@ -local UNIT_TESTING = true +local UNIT_TESTING = false +local function pwd() + local info = debug.getinfo(1, "S") + local path = info.source:match("@(.*)") + local dir = path:match("(.*[/\\])") or "./" + return dir +end +package.path = pwd() .. "?.lua;" .. package.path if UNIT_TESTING then - local function pwd() - local info = debug.getinfo(1, "S") - local path = info.source:match("@(.*)") - local dir = path:match("(.*[/\\])") or "./" - return dir - end - package.path = pwd() .. "?.lua;" .. package.path local luarocks_path = os.getenv("HOME") .. "/.luarocks/share/lua/5.3/?.lua" package.path = package.path .. ";" .. luarocks_path end diff --git a/doc/generic/pgf/lib/singleexamplesfinder.lua b/doc/generic/pgf/lib/singleexamplesfinder.lua new file mode 100644 index 000000000..cd2505cc9 --- /dev/null +++ b/doc/generic/pgf/lib/singleexamplesfinder.lua @@ -0,0 +1,102 @@ +local UNIT_TESTING = false +local function pwd() + local info = debug.getinfo(1, "S") + local path = info.source:match("@(.*)") + local dir = path:match("(.*[/\\])") or "./" + return dir +end +package.path = pwd() .. "?.lua;" .. package.path + +if UNIT_TESTING then + local luarocks_path = os.getenv("HOME") .. "/.luarocks/share/lua/5.3/?.lua" + package.path = package.path .. ";" .. luarocks_path +end + +local lpeg = require("lpeg") +local loc = lpeg.locale() +local u = require("utils") +local SP = u.SP +local str = require("stringmatcher") +local C, Ct, P, V = lpeg.C, lpeg.Ct, lpeg.P, lpeg.V + +local finder = {} + +local function peek(...) + -- print("peek/captured: ", tostring(...)) + return ... +end + +-- Grammar to extract code from function call to "examples" with a string parameter +finder.grammar = + P { + "examples", + examples = V "anything" * Ct(V "codeexample") / peek, + codeexample = V "begincodeexample" * str, + begincodeexample = (loc.space + lpeg.P "\r") ^ 1 * "examples" * SP * "=" * SP, + anything = (1 - V "codeexample") ^ 0 +} + +function finder.get_options(_) + return {} +end + +function finder.get_content(s) + assert(type(s) == "string") + return s +end + +function finder.get_name() + return "single" +end + +if not UNIT_TESTING then + return finder +end + +local test_case1 = [=[ + examples = [[" + example code + "]] +]=] + +do + local matches = finder.grammar:match(test_case1) + assert(#matches == 1) + assert(u.strip(u.get_string(matches[1])) == "example code") +end + +local test_case2 = + [=[ + examples = [[" + \begin{tikzpicture} + \graph [simple necklace layout, node distance=1cm, node sep=0pt, + nodes={draw,circle,as=.}] + { + 1 -- 2 [minimum size=2cm] -- 3 -- + 4 -- 5 -- 6 -- 7 --[orient=up] 8 + }; + \draw [red,|-|] (1.center) -- ++(0:1cm); + \draw [red,|-|] (5.center) -- ++(180:1cm); + \end{tikzpicture} + "]] +]=] + +do + local matches = finder.grammar:match(test_case2) + assert(#matches == 1) + assert( + u.strip(u.get_string(matches[1])) == + [[\begin{tikzpicture} + \graph [simple necklace layout, node distance=1cm, node sep=0pt, + nodes={draw,circle,as=.}] + { + 1 -- 2 [minimum size=2cm] -- 3 -- + 4 -- 5 -- 6 -- 7 --[orient=up] 8 + }; + \draw [red,|-|] (1.center) -- ++(0:1cm); + \draw [red,|-|] (5.center) -- ++(180:1cm); + \end{tikzpicture}]] + ) +end + +return finder