Include margin-notes.lua
This commit is contained in:
245
margin-notes/test/test-functions.lua
Normal file
245
margin-notes/test/test-functions.lua
Normal file
@ -0,0 +1,245 @@
|
||||
FORMAT = 'latex'
|
||||
|
||||
-- Configuration
|
||||
|
||||
local metadata_test = [[
|
||||
---
|
||||
mrgnn-define-renderings:
|
||||
- class: term
|
||||
body-text: '`**§content§**`'
|
||||
note-text: '`§attr.lem§: *§attr.def§*`'
|
||||
- class: warning
|
||||
body-text: ''
|
||||
note-text: '`**§content§**`'
|
||||
- class: custom-csname
|
||||
body-text: ''
|
||||
note-text: '`§content§`'
|
||||
csname: mrgnnCustom
|
||||
---
|
||||
]]
|
||||
|
||||
local meta = pandoc.read(metadata_test).meta
|
||||
get_renderings_config(meta)
|
||||
assert(config.term.body_text == '**§content§**')
|
||||
assert(config.term.note_text == '§attr.lem§: *§attr.def§*')
|
||||
assert(not config.term.docx_body_text)
|
||||
assert(not config.term.docx_note_text)
|
||||
assert(config.term.odt_body_text == '**§content§**')
|
||||
assert(config.term.odt_note_text == '§attr.lem§: *§attr.def§*')
|
||||
assert(not config.warning.body_text)
|
||||
assert(config.warning.note_text == '**§content§**')
|
||||
assert(not config.warning.docx_body_text)
|
||||
assert(not config.warning.docx_note_text)
|
||||
assert(not config.warning.odt_body_text)
|
||||
assert(config.warning.odt_note_text == '**§content§**')
|
||||
|
||||
-- Creation of marginal notes
|
||||
|
||||
local span_simple = pandoc.Span(pandoc.Str('test'))
|
||||
local span_with_spaces = pandoc.Span({ pandoc.Str('another'),
|
||||
pandoc.Space(), pandoc.Str('with'),
|
||||
pandoc.Space(), pandoc.Str('spaces') })
|
||||
local span_definition = pandoc.Span(pandoc.Str('whales'),
|
||||
{ lem = 'whale', def = 'an animal' })
|
||||
local span_paragraph = pandoc.Span(pandoc.Str('test'), { par = 2 })
|
||||
local span_hyperlink = pandoc.Span(
|
||||
{ pandoc.Str('a'), pandoc.Space(), pandoc.Str('description') },
|
||||
{ target = 'www.example.org' })
|
||||
local span_hyperlink_bis = pandoc.Span(
|
||||
{ pandoc.Str('My'), pandoc.Space(),
|
||||
pandoc.Str('other'), pandoc.Space(), pandoc.Str('link') },
|
||||
{ target = 'www.example.net' })
|
||||
local span_page_range = pandoc.Span(pandoc.Str('test'), { begin = 3, ['end'] = 5 })
|
||||
|
||||
local pandoc_fragment = template_to_pandoc_fragment('Fixed value')
|
||||
assert(#pandoc_fragment == 3)
|
||||
assert(pandoc_fragment[1].t == 'Str')
|
||||
assert(pandoc_fragment[1].text == 'Fixed')
|
||||
assert(pandoc_fragment[2].t == 'Space')
|
||||
assert(pandoc_fragment[3].t == 'Str')
|
||||
assert(pandoc_fragment[3].text == 'value')
|
||||
local placeholders_paths = get_paths_to_placeholders(pandoc_fragment)
|
||||
assert(#placeholders_paths == 0)
|
||||
local filled = replace_placeholders(pandoc_fragment, placeholders_paths,
|
||||
span_simple.content, span_simple.attributes)
|
||||
assert(#filled == 3)
|
||||
assert(filled[1].t == 'Str')
|
||||
assert(filled[1].text == 'Fixed')
|
||||
assert(filled[2].t == 'Space')
|
||||
assert(filled[3].t == 'Str')
|
||||
assert(filled[3].text == 'value')
|
||||
|
||||
local pandoc_fragment = template_to_pandoc_fragment('§content§')
|
||||
assert(pandoc_fragment[1].t == 'Str')
|
||||
assert(pandoc_fragment[1].text ==
|
||||
PLACEHOLDER_BEGIN .. PLACEHOLDER_LABEL ..
|
||||
'content' ..
|
||||
PLACEHOLDER_LABEL .. PLACEHOLDER_END)
|
||||
local placeholders_paths = get_paths_to_placeholders(pandoc_fragment)
|
||||
assert(#placeholders_paths == 1)
|
||||
assert(#placeholders_paths[1] == 2)
|
||||
assert(placeholders_paths[1][2] == 'text')
|
||||
local filled = replace_placeholders(pandoc_fragment, placeholders_paths,
|
||||
span_simple.content, span_simple.attributes)
|
||||
assert(#filled == 1)
|
||||
assert(filled[1].t == 'Str')
|
||||
assert(filled[1].text == span_simple.content[1].text)
|
||||
local pandoc_fragment = template_to_pandoc_fragment('§content§')
|
||||
local filled = replace_placeholders(pandoc_fragment, placeholders_paths,
|
||||
span_with_spaces.content, span_with_spaces.attributes)
|
||||
assert(#filled == 5)
|
||||
assert(filled[1].t == 'Str')
|
||||
assert(filled[1].text == span_with_spaces.content[1].text)
|
||||
assert(filled[5].t == 'Str')
|
||||
assert(filled[5].text == span_with_spaces.content[5].text)
|
||||
|
||||
local pandoc_fragment = template_to_pandoc_fragment('**§content§**')
|
||||
assert(pandoc_fragment[1].t == 'Strong')
|
||||
assert(pandoc_fragment[1].content[1].t == 'Str')
|
||||
local placeholders_paths = get_paths_to_placeholders(pandoc_fragment)
|
||||
assert(#placeholders_paths == 1)
|
||||
assert(#placeholders_paths[1] == 4)
|
||||
assert(placeholders_paths[1][1] == 1)
|
||||
assert(placeholders_paths[1][2] == 'content')
|
||||
assert(placeholders_paths[1][3] == 1)
|
||||
assert(placeholders_paths[1][4] == 'text')
|
||||
local filled = replace_placeholders(pandoc_fragment, placeholders_paths,
|
||||
span_simple.content, span_simple.attributes)
|
||||
assert(#filled == 1)
|
||||
assert(filled[1].t == 'Strong')
|
||||
assert(#filled[1].content == 1)
|
||||
assert(filled[1].content[1].t == 'Str')
|
||||
assert(filled[1].content[1].text == 'test')
|
||||
|
||||
local pandoc_fragment = template_to_pandoc_fragment('§attr.lem§: *§attr.def§.*')
|
||||
assert(pandoc_fragment[1].t == 'Str')
|
||||
assert(pandoc_fragment[1].text ==
|
||||
PLACEHOLDER_BEGIN .. PLACEHOLDER_LABEL ..
|
||||
'attr.lem' ..
|
||||
PLACEHOLDER_LABEL .. PLACEHOLDER_END..
|
||||
':')
|
||||
assert(pandoc_fragment[3].t == 'Emph')
|
||||
assert(pandoc_fragment[3].content[1].t == 'Str')
|
||||
assert(pandoc_fragment[3].content[1].text ==
|
||||
PLACEHOLDER_BEGIN .. PLACEHOLDER_LABEL ..
|
||||
'attr.def' ..
|
||||
PLACEHOLDER_LABEL .. PLACEHOLDER_END..
|
||||
'.')
|
||||
local placeholders_paths = get_paths_to_placeholders(pandoc_fragment)
|
||||
assert(#placeholders_paths == 2)
|
||||
assert(#placeholders_paths[1] == 2)
|
||||
assert(placeholders_paths[1][1] == 1)
|
||||
assert(#placeholders_paths[2] == 4)
|
||||
assert(placeholders_paths[2][1] == 3)
|
||||
assert(placeholders_paths[2][2] == 'content')
|
||||
assert(placeholders_paths[2][3] == 1)
|
||||
assert(placeholders_paths[2][4] == 'text')
|
||||
local filled = replace_placeholders(pandoc_fragment, placeholders_paths,
|
||||
span_definition.content, span_definition.attributes)
|
||||
assert(#filled == 3)
|
||||
assert(filled[1].t == 'Str')
|
||||
assert(filled[1].text == 'whale:')
|
||||
assert(filled[3].t == 'Emph')
|
||||
assert(#filled[3].content == 3)
|
||||
assert(filled[3].content[1].t == 'Str')
|
||||
assert(filled[3].content[1].text == 'an')
|
||||
-- If some keys are not present,
|
||||
-- the placeholders are replaced with bold UNDEFINED
|
||||
-- and a warning is issued.
|
||||
local pandoc_fragment = template_to_pandoc_fragment('§attr.lem§: *§attr.def§.*')
|
||||
local filled = replace_placeholders(pandoc_fragment, placeholders_paths,
|
||||
span_simple.content, span_simple.attributes)
|
||||
assert(#filled == 4)
|
||||
assert(filled[1].t == 'Strong')
|
||||
assert(filled[1].content[1].text == UNDEFINED)
|
||||
assert(filled[4].t == 'Emph')
|
||||
assert(#filled[4].content == 2)
|
||||
assert(filled[4].content[1].t == 'Strong')
|
||||
assert(filled[4].content[1].content[1].text == UNDEFINED)
|
||||
|
||||
local pandoc_fragment = template_to_pandoc_fragment('See \\§ §attr.par§')
|
||||
assert(pandoc_fragment[2].t == 'Space')
|
||||
assert(pandoc_fragment[3].t == 'Str')
|
||||
assert(pandoc_fragment[3].text ==
|
||||
'§ ' ..
|
||||
PLACEHOLDER_BEGIN .. PLACEHOLDER_LABEL ..
|
||||
'attr.par' ..
|
||||
PLACEHOLDER_LABEL .. PLACEHOLDER_END)
|
||||
local placeholders_paths = get_paths_to_placeholders(pandoc_fragment)
|
||||
assert(#placeholders_paths == 1)
|
||||
assert(#placeholders_paths[1] == 2)
|
||||
assert(placeholders_paths[1][1] == 3)
|
||||
local filled = replace_placeholders(pandoc_fragment, placeholders_paths,
|
||||
span_paragraph.content, span_paragraph.attributes)
|
||||
assert(#filled == 3)
|
||||
assert(filled[3].t == 'Str')
|
||||
assert(filled[3].text == '§ 2')
|
||||
|
||||
local pandoc_fragment = template_to_pandoc_fragment('[§content§](https://§attr.target§)')
|
||||
assert(pandoc_fragment[1].target ==
|
||||
'https://' ..
|
||||
PLACEHOLDER_BEGIN .. PLACEHOLDER_LABEL ..
|
||||
'attr.target' ..
|
||||
PLACEHOLDER_LABEL .. PLACEHOLDER_END)
|
||||
local placeholders_paths = get_paths_to_placeholders(pandoc_fragment)
|
||||
assert(#placeholders_paths[2] == 2)
|
||||
assert(placeholders_paths[2][1] == 1)
|
||||
assert(placeholders_paths[2][2] == 'target')
|
||||
local filled = replace_placeholders(pandoc_fragment, placeholders_paths,
|
||||
span_hyperlink.content, span_hyperlink.attributes)
|
||||
assert(#filled == 1)
|
||||
assert(filled[1].t == 'Link')
|
||||
assert(#filled[1].content == 3)
|
||||
assert(filled[1].target == 'https://www.example.org')
|
||||
|
||||
local pandoc_fragment = template_to_pandoc_fragment('pp. §attr.begin§–§attr.end§')
|
||||
assert(#pandoc_fragment == 1)
|
||||
assert(pandoc_fragment[1].t == 'Str')
|
||||
local placeholders_paths = get_paths_to_placeholders(pandoc_fragment)
|
||||
assert(#placeholders_paths == 1)
|
||||
assert(#placeholders_paths[1] == 2)
|
||||
local filled = replace_placeholders(pandoc_fragment, placeholders_paths,
|
||||
span_page_range.content, span_page_range.attributes)
|
||||
assert(#filled == 1)
|
||||
assert(filled[1].t == 'Str')
|
||||
assert(filled[1].text == 'pp. 3–5')
|
||||
|
||||
-- Top-level invocation
|
||||
|
||||
define_rendering_functions(meta)
|
||||
|
||||
local text_with_note = {}
|
||||
|
||||
local span_term_one = pandoc.Span(pandoc.Str('whales'),
|
||||
{ class = 'term', lem = 'whale', def = 'an animal' })
|
||||
local span_term_two = pandoc.Span(pandoc.Str('wand'),
|
||||
{ class = 'term', lem = 'wand', def = 'a magical device' })
|
||||
|
||||
text_with_note = render_margin_notes(span_term_one)
|
||||
assert(#text_with_note[1].content == 1)
|
||||
assert(text_with_note[1].content[1].t == 'Strong')
|
||||
assert(text_with_note[1].content[1].content[1].text == 'whales')
|
||||
assert(text_with_note[2].t == 'RawInline')
|
||||
assert(text_with_note[6].t == 'RawInline')
|
||||
|
||||
text_with_note = render_margin_notes(span_term_two)
|
||||
assert(#text_with_note[1].content == 1)
|
||||
assert(text_with_note[1].content[1].t == 'Strong')
|
||||
assert(text_with_note[1].content[1].content[1].text == 'wand')
|
||||
|
||||
local span_warning_one = pandoc.Span(pandoc.Str('beware!'),
|
||||
{ class = 'warning' })
|
||||
|
||||
text_with_note = render_margin_notes(span_warning_one)
|
||||
assert(text_with_note[3].t == 'Strong')
|
||||
assert(text_with_note[3].content[1].t == 'Str')
|
||||
assert(text_with_note[3].content[1].text == 'beware!')
|
||||
|
||||
-- Custom control sequences for LaTeX and ConTeXt
|
||||
|
||||
local span_custom_csname = pandoc.Span(pandoc.Str('dummy'),
|
||||
{ class = 'custom-csname' })
|
||||
|
||||
text_with_note = render_margin_notes(span_custom_csname)
|
||||
assert(text_with_note[2].t == 'RawInline')
|
||||
assert(text_with_note[2].text == '\\mrgnnCustom{')
|
Reference in New Issue
Block a user