June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,31 @@
|
|||
USING: combinators csv io kernel sequences strings ;
|
||||
IN: rosetta-code.csv-to-html
|
||||
|
||||
CONSTANT: input
|
||||
|
||||
"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!"
|
||||
|
||||
: escape-chars ( seq -- seq' )
|
||||
[
|
||||
{
|
||||
{ CHAR: & [ "&" ] }
|
||||
{ CHAR: ' [ "'" ] }
|
||||
{ CHAR: < [ "<" ] }
|
||||
{ CHAR: > [ ">" ] }
|
||||
[ 1string ]
|
||||
} case
|
||||
] { } map-as concat ;
|
||||
|
||||
: tag ( str tag -- <tag>str</tag> )
|
||||
[ "<" ">" surround ] [ "</" ">" surround ] bi surround ;
|
||||
|
||||
: csv>table ( seq -- str )
|
||||
[ [ "td" tag ] map concat "tr" tag " " prepend ] map
|
||||
{ "<table>" } prepend { "</table>" } append "\n" join ;
|
||||
|
||||
input escape-chars string>csv csv>table print
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
<table>
|
||||
<tr><td>Character</td><td>Speech</td></tr>
|
||||
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
|
||||
<tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></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>
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
: BEGIN-COLUMN ." <td>" ;
|
||||
: END-COLUMN ." </td>" ;
|
||||
|
||||
: BEGIN-ROW ." <tr>" BEGIN-COLUMN ;
|
||||
: END-ROW END-COLUMN ." </tr>" CR ;
|
||||
|
||||
: CSV2HTML
|
||||
." <table>" CR BEGIN-ROW
|
||||
BEGIN KEY DUP #EOF <> WHILE
|
||||
CASE
|
||||
10 OF END-ROW BEGIN-ROW ENDOF
|
||||
[CHAR] , OF END-COLUMN BEGIN-COLUMN ENDOF
|
||||
[CHAR] < OF ." <" ENDOF
|
||||
[CHAR] > OF ." >" ENDOF
|
||||
[CHAR] & OF ." &" ENDOF
|
||||
DUP EMIT
|
||||
ENDCASE
|
||||
REPEAT
|
||||
END-ROW ." </table>" CR
|
||||
;
|
||||
|
||||
CSV2HTML BYE
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
<table>
|
||||
<tr><td>Character</td><td>Speech</td></tr>
|
||||
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
|
||||
<tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></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>
|
||||
|
|
@ -5,6 +5,7 @@ import (
|
|||
"encoding/csv"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var c = `Character,Speech
|
||||
|
|
@ -27,7 +28,7 @@ func csvToHtml(c string) (string, error) {
|
|||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var b bytes.Buffer
|
||||
var b strings.Builder
|
||||
err = template.Must(template.New("").Parse(`<table>
|
||||
{{range .}} <tr>{{range .}}<td>{{.}}</td>{{end}}</tr>
|
||||
{{end}}</table>
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"flag"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var csvStr = `Character,Speech
|
||||
|
|
@ -34,7 +35,7 @@ func csvToHtml(csvStr string, headings bool) (string, error) {
|
|||
if headings {
|
||||
tStr = tHeadings
|
||||
}
|
||||
var b bytes.Buffer
|
||||
var b strings.Builder
|
||||
err = template.Must(template.New("").Parse(tStr)).Execute(&b, data)
|
||||
return b.String(), err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
#A translation of the C code posted
|
||||
html_table := proc(str)
|
||||
local char;
|
||||
printf("<table>\n<tr><td>");
|
||||
for char in str do
|
||||
if char = "\n" then
|
||||
printf("</td></tr>\n<tr><td>")
|
||||
elif char = "," then
|
||||
printf("</td><td>")
|
||||
elif char = "<" then
|
||||
printf("<")
|
||||
elif char = ">" then
|
||||
printf(">")
|
||||
elif char = "&" then
|
||||
printf("&")
|
||||
else
|
||||
printf(char)
|
||||
end if;
|
||||
end do;
|
||||
printf("</td></tr>\n</table>");
|
||||
end proc;
|
||||
|
||||
html_table("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!");
|
||||
39
Task/CSV-to-HTML-translation/Red/csv-to-html-translation.red
Normal file
39
Task/CSV-to-HTML-translation/Red/csv-to-html-translation.red
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
Red []
|
||||
|
||||
csv: {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!}
|
||||
|
||||
add2html: func [ bl ] [append html rejoin bl ] ;; helper function to add block data to html string
|
||||
|
||||
;;----------------------------------------------------------------------
|
||||
csv2html: func ["function to generate string with html table from csv data file"
|
||||
;;----------------------------------------------------------------------
|
||||
s [string!] "input .csv data"
|
||||
][
|
||||
arr: split s newline ;; generate array (series) from string
|
||||
html: copy "<table border=1>^/" ;; init html string
|
||||
|
||||
forall arr [ ;; i use forall here so that i can test for head? of series ...
|
||||
either head? arr [ append html "<tr bgcolor=wheat>"]
|
||||
[ append html "<tr>"]
|
||||
replace/all first arr "<" "<" ;; escape "<" and ">" characters
|
||||
replace/all first arr ">" ">"
|
||||
foreach col split first arr "," [
|
||||
either head? arr [
|
||||
add2html ['<th> col '</th>]
|
||||
][
|
||||
add2html ['<td> col '</td>]
|
||||
]
|
||||
]
|
||||
add2html ['</tr> newline]
|
||||
]
|
||||
return add2html ['</table>]
|
||||
]
|
||||
;;----------------------------------------------------------------------
|
||||
|
||||
print csv2html csv ;; call function
|
||||
write %data.html csv2html csv ;; write to file
|
||||
Loading…
Add table
Add a link
Reference in a new issue