Display an outline as a nested table: Difference between revisions

Added AutoHotkey
No edit summary
(Added AutoHotkey)
Line 80:
Display your nested table on this page.
<br>
 
=={{header|AutoHotkey}}==
<lang AutoHotkey>outline2table(db, Delim:= "`t"){
oNum:=[], oMID:=[], oNod := [], oKid := [], oPnt := [], oMbr := [], oLvl := []
oCrl := ["#ffffe6;", "#ffebd2;", "#f0fff0;", "#e6ffff;", "#ffeeff;"]
col := 0, out := "", anc := ""
; create numerical index for each line
for i, line in StrSplit(db, "`n", "`r")
{
RegExMatch(line, "^(\t*)(.*)$", m)
out .= m1 . i "`n"
oNum[i] := m2
}
db := Trim(out, "`n")
 
; create list of members, parents, kids and their ancestors
for i, mbr in StrSplit(db, "`n", "`r")
{
lvl := 1
While (SubStr(mbr, 1, 1) = Delim)
lvl++, mbr := SubStr(mbr, 2)
if (pLvl >= lvl) && pMbr
col++
, oMbr[pLvl, pMbr] .= "col:" col ",anc:" anc
, oKid[pLvl, pMbr] .= "col:" col ",anc:" anc
if (pLvl > lvl) && pMbr
loop % pLvl - lvl
anc := RegExReplace(anc, "\d+_?$")
if (pLvl < lvl) && pMbr
anc .= pMbr "_"
, oMbr[pLvl, pMbr] .= "col:" col+1 ",anc:" anc
, oPnt[pLvl, pMbr] .= "col:" col+1 ",anc:" anc
pLvl := lvl
pMbr := mbr
;~ oMID[lvl] := TV_Add(mbr, oMID[lvl-1], "Expand")
}
; last one on the list
col++
oMbr[pLvl, pMbr] .= "col:" col ",anc:" anc
oKid[pLvl, pMbr] .= "col:" col ",anc:" anc
 
; setup node color
clr := 1
for lvl, obj in oMbr
for node, str in obj
if (lvl <= 2)
oNod[node, "clr"] := clr++
else
oNod[node, "clr"] := oNod[StrSplit(str, "_").2, "clr"]
; setup node level/column/width
for lvl, obj in oKid
for node, str in obj
{
x := StrSplit(str, ",")
col := StrReplace(x.1, "col:")
anc := Trim(StrReplace(x.2, "anc:"), "_")
for j, a in StrSplit(anc, "_")
oNod[a, "wid"] := (oNod[a, "wid"]?oNod[a, "wid"]:0) + 1
oNod[node, "lvl"] := lvl
oNod[node, "col"] := col
oNod[node, "wid"] := 1
}
for lvl, obj in oPnt
for node, str in obj
{
x := StrSplit(str, ",")
col := StrReplace(x.1, "col:")
anc := Trim(StrReplace(x.2, "anc:"), "_")
oNod[node, "lvl"] := lvl
oNod[node, "col"] := col
}
; setup members by level
for node, obj in oNod
oLvl[obj["lvl"], node] := 1
maxW := 0
for node in oLvl[1]
maxW += oNod[node, "wid"]
; setup HTML
html := "<table class=""wikitable"" style=""text-align: center;"">`n"
for lvl, obj in oLvl
{
pCol := 1
html .= "<tr>`n"
for node, bool in obj
{
while (oNod[node, "col"] <> pCol)
pCol++, html .= "`t<td style=""background: #F9F9F9;""></td>`n"
pCol += oNod[node, "wid"]
if !cNum := Mod(oNod[node, "clr"], 5)
cNum := 5
html .= "`t<td style=""background: " oCrl[cNum] """ colspan=""" oNod[node, "wid"] """>" oNum[node] "</td>`n"
}
while (pCOl <= maxW)
pCol++, html .= "`t<td style=""background: #F9F9F9;""></td>`n"
html .= "</tr>`n"
}
html .= "</table>"
; setup wikitable
wTable := "{| class=""wikitable"" style=""text-align: center;""`n"
for lvl, obj in oLvl
{
pCol := 1
wTable .= "|-`n"
for node, bool in obj
{
while (oNod[node, "col"] <> pCol)
pCol++, wTable .= "| | `n"
pCol += oNod[node, "wid"]
if !cNum := Mod(oNod[node, "clr"], 5)
cNum := 5
wTable .= "| style=""background: " oCrl[cNum] """ colspan=""" oNod[node, "wid"] " |" oNum[node] "`n"
}
while (pCOl <= maxW)
pCol++, wTable .= "| | `n"
}
wTable .= "|}`n"
return [html, wTable]
}
</lang>
Examples:<lang AutoHotkey>db =
(
Display an outline as a nested table.
Parse the outline to a tree,
measuring the indent of each line,
translating the indentation to a nested structure,
and padding the tree to even depth.
count the leaves descending from each node,
defining the width of a leaf as 1,
and the width of a parent node as a sum.
(The sum of the widths of its children)
and write out a table with 'colspan' values
either as a wiki table,
or as HTML.
)
 
Gui, add, ActiveX, vDocument w1000 r14, HTMLFile
result := outline2table(db)
Document.Write(result.1)
Gui, Show
MsgBox % "HTML:`n" result.1 "`n`nWikitable:`n" result.2
return</lang>
{{out}}
HTML:
<table class="wikitable" style="text-align: center;">
<tr>
<td style="background: #ffffe6;" colspan="7">Display an outline as a nested table.</td>
</tr>
<tr>
<td style="background: #ffebd2;" colspan="3">Parse the outline to a tree,</td>
<td style="background: #f0fff0;" colspan="2">count the leaves descending from each node,</td>
<td style="background: #e6ffff;" colspan="2">and write out a table with 'colspan' values</td>
</tr>
<tr>
<td style="background: #ffebd2;" colspan="1">measuring the indent of each line,</td>
<td style="background: #ffebd2;" colspan="1">translating the indentation to a nested structure,</td>
<td style="background: #ffebd2;" colspan="1">and padding the tree to even depth.</td>
<td style="background: #f0fff0;" colspan="1">defining the width of a leaf as 1,</td>
<td style="background: #f0fff0;" colspan="1">and the width of a parent node as a sum.</td>
<td style="background: #e6ffff;" colspan="1">either as a wiki table,</td>
<td style="background: #e6ffff;" colspan="1">or as HTML.</td>
</tr>
<tr>
<td style="background: #F9F9F9;"></td>
<td style="background: #F9F9F9;"></td>
<td style="background: #F9F9F9;"></td>
<td style="background: #F9F9F9;"></td>
<td style="background: #f0fff0;" colspan="1">(The sum of the widths of its children)</td>
<td style="background: #F9F9F9;"></td>
<td style="background: #F9F9F9;"></td>
</tr>
</table>
 
Wikitable:
{| class="wikitable" style="text-align: center;"
|-
| style="background: #ffffe6;" colspan="7 |Display an outline as a nested table.
|-
| style="background: #ffebd2;" colspan="3 |Parse the outline to a tree,
| style="background: #f0fff0;" colspan="2 |count the leaves descending from each node,
| style="background: #e6ffff;" colspan="2 |and write out a table with 'colspan' values
|-
| style="background: #ffebd2;" colspan="1 |measuring the indent of each line,
| style="background: #ffebd2;" colspan="1 |translating the indentation to a nested structure,
| style="background: #ffebd2;" colspan="1 |and padding the tree to even depth.
| style="background: #f0fff0;" colspan="1 |defining the width of a leaf as 1,
| style="background: #f0fff0;" colspan="1 |and the width of a parent node as a sum.
| style="background: #e6ffff;" colspan="1 |either as a wiki table,
| style="background: #e6ffff;" colspan="1 |or as HTML.
|-
| |
| |
| |
| |
| style="background: #f0fff0;" colspan="1 |(The sum of the widths of its children)
| |
| |
|}
 
=={{header|Go}}==
299

edits