-- I suppose that you always use plain strings in the -- "headword", "transliteration" and "match" fields, -- so I stringify the corresponding Inlines to be able -- to more easily insert the values in the LaTeX string. local stringify = pandoc.utils.stringify local open_glslink_scd_arg = pandoc.RawInline('latex', '{') local close_glslink_scd_arg = pandoc.RawInline('latex', '}') -- I use two tables: one to store the data relative to the headwords, -- the other to map the forms to the corresponding entries -- in headwords_data. -- Since the entries in headwords_data are tables -- and tables are always passed by reference in Lua, -- this approach avoids a lot of redundant writings in memory. local headwords_data = {} local forms_to_headwords = {} local function get_glossary_data(meta) for _, entry in ipairs(meta.glossary.entries) do local headword = stringify(entry.headword) headwords_data[headword] = { headword = headword, text = entry.text, transliteration = stringify(entry.transliteration) } for _, form in ipairs(entry.match) do forms_to_headwords[stringify(form)] = headwords_data[headword] end end end local function tag_words(span) if span.attributes.lang == 'el' then local content = stringify(span.content) local word_data = forms_to_headwords[content] if word_data then local linguistic_tags = -- If the "transliteration" field is missing, Lua will throw an error. -- I suppose that this should not happen, but if it can be so, -- uncomment the following line (supposing that the lonely @ -- will not cause problems): -- word_data.transliteration = word_data.transliteration or '' pandoc.RawInline('latex', '\\index{' .. word_data.transliteration .. '@' .. word_data.headword .. '}' .. '\\glslink{' .. word_data.transliteration .. '}') return { linguistic_tags, open_glslink_scd_arg, span, close_glslink_scd_arg } end end end return { { Meta = get_glossary_data }, { Span = tag_words } }