File size: 7,063 Bytes
a67ae61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
-------------------------------------------------------------------------- --
-- 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 = {"<b>O</b>rdinary <b>D</b>ifferential <b>E</b>quation",
  "ODE"},

  cnm = { "<b>C</b>luster-based <b>N</b>etwork <b>M</b>odeling",
    "CNM"},

  cnmc = {"<b>c</b>ontrol-oriented <b>C</b>luster-based <b>N</b>etwork <b>M</b>odeling",
    "CNMc"},

  cmm = { "<b>C</b>luster <b>M</b>arkov-based <b>M</b>odeling",
    "CMM"},

  cfd = {"<b>C</b>omputational <b>F</b>luid <b>D</b>ynamics",
    "CFD"},

  rans = {"<b>R</b>eynolds <b>A</b>veraged <b>N</b>avier <b>S</b>tockes",
    "RANS"},

  dlr = {"German Aerospace Center",
    "DLR"},

  gpu = {"<b>G</b>raphics <b>P</b>rocessing <b>U</b>nit",
    "GPU"},

  cpu = {"<b>C</b>omputer <b>P</b>rocessing <b>U</b>nit",
    "CPU"},

  sdic = {"<b>S</b>ensitive  <b>D</b>ependence on <b>I</b>nitial <b>C</b>onditions",
    "SDIC"},

  nmf = {"<b>N</b>on-negative <b>M</b>atrix <b>F</b>actorization",
    "NMF"},

  svd = {"<b>S</b>ingular <b>V</b>alue <b>D</b>ecomposition",
    "SVD"},

  rf = {"<b>R</b>andom <b>F</b>orest",
    "RF"},

  cpd = {"<b>C</b>luster <b>P</b>robability <b>D</b>istribution",
    "CPD"},

  cpevol = {"<b>C</b>entroid <b>P</b>osition <b>E</b>volution",
    "CPE"},

  dtw = {"<b>D</b>ynamical <b>T</b>ime <b>W</b>arping",
    "DTW"},

  knn = {"<b>KN</b>earest <b>N</b>eighbor",
    "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}
}