Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
-- This file:
--   https://anggtwu.net/LUA/DiagTikz1.lua.html
--   https://anggtwu.net/LUA/DiagTikz1.lua
--           (find-angg "LUA/DiagTikz1.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
-- (With a lot of help from Leandro Abib)
-- Version: 2026jun21
--
-- (defun e () (interactive) (find-angg "LUA/DiagTikz1.lua"))
-- (defun o () (interactive) (find-angg "LUA/DiagForth1.lua"))
-- (find-dn6 "diagtikz.lua")
-- (find-dn6 "diagforth.lua" "diagram")
-- (find-dn6 "diagforth.lua" "enddiagram")

-- «.tforths»			(to "tforths")
-- «.TikzForth»			(to "TikzForth")
-- «.txyrun»			(to "txyrun")
-- «.diagram»			(to "diagram")
-- «.enddiagram»		(to "enddiagram")
-- «.2D-and-2Dx»		(to "2D-and-2Dx")
-- «.arrows»			(to "arrows")
-- «.diagmiddle»		(to "diagmiddle")
-- «.coords»			(to "coords")
-- «.Tikz_nodes»		(to "Tikz_nodes")
-- «.Tikz_arrows»		(to "Tikz_arrows")
-- «.arrow-modifiers»		(to "arrow-modifiers")
-- «.Tikz_diag_body»		(to "Tikz_diag_body")
-- «.tdiag-head»		(to "tdiag-head")
-- «.preamble»			(to "preamble")
-- «.test»			(to "test")

-- (find-angg "LUA/Tikz2.lua")
-- (find-angg "LUA/Tikz2.lua" "test-loeliger")
require "Tikz2"   -- (find-angg "LUA/Tikz2.lua")
require "Pict4"   -- (find-angg "LUA/Pict4.lua")

-- «tforths»  (to ".tforths")
-- Based on: (find-angg "LUA/DiagForth1.lua" "forths")
tforths = tforths or {}  -- Similar to forths[], 
tforthe = {}             -- Similar to tforths[], but defines eaters.


-- «TikzForth»  (to ".TikzForth")
-- Based on: (find-angg "LUA/DiagForth1.lua")
--
TikzForth = Class {
  type    = "TikzForth",
  __index = {
    w     = function (tf) return getword() or werror() end,
    e     = function (tf) return getwordasluaexpr()    end,
    r     = function (tf) return getrestofline()       end,
    w_w   = function (tf) return tf:w(),tf:w()         end,
    w_w_w = function (tf) return tf:w(),tf:w(),tf:w()  end,
    e_e   = function (tf) return tf:e(),tf:e()         end,
    e_e_e = function (tf) return tf:e(),tf:e(),tf:e()  end,
    e_e_w = function (tf) return tf:e(),tf:e(),tf:w()  end,
    --
    getargs = function (tf,word)
        local ga = tforthe[word]
        if not ga then return end
        if type(ga) == "function" then return ga() end
        if type(ga) == "string" then
          local methodname = ga:gsub(",", "_")
          if not tf[methodname] then
	    error("Bad getargs method in "..word.." -> "..methodname)
	  end
	  return tf[methodname](tf)
	end
	error("Bad getargs in word: "..word)
      end,
    txyrun1 = function (tf,word)
        if   tforths[word] then tforths[word](tf:getargs(word))
        elseif nodes[word] then ds:push(nodes[word])
        else Error("Unknown word: "..word)
        end
      end,
    txyrun = function (tf,str,pos_)
        local oldvalues = pack(subj,startcol,endcol,word,pos)
        setsubj(str, pos_ or 1)
        while getword() do tf:txyrun1(word) end
        subj,startcol,endcol,word,pos = unpack(oldvalues)
      end,
    --
    run1 = function (tf,cmd)
        if type(cmd) == "string" then cmd = {cmd} end
	local word = cmd[1]
        if not forths[word] then error("Unknown word: "..word) end
	forths[word](unpack(cmd, 2))
      end,
    --
  },
}

-- «txyrun»  (to ".txyrun")
-- Based on: (find-angg "LUA/DiagForth1.lua" "dxyrun")
txyrun = function (str, pos_)
    TikzForth{}:txyrun(str,pos_)
  end


-- «diagram»  (to ".diagram")
-- Based on: (find-dn6 "diagforth.lua" "diagram")
tforthe["diagram"] = "w"
tforths["diagram"] = function (name)
    xys = {}
    nodes  = VerticalTable {}
    arrows = VerticalTable {}
    lasty = nil
    diagramname = name
  end

-- «enddiagram»  (to ".enddiagram")
-- Based on: (find-dn6 "diagforth.lua" "enddiagram")
tforths["enddiagram"] = function ()
  --Tikz_diag_def(diagramname, tf:hyperlink()):output()
  --Tikz_diag_def(diagramname):output()
    Tikz_def_diag(diagramname):output()
  end




-- «2D-and-2Dx»  (to ".2D-and-2Dx")
-- Based on: (find-dn6 "diagforth.lua" "2D-and-2Dx")
tforths["2Dx"] = dxy2Dx
tforths["2D"]  = dxy2D

-- Based on: (find-dn6 "diagforth.lua" "forths")
tforthe["#"] = "r"
tforths["#"] = function () end

-- Based on: (find-dn6 "diagforth.lua" "dxyren")
tforthe["ren"] = "r"
tforths["ren"] = function (restofline) dxyren(restofline) end

-- Based on: (find-dn6 "diagforth.lua" "metastack")
tforths["(("] = function () depths:ppush() end
tforths["))"] = function () depths:ppop() end

-- «arrows»  (to ".arrows")
-- Based on: (find-dn6 "diagforth.lua" "arrows")
tpusharrow = function (drawopts, pathop)
    local from, to = ds:pick(1), ds:pick(0)
    from.used = 1
    to  .used = 1
    ds:push(storearrow(DxyArrow {
      from     = from.noden,
      to       = to  .noden,
      drawopts = drawopts,
      pathop   = pathop or "--"
    }))
  end

-- See: (find-es "tikz" "leandro-abib")
tforths["->"]  = function () tpusharrow("->")  end
tforths["|->"] = function () tpusharrow("|->") end
tforths["<-"]  = function () tpusharrow("<-")  end
tforths["<-|"] = function () tpusharrow("<-|") end
tforths["<->"] = function () tpusharrow("<->") end

-- «diagmiddle»  (to ".diagmiddle")
-- Based on: (find-angg "LUA/DiagForth1.lua" "diagmiddle")
tforthe["harrownodes"]  = "e,e,e"
tforthe["varrownodes"]  = "e,e,e"
tforthe["dharrownodes"] = "e,e,e"
tforthe["dvarrownodes"] = "e,e,e"
tforths["harrownodes"]  = function (dx0, dx1, dx2)  harrownodes(dx0, dx1, dx2) end
tforths["varrownodes"]  = function (dx0, dx1, dx2)  varrownodes(dx0, dx1, dx2) end
tforths["dharrownodes"] = function (dx0, dx1, dx2) dharrownodes(dx0, dx1, dx2) end
tforths["dvarrownodes"] = function (dx0, dx1, dx2) dvarrownodes(dx0, dx1, dx2) end


-- «coords»  (to ".coords")
-- Based on: (find-dn6 "diagtex.lua" "coords")
txyorigx = 100
txyorigy = 100
txyscale = 0.064
trealx = function (x) return  txyscale * (x - txyorigx) end
trealy = function (y) return -txyscale * (y - txyorigy) end
trealxy = function (x, y) return trealx(x), trealy(y) end

-- «Tikz_nodes»  (to ".Tikz_nodes")
-- See: (find-es "tikz" "leandro-abib")
--      \node[anchor = center] (RC) at (3,2) {$RC$};
Tikz_node_tag  = function (node) return node.tag or node.noden end
Tikz_node_tex  = function (node) return node.TeX or node.tex or node.tag end
Tikz_node_decl = function (node)
    local x,y = trealxy(node.x, node.y)
    local tag = Tikz_node_tag(node)
    local tex = Tikz_node_tex(node)
    return pformat("\\node [anchor=center] (%s) at (%s,%s) {$%s$};\n", tag, x,y, tex)
  end
Tikz_nodes = function ()
    local decls = VTable {}
    for i=1,#nodes do
      if nodes[i].used then table.insert(decls, Tikz_node_decl(nodes[i])) end
    end
    return table.concat(decls)
  end

-- «Tikz_arrows»  (to ".Tikz_arrows")
-- Based on: (find-dn6 "diagtex.lua" "arrow_to_TeX")
--      See: (find-es "tikz" "leandro-abib")
-- \draw [<-] (A4) -- (A5) node[midway,above] {$\scriptstyle L$};
Tikz_arrow_plabel = function (arrow)
    local placement = arrow.placement
    local label     = arrow.label
    if not (placement and label) then return "" end
    local expand    = {a="above", b="below", l="left", r="right"}
    local fmt       = " node[midway,%s] {$\\scriptstyle %s$}"
    return format(fmt, expand[placement] or placement, label)
  end
Tikz_arrow_tex = function (arrow)
    local tag_from = Tikz_node_tag(nodes[arrow.from])
    local tag_to   = Tikz_node_tag(nodes[arrow.to])
    local drawopts = arrow.drawopts
    local pathop   = arrow.pathop
    local plabel   = Tikz_arrow_plabel(arrow)
    return format("\\draw [%s] (%s) %s (%s)%s;\n", drawopts, tag_from, pathop, tag_to, plabel)
  end
Tikz_arrows = function ()
    return mapconcat(Tikz_arrow_tex, arrows)
  end

-- «arrow-modifiers»  (to ".arrow-modifiers")
-- Based on: (find-angg "LUA/DiagForth1.lua" "arrow-modifiers")
tforthe[".plabel="] = "w,w"
tforths[".plabel="] = function (p,label)
    ds:pick(0).placement = p
    ds:pick(0).label     = label
  end

-- No-ops at this moment:
tforths["sl^^"] = function () ds:pick(0).slide =    "5pt" end
tforths["sl^"]  = function () ds:pick(0).slide =  "2.5pt" end
tforths["sl_"]  = function () ds:pick(0).slide = "-2.5pt" end
tforths["sl__"] = function () ds:pick(0).slide =   "-5pt" end


-- «Tikz_diag_body»  (to ".Tikz_diag_body")
-- See: (find-angg "LUA/PictRect1.lua" "Pict")
--      (find-angg "LUA/PictRect1.lua" "Pict" "wrapbe")
--      (find-dn6 "diagtex.lua" "arrows_to_defdiag")
Tikz_diag_body = function ()
    return Pict(Rect.from(Tikz_nodes()..Tikz_arrows()))
  end
Tikz_diag = function ()
    return Tikz_diag_body():wrapbe("\\begin{tikzpicture}", "\\end{tikzpicture}")
  end
Tikz_def_diag_topline = function (name)
    return format("\\def\\TikzDiag%s{", name)
  end
Tikz_def_diag = function (name, hyperlink)
  --local topline = format("\\deftdiag {%s} {   %% %s", name, hyperlink or "")
    local topline = format("\\deftdiag {%s} {", name)
    local topline = format("\\deftdiag{%s}{", name)
    local topline = Tikz_def_diag_topline(name)
    return Tikz_diag():wrapbe(topline, "}")
  end


-- «tdiag-head»  (to ".tdiag-head")
-- Based on: (find-dn6 "heads6.lua" "diag-head")
registerhead "%T" {
  name = "tdiag",
  action = function ()
      local i,j,diaglines = tf:getblock()
    --for n=i,j do txyrun(untabify(texlines:line(n)), 3, n) end
      for n=i,j do txyrun(untabify(tf:line(n)), 3, n) end
    end,
}



-- «preamble»  (to ".preamble")
-- (find-LATEX "2026diagtikz-test.tex" "diagtikz-preamble")
--[==[
% Based on: (find-dn6 "preamble6.lua" "preamble1" "defdiag")
\def\deftdiag      #1#2{\expandafter\def\csname tdiag-#1\endcsname{#2}}
\def\iftdiagundefined#1{\expandafter\ifx\csname tdiag-#1\endcsname\relax}
\def\tdiag#1{\iftdiagundefined{#1}
    \errmessage{UNDEFINED TIKZ DIAGRAM: #1}
  \else
    \csname tdiag-#1\endcsname
  \fi
}
--]==]


-- (find-dn6 "diagtex.lua" "DxyArrow")
-- (find-dn6 "diagtex.lua" "arrow_to_TeX")



-- «test»  (to ".test")
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
loaddednat7()        -- (find-angg "LUA/lua50init.lua" "loaddednat7")
dednat7oldheads()    -- (find-angg "LUA/dednat7load.lua" "requires")
require "DiagTikz1"

-- (find-efunction 'find-dednat7debug-links)
-- tf       = TeXFile.read("/dev/null")
-- texlines = tf

txyrun [[ diagram Foo        ]]
txyrun [[ 2Dx     100    +20 ]]
txyrun [[ 2D  100 A0 <-| A1  ]]
txyrun [[ 2D      |       |  ]]
txyrun [[ 2D      |  <->  |  ]]
txyrun [[ 2D      v       v  ]]
txyrun [[ 2D  +20 A2 |-> A3  ]]
txyrun [[ 2D                 ]]
txyrun [[ ren A0 A1 ==> LA A ]]
txyrun [[ ren A2 A3 ==> B RB ]]
txyrun [[                    ]]
txyrun [[ (( A0 A1 <-|       ]]
txyrun [[    A0 A2 ->        ]]
txyrun [[    A1 A3 ->        ]]
txyrun [[    A2 A3 |->       ]]
txyrun [[ ))                 ]]
txyrun [[ enddiagram         ]]


= nodes
= arrows

= Tikz_nodes()
txyscale = 0.1
= Tikz_nodes()
= Tikz_arrows()
= Tikz_diag_body()
= Tikz_diag()
= Tikz_diag_def("foo")
= Tikz_diag_def("foo", "(bar)")

= Rect.from(Tikz_nodes()..Tikz_arrows())
= Pict(Rect.from(Tikz_nodes()..Tikz_arrows()))




-- (find-angg "LUA/Tos.lua")


--]==]






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