Skip to content

Commit

Permalink
Adjust preamble extraction to remove extra braces
Browse files Browse the repository at this point in the history
  • Loading branch information
hansonchar committed Jun 23, 2024
1 parent 4523d5b commit d5a33db
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
build/*
**/*.o
**/*.so
tex/generic/pgf/pgf.revision.tex
23 changes: 7 additions & 16 deletions doc/generic/pgf/lib/examplescasfinder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ local C, P, V, Ct, Cf, Cg = lpeg.C, lpeg.P, lpeg.V, lpeg.Ct, lpeg.Cf, lpeg.Cg
-- end
-- )

local tostring = UNIT_TESTING and require "ml".tstring or nil
-- local tostring = UNIT_TESTING and require "ml".tstring or nil

local finder = {}

Expand Down Expand Up @@ -74,18 +74,8 @@ finder.grammar =
exentry = str * P(",") ^ -1
}

local function preamble(options)
local p = SP * P "preamble" * SP * "=" * SP * C(P(1) ^ 1)
local matches = p:match(u.get_string(options))
local table = {}
if matches then
table.preamble = matches
end
return table
end

function finder.get_options(e)
return e.options and preamble(e.options) or {}
return e.options and u.preamble(e.options) or {}
end

function finder.get_content(e)
Expand Down Expand Up @@ -184,10 +174,11 @@ local test_case4 =
do
local matches = finder.grammar:match(test_case4)
assert(#matches == 2)
assert(
u.strip(u.get_string(matches[1].options)) ==
[[preamble={\usetikzlibrary{graphs,graphdrawing} \usegdlibrary{force}}]]
)
local expected_options = [[preamble={\usetikzlibrary{graphs,graphdrawing} \usegdlibrary{force}}]]
assert(u.strip(u.get_string(matches[1].options)) == expected_options)
local t = u.preamble(expected_options)
assert(t.preamble == [[\usetikzlibrary{graphs,graphdrawing} \usegdlibrary{force}]])

assert(
u.strip(u.get_string(matches[1].code)) ==
[[\tikz \graph [spring electrical layout, horizontal=0 to 1]
Expand Down
12 changes: 1 addition & 11 deletions doc/generic/pgf/lib/examplesfinder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,8 @@ finder.grammar =
codekv = Cg(C("code") * SP * "=" * str)
}

local function preamble(options)
local p = SP * P "preamble" * SP * "=" * SP * C(P(1) ^ 1)
local matches = p:match(u.get_string(options))
local table = {}
if matches then
table.preamble = matches
end
return table
end

function finder.get_options(e)
return e.options and preamble(e.options) or {}
return e.options and u.preamble(e.options) or {}
end

function finder.get_content(e)
Expand Down
4 changes: 2 additions & 2 deletions doc/generic/pgf/lib/examplewithoptionfinder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ local function preamble(options)
local matches = p:match(options)
local table = {}
if matches then
table.preamble = matches
table.preamble = u.strip_braces(matches)
end
return table
end
Expand Down Expand Up @@ -135,7 +135,7 @@ example {

do
local matches = finder.grammar:match(test_case2)
print("#matches:", #matches)
assert(#matches == 2)
assert(u.strip(finder.get_options(matches[1]).preamble) == [[first example preamble]])
assert(u.strip(finder.get_content(matches[1])) == [[first example code]])
assert(not finder.get_options(matches[2]).preamble)
Expand Down
10 changes: 10 additions & 0 deletions doc/generic/pgf/lib/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,14 @@ function u.walk(sourcedir, targetdir, finder)
end
end

function u.preamble(options)
local p = u.SP * lpeg.P "preamble" * u.SP * "=" * u.SP * lpeg.C(lpeg.P(1) ^ 1)
local matches = p:match(u.get_string(options))
local table = {}
if matches then
table.preamble = u.strip_braces(matches)
end
return table
end

return utils

0 comments on commit d5a33db

Please sign in to comment.