CSV to HTML translation: Difference between revisions

Content added Content deleted
(Undo revision 316512 by Grondilu (talk) my bad : there's already a unix shell section)
(→‎{{header|UNIX Shell}}: simpler version (doesn't work with ksh anymore, though))
Line 5,530: Line 5,530:
=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
{{works with|bash}}
{{works with|bash}}
{{works with|ksh}}
<lang bash>csv2html() {
<lang bash>csv2html() {
IFS=,
IFS=,
echo "<table>"
echo "<table>"

echo "<thead>"
echo "<thead>"
read -r speaker text
read -a fields
htmlrow "$speaker" "$text" th
htmlrow th "${fields[@]}"
echo "</thead>"
echo "</thead>"

echo "<tbody>"
echo "<tbody>"
while read -r speaker text; do
while read -a fields
htmlrow "$speaker" "$text"
do htmlrow td "${fields[@]}"
done
done
echo "</tbody>"
echo "</tbody>"
echo "</table>"
echo "</table>"
}
}

htmlrow() {
htmlrow() {
cell=${3:-td}
cell=$1
shift
printf "<tr><%s>%s</%s><%s>%s</%s></tr>\n" \
echo "<tr>"
"$cell" "$(escape_html "$1")" "$cell" \
for field
"$cell" "$(escape_html "$2")" "$cell"
do echo "<$cell>$(escape_html "$field")</$cell>"
done
echo "</tr>"
}
}

escape_html() {
escape_html() {
str=${1//\&/&amp;}
str=${1//\&/&amp;}
Line 5,562: Line 5,564:
}
}


csv2html <<-END
html=$(
Character,Speech
csv2html <<-END
The multitude,The messiah! Show us the messiah!
Character,Speech
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,The messiah! Show us the messiah!
The multitude,Who are you?
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
Brians mother,I'm his mother; that's who!
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!
END</lang>
The multitude,Behold his mother! Behold his mother!
END
)
echo "$html"</lang>


{{output}}
{{output}}
<lang html5><table>
<lang html5><table>
<thead>
<thead>
<tr>
<tr><th>Character</th><th>Speech</th></tr>
<th>Character</th>
<th>Speech</th>
</tr>
</thead>
</thead>
<tbody>
<tbody>
<tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<td>The multitude</td>
<tr><td>Brians mother</td><td>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;</td></tr>
<tr><td>The multitude</td><td>Who are you?</td></tr>
<td>The messiah! Show us the messiah!</td>
</tr>
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
<td>Brians mother</td>
<td>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;</td>
</tr>
<tr>
<td>The multitude</td>
<td>Who are you?</td>
</tr>
<tr>
<td>Brians mother</td>
<td>I'm his mother; that's who!</td>
</tr>
<tr>
<td>The multitude</td>
<td>Behold his mother! Behold his mother!</td>
</tr>
</tbody>
</tbody>
</table></lang>
</table></lang>