Display an outline as a nested table: Difference between revisions

Content added Content deleted
m (J: better contrast)
m (syntax highlighting fixup automation)
Line 82: Line 82:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>outline2table(db, Delim:= "`t"){
<syntaxhighlight lang="autohotkey">outline2table(db, Delim:= "`t"){
oNum:=[], oMID:=[], oNod := [], oKid := [], oPnt := [], oMbr := [], oLvl := []
oNum:=[], oMID:=[], oNod := [], oKid := [], oPnt := [], oMbr := [], oLvl := []
oCrl := ["#ffffe6;", "#ffebd2;", "#f0fff0;", "#e6ffff;", "#ffeeff;"]
oCrl := ["#ffffe6;", "#ffebd2;", "#f0fff0;", "#e6ffff;", "#ffeeff;"]
Line 211: Line 211:
return [html, wTable]
return [html, wTable]
}
}
</syntaxhighlight>
</lang>
Examples:<lang AutoHotkey>db =
Examples:<syntaxhighlight lang="autohotkey">db =
(
(
Display an outline as a nested table.
Display an outline as a nested table.
Line 233: Line 233:
Gui, Show
Gui, Show
MsgBox % "HTML:`n" result.1 "`n`nWikitable:`n" result.2
MsgBox % "HTML:`n" result.1 "`n`nWikitable:`n" result.2
return</lang>
return</syntaxhighlight>
{{out}}
{{out}}
HTML:
HTML:
Line 292: Line 292:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 445: Line 445:
toNest(iNodes2, 0, 0, &n2)
toNest(iNodes2, 0, 0, &n2)
fmt.Println(toMarkup(n2, cols2, 4))
fmt.Println(toMarkup(n2, cols2, 4))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 503: Line 503:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>{-# LANGUAGE TupleSections #-}
<syntaxhighlight lang="haskell">{-# LANGUAGE TupleSections #-}


module OutlineTree where
module OutlineTree where
Line 644: Line 644:
<> "style=\"text-align: center;\"\n|-\n"
<> "style=\"text-align: center;\"\n|-\n"
<> intercalate "|-\n" (wikiRow <$> rows)
<> intercalate "|-\n" (wikiRow <$> rows)
<> "|}"</lang>
<> "|}"</syntaxhighlight>
{{Out}}
{{Out}}
{| class="wikitable" style="text-align: center;"
{| class="wikitable" style="text-align: center;"
Line 675: Line 675:
Implementation:
Implementation:


<lang J>depth=: (i.~ ~.)@(0 i."1~' '=];._2)
<syntaxhighlight lang="j">depth=: (i.~ ~.)@(0 i."1~' '=];._2)
tree=: (i: 0>.<:@{:)\
tree=: (i: 0>.<:@{:)\
width=: {{NB. y is tree
width=: {{NB. y is tree
Line 707: Line 707:
end.
end.
r=.r,'|}',LF
r=.r,'|}',LF
}}</lang>
}}</syntaxhighlight>


Given the task example outline:
Given the task example outline:


<lang J>outline=:{{)n
<syntaxhighlight lang="j">outline=:{{)n
Display an outline as a nested table.
Display an outline as a nested table.
Parse the outline to a tree,
Parse the outline to a tree,
Line 724: Line 724:
either as a wiki table,
either as a wiki table,
or as HTML.
or as HTML.
}}</lang>
}}</syntaxhighlight>


Generated output from <tt>task outline</tt> was:
Generated output from <tt>task outline</tt> was:
Line 754: Line 754:


=={{header|JavaScript}}==
=={{header|JavaScript}}==
<lang javascript>(() => {
<syntaxhighlight lang="javascript">(() => {
"use strict";
"use strict";


Line 1,299: Line 1,299:
// MAIN ---
// MAIN ---
return main();
return main();
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
{| class="wikitable" style="text-align:center;"
{| class="wikitable" style="text-align:center;"
Line 1,327: Line 1,327:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using DataFrames
<syntaxhighlight lang="julia">using DataFrames


text = """
text = """
Line 1,404: Line 1,404:
textplus = text * " Optionally add color to the nodes."
textplus = text * " Optionally add color to the nodes."
htmlfromdataframe(processtable(textplus))
htmlfromdataframe(processtable(textplus))
</lang>{{out}}
</syntaxhighlight>{{out}}
<h4>A Rosetta Code Nested Table</h4><table style="width:100%" class="wikitable" >
<h4>A Rosetta Code Nested Table</h4><table style="width:100%" class="wikitable" >
<tr>
<tr>
Line 1,479: Line 1,479:


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>s = "Display an outline as a nested table.
<syntaxhighlight lang="mathematica">s = "Display an outline as a nested table.
Parse the outline to a tree,
Parse the outline to a tree,
measuring the indent of each line,
measuring the indent of each line,
Line 1,598: Line 1,598:
]
]
AppendTo[str, "</table>"];
AppendTo[str, "</table>"];
StringRiffle[str, "\n"]</lang>
StringRiffle[str, "\n"]</syntaxhighlight>
{{out}}
{{out}}
<pre><table style='text-align: center;'>
<pre><table style='text-align: center;'>
Line 1,631: Line 1,631:
=={{header|Nim}}==
=={{header|Nim}}==


<lang Nim>import strutils
<syntaxhighlight lang="nim">import strutils


const Outline = """Display an outline as a nested table.
const Outline = """Display an outline as a nested table.
Line 1,800: Line 1,800:
nodelists.writeWikiTable()
nodelists.writeWikiTable()
echo "HTML:"
echo "HTML:"
nodelists.writeHtml()</lang>
nodelists.writeHtml()</syntaxhighlight>


{{out}}
{{out}}
Line 1,859: Line 1,859:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict;
use strict;
Line 1,913: Line 1,913:
and write out a table with 'colspan' values
and write out a table with 'colspan' values
either as a wiki table,
either as a wiki table,
or as HTML.</lang>
or as HTML.</syntaxhighlight>
{{out}}
{{out}}
<table border=1 cellspacing=0>
<table border=1 cellspacing=0>
Line 1,946: Line 1,946:
=={{header|Phix}}==
=={{header|Phix}}==
Can output in either html or wikitable markup
Can output in either html or wikitable markup
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">html</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">,</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">html</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">,</span>
Line 2,063: Line 2,063:
<span style="color: #000000;">markup</span><span style="color: #0000FF;">(</span><span style="color: #000000;">outlines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">colours</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #000000;">markup</span><span style="color: #0000FF;">(</span><span style="color: #000000;">outlines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">colours</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
in html:
in html:
Line 2,184: Line 2,184:
=={{header|Python}}==
=={{header|Python}}==
===Python: Procedural===
===Python: Procedural===
<lang python>"""Display an outline as a nested table. Requires Python >=3.6."""
<syntaxhighlight lang="python">"""Display an outline as a nested table. Requires Python >=3.6."""


import itertools
import itertools
Line 2,452: Line 2,452:
table_format = "wiki"
table_format = "wiki"


example(table_format)</lang>
example(table_format)</syntaxhighlight>
{{out}}
{{out}}


Line 2,514: Line 2,514:


===Python: Functional===
===Python: Functional===
<lang python>'''Display an outline as a nested table'''
<syntaxhighlight lang="python">'''Display an outline as a nested table'''


from itertools import chain, cycle, takewhile
from itertools import chain, cycle, takewhile
Line 2,877: Line 2,877:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{Out}}
{{Out}}
{| class="wikitable" style="text-align: center;"
{| class="wikitable" style="text-align: center;"
Line 2,911: Line 2,911:
Strictly speaking, this is not a nested table. It is just a single level table that has some column spans > 1. For an example of using actual nested tables, see the task entry: [[Rosetta_Code/List_authors_of_task_descriptions#Raku|List_authors_of_task_descriptions#Raku]], [[Rosetta_Code/List_authors_of_task_descriptions/Full_list|(and full output)]].
Strictly speaking, this is not a nested table. It is just a single level table that has some column spans > 1. For an example of using actual nested tables, see the task entry: [[Rosetta_Code/List_authors_of_task_descriptions#Raku|List_authors_of_task_descriptions#Raku]], [[Rosetta_Code/List_authors_of_task_descriptions/Full_list|(and full output)]].


<lang perl6>my $outline = q:to/END/;
<syntaxhighlight lang="raku" line>my $outline = q:to/END/;
Display an outline as a nested table.
Display an outline as a nested table.
Parse the outline to a tree,
Parse the outline to a tree,
Line 3,055: Line 3,055:
}
}
( $r, $g, $b ).map( ((*+$m) * 255).Int)».base(16).join
( $r, $g, $b ).map( ((*+$m) * 255).Int)».base(16).join
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,123: Line 3,123:
{{libheader|Wren-dynamic}}
{{libheader|Wren-dynamic}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/dynamic" for Struct
<syntaxhighlight lang="ecmascript">import "/dynamic" for Struct
import "/fmt" for Fmt
import "/fmt" for Fmt


Line 3,271: Line 3,271:
var iNodes2 = makeIndent.call(outline2, 4)
var iNodes2 = makeIndent.call(outline2, 4)
toNest.call(iNodes2, 0, 0, n2)
toNest.call(iNodes2, 0, 0, n2)
System.print(toMarkup.call(n2, cols2, 4))</lang>
System.print(toMarkup.call(n2, cols2, 4))</syntaxhighlight>


{{out}}
{{out}}
Line 3,329: Line 3,329:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>fcn parseOutline(outline){ //--> "tree" annotated with spans
<syntaxhighlight lang="zkl">fcn parseOutline(outline){ //--> "tree" annotated with spans
var [const] indent=" "*100; // no tabs
var [const] indent=" "*100; // no tabs


Line 3,392: Line 3,392:
out.writeln("|}");
out.writeln("|}");
out.text
out.text
}</lang>
}</syntaxhighlight>
<lang zkl>outlineText:=Data(Void,
<syntaxhighlight lang="zkl">outlineText:=Data(Void,
#<<<
#<<<
"Display an outline as a nested table.
"Display an outline as a nested table.
Line 3,411: Line 3,411:


rows,cols,title,trees := parseOutline(outlineText);
rows,cols,title,trees := parseOutline(outlineText);
makeMarkup(rows,cols,title,trees).println();</lang>
makeMarkup(rows,cols,title,trees).println();</syntaxhighlight>
{{out}}
{{out}}
{| class="wikitable" style="text-align: center;"
{| class="wikitable" style="text-align: center;"
Line 3,437: Line 3,437:


And the Raku example:
And the Raku example:
<lang zkl>outlineText:=Data(Void,
<syntaxhighlight lang="zkl">outlineText:=Data(Void,
#<<<
#<<<
"Display an outline as a nested table.
"Display an outline as a nested table.
Line 3,457: Line 3,457:


rows,cols,title,trees := parseOutline(outlineText);
rows,cols,title,trees := parseOutline(outlineText);
makeMarkup(rows,cols,title,trees).println();</lang>
makeMarkup(rows,cols,title,trees).println();</syntaxhighlight>
{{out}}
{{out}}
{| class="wikitable" style="text-align: center;"
{| class="wikitable" style="text-align: center;"