Skip to content

Commit

Permalink
Enable table column alignment in HTML
Browse files Browse the repository at this point in the history
Note that tables that do not explicitly define alignment default to be
right-aligned.

Fix #511
  • Loading branch information
mortenpi committed Mar 24, 2019
1 parent 9d30975 commit 1baf932
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/Writers/HTMLWriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1053,10 +1053,28 @@ function mdconvert(paragraph::Markdown.Paragraph, parent::Markdown.List; kwargs.
return (list_has_loose_field && !parent.loose) ? content : Tag(:p)(content)
end

mdconvert(t::Markdown.Table, parent; kwargs...) = Tag(:table)(
Tag(:tr)(map(x -> Tag(:th)(mdconvert(x, t; kwargs...)), t.rows[1])),
map(x -> Tag(:tr)(map(y -> Tag(:td)(mdconvert(y, x; kwargs...)), x)), t.rows[2:end])
)
function mdconvert(t::Markdown.Table, parent; kwargs...)
@tags table tr th td
alignment_style = map(t.align) do align
if align == :r
"text-align: right"
elseif align == :c
"text-align: center"
else
"text-align: left"
end
end
table(
tr(map(enumerate(t.rows[1])) do (i, x)
th[:style => alignment_style[i]](mdconvert(x, t; kwargs...))
end),
map(t.rows[2:end]) do x
tr(map(enumerate(x)) do (i, y) # each cell in a row
td[:style => alignment_style[i]](mdconvert(y, x; kwargs...))
end)
end
)
end

mdconvert(expr::Union{Expr,Symbol}, parent; kwargs...) = string(expr)

Expand Down
14 changes: 14 additions & 0 deletions test/examples/src/man/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,17 @@ Below is a nicely rendered version of `^D`:
```@raw html
<kbd>Ctrl</kbd> + <kbd>D</kbd>
```

## Tables

| object | implemented | value |
|--------|-------------|------------|
| `A` || 10.00 |
| `BB` || 1000000.00 |

With explicit alignment.

| object | implemented | value |
| :--- | :---: | ---: |
| `A` || 10.00 |
| `BB` || 1000000.00 |

0 comments on commit 1baf932

Please sign in to comment.