Jump to content

CSV to HTML translation: Difference between revisions

→‎{{header|Go}}: library path update, also enhanced extra credit version
(→‎{{header|Go}}: language change. built in error type.)
(→‎{{header|Go}}: library path update, also enhanced extra credit version)
Line 764:
"fmt"
"strings"
"text/template"
)
 
Line 789:
return b.String()
}</lang>
Extra credit version interprets "optionally" with a command line option. When running the compiled program, a command line option of -h does the special formatting for the heading line. Also different in this version is some error checking, a few things pulled apart and given names for the purpose of readability, and use of a template set to show how templates can be composed of common pieces.
 
Also different in this version, the csv package is used to parse the input. It offers little advantage for the simple data of the task, but becomes nice for real problems.
 
A data structure with named fields is used. Again, maybe frivolous for this task, but it helps organize larger problems.
 
The html/template package is used instead of text/template. This adds protection against code injection attacks.
 
Finally, the template definition contains three templates, showing how templates can be composed.
<lang go>package main
 
import (
"bytes"
"encoding/csv"
"errors"
"flag"
"fmt"
"stringshtml/template"
"template"
)
 
const csvcsvIn = `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>
Line 811 ⟶ 819:
headings := flag.Bool("h", false, "format first row as column headings")
flag.Parse()
if html, err := csvToHtml(csvcsvIn, *headings); err == nil {
fmt.Println(html)
} else {
Line 827 ⟶ 835:
}
 
func csvToHtml(csvcsvIn string, specialHeadings bool) (string, error) {
// use csv package to convert single string into 2D array of strings.
lines := strings.Split(csv, "\n")
lines, err := csv.NewReader(bytes.NewBufferString(csvIn)).ReadAll()
if len(hl)err != 2nil {
return "", fmt.Errorf(err
}
if len(lines) == 0 {
return "", errors.New("no data")
}
if len(bllines[0]) != 2 {
return "", fmt.Errorf("%d column headingscolums found. 2 required", len(hllines[0]))
}
// move strings from 2D array to data structure with field names.
var s script
if specialHeadings {
hls.Headings := strings.Split(line{lines[0][0], ",")lines[0][1]}
if len(hl) != 2 {
return "", fmt.Errorf(
"%d column headings found. 2 required", len(hl))
}
s.Headings = line{hl[0], hl[1]}
lines = lines[1:]
}
s.Body = make([]line, len(lines))
for i, lbl := range lines {
bl := strings.Split(l, ",")
if len(bl) != 2 {
return "", fmt.Errorf(
"%d csv items found in line %d of script. 2 required",
len(bl), i+1)
}
s.Body[i] = line{bl[0], bl[1]}
}
set// :=use html/template.SetMust(new(template.Set).Parse(tables)) package to generate output
set := template.Must(template.New("").Parse(tables))
tmpl := "tableNoHeadings"
if specialHeadings {
Line 858 ⟶ 864:
}
var b bytes.Buffer
err := set.ExecuteExecuteTemplate(&b, tmpl, s)
return b.String(), err
}
Line 875 ⟶ 881:
{{end}}`</lang>
Output of the extra credit version, with and without the -h option:
<lang html5>> csv2html -h
> csv2html -h
<table>
<tr><th>Character</th><th>Speech</th></tr>
Line 894 ⟶ 899:
<tr><td>Brians mother</td><td>I&#39;m his mother; that&#39;s who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></lang>
</lang>
 
=={{header|Groovy}}==
1,707

edits

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