Display an outline as a nested table: Difference between revisions
Content added Content deleted
Line 1,131: | Line 1,131: | ||
</ol> |
</ol> |
||
</div> |
</div> |
||
=={{header|Phix}}== |
|||
Can output in either html or wikitable markup |
|||
<lang Phix>constant html = false, |
|||
outlines = {""" |
|||
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.""", """ |
|||
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) |
|||
Propagating the sums upward as necessary. |
|||
and write out a table with 'colspan' values |
|||
either as a wiki table, |
|||
or as HTML. |
|||
Optionally add color to the nodes."""} |
|||
constant yellow = "#ffffe6;", |
|||
orange = "#ffebd2;", |
|||
green = "#f0fff0;", |
|||
blue = "#e6ffff;", |
|||
pink = "#ffeeff;", |
|||
colours = {{yellow, orange, green, blue, pink}, |
|||
{blue, yellow, orange, green, pink}} |
|||
function calc_spans(sequence lines, integer ldx) |
|||
sequence children = lines[ldx][$] |
|||
if length(children)!=0 then |
|||
integer span = 0 |
|||
for i=1 to length(children) do |
|||
integer child = children[i] |
|||
lines = calc_spans(lines,child) |
|||
span += lines[child][4] |
|||
end for |
|||
lines[ldx][4] = span |
|||
-- else -- (span already 1) |
|||
end if |
|||
return lines |
|||
end function |
|||
procedure markup(string outline, sequence colours) |
|||
sequence lines = split(outline,"\n",no_empty:=true), |
|||
pi = {}, -- indents (to locate parents) |
|||
pdx = {}, -- indexes for "" |
|||
children = {} |
|||
string text |
|||
integer maxdepth = 0, |
|||
parent, depth, span |
|||
for i=1 to length(lines) do |
|||
string line = trim_tail(lines[i]) |
|||
text = trim_head(line) |
|||
integer indent = length(line)-length(text) |
|||
-- remove any completed parents |
|||
while length(pi) and indent<=pi[$] do |
|||
pi = pi[1..$-1] |
|||
pdx = pdx[1..$-1] |
|||
end while |
|||
parent = 0 |
|||
if length(pi) then |
|||
parent = pdx[$] |
|||
lines[parent][$] &= i -- (update children) |
|||
end if |
|||
pi &= indent |
|||
pdx &= i |
|||
depth = length(pi) |
|||
span = 1 -- (default/assume no children[=={}]) |
|||
lines[i] = {i,depth,indent,span,parent,text,children} |
|||
maxdepth = max(maxdepth,depth) |
|||
end for |
|||
lines = calc_spans(lines,1) |
|||
string res = iff(html?"<table class=\"wikitable\" style=\"text-align: center;\">\n" |
|||
:"{| class=\"wikitable\" style=\"text-align: center;\"\n") |
|||
for d=1 to maxdepth do |
|||
res &= iff(html?"<tr>\n" |
|||
:"|-\n") |
|||
integer cdx = 1 |
|||
for i=1 to length(lines) do |
|||
{{},depth,{},span,parent,text,children} = lines[i] |
|||
if depth=2 then cdx += 1 end if |
|||
string style = sprintf(`style="background: %s"`,{colours[cdx]}) |
|||
if depth=d then |
|||
if span!=1 then style &= sprintf(` colspan="%d"`,span) end if |
|||
res &= sprintf(iff(html?"<td %s>%s</td>\n" |
|||
:"| %s | %s\n"),{style,text}) |
|||
elsif depth<d and children={} then |
|||
-- res &= iff(html?"<td></td>\n" |
|||
-- :"| |\n") |
|||
res &= sprintf(iff(html?"<td %s></td>\n" |
|||
:"| %s |\n"),{style}) |
|||
end if |
|||
end for |
|||
if html then |
|||
res &= "</tr>\n" |
|||
end if |
|||
end for |
|||
res &= iff(html?"</table>\n" |
|||
:"|}\n") |
|||
puts(1,res) |
|||
end procedure |
|||
for i=1 to length(outlines) do |
|||
markup(outlines[i],colours[i]) |
|||
end for</lang> |
|||
{{out}} |
|||
in 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;">measuring the indent of each line,</td> |
|||
<td style="background: #ffebd2;">translating the indentation to a nested structure,</td> |
|||
<td style="background: #ffebd2;">and padding the tree to even depth.</td> |
|||
<td style="background: #f0fff0;">defining the width of a leaf as 1,</td> |
|||
<td style="background: #f0fff0;">and the width of a parent node as a sum.</td> |
|||
<td style="background: #e6ffff;">either as a wiki table,</td> |
|||
<td style="background: #e6ffff;">or as HTML.</td> |
|||
</tr> |
|||
<tr> |
|||
<td style="background: #ffebd2;"></td> |
|||
<td style="background: #ffebd2;"></td> |
|||
<td style="background: #ffebd2;"></td> |
|||
<td style="background: #f0fff0;"></td> |
|||
<td style="background: #f0fff0;">(The sum of the widths of its children)</td> |
|||
<td style="background: #e6ffff;"></td> |
|||
<td style="background: #e6ffff;"></td> |
|||
</tr> |
|||
</table> |
|||
and |
|||
<table class="wikitable" style="text-align: center;"> |
|||
<tr> |
|||
<td style="background: #e6ffff;" colspan="9">Display an outline as a nested table.</td> |
|||
</tr> |
|||
<tr> |
|||
<td style="background: #ffffe6;" colspan="3">Parse the outline to a tree,</td> |
|||
<td style="background: #ffebd2;" colspan="3">count the leaves descending from each node,</td> |
|||
<td style="background: #f0fff0;" colspan="2">and write out a table with 'colspan' values</td> |
|||
<td style="background: #ffeeff;">Optionally add color to the nodes.</td> |
|||
</tr> |
|||
<tr> |
|||
<td style="background: #ffffe6;">measuring the indent of each line,</td> |
|||
<td style="background: #ffffe6;">translating the indentation to a nested structure,</td> |
|||
<td style="background: #ffffe6;">and padding the tree to even depth.</td> |
|||
<td style="background: #ffebd2;">defining the width of a leaf as 1,</td> |
|||
<td style="background: #ffebd2;" colspan="2">and the width of a parent node as a sum.</td> |
|||
<td style="background: #f0fff0;">either as a wiki table,</td> |
|||
<td style="background: #f0fff0;">or as HTML.</td> |
|||
<td style="background: #ffeeff;"></td> |
|||
</tr> |
|||
<tr> |
|||
<td style="background: #ffffe6;"></td> |
|||
<td style="background: #ffffe6;"></td> |
|||
<td style="background: #ffffe6;"></td> |
|||
<td style="background: #ffebd2;"></td> |
|||
<td style="background: #ffebd2;">(The sum of the widths of its children)</td> |
|||
<td style="background: #ffebd2;">Propagating the sums upward as necessary.</td> |
|||
<td style="background: #f0fff0;"></td> |
|||
<td style="background: #f0fff0;"></td> |
|||
<td style="background: #ffeeff;"></td> |
|||
</tr> |
|||
</table> |
|||
or in wikitable markup: |
|||
{| 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;" | measuring the indent of each line, |
|||
| style="background: #ffebd2;" | translating the indentation to a nested structure, |
|||
| style="background: #ffebd2;" | and padding the tree to even depth. |
|||
| style="background: #f0fff0;" | defining the width of a leaf as 1, |
|||
| style="background: #f0fff0;" | and the width of a parent node as a sum. |
|||
| style="background: #e6ffff;" | either as a wiki table, |
|||
| style="background: #e6ffff;" | or as HTML. |
|||
|- |
|||
| style="background: #ffebd2;" | |
|||
| style="background: #ffebd2;" | |
|||
| style="background: #ffebd2;" | |
|||
| style="background: #f0fff0;" | |
|||
| style="background: #f0fff0;" | (The sum of the widths of its children) |
|||
| style="background: #e6ffff;" | |
|||
| style="background: #e6ffff;" | |
|||
|} |
|||
and |
|||
{| class="wikitable" style="text-align: center;" |
|||
|- |
|||
| style="background: #e6ffff;" colspan="9" | Display an outline as a nested table. |
|||
|- |
|||
| style="background: #ffffe6;" colspan="3" | Parse the outline to a tree, |
|||
| style="background: #ffebd2;" colspan="3" | count the leaves descending from each node, |
|||
| style="background: #f0fff0;" colspan="2" | and write out a table with 'colspan' values |
|||
| style="background: #ffeeff;" | Optionally add color to the nodes. |
|||
|- |
|||
| style="background: #ffffe6;" | measuring the indent of each line, |
|||
| style="background: #ffffe6;" | translating the indentation to a nested structure, |
|||
| style="background: #ffffe6;" | and padding the tree to even depth. |
|||
| style="background: #ffebd2;" | defining the width of a leaf as 1, |
|||
| style="background: #ffebd2;" colspan="2" | and the width of a parent node as a sum. |
|||
| style="background: #f0fff0;" | either as a wiki table, |
|||
| style="background: #f0fff0;" | or as HTML. |
|||
| style="background: #ffeeff;" | |
|||
|- |
|||
| style="background: #ffffe6;" | |
|||
| style="background: #ffffe6;" | |
|||
| style="background: #ffffe6;" | |
|||
| style="background: #ffebd2;" | |
|||
| style="background: #ffebd2;" | (The sum of the widths of its children) |
|||
| style="background: #ffebd2;" | Propagating the sums upward as necessary. |
|||
| style="background: #f0fff0;" | |
|||
| style="background: #f0fff0;" | |
|||
| style="background: #ffeeff;" | |
|||
|} |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |