Jump to content

CSV to HTML translation: Difference between revisions

→‎{{header|UNIX Shell}}: simpler version (doesn't work with ksh anymore, though)
(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:
=={{header|UNIX Shell}}==
{{works with|bash}}
{{works with|ksh}}
<lang bash>csv2html() {
IFS=,
echo "<table>"
 
echo "<thead>"
read -ra speaker textfields
htmlrow "$speaker"th "$text{fields[@]}" th
echo "</thead>"
 
echo "<tbody>"
while read -ra speaker text; dofields
do htmlrow "$speaker"td "$text{fields[@]}"
done
echo "</tbody>"
echo "</table>"
}
 
htmlrow() {
cell=${3:-td}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 "$1field")" "</$cell>" \
done
echo "$html"</langtr>"
}
 
escape_html() {
str=${1//\&/&amp;}
Line 5,562 ⟶ 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>
<tr><td>Brians mother</td><td>,I'm his mother; that's who!</td></tr>
The multitude,Who are you?
BriansThe mothermultitude,I'mBehold his mother;! that'sBehold whohis mother!
END</lang>
The multitude,Behold his mother! Behold his mother!
END
)
echo "$html"</lang>
 
{{output}}
<lang html5><table>
<thead>
<tr>
<tr><th>Character</th><th>Speech</th></tr>
<th>Speech</th>
</tr>
</thead>
<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>Whomessiah! areShow you?us the messiah!</td></tr>
</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>
<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>
<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>
The multitude,<td>Behold his mother! Behold his mother!</td>
</tr>
</tbody>
</table></lang>
1,934

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.