Skip to content

Commit

Permalink
Add u.get_string(s) to extract any muti-nested string
Browse files Browse the repository at this point in the history
  • Loading branch information
hansonchar committed Jun 19, 2024
1 parent db81313 commit 5df7609
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 10 deletions.
6 changes: 3 additions & 3 deletions doc/generic/pgf/lib/examplesfinder.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local UNIT_TESTING = true
local UNIT_TESTING = false

if UNIT_TESTING then
local function pwd()
Expand Down Expand Up @@ -81,7 +81,7 @@ finder.grammar =

local function preamble(options)
local p = SP * P "preamble" * SP * "=" * SP * C(P(1) ^ 1)
local matches = p:match(options)
local matches = p:match(u.get_string(options))
local table = {}
if matches then
table.preamble = matches
Expand All @@ -94,7 +94,7 @@ function finder.get_options(e)
end

function finder.get_content(e)
return e.code or ""
return e.code and u.get_string(e.code) or ""
end

function finder.get_name()
Expand Down
75 changes: 68 additions & 7 deletions doc/generic/pgf/lib/utils.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,74 @@
local lfs = require "lfs"
local lpeg = require "lpeg"
local DEBUG = false
local loc = lpeg.locale()

local UNIT_TESTING = false

if DEBUG then
if UNIT_TESTING then
local luarocks_path = os.getenv("HOME") .. "/.luarocks/share/lua/5.3/?.lua"
package.path = package.path .. ";" .. luarocks_path
end
local tostring = DEBUG and require "ml".tstring or nil
local tostring = UNIT_TESTING and require "ml".tstring or nil

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 stringmatcher = require "stringmatcher"

-- luacheck:ignore 542 (Empty if branch.)
local utils = {}
local u = utils

function u.get_string(s)
local inner = s
while inner do
inner = stringmatcher:match(s)
if inner then
s = inner
end
end
return s
end

if UNIT_TESTING then
local string_in_string =
[=[
[["
\tikz \graph [spring electrical layout, horizontal=0 to 1]
{ [clique] 1 [electric charge=5], 2, 3, 4 };
"]]
]=]
assert(
u.get_string(string_in_string) ==
[[
\tikz \graph [spring electrical layout, horizontal=0 to 1]
{ [clique] 1 [electric charge=5], 2, 3, 4 };
]]
)

local string_only =
[=[
[[
\tikz \graph [spring electrical layout, horizontal=0 to 1]
{ [clique] 1 [electric charge=5], 2, 3, 4 };
]]
]=]
-- print(u.get_string(string_only))
assert(
u.get_string(string_only) ==
[[
\tikz \graph [spring electrical layout, horizontal=0 to 1]
{ [clique] 1 [electric charge=5], 2, 3, 4 };]]
)
end

utils.pathsep = package.config:sub(1, 1)

-- strip leading and trailing whitespace
Expand All @@ -23,15 +80,19 @@ function u.strip_braces(str)
return str:match "^{?(.-)}?$"
end

local loc = lpeg.locale()

-- optional whitespace
u.SP = loc.space ^ 0
u.SP = (loc.space + lpeg.P "\r") ^ 0

-- for backward compatibility
-- u.ws = lpeg.S " \t\n\r" ^ 0
u.ws = u.SP

if UNIT_TESTING then
local crlf = "\n\r"
assert(loc.space:match(crlf) == 2)
assert(u.SP:match(crlf) == 3)
end

-- match string literal
function u.lit(str)
return u.ws * lpeg.P(str) * u.ws
Expand Down Expand Up @@ -93,7 +154,7 @@ function u.walk(sourcedir, targetdir, finder)
-- extract all code examples
local matches = finder.grammar:match(text) or {}

if DEBUG then
if UNIT_TESTING then
print("matches:", matches)
print("tostring(matches):", tostring(matches))
end
Expand Down

0 comments on commit 5df7609

Please sign in to comment.