Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: inserting tables with \tableinput command #197

Merged
merged 10 commits into from
Sep 2, 2019
10 changes: 6 additions & 4 deletions docs/src/man/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,9 @@ You can insert tables directly from CSV files with the `\tableinput{header}{path
If you generate the file on-the-fly, you should follow this example:
`````judoc
```julia:./tableinput/gen
testcsv = "h1,h2,h3\n152,some string, 1.5f0\n0,another string,2.87"
testcsv = "h1,h2,h3
152,some string, 1.5f0
0,another string,2.87"
write("assets/pages/tableinput/testcsv.csv", testcsv)
```
`````
Expand All @@ -701,8 +703,8 @@ Which will result in:
| --- | -------------- | ----- |
| 152 | some string | 1.5f0 |
| 0 | another string | 2.87 |

or if you want to specify the headers:
In this case given no header was specified in the call, a header was generated from the first line in the CSV (here: h1, h2).
cserteGT3 marked this conversation as resolved.
Show resolved Hide resolved
If you want, you can specify the header:
`````judoc
\tableinput{custom h1, custom h2, custom h3}{./tableinput/testcsv.csv}
`````
Expand All @@ -724,7 +726,7 @@ Or you can include an existing CSV file (`assets/test2.csv`):
There's a couple of rules that you have to keep in mind when using the `\tableinput{}{}` command:
* The CSV file must have a header line.
cserteGT3 marked this conversation as resolved.
Show resolved Hide resolved
* Columns must be separated with comma (`,`).
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with -> by a

* If header is specified, its length must be matched with the table in the file.
* If a header is specified, its length must match the number of columns of the file.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of the file -> in the file


## Page variables

Expand Down
14 changes: 6 additions & 8 deletions src/converter/lx_simple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,22 @@ If header is empty, the first row of the file will be used for header.
"""
function csv2html(path, header)::String
csvcontent = readdlm(path, ',', String, header=false)
tablesize = size(csvcontent)
nrows, ncols = size(csvcontent)
# replacing header
if ! isempty(header)
newheader = split(header, ",")
hs = size(newheader,1)
hs != tablesize[2] && return html_err("header ($hs) and table ($(tablesize[2])) size does not match")
hs != ncols && return html_err("header size ($hs) and number of columns ($ncols) do not match")
csvcontent[1,:] = newheader
end
io = IOBuffer()
# writing the header
for a in csvcontent[1,:]
write(io, "| ", a, " ")
end
write(io, prod("| " * csvcontent[1, i] * " " for i in 1:ncols))
# writing end of header & header separator
write(io, "|\n|", repeat( " ----- |", tablesize[2]), "\n")
write(io, "|\n|", repeat( " ----- |", ncols), "\n")
# writing content
for i in 2:tablesize[1]
for j in 1:tablesize[2]
for i in 2:nrows
for j in 1:ncols
write(io, "| ", csvcontent[i,j], " ")
end
write(io, "|\n")
Expand Down
2 changes: 1 addition & 1 deletion test/converter/lx_simple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ end
\tableinput{,}{/assets/testcsv.csv}
Done.
""" |> seval
shouldbe = """<p>A table: <p><span style=\"color:red;\">// header (2) and table (3) size does not match //</span></p>
shouldbe = """<p>A table: <p><span style=\"color:red;\">// header size (2) and number of columns (3) do not match //</span></p>
Done.</p>"""
@test isapproxstr(h, shouldbe)
end