2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,19 +1,25 @@
|
|||
Consider a simplified CSV format where all rows are separated by a newline
|
||||
and all columns are separated by commas.
|
||||
|
||||
No commas are allowed as field data, but the data may contain
|
||||
other characters and character sequences that would
|
||||
normally be escaped when converted to HTML
|
||||
normally be ''escaped'' when converted to HTML
|
||||
|
||||
The task is to create a function that takes a string representation of the CSV data
|
||||
|
||||
;Task:
|
||||
Create a function that takes a string representation of the CSV data
|
||||
and returns a text string of an HTML table representing the CSV data.
|
||||
Use the following data as the CSV text to convert, and show your output.
|
||||
:Character,Speech
|
||||
:The multitude,The messiah! Show us the messiah!
|
||||
:Brians mother,<nowiki><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></nowiki>
|
||||
:The multitude,Who are you?
|
||||
:Brians mother,I'm his mother; that's who!
|
||||
:The multitude,Behold his mother! Behold his mother!
|
||||
|
||||
For extra credit, ''optionally'' allow special formatting
|
||||
for the first row of the table as if it is the tables header row
|
||||
Use the following data as the CSV text to convert, and show your output.
|
||||
: Character,Speech
|
||||
: The multitude,The messiah! Show us the messiah!
|
||||
: Brians mother,<nowiki><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></nowiki>
|
||||
: The multitude,Who are you?
|
||||
: Brians mother,I'm his mother; that's who!
|
||||
: The multitude,Behold his mother! Behold his mother!
|
||||
|
||||
|
||||
;Extra credit:
|
||||
''Optionally'' allow special formatting for the first row of the table as if it is the tables header row
|
||||
(via <nowiki><thead></nowiki> preferably; CSS if you must).
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
::Batch Files are terrifying when it comes to string processing.
|
||||
::But well, a decent implementation!
|
||||
@echo off
|
||||
|
||||
REM Below is the CSV data to be converted.
|
||||
REM Exactly three colons must be put before the actual line.
|
||||
|
||||
:::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!
|
||||
|
||||
setlocal disabledelayedexpansion
|
||||
echo ^<table^>
|
||||
for /f "delims=" %%A in ('findstr "^:::" "%~f0"') do (
|
||||
set "var=%%A"
|
||||
setlocal enabledelayedexpansion
|
||||
REM The next command removes the three colons...
|
||||
set "var=!var:~3!"
|
||||
|
||||
REM The following commands to the substitions per line...
|
||||
set "var=!var:&=&!"
|
||||
set "var=!var:<=<!"
|
||||
set "var=!var:>=>!"
|
||||
set "var=!var:,=</td><td>!"
|
||||
|
||||
echo ^<tr^>^<td^>!var!^</td^>^</tr^>
|
||||
endlocal
|
||||
)
|
||||
echo ^</table^>
|
||||
|
|
@ -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,6 @@
|
|||
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!
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
(require 'clojure.string)
|
||||
|
||||
(def escapes
|
||||
{\< "<", \> ">", \& "&"})
|
||||
|
||||
(defn escape
|
||||
[content]
|
||||
(clojure.string/escape content escapes))
|
||||
|
||||
(defn tr
|
||||
[cells]
|
||||
(format "<tr>%s</tr>"
|
||||
(apply str (map #(str "<td>" (escape %) "</td>") cells))))
|
||||
|
||||
;; turn a seq of seq of cells into a string.
|
||||
(defn to-html
|
||||
[tbl]
|
||||
(format "<table><tbody>%s</tbody></thead>"
|
||||
(apply str (map tr tbl))))
|
||||
|
||||
;; Read from a string to a seq of seq of cells.
|
||||
(defn from-csv
|
||||
[text]
|
||||
(map #(clojure.string/split % #",")
|
||||
(clojure.string/split-lines text)))
|
||||
|
||||
(defn -main
|
||||
[]
|
||||
(let [lines (line-seq (java.io.BufferedReader. *in*))
|
||||
tbl (map #(clojure.string/split % #",") lines)]
|
||||
(println (to-html tbl)))
|
||||
|
|
@ -0,0 +1 @@
|
|||
<table><tbody><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></tbody></thead>
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
SUBROUTINE CSVTEXT2HTML(FNAME,HEADED) !Does not recognise quoted strings.
|
||||
Converts without checking field counts, or noting special characters.
|
||||
CHARACTER*(*) FNAME !Names the input file.
|
||||
LOGICAL HEADED !Perhaps its first line is to be a heading.
|
||||
INTEGER MANY !How long is a piece of string?
|
||||
PARAMETER (MANY=666) !This should suffice.
|
||||
CHARACTER*(MANY) ALINE !A scratchpad for the input.
|
||||
INTEGER MARK(0:MANY + 1) !Fingers the commas on a line.
|
||||
INTEGER I,L,N !Assistants.
|
||||
CHARACTER*2 WOT(2) !I don't see why a "table datum" could not be for either.
|
||||
PARAMETER (WOT = (/"th","td"/)) !A table heding or a table datum
|
||||
INTEGER IT !But, one must select appropriately.
|
||||
INTEGER KBD,MSG,IN !A selection.
|
||||
COMMON /IOUNITS/ KBD,MSG,IN !The caller thus avoids collisions.
|
||||
OPEN(IN,FILE=FNAME,STATUS="OLD",ACTION="READ",ERR=661) !Go for the file.
|
||||
WRITE (MSG,1) !Start the blather.
|
||||
1 FORMAT ("<Table border=1>") !By stating that a table follows.
|
||||
MARK(0) = 0 !Syncopation for the comma fingers.
|
||||
N = 0 !No records read.
|
||||
|
||||
10 READ (IN,11,END = 20) L,ALINE(1:MIN(L,MANY)) !Carefully acquire some text.
|
||||
11 FORMAT (Q,A) !Q = number of characters yet to read, A = characters.
|
||||
N = N + 1 !So, a record has been read.
|
||||
IF (L.GT.MANY) THEN !Perhaps it is rather long?
|
||||
WRITE (MSG,12) N,L,MANY !Alas!
|
||||
12 FORMAT ("Line ",I0," has length ",I0,"! My limit is ",I0) !Squawk/
|
||||
L = MANY !The limit actually read.
|
||||
END IF !So much for paranoia.
|
||||
IF (N.EQ.1 .AND. HEADED) THEN !Is the first line to be treated specially?
|
||||
WRITE (MSG,*) "<tHead>" !Yep. Nominate a heading.
|
||||
IT = 1 !And select "th" rather than "td".
|
||||
ELSE !But mostly,
|
||||
IT = 2 !Just another row for the table.
|
||||
END IF !So much for the first line.
|
||||
NCOLS = 0 !No commas have been seen.
|
||||
DO I = 1,L !So scan the text for them.
|
||||
IF (ICHAR(ALINE(I:I)).EQ.ICHAR(",")) THEN !Here?
|
||||
NCOLS = NCOLS + 1 !Yes!
|
||||
MARK(NCOLS) = I !The texts are between commas.
|
||||
END IF !So much for that character.
|
||||
END DO !On to the next.
|
||||
NCOLS = NCOLS + 1 !This is why the + 1 for the size of MARK.
|
||||
MARK(NCOLS) = L + 1 !End-of-line is as if a comma was one further along.
|
||||
WRITE (MSG,13) !Now roll all the texts.
|
||||
1 (WOT(IT), !This starting a cell,
|
||||
2 ALINE(MARK(I - 1) + 1:MARK(I) - 1), !This being the text between the commas,
|
||||
3 WOT(IT), !And this ending each cell.
|
||||
4 I = 1,NCOLS), !For this number of columns.
|
||||
5 "/tr" !And this ends the row.
|
||||
13 FORMAT (" <tr>",666("<",A,">",A,"</",A,">")) !How long is a piece of string?
|
||||
IF (N.EQ.1 .AND. HEADED) WRITE (MSG,*) "</tHead>" !Finish the possible header.
|
||||
GO TO 10 !And try for another record.
|
||||
|
||||
20 CLOSE (IN) !Finished with input.
|
||||
WRITE (MSG,21) !And finished with output.
|
||||
21 FORMAT ("</Table>") !This writes starting at column one.
|
||||
RETURN !Done!
|
||||
Confusions.
|
||||
661 WRITE (MSG,*) "Can't open file ",FNAME !Alas.
|
||||
END !So much for the conversion.
|
||||
|
||||
INTEGER KBD,MSG,IN
|
||||
COMMON /IOUNITS/ KBD,MSG,IN
|
||||
KBD = 5 !Standard input.
|
||||
MSG = 6 !Standard output.
|
||||
IN = 10 !Some unspecial number.
|
||||
|
||||
CALL CSVTEXT2HTML("Text.csv",.FALSE.) !The first line is not special.
|
||||
WRITE (MSG,*)
|
||||
CALL CSVTEXT2HTML("Text.csv",.TRUE.) !The first line is a heading.
|
||||
END
|
||||
|
|
@ -1,28 +1,28 @@
|
|||
/*REXX program converts CSV ───► HTML table representing the CSV data. */
|
||||
arg header_ . /*determine if the user wants a header.*/
|
||||
wantsHdr= (header_=='HEADER') /*is the arg (low/upp/mix case)=HEADER?*/
|
||||
/*REXX program converts CSV ───► HTML table representing the CSV data. */
|
||||
arg header_ . /*obtain an uppercase version of args. */
|
||||
wantsHdr= (header_=='HEADER') /*is the arg (low/upp/mix case)=HEADER?*/
|
||||
/* [↑] determine if user wants a hdr. */
|
||||
iFID= 'CSV_HTML.TXT' /*the input fileID to be used. */
|
||||
if wantsHdr then oFID= 'OUTPUTH.HTML' /*the output fileID with header.*/
|
||||
else oFID= 'OUTPUT.HTML' /* " " " without " */
|
||||
|
||||
iFID= 'CSV_HTML.TXT' /*the input fileID to be used. */
|
||||
if wantsHdr then oFID= 'OUTPUTH.HTML' /*the output fileID with header.*/
|
||||
else oFID= 'OUTPUT.HTML' /* " " " without " */
|
||||
|
||||
do rows=0 while lines(iFID)\==0 /*read the rows from a (text/txt) file.*/
|
||||
row.rows=strip(linein(iFID))
|
||||
do rows=0 while lines(iFID)\==0 /*read the rows from a (text/txt) file.*/
|
||||
row.rows= strip( linein(iFID) )
|
||||
end /*rows*/
|
||||
|
||||
convFrom= '& < > "' /*special characters to be converted. */
|
||||
convTo = '& < > "' /*display what they are converted into.*/
|
||||
convFrom= '& < > "' /*special characters to be converted. */
|
||||
convTo = '& < > "' /*display what they are converted into.*/
|
||||
|
||||
call write , '<html>'
|
||||
call write , '<table border=4 cellpadding=9 cellspacing=1>'
|
||||
|
||||
do j=0 for rows; call write 5, '<tr>'
|
||||
tx='td'
|
||||
if wantsHdr & j==0 then tx='th'
|
||||
do j=0 for rows; call write 5, '<tr>'
|
||||
tx= 'td'
|
||||
if wantsHdr & j==0 then tx= 'th' /*if user wants a header, then oblige. */
|
||||
|
||||
do while row.j\==''; parse var row.j yyy ',' row.j
|
||||
do while row.j\==''; parse var row.j yyy "," row.j
|
||||
do k=1 for words(convFrom)
|
||||
yyy=changestr(word(convFrom, k), yyy, word(convTo, k))
|
||||
yyy=changestr( word( convFrom, k), yyy, word( convTo, k))
|
||||
end /*k*/
|
||||
call write 10, '<'tx">"yyy'</'tx">"
|
||||
end /*forever*/
|
||||
|
|
@ -31,6 +31,6 @@ call write , '<table border=4 cellpadding=9 cellspacing=1>'
|
|||
call write 5, '<tr>'
|
||||
call write , '</table>'
|
||||
call write , '</html>'
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
write: call lineout oFID, left('', 0 || arg(1))arg(2); return
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
write: call lineout oFID, left('', 0 || arg(1) )arg(2); return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue