Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
-- This file:
--   http://anggtwu.net/LUA/Caepro3.lua.html
--   http://anggtwu.net/LUA/Caepro3.lua
--          (find-angg "LUA/Caepro3.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun c1 () (interactive) (find-angg "LUA/Caepro1.lua"))
-- (defun c2 () (interactive) (find-angg "LUA/Caepro2.lua"))
-- (defun c3 () (interactive) (find-angg "LUA/Caepro3.lua"))
-- (defun c4 () (interactive) (find-angg "LUA/Caepro4.lua"))
--
-- (defun g2 () (interactive) (find-angg "LUA/Gram2.lua"))
-- (defun g3 () (interactive) (find-angg "LUA/Gram3.lua"))
--
-- (defun e () (interactive) (find-angg "LUA/Caepro3.lua"))

-- (find-angg "LUA/Caepro2.lua" "Jpg")
-- (find-angg "LUA/Caepro2.lua" "Pdf")
-- (find-angg "LUA/Caepro2.lua" "Pdfs")
-- (find-angg "LUA/Caepro2.lua" "Html")

-- «.MkTable»		(to "MkTable")
-- «.MkTable-tests»	(to "MkTable-tests")
-- «.AnyOf»		(to "AnyOf")
-- «.AnyOf-tests»	(to "AnyOf-tests")
-- «.anyofs»		(to "anyofs")
-- «.anyofs-tests»	(to "anyofs-tests")
-- «.abbrevpat»		(to "abbrevpat")
-- «.abbrevpat-tests»	(to "abbrevpat-tests")
-- «.Abbrevs»		(to "Abbrevs")
-- «.Abbrevs-tests»	(to "Abbrevs-tests")
-- «.run_options»		(to "run_options")
-- «.run_options-tests»	(to "run_options-tests")

require "Gram2"   -- (find-angg "LUA/Gram2.lua")





--  __  __ _    _____     _     _      
-- |  \/  | | _|_   _|_ _| |__ | | ___ 
-- | |\/| | |/ / | |/ _` | '_ \| |/ _ \
-- | |  | |   <  | | (_| | |_) | |  __/
-- |_|  |_|_|\_\ |_|\__,_|_.__/|_|\___|
--                                     
-- «MkTable»  (to ".MkTable")

MkTable = Class {
  type = "MkTable",
  from = function (bigstr, tag)
      local mkt = MkTable {bigstr=bigstr, tag=tag, _=VTable{}}
      for k,v in mkt:gmatch() do mkt._[k] = v end
      return mkt
    end,
  __tostring = function (mkt) return mkt.bigstr end,
  __index = {
    pat = "(%S+) +%-> +(%S+)",
    gmatch = function (mkt)   return mkt.bigstr:gmatch(mkt.pat) end,
    kvs    = function (mkt)   return mkt.bigstr:gmatch(mkt.pat) end,
    v      = function (mkt,k) return mkt._[k] end,
  },
}

mktable = function (bigstr) return MkTable.from(bigstr)._ end

-- «MkTable-tests»  (to ".MkTable-tests")
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Caepro3.lua"
bigstr = [[
  foobar  -> OOBAR    (Comment)
  foo     -> OO
  foob    -> OOB
]]
mkt = MkTable.from(bigstr)
= mkt
= mkt:v("foo")
= mkt._
  for k,v in mkt:gmatch() do print(k,v) end
  for k,v in mkt:kvs() do print(k,v) end

--]==]



--     _                 ___   __ 
--    / \   _ __  _   _ / _ \ / _|
--   / _ \ | '_ \| | | | | | | |_ 
--  / ___ \| | | | |_| | |_| |  _|
-- /_/   \_\_| |_|\__, |\___/|_|  
--                |___/           
--
-- «AnyOf»  (to ".AnyOf")
-- See: (find-angg "LUA/Gram2.lua" "anyof")

AnyOf = Class {
  type = "AnyOf",
  from = function (bigstr, tag, default)
      local mt = MkTable.from(bigstr)
      return AnyOf {mt=mt, tag=tag, default=default}
    end,
  __tostring = function (mtp) return mtp.mt.bigstr end,
  __index = {
    kvs = function (mtp)    return mtp.mt:kvs() end,
    v   = function (mtp, k) return mtp.mt:v(k)  end,
    --
    -- Basic patterns:
    Cg1 = function (mtp, k) return Cc(mtp:v(k)):Cg(mtp.tag) end,
    Cg  = function (mtp, k) return mtp.tag and mtp:Cg1(k) or P"" end,
    Cs  = function (mtp, k) return Cs(k) * mtp:Cg(k) end,
    --
    -- Patterns for the default:
    Csd = function (mtp) return Cc(mtp.default) end,
    Cgd = function (mtp) return mtp:Cg(mtp.default) end,
    Cd  = function (mtp) return mtp:Csd() * mtp:Cgd() end,
    --
    pat = function (mtp)
        local p = P(false)
        for k,v in mtp:kvs() do p = p + mtp:Cs(k) end
        return p
      end,
    opt = function (mtp)
        local p = mtp:pat()
        if mtp.default then p = p + mtp:Cd() end
        return p
      end
  },
}

mktablepat = function (bigstr, tag, default)
    return AnyOf.from(bigstr, tag, default)
  end

-- «AnyOf-tests»  (to ".AnyOf-tests")
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Caepro3.lua"
bigstr = [[
  a -> AA
  b -> BB
  c -> CC
]]
mtp = AnyOf.from(bigstr, "TAG", "b")
= mtp
= mtp:v("b")
= mtp.default
s = Cs"_"
p = mtp:pat()
p = mtp:opt()
p = (s * p * s):Ct()
PP(p:match("_a_"))
PP(p:match("__"))

p = AnyOf.from(bigstr, "TAG", "b"):opt()
p = (s * p * s):Ct()
PP(p:match("_a_"))
PP(p:match("__"))

--]==]



--                           __     
--   __ _ _ __  _   _  ___  / _|___ 
--  / _` | '_ \| | | |/ _ \| |_/ __|
-- | (_| | | | | |_| | (_) |  _\__ \
--  \__,_|_| |_|\__, |\___/|_| |___/
--              |___/               
--
-- «anyofs»  (to ".anyofs")

sems = AnyOf.from([[
    x -> 2019.1   y -> 2019.2
    a -> 2020.1   b -> 2020.2
    c -> 2021.1   d -> 2021.2
    e -> 2022.1   f -> 2022.2
    g -> 2023.1
  ]], "yyyyds", "f")

Ms = AnyOf.from([[
    2 -> C2   3 -> C3   4 -> C4
    5 -> ES   7 -> LA
  ]], "MM")

otherHtmls = AnyOf.from([[
                (find-TH "2023-caepro")

    Issomuda  -> 2023-caepro0
    SobreaVR  -> 2023-caepro-VR
    Oquesobra -> 2023-caepro-o-que-sobra
    Visaud    -> 2023-visual-vs-auditivo
    Slogans   -> 2023-precisamos-de-mais-slogans

    Sapt      -> 2021aulas-por-telegram
    Somas     -> 2021-1-C2-somas-1-dicas
    Cabos     -> 2021-2-c3-cabos-na-diagonal
    AprC2     -> 2022-apresentacao-sobre-C2
    CalcEasy  -> mathologer-calculus-easy

  ]], "htmlstem")

anggPdfs = AnyOf.from([[
    Bort3  -> 2019.2-C3/Bortolossi/bortolossi-cap-3
    Bort4  -> 2019.2-C3/Bortolossi/bortolossi-cap-4
    Bort5  -> 2019.2-C3/Bortolossi/bortolossi-cap-5
    Bort6  -> 2019.2-C3/Bortolossi/bortolossi-cap-6
    Bort7  -> 2019.2-C3/Bortolossi/bortolossi-cap-7
    Bort8  -> 2019.2-C3/Bortolossi/bortolossi-cap-8
    Bort10 -> 2019.2-C3/Bortolossi/bortolossi-cap-10
    Bort11 -> 2019.2-C3/Bortolossi/bortolossi-cap-11
    Bort12 -> 2019.2-C3/Bortolossi/bortolossi-cap-12
    Leit1  -> tmp/leithold-pt-cap1
    Leit2  -> tmp/leithold-pt-cap2
    Leit3  -> tmp/leithold-pt-cap3
    Leit4  -> tmp/leithold-pt-cap4
    Leit5  -> tmp/leithold-pt-cap5
    Leit6  -> tmp/leithold-pt-cap6
    Leit7  -> tmp/leithold-pt-cap7
    Leit8  -> tmp/leithold-pt-cap8
    Leit9  -> tmp/leithold-pt-cap9
  ]], "pdfstem")

externalPdfs = AnyOf.from([[
    Miranda  -> http://hostel.ufabc.edu.br/~daniel.miranda/calculo/calculo.pdf
  ]], "pdfurl")



-- «anyofs-tests»  (to ".anyofs-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Caepro3.lua"
p = otherHtmls:pat():Ct()
PP(p:match("Slogans01:23"))

--]]





--        _     _                                _   
--   __ _| |__ | |__  _ __ _____   ___ __   __ _| |_ 
--  / _` | '_ \| '_ \| '__/ _ \ \ / / '_ \ / _` | __|
-- | (_| | |_) | |_) | | |  __/\ V /| |_) | (_| | |_ 
--  \__,_|_.__/|_.__/|_|  \___| \_/ | .__/ \__,_|\__|
--                                  |_|              
-- «abbrevpat»  (to ".abbrevpat")

defaultsem = "f"

gr,V,VA,VE,PE  = Gram.new()
V.m            = Cs(S"23457")               -- matéria: C2, C3, C4, LA, ES
V.sem          = Cs(S"xyabcdefg")           -- semestre
V.optsem       = Cs(V.sem) + Cc(defaultsem) -- semestre (opcional)
V.page         = Cs(R"09"^1)                -- página do PDF
V.optpage      = Cs(V.page) + Cc("1")       -- página do PDF (opcional)
V.numpdf       = Cs(R"09"^1)                -- número do PDF (para logs pdfizados)
V.turma        = Cs(S"cem")                 -- turma (para logs pdfizados)
V.anchor       = (P"#"^-1 * Cs(P(1)^1)) / function (str) return "#"..str end
V.optanchor    = V.anchor + Cc""

VA.Tudo        = V.m * V.optsem * Cs("T") * V.optpage
VA.semPage     = V.m * V.optsem * Cs("P") * V.optanchor
VA.quadros     = V.m * V.optsem * Cs("Q") * V.optpage
VA.quadrosJpgs = V.m * V.optsem * Cs("J") * V.optpage
VA.logPdfizado = V.m * V.optsem * Cs("L") * V.numpdf * V.turma * V.optpage
-- VA.otherHtml   = (Cs"Sapt" + Cs"Visaud" + Cs"Slogans" + Cs"Issomuda" + Cs"CalcEasy" +
--                   Cs"Cabos" + Cs"Apr") * V.optanchor
VA.otherHtml   = otherHtmls:pat() * V.optanchor
VA.anggPdf     = anggPdfs:pat() * S"pP"^-1 * V.optpage
VA.externalPdf = externalPdfs:pat() * S"pP"^-1 * V.optpage

V.any          = V.Tudo + V.semPage + V.quadros + V.quadrosJpgs
               + V.logPdfizado + V.otherHtml + V.anggPdf + V.externalPdf

abbrevpat      = gr:compile("any")

-- «abbrevpat-tests»  (to ".abbrevpat-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Caepro3.lua"
= gr:cm("Tudo",        "2eT1")
= gr:cm("Tudo",        "5T")
= gr:cm("semPage",     "2P")
= gr:cm("semPage",     "2aP#foo")
= gr:cm("semPage",     "5aP1:23")
= gr:cm("quadros",     "2xQ1")
= gr:cm("quadros",     "3Q")
= gr:cm("quadrosJpgs", "2xQ1")
= gr:cm("quadrosJpgs", "3Q")
= gr:cm("logPdfizado", "3dL2m")
= gr:cm("logPdfizado", "3dL2m42")
= gr:cm("logPdfizado", "3L2m42")
= gr:cm("otherHtml",   "Sapt")
= gr:cm("otherHtml",   "Sapt01:23")
 gr:cmp("otherHtml",   "Sapt01:23")
 gr:cmp("anggPdf",     "Bort7")
 gr:cmp("anggPdf",     "Bort7p4")
 gr:cmp("externalPdf", "Miranda")

= gr:cm("any",         "2eT1")
= gr:cm("any",         "5T")
= gr:cm("any",         "2P")
= gr:cm("any",         "2aP#foo")
= gr:cm("any",         "5aP1:23")
= gr:cm("any",         "2xQ1")
= gr:cm("any",         "3Q")
= gr:cm("any",         "2xQ1")
= gr:cm("any",         "3Q")
= gr:cm("any",         "3dL2m")
= gr:cm("any",         "3dL2m42")
= gr:cm("any",         "3L2m42")
= gr:cm("any",         "Sapt")
= gr:cm("any",         "Sapt01:23")
= abbrevpat:match      "Sapt01:23"


--]]


--     _    _     _                        
--    / \  | |__ | |__  _ __ _____   _____ 
--   / _ \ | '_ \| '_ \| '__/ _ \ \ / / __|
--  / ___ \| |_) | |_) | | |  __/\ V /\__ \
-- /_/   \_\_.__/|_.__/|_|  \___| \_/ |___/
--                                         
-- «Abbrevs»  (to ".Abbrevs")
--
Abbrevs = Class {
  type    = "Abbrevs",
  __index = {
    semestres = mktable [[
        x -> 2019.1   y -> 2019.2
        a -> 2020.1   b -> 2020.2
        c -> 2021.1   d -> 2021.2
        e -> 2022.1   f -> 2022.2
        g -> 2023.1
      ]],
    MMs = mktable [[
        2 -> C2  3 -> C3  4 -> C4
        5 -> ES  7 -> LA
      ]],
    otherHtmls = mktable [[
        Sapt     -> 2021aulas-por-telegram
        Visaud   -> 2023-visual-vs-auditivo
        Slogans  -> 2023-precisamos-de-mais-slogans
        Issomuda -> 2023-caepro0
        CalcEasy -> mathologer-calculus-easy
        Somas    -> 2021-1-C2-somas-1-dicas
        Cabos    -> 2021-2-c3-cabos-na-diagonal
        Apr      -> 2022-apresentacao-sobre-C2
      ]],
    quadros = mktable [[
        2x -> 2019.1-C2/2019.1-C2.pdf   3x -> 2019.1-C3/2019.1-C3.pdf
        2y -> 2019.2-C2/2019.2-C2.pdf   3y -> 2019.2-C3/2019.2-C3.pdf
        2e -> 2022.1-C2/C2-quadros.pdf  3e -> 2022.1-C3/C3-quadros.pdf
        2f -> 2022.2-C2/C2-quadros.pdf  3f -> 2022.2-C3/C3-quadros.pdf
        2g -> 2023.1-C2/C2-quadros.pdf
        4g -> 2023.1-C4/C4-quadros.pdf
        7g -> 2023.1-LA/LA-quadros.pdf
      ]],
    fmts = mktable [[
        Tudo        -> LATEX/<yyyy_s>-<MM>-tudo.pdf
        semPage     -> <yyyyds>-<MM>.html
        logPdfizado -> logs-pdfizados/<M><sem>L<numpdf><turma>.pdf
        otherHtml   -> <hstem>.html<a>
        anggPdf     -> <pdfstem>.pdf
        externalPdf -> <pdfurl>
        quadros     -> <quadro>
      ]],
    whats = mktable [[
        Tudo        -> pdf
        semPage     -> html
        logPdfizado -> pdf
        otherHtml   -> html
        anggPdf     -> pdf
        externalPdf -> externalpdf
        quadros     -> pdf
      ]],
    --
    yyyyds = function (ab, A) return  ab.semestres[A.sem] end,
    yyyy_s = function (ab, A) return (ab.semestres[A.sem]:gsub("%.", "-")) end,
    MM     = function (ab, A) return ab.MMs[A.M] end,
    quadro = function (ab, A) return ab.quadros[A.M..A.sem] end,
    --
    subst  = function (ab, fmt, A)
        local f = function (s)
            if s == "MM"     then return ab:MM    (A) end
            if s == "yyyyds" then return ab:yyyyds(A) end
            if s == "yyyy_s" then return ab:yyyy_s(A) end
            if s == "quadro" then return ab:quadro(A) end
            if s == "hpage"  then return A.p and "#page="..A.p or "" end
            if s == "hstem"  then return A.htmlstem end
         -- if s == "hstem"  then return ab.otherHtmls[A.short] end
            return A[s]
          end
        return (fmt:gsub("<(.-)>", f))
      end,
    --
    ast = function (ab, str) return abbrevpat:match(str) end,
    --
    A   = function (ab, str) return ab:A1(ab:ast(str)) end,
    A1  = function (ab, ast) return HTable(ab:As(ast)[ast[0]]) end,
    As  = function (ab, ast)
        return VTable {
          Tudo        = {M=ast[1], sem=ast[2], p=ast[4]},
          semPage     = {M=ast[1], sem=ast[2], a=ast[4]},
          quadros     = {M=ast[1], sem=ast[2], p=ast[4]},
          logPdfizado = {M=ast[1], sem=ast[2], numpdf=ast[4], turma=ast[5], p=ast[6]},
          otherHtml   = {short=ast[1], a=ast[2], htmlstem=ast.htmlstem},
          anggPdf     = {short=ast[1], p=ast[2], pdfstem=ast.pdfstem},
          externalPdf = {short=ast[1], p=ast[2], pdfurl=ast.pdfurl},
        }
      end,
    --
    fmt   = function (ab, str) return ab:fmt0(ab:ast(str)) end,
    fmt0  = function (ab, ast) return ab:subst(ab:fmt00(ast), ab:A1(ast)) end,
    fmt00 = function (ab, ast) return ab.fmts[ast[0]] end,
    --
    reduce = function (ab, str)
        local ast  = ab:ast(str)
        local what = ab.whats[ast[0]]
        local page = ab:A(str).p
        return what, ab:fmt(str), page
      end,
    --
    pcall = function (ab, f)
        local ok,result = pcall(f)
        if ok then return result end
      end,
    sexp0 = function (ab, str)
        local what,baseurl,page = ab:reduce(str)
        local spage = page and " "..page or ""
        if what == "html" then return format('(brg "file:///home/edrx/TH/L/%s")', baseurl) end
        if what == "pdf"  then return format('(find-pdf "~/%s"%s)', baseurl, spage) end
        if what == "externalpdf" then
          return format('(find-pdf-page (ee-url-to-fname "%s")%s)', baseurl, spage)
        end
      end,
    anggurl0 = function (ab, str)
        local what,baseurl,page = ab:reduce(str)
        local prefix = (what == "externalpdf") and "" or "http://anggtwu.net/"
        local hpage = page and "#page="..page or ""
        return prefix..baseurl..hpage
      end,
    sexp    = function (ab, str) return ab:pcall(function() return ab:sexp0(str)    end) end,
    anggurl = function (ab, str) return ab:pcall(function() return ab:anggurl0(str) end) end,
  },
}


-- «Abbrevs-tests»  (to ".Abbrevs-tests")
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Caepro3.lua"
ab = Abbrevs {}
= ab.semestres
=  ab:yyyyds                               {sem="x"}
=  ab:yyyy_s                               {sem="x"}
=  ab:subst  ("<sem>:<yyyyds>:<yyyy_s>",   {sem="x"})
= ab.MMs
=  ab:MM                            {M="5"}
=  ab:subst  ("<M>:<MM>",           {M="5"})
= ab.quadros
=  ab:quadro                        {M="2", sem="x"}
=  ab:subst  ("<M>:<sem>:<quadro>", {M="2", sem="x"})
= ab.otherHtmls
=  ab:subst  ("<short>:<hstem>",    {short="Cabos"})

=       ab:ast "2yT1"
= ab:As(ab:ast "2yT1")
= ab:A1(ab:ast "2yT1")
= ab:A         "2yT1"

= ab:fmt00 {[0]="Tudo"}
= ab:fmt0 (ab:ast "2yT1")
= ab:fmt          "2yT1"
= ab:fmt          "2T"
= ab:fmt          "5T"

= ab:fmt          "Cabos"
= ab:ast          "Cabos"
= ab:fmt          "Cabos"
= ab:fmt          "Cabos01:23"
= ab:fmt          "Cabos#01:23"
= ab:fmt          "Bort7"

= ab:reduce       "Cabos"
= ab:reduce       "Cabos#01:23"
= ab:reduce       "2T"
= ab:reduce       "2T66"

= ab:anggurl      "Cabos#01:12"
= ab:anggurl      "2T66"
= ab:sexp         "Cabos#01:12"
= ab:sexp         "2T66"
= ab:sexp         "Bort7"
= ab:sexp         "Bort7p12"

= ab:fmt          "Miranda"
= ab:reduce       "Miranda"
= ab:reduce       "Miranda65"
= ab:sexp         "Miranda"
= ab:sexp         "Miranda65"
= ab:anggurl      "Miranda65"

-- (brxpdfl "http://hostel.ufabc.edu.br/~daniel.miranda/calculo/calculo.pdf" 65)
-- (find-pdf-page "http://hostel.ufabc.edu.br/~daniel.miranda/calculo/calculo.pdf" 65)
-- (find-pdf-page "$S/http/hostel.ufabc.edu.br/~daniel.miranda/calculo/calculo.pdf" 65)

= ab:sexp0        "f2wT66"   -- err
= ab:sexp         "f2wT66"   -- err

--]==]


-- «run_options»  (to ".run_options")
-- (find-angg ".emacs" "caepro3")
-- (find-es "lua5" "run_options")
--
run_options = function (a, b)
    ab = Abbrevs {}
    if     a == nil       then return
    elseif a == "Caepro3" then return
    elseif a == "-sexp"   then print(ab:sexp(b))
    elseif a == "-url"    then print(ab:anggurl(b))
    else PP("Bad options:", a, b)
    end
  end

run_options(...)

-- «run_options-tests»  (to ".run_options-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Caepro3.lua"
run_options("-sexp", "2T66")
run_options("-sexp", "_2T66")
run_options("-sexp", "Leit4")
run_options("-sexp", "Leit4p10")
run_options("-sexp", "7gQ")

* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
require "Caepro3"

--]]




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