-------------------------------------------------------------------------- -- -- Does work searches for Hello and replaces it with foundyou -- function Para(elem) -- for i, item in ipairs(elem.content) do -- if item.text == "Hello" then -- elem.content[i].text = "foundyou" -- end -- end -- return elem -- end -- ------------------------- read the header content ------------------------ -- -- Define the filter function local function headerfilter(elem) if elem.t == 'Header' then local text = pandoc.utils.stringify(elem.content) io.write('\27[32mCurrent header:', text ,'\n--------\27[0m\n') end return elem end -- Define the filter function local function glsfilter_1(elem) if elem.t == 'Para' then -- local text = pandoc.utils.stringify(elem) local text = elem.text local pattern = '\\gls' local match = text:match(pattern) if match then print("\27[32mMatch found:", match, '\27[0m\n') print(text, '\n--------\n') local link = pandoc.Link(match, '#' .. match) -- return pandoc.Para{link} return "replaced" else print("\27[31mNo match found\27[0m\n") print(text, '\n--------\n') end end return elem end gls_Dict = { ode = {"Ordinary Differential Equation", "ODE"}, cnm = { "Cluster-based Network Modeling", "CNM"}, cnmc = {"control-oriented Cluster-based Network Modeling", "CNMc"}, cmm = { "Cluster Markov-based Modeling", "CMM"}, cfd = {"Computational Fluid Dynamics", "CFD"}, rans = {"Reynolds Averaged Navier Stockes", "RANS"}, dlr = {"German Aerospace Center", "DLR"}, gpu = {"Graphics Processing Unit", "GPU"}, cpu = {"Computer Processing Unit", "CPU"}, sdic = {"Sensitive Dependence on Initial Conditions", "SDIC"}, nmf = {"Non-negative Matrix Factorization", "NMF"}, svd = {"Singular Value Decomposition", "SVD"}, rf = {"Random Forest", "RF"}, cpd = {"Cluster Probability Distribution", "CPD"}, cpevol = {"Centroid Position Evolution", "CPE"}, dtw = {"Dynamical Time Warping", "DTW"}, knn = {"KNearest Neighbor", "KNN"}, } -- -------------------------------------------------------------------------- -- local function headerfilter(elem) if elem.t == 'Header' then local text = pandoc.utils.stringify(elem.content) io.write('\27[32mCurrent header:', text ,'\n--------\27[0m\n') end return elem end -- Define the filter function local function glsfilter(elem) if elem.t == 'Para' then local has_match = false -- Traverse the element tree and replace matched elements local new_content = {} for _, item in ipairs(elem.content) do -- -------------------------------- gls ------------------------------- -- if item.t == 'RawInline' then local gls_Pat = '\\gls{(%w+)}' local gls_First_Pat = '\\glsfirst{(%w+)}' local gls_Pl_Pat = '\\glspl{(%w+)}' local text = item.text -- will only show the latex \command{} content -- print("current line is: ", text) -- was tested with: -- jav test: \gls{rans} \gls{gpu} \gls{rans} \gls{cfd} \gls{cfd} -- it does replace each occurence correcly local gls_Match = string.match(text,gls_Pat) local gls_First_Match = string.match(text,gls_First_Pat) local gls_Pl_Match = string.match(text,gls_Pl_Pat) if gls_Match then has_match = true long_Term = gls_Dict[gls_Match][1] bold_Abbrev = gls_Dict[gls_Match][2] -- to make sure that the code is understood as valid html code it must be converted to RawInline with the html input html_String = pandoc.RawInline('html',long_Term) span_Var = pandoc.Span(html_String, {class = 'gls_Content'}) -- print("span_Var: ",span_Var) -- see: https://pandoc.org/lua-filters.html#pandoc.link local link = pandoc.Link( {bold_Abbrev,span_Var}, '../0_Deco/3_Used_Abbrev.qmd'..'#' .. gls_Match, nil, -- add id and class {id = gls_Match.. "gls", class = 'gls'}) table.insert(new_content, link) end -- ------------------------ gls_First_Match ----------------------- -- if gls_First_Match then has_match = true -- to make sure that the code is understood as valid html code it must be converted to RawInline with the html input long_Term = gls_Dict[gls_First_Match][1] bold_Abbrev = gls_Dict[gls_First_Match][2] -- to make sure that the code is understood as valid html code it must be converted to RawInline with the html input html_String = pandoc.RawInline('html',long_Term.. " (".. bold_Abbrev .. ")") local link = pandoc.Link( html_String , '../0_Deco/3_Used_Abbrev.qmd'..'#' .. gls_First_Match, nil, -- add id and class {id = gls_First_Match.. "gls", class = 'gls'}) table.insert(new_content, link) end -- ------------------------- gls_Pl_Match ------------------------- -- if gls_Pl_Match then has_match = true long_Term = gls_Dict[gls_Pl_Match][1] bold_Abbrev = gls_Dict[gls_Pl_Match][2] -- to make sure that the code is understood as valid html code it must be converted to RawInline with the html input html_String = pandoc.RawInline('html',long_Term .. "s") span_Var = pandoc.Span(html_String, {class = 'gls_Content'}) -- see: https://pandoc.org/lua-filters.html#pandoc.link local link = pandoc.Link( {bold_Abbrev .. "s",span_Var}, '../0_Deco/3_Used_Abbrev.qmd'..'#' .. gls_Pl_Match, nil, -- add id and class {id = gls_Pl_Match.. "gls", class = 'gls'}) table.insert(new_content, link) else -- Print non-matching text in red -- io.write('\27[31mNo match found: ' .. text .. '\27[0m\n') table.insert(new_content, item) end else table.insert(new_content, item) end end -- If no matches were found, return the original element if not has_match then -- print("No match found and return simply the regular element") return elem end return pandoc.Para(new_content) end return elem end -- Export the filter function as a table return { {Header = headerfilter}, {Para = glsfilter} }