\documentclass{scrartcl} % set document class: manual at https://ctan.org/pkg/koma-script
\usepackage{luacode}

\begin{document}
  \begin{luacode*}
    os.execute("curl -o temp.js https://bbloomf.github.io/jgabc/propersdata.js")
    fileRef = io.open("temp.js","rb")
    os.execute("curl -o GR61_prolog.tex http://www.antiphonale.net/GR61_Prolog.tex")
    outFileRef = io.open("GR61_out.tex","w+")
    io.output(outFileRef);
    io.write("\\input GR61_prolog\n")
    io.write("\\begin{document}\n")
    captions = {}
    no = 1
    texLines = {}
    multi = false
    for line in fileRef:lines() do
      line = string.gsub(line,[[^%s*(.-)%s*$]], [[%1]])
      line = string.gsub(line,[["]],[[]])
      if (line == "var sundayKeys = [") 
        or (line == "var ultimaeDominicaePostPentecosten = [")
        or (line == "var otherKeys = [")
        or (line == "var saintKeys = [")
      then
        section = 1
      end
      if line == "var proprium = {" then
        section = 2
      end
      if (line == "];") or (line == "};" and section == 2) then
        section = 0
      end
      if section == 1 then
        idx = string.find(line,":")
        if idx then
          line = string.sub(line,1+idx,-1)
          idx = string.find(line,",")
          key = string.sub(line,1,idx-1)
          line = string.sub(line,1+idx,-1)
          idx = string.find(line,":")
          line = string.sub(line,1+idx,-1)
          idx = string.find(line,",")
          caption = string.sub(line,1,idx-1)
          if string.find(line,"Sunday") and string.find(caption,"Dominica")==nil then
            caption = "Dominica " .. caption
          end
          captions[key] = {caption,no}
          no = no + 1
        end
      end
      if section == 2 then
        idx = string.find(line,":") 
        if idx or multi then
          if string.find(line,"{") then
            key = string.sub(line,1,idx-1)
            item = captions[key]
            if item then
              currentCaption = item[1]
              currentNo = item[2]
              mno = 0
              mmno = 0
              multi = false
            else
              currentNo = nil
            end
          elseif currentNo then
            if not multi then
              key = string.sub(line,1,idx-1)
            end
            if multi or string.find(key,"ID") then
              if string.find(line,"%[") then
                multi = true
                mmno = 0
              elseif string.find(line,"%]") then
                multi = false
              else
                if multi then
                  id = string.gsub(line,",","")
                  mmno = mmno + 1
                else
                  id = string.sub(line,idx+2,-2)
                  key = string.sub(key,1,2)
                end
                if mno == 0 then
                  texLines[10000*currentNo] = ">" .. currentCaption
                end
                keyID = string.find("insegrtralofco",key)
                if keyID then
                  keyID = (keyID + 1)/2
                  mno = keyID
                else
                  keyID = 50
                end
                texLines[10000*currentNo+100*mno+mmno] = "#" .. string.sub(key,1,2) .. id
              end
            end
          end
        end
      end
    end
    fileRef:close()
  \end{luacode*}
  \begin{luacode*}
    sortedNos = {}
    n = 0
    for no,cmd in pairs(texLines) do
      n = n+1
      sortedNos[n] = no
    end
    table.sort(sortedNos)
    for _,no in pairs(sortedNos) do
      cmd = texLines[no]
      if tonumber(no) > 0 then
        type = string.sub(cmd,1,1) 
        cmd = string.sub(cmd,2,-1)
        if ">" == type then
          io.write("\\section{" .. cmd .. "}\n")
        elseif "#" == type then
         io.write("\\includeGB{" .. string.sub(cmd,3,-1) .. "}{" .. string.upper(string.sub(cmd,1,2)) .. "}\n")
        end
      end
    end
    io.write("\\end{document}")
    io.close(outFileRef,"w+")
  \end{luacode*}
\end{document}
