Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
-- This file:
--   http://angg.twu.net/LUA/bib.lua.html
--   http://angg.twu.net/LUA/bib.lua
--           (find-angg "LUA/bib.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun e () (interactive) (find-angg "LUA/bib.lua"))

-- «.tests»	(to "tests")
-- «.meteor»	(to "meteor")

require "re"

countnewlines = function (str)
    local str = str:gsub("[^\n]", "")
    return #str
  end

getcomments_pat = '{("%" [^%nl]* %nl)*}'
getcomments_mat = Re({}):c(getcomments_pat)
getcomments = function (s) return (getcomments_mat(s):gsub("\n$", "")) end
getanchor   = function (s) return s:match("«([!-~]+)»") end
indexanchor = function (s)
    local leftpart  = format('%% «.bib-%s» ', s)
    local rightpart = format('(to "bib-%s")', s)
    local line = format('%-32s%s', leftpart, rightpart)
    return line
  end
sectionanchor = function (s)
    return format('%% «bib-%s»  (to ".bib-%s")\n', s, s)
  end
withoutfirstline = function (s) return s:match("^[^\n]*\n(.*)$") end
withoutanchor = function (s)
    if not s:match("«") then return s end
    return withoutfirstline(s)
  end
withanchor = function (s, name)
    return sectionanchor(name)..withoutanchor(s)
  end


--[[
 (eepitch-lua51)
 (eepitch-kill)
 (eepitch-lua51)
dofile "bib.lua"
--           (find-angg "LATEX/catsem-slides.bib")
bf     = BibFile.read("~/LATEX/catsem-slides.bib"):filter('b.name')
bfs    = bf:toset()
bfkeys = bfs:ks()
PP(bfkeys)
= bfs:get("CWM2")
= bfs:get("CWM2").body
bo = bfs:get("CWM2").body
= withoutfirstline(bo)
= withanchor(bo, "Foo")

--]]



BibBlock = Class {
  type    = "BibBlock",
  __tostring = function (bb) return bb:link() end,
  __index = {
    link = function (bb)
        return format('(find-fline "%s" %d "%s")', bb.fname, bb.line, bb.name or "")
      end,
    comments = function (bb) return getcomments(bb.body) end,
    anchor   = function (bb) return getanchor  (bb.body) end,
    bodywithanchor = function (bb)
        if bb:anchor() then return bb.body end
        return sectionanchor(bb.name)..bb.body
      end,
  },
}

BibFile = Class {
  type = "BibFile",
  read = function (fname)
      local bigstr = ee_readfile(fname)
      local blocks = {}
      for pos,body in bigstr:gmatch("()(.-)\n\n+") do
	local line = countnewlines(bigstr:sub(1, pos-1))+1
	local kind,name = body:match("@([A-Za-z]+)%{([A-Za-z0-9:]+),")
	local block = {
          fname=fname, body=body, line=line, pos=pos, kind=kind, name=name,
        }
        table.insert(blocks, BibBlock(block))
      end
      return BibFile {fname=fname, bigstr=bigstr, blocks=blocks}
    end,
  __tostring = function (bf) return bf:tostring() end,
  __index = {
    tostring = function (bf)
        local str = bf.fname..":"
        for i,block in ipairs(bf.blocks) do
          str = format("%s\n%d: %s", str, i, tostring(block))
        end
        return str
      end,
    withtheseblocks = function (bf, blocks)
        return BibFile {fname=bf.fname, bigstr=bf.bigstr, blocks=blocks}
      end,
    filter = function (bf, f)
        if type(f) == "string" then
          f = Code.ve("b => "..f)
        end
        local blocks = {}
        for i,block in ipairs(bf.blocks) do
          if f(block) then table.insert(blocks, block) end
        end        
        return bf:withtheseblocks(blocks)
      end,
    toset = function (bf)
        local s = SetL.new()
        for i,b in ipairs(bf.blocks) do s:add(b.name, b) end
        return s
      end,
    iblocks = function (bf)
        return ipairs(bf.blocks) 
      end,
    buildbib = function (bf, names)
        local bibblocks = bf:filter('b.name'):toset()
        local names = names or bibblocks:ks()
        return buildbib(bibblocks, names)
      end,
  },
}

buildbib = function (bibblocks, names)
    local index = mapconcat(indexanchor, names, "\n")
    local getbody = function (name) return bibblocks:get(name).body end
    local getbody2 = function (name) return withanchor(getbody(name), name) end
    local entries = mapconcat(getbody2, names, "\n\n")
    return index.."\n\n"..entries
  end


-- «tests»  (to ".tests")
--[==[

* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "bib.lua"
--           (find-angg "LATEX/catsem-u.bib")
--           (find-angg "LATEX/catsem-slides.bib")
bf     = BibFile.read("~/LATEX/catsem-slides.bib")
bf     = BibFile.read("~/LATEX/catsem-slides.bib"):filter('b.name')
bfs    = bf:toset()
bfkeys = bfs:ks()
PP(bfkeys)

for i,key in ipairs(bfkeys) do
  print(key, bfs:get(key):anchor())
end

= bf.blocks[3]
= bf
= bf:filter(Code.ve 'b => b.name')
= bf:filter('b.name == "Brady"')
= bf:filter('b.name')
= bf:filter('b.name'):toset()
= bf:filter('b.name'):toset():get("Kromer")
= bf:filter('b.name'):toset():ksc()
= bf:filter('b.name'):toset():ksc(" ")

bb  = bfs:get("HOTT")
bbb = bfs:get("HOTT").body
= bb
= bb.body
= bb:comments()
= bb:anchor()
= bb:bodywithanchor()

= BibFile.read("~/LATEX/catsem-u.bib")

= bfs:get("HOTT").body
= bfs:get("HOTT"):anchor()
= bfs:get("HOTT"):bodywithanchor()
PPPV(bfs:get("HOTT"))


bbb = bfs:get("CWM2").body
= bbb
= withoutanchor(bbb, "Foo")
= withanchor(bbb, "Foo")

= buildbib(bfs, {"HOTT", "SICP", "CWM2"})
= bf:buildbib({"HOTT", "SICP", "CWM2"})
= bf:buildbib()

--]==]


--[[
 (eepitch-lua51)
 (eepitch-kill)
 (eepitch-lua51)
dofile "bib.lua"
--           (find-angg "LATEX/catsem-slides.bib")
bf     = BibFile.read("~/LATEX/catsem-slides.bib")
= bf:buildbib()

--]]


-- «meteor»  (to ".meteor")
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "bib.lua"

names0 = [=[ AbramskyTzevelekos AgdaUserManual Awodey BadiouLoW
  BadiouMoTT CWM2 CaccamoPhD CaccamoWinskel CarsonSappho ChengMorally
  CoeckeNewStruP CoeckePQP Corfield DSLsofMath EilenbergSteenrod
  Elephant1 FongSpivak Freyd76 FreydScedrov Ganesalingam Hazratpour
  HuCarette IDARCT Jacobs Jamnik Kromer KromerSlides LawvereRosebrugh
  Leinster MDE MacLaneNotes MarsdenCTUSD MilewskiCTFPOCaml MissingAgda
  NederpeltGeuvers Norell08 OchsLucatelli OchsTallinnAbs OchsVGMS2018
  OchsWLD2019 PH1 PH2 PenroseSIGGRAPH2020 Perrone PowerPasting Riehl
  SICP SeelyBeck SeelyDiff SeelyLCCC SeelyPLC SelingerLN Taylor
  ThompsonGardner VanBenthemJutting77 WadlerPLFA ]=]

namess = Set.from(split(names0))
= namess

bf     = BibFile.read("~/LATEX/catsem-ab.bib"):filter('b.name')
bfs    = bf:toset()
bfkeys = bfs:ks()
PP(bfkeys)

for i,key in ipairs(bfkeys) do
  print(key, bfs:get(key):anchor())
end

= (bfs * namess)
= (bfs * namess):get"PH1"
= (bfs * namess):get"PH1".body
= (bfs * namess):ksc()

newbib = bfs * namess
= newbib
= newbib:ksc()
for key,val in newbib:gen() do print(key) end
out = "\n"
for key,val in newbib:gen() do out = out..(val.body).."\n\n" end
= out
out2 = (out:gsub("\n%%[^\n]*", ""))
= out2

-- (find-fline "~/LATEX/2022meteor-test2.bib")

--]==]






-- Local Variables:
-- coding:  utf-8-unix
-- End: