Skip to content

Commit

Permalink
Add examplescasfinder to extract + support singleexamplesfinder
Browse files Browse the repository at this point in the history
  • Loading branch information
hansonchar committed Jun 20, 2024
1 parent 5d6c904 commit 2c8bccf
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 9 deletions.
8 changes: 7 additions & 1 deletion doc/generic/pgf/extract.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
16 changes: 8 additions & 8 deletions doc/generic/pgf/lib/examplescasfinder.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down
102 changes: 102 additions & 0 deletions doc/generic/pgf/lib/singleexamplesfinder.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2c8bccf

Please sign in to comment.