# Processing luatex callback functions when creating glossary with bib2gls

In my project, I am looking for simple, but automated string replacement. For that I have hit upon this question and its answer:

What packages provide text/command substitution for LaTeX, XeTeX and LuaLaTeX?

which works perfect for any text, except the text written with bib2gls into pdf file.

For showing that, I created such MWE:

\documentclass{article}

\usepackage{filecontents}

\begin{filecontents}{glossary.bib}
% Encoding: UTF-8
@entry{replacement,
name = {Replacement of dark in name field},
description = {Replacement of dark in description field.}}
\end{filecontents}

\usepackage{luacode}
\begin{luacode}
function replace_dark_with_bright ( s )
s = string.gsub ( s, "dark" , "bright" )
return s
end

\end{luacode}
\AtBeginDocument{%
"process_input_buffer", replace_dark_with_bright, "replace_dark_with_bright" )}
}

\usepackage[record, nopostdot,%
nostyles, stylemods={all},% do the adjustments for the longbooktabs styles
]{glossaries-extra}

src={glossary},%
sort={en},
sort-field={name},
charset=UTF-8,%
]

\begin{document}
Always look on the dark side of life.

\gls{replacement}.

\setglossarystyle{altlistgroup}

\printunsrtglossary[type={main}]

\end{document}


Is there a callback function, with which I can achieve such replacement as is done in standard TeX text?

I went as far as creating database of glossary entries, so usage of bib2gls is required. Replacement in name field is desirable, but the key one is replacement in description field.

As far as this question is highly localized, I would be happy for any kind of help: Solution (yes, that is always the best) or at least guidance through the problem.

Thank you for any help, Tomas

Load the glossary later so that the callback has a chance:

\documentclass{article}

\usepackage{filecontents}

\begin{filecontents}{glossary.bib}
% Encoding: UTF-8
@entry{replacement,
name = {Replacement of dark in name field},
description = {Replacement of dark in description field.}}
\end{filecontents}

\usepackage{luacode}
\begin{luacode}
function replace_dark_with_bright ( s )
s = string.gsub ( s, "dark" , "bright" )
return s
end

\end{luacode}
\AtBeginDocument{%
"process_input_buffer", replace_dark_with_bright, "replace_dark_with_bright" )}
}

\usepackage[record, nopostdot,%
nostyles, stylemods={all},% do the adjustments for the longbooktabs styles
]{glossaries-extra}

\AtBeginDocument{%
src={glossary},%
sort={en},
sort-field={name},
charset=UTF-8,%
]}

\begin{document}
Always look on the dark side of life.

\gls{replacement}.

\setglossarystyle{altlistgroup}

\printunsrtglossary[type={main}]

\end{document}