From 6a0e08db272f09091ffdfc3f8766c91979138750 Mon Sep 17 00:00:00 2001 From: Henri Menke Date: Wed, 17 Jun 2020 20:32:22 +1200 Subject: [PATCH] Add options to Lua examples #640 #839 --- .../lua/pgf/gd/force/ControlElectric.lua | 28 ++++++++++++------- .../pgf/lua/pgf/manual/DocumentParser.lua | 25 +++++++++++++---- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/tex/generic/pgf/graphdrawing/lua/pgf/gd/force/ControlElectric.lua b/tex/generic/pgf/graphdrawing/lua/pgf/gd/force/ControlElectric.lua index 47e3cee45..c9f129c28 100644 --- a/tex/generic/pgf/graphdrawing/lua/pgf/gd/force/ControlElectric.lua +++ b/tex/generic/pgf/graphdrawing/lua/pgf/gd/force/ControlElectric.lua @@ -39,16 +39,24 @@ declare { Two typical effects of increasing the |electric charge| are distortion of symmetries and an upscaling of the drawings. "]], - examples = {[[" - \tikz \graph [spring electrical layout, horizontal=0 to 1] - { 0 [electric charge=1] -- subgraph C_n [n=10] }; - "]],[[" - \tikz \graph [spring electrical layout, horizontal=0 to 1] - { 0 [electric charge=5] -- subgraph C_n [n=10] }; - "]],[[" - \tikz \graph [spring electrical layout, horizontal=0 to 1] - { [clique] 1 [electric charge=5], 2, 3, 4 }; - "]] + examples = { + { + options = [["preamble={\usetikzlibrary{graphs,graphdrawing} \usegdlibrary{force}}"]], + code = [[" + \tikz \graph [spring electrical layout, horizontal=0 to 1] + { 0 [electric charge=1] -- subgraph C_n [n=10] }; + "]] + },{ + code = [[" + \tikz \graph [spring electrical layout, horizontal=0 to 1] + { 0 [electric charge=5] -- subgraph C_n [n=10] }; + "]] + },{ + code = [[" + \tikz \graph [spring electrical layout, horizontal=0 to 1] + { [clique] 1 [electric charge=5], 2, 3, 4 }; + "]] + } } } diff --git a/tex/generic/pgf/lua/pgf/manual/DocumentParser.lua b/tex/generic/pgf/lua/pgf/manual/DocumentParser.lua index d2f678082..bd2a7026a 100644 --- a/tex/generic/pgf/lua/pgf/manual/DocumentParser.lua +++ b/tex/generic/pgf/lua/pgf/manual/DocumentParser.lua @@ -159,7 +159,17 @@ local function process_examples(t) local n = {} for i=1,#t do - n[i] = process_string(strip_quotes(t[i])) + local code, options + if type(t[i]) == "table" then + code = assert(t[i].code) + options = t[i].options + else + code = t[i] + end + n[i] = { + options = process_string(strip_quotes(options)), + code = process_string(strip_quotes(code)) + } end return n end @@ -391,8 +401,9 @@ DocumentParser.addRenderer ( print_on_output(output, "\\par\\smallskip\\emph{Example" .. (((#e>1) and "s") or "") .. "}\\par") for _,example in ipairs(e) do - print_on_output(output, "\\begin{codeexample}[]") - print_lines_on_output(output, example) + local opts = table.concat(example.options or {}, "") + print_on_output(output, "\\begin{codeexample}[" .. opts .. "]") + print_lines_on_output(output, example.code) print_on_output(output, "\\end{codeexample}") end end @@ -417,8 +428,12 @@ DocumentParser.addRenderer ( function print_lines_on_output(output, lines) - for _,l in ipairs(lines or {}) do - output[#output+1] = l + for n,l in ipairs(lines or {}) do + if (n == 1 or n == #lines) and l == "" then + -- skip leading and trailing blank lines + else + output[#output+1] = l + end end end