diff --git a/margin-notes/Makefile b/margin-notes/Makefile index 67fcc04..f808fac 100644 --- a/margin-notes/Makefile +++ b/margin-notes/Makefile @@ -15,9 +15,9 @@ test-internal: margin-notes.lua test/test-functions.lua @echo -e '==========================\nAll internal tests passed.\n==========================\n' rm --interactive=never test/tmp.lua -test: margin-notes.lua test/test.md - pandoc -t native -L margin-notes.lua test/test.md > test/tmp.native - diff test/tmp.native test/test.native +test: margin-notes.lua sample.md + pandoc -t native -L margin-notes.lua sample.md > test/tmp.native + diff test/tmp.native test/sample.native @echo -e '\n===============\ntest passed.\n===============\n' rm test/tmp.native diff --git a/margin-notes/margin-notes.lua b/margin-notes/margin-notes.lua index c7b639e..a880218 100644 --- a/margin-notes/margin-notes.lua +++ b/margin-notes/margin-notes.lua @@ -419,23 +419,35 @@ end local function template_to_function(template) --[[ - inlines_with_placeholders cannot be memoized - for it is a reference to a table that will be changed - by replace_placeholders. - paths_to_placeholders can be memoized - because it is only traversed once it has been created. + Returns a function that takes data and inserts it + into the given template. + ]]-- + --[[ + – inlines_with_placeholders and paths_to_placeholders + are created and memoized the first time the returned + function is called. + – inlines_with_placeholders cannot be used directly + for it is a reference to a table that would be changed + by replace_placeholders. That's why we create a deep + copy of it via the walk function at every call. + – paths_to_placeholders can be used directly by + replace_placeholders because it is only traversed. ]]-- if template then local paths_to_placeholders + local inlines_with_placeholders return function(instance_content, instance_attr) - local inlines_with_placeholders = template_to_pandoc_fragment(template) + if not inlines_with_placeholders then + inlines_with_placeholders = template_to_pandoc_fragment(template) + end + local inlines_copy = inlines_with_placeholders:walk({}) if not paths_to_placeholders then paths_to_placeholders = get_paths_to_placeholders(inlines_with_placeholders) end return replace_placeholders( - inlines_with_placeholders, paths_to_placeholders, + inlines_copy, paths_to_placeholders, instance_content, instance_attr) end end diff --git a/margin-notes/test/sample.native b/margin-notes/test/sample.native new file mode 100644 index 0000000..712299d --- /dev/null +++ b/margin-notes/test/sample.native @@ -0,0 +1,120 @@ +[ Para + [ Span ( "" , [ "warning" ] , [] ) [] + , Strong [ Str "Important!" ] + , Str "You" + , Space + , Str "can" + , Space + , Str "use" + , Space + , Str "LaTeX" + , Space + , Str "to" + , Space + , Str "include" + , SoftBreak + , Span + ( "" + , [ "term" ] + , [ ( "lem" , "BibTeX" ) + , ( "def" + , "A program designed to format bibliographical entries" + ) + ] + ) + [ Strong [ Str "BibTeX" ] ] + , Str "BibTeX:" + , Space + , Emph + [ Str "A" + , Space + , Str "program" + , Space + , Str "designed" + , Space + , Str "to" + , Space + , Str "format" + , Space + , Str "bibliographical" + , Space + , Str "entries" + ] + , Space + , Str "citations." + , Space + , Str "Note" + , Space + , Str "that" + , Space + , Str "in" + , Space + , Str "LaTeX" + , Space + , Str "environments," + , SoftBreak + , Str "the" + , Space + , Str "material" + , Space + , Str "between" + , Space + , Str "the" + , Space + , Str "begin" + , Space + , Str "and" + , Space + , Str "end" + , Space + , Str "tags" + , Space + , Str "will" + , Space + , Str "be" + , Space + , Str "interpreted" + , SoftBreak + , Str "as" + , Space + , Span + ( "" + , [ "term" ] + , [ ( "lem" , "Raw code" ) + , ( "def" , "Code inserted **untouched** in the output." ) + ] + ) + [ Strong [ Str "raw" , Space , Str "LaTeX" ] ] + , Str "Raw" + , Space + , Str "code:" + , Space + , Emph + [ Str "Code" + , Space + , Str "inserted" + , Space + , Strong [ Str "untouched" ] + , Space + , Str "in" + , Space + , Str "the" + , Space + , Str "output." + ] + , Str "," + , Space + , Str "not" + , Space + , Str "as" + , Space + , Str "Markdown." + ] +, Para + [ Str "(From" + , Space + , Str "Pandoc" + , Space + , Str "manual)" + ] +]