diff --git a/doc/generic/pgf/lib/examplesfinder.lua b/doc/generic/pgf/lib/examplesfinder.lua index 24eee479a..4818fe1ff 100644 --- a/doc/generic/pgf/lib/examplesfinder.lua +++ b/doc/generic/pgf/lib/examplesfinder.lua @@ -1,4 +1,4 @@ -local UNIT_TESTING = true +local UNIT_TESTING = false if UNIT_TESTING then local function pwd() @@ -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 @@ -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() diff --git a/doc/generic/pgf/lib/utils.lua b/doc/generic/pgf/lib/utils.lua index 9164f8dba..0ce0b80c2 100644 --- a/doc/generic/pgf/lib/utils.lua +++ b/doc/generic/pgf/lib/utils.lua @@ -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 @@ -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 @@ -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