CSV to HTML translation: Difference between revisions

(Add solution for rust based on C)
Line 5,570:
</table></lang>
 
=={{header|XSLTVisual Basic 2.0NET}}==
{{works with|.NET Framework}}
 
Uses XML literals and the TextFieldParser class of the VB runtime, which can parse delimited or fixed-width files.
 
The optional first command-line argument denotes whether to use <thead /> for the first row. The optional second argument specifies the path of the CSV file. If no second argument is given, the program reads from the console until stop characters are encountered.
 
TextFieldParser is designed to work with files and so makes heavy use of peeking, which results in buggy behavior when signaling end-of-file using the console. The most reliable way seems to be alternately pressing enter and Ctrl+Z after the last character of the last line of data.
 
<lang vbnet>Imports Microsoft.VisualBasic.FileIO
 
Module Program
Sub Main(args As String())
Dim parser As TextFieldParser
Try
If args.Length > 1 Then
parser = My.Computer.FileSystem.OpenTextFieldParser(args(1), ",")
Else
parser = New TextFieldParser(Console.In) With {.Delimiters = {","}}
End If
 
Dim getLines =
Iterator Function() As IEnumerable(Of String())
Do Until parser.EndOfData
Yield parser.ReadFields()
Loop
End Function
 
Dim result = CSVTOHTML(getLines(), If(args.Length > 0, Boolean.Parse(args(0)), False))
 
Console.WriteLine(result)
Finally
If parser IsNot Nothing Then parser.Dispose()
End Try
End Sub
 
Function CSVTOHTML(lines As IEnumerable(Of IEnumerable(Of String)), useTHead As Boolean) As XElement
Dim getRow = Function(row As IEnumerable(Of String)) From field In row Select <td><%= field %></td>
 
CSVTOHTML =
<table>
<%= From l In lines.Select(
Function(line, i)
If useTHead AndAlso i = 0 Then
Return <thead><%= getRow(line) %></thead>
Else
Return <tr><%= getRow(line) %></tr>
End If
End Function) %>
</table>
End Function
 
End Module</lang>
 
{{out|input=true}}
<lang html5>Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!
^Z
^Z
<table>
<thead>
<td>Character</td>
<td>Speech</td>
</thead>
<tr>
<td>The multitude</td>
<td>The messiah! Show us the messiah!</td>
</tr>
<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>
</table></lang>
 
=={{header|XSLT 2.0}}==
 
<h3>Setup</h3>
Anonymous user