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

Add github flavored markdown horizontal style #59

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions lib/table_rex/renderer/text.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule TableRex.Renderer.Text do
# vertical_styles: [:all, :frame, :off]

# Which horizontal/vertical styles render a specific separator.
@render_horizontal_frame_styles [:all, :frame, :header]
@render_horizontal_frame_styles [:all, :frame, :header, :gfm]
@render_vertical_frame_styles [:all, :frame]
@render_column_separators_styles [:all]
@render_row_separators_styles [:all]
Expand Down Expand Up @@ -44,6 +44,7 @@ defmodule TableRex.Renderer.Text do
* `:all`: display separators between and around every row.
* `:header`: display outer and header horizontal separators only.
* `:frame`: display outer horizontal separators only.
* `:gfm`: display all separators except top and bottom to comply with github flavored markdown
* `:off`: display no horizontal separators.

`vertical_styles` controls vertical separators and can be one of:
Expand All @@ -61,6 +62,8 @@ defmodule TableRex.Renderer.Text do
render_vertical_frame? = opts[:vertical_style] in @render_vertical_frame_styles
render_column_separators? = opts[:vertical_style] in @render_column_separators_styles
render_row_separators? = opts[:horizontal_style] in @render_row_separators_styles
render_frame_top? = opts[:horizontal_style] != :gfm
render_frame_bottom? = opts[:horizontal_style] != :gfm
table_width = table_width(col_widths, vertical_frame?: render_vertical_frame?)
intersections = intersections(table_width, col_widths, vertical_style: opts[:vertical_style])

Expand All @@ -72,7 +75,9 @@ defmodule TableRex.Renderer.Text do
render_horizontal_frame?: render_horizontal_frame?,
render_vertical_frame?: render_vertical_frame?,
render_column_separators?: render_column_separators?,
render_row_separators?: render_row_separators?
render_row_separators?: render_row_separators?,
render_frame_top?: render_frame_top?,
render_frame_bottom?: render_frame_bottom?
}

rendered =
Expand All @@ -89,6 +94,10 @@ defmodule TableRex.Renderer.Text do
{:ok, rendered}
end

defp render_top_frame({table, %Meta{render_frame_top?: false} = meta, opts, rendered}) do
{table, meta, opts, rendered}
end

defp render_top_frame({table, %Meta{render_horizontal_frame?: false} = meta, opts, rendered}) do
{table, meta, opts, rendered}
end
Expand Down Expand Up @@ -145,7 +154,7 @@ defmodule TableRex.Renderer.Text do
defp render_title_separator(
{table, meta, %{horizontal_style: horizontal_style} = opts, rendered}
)
when horizontal_style in [:all, :header] do
when horizontal_style in [:all, :header, :gfm] do
line =
render_line(
meta.table_width,
Expand Down Expand Up @@ -186,7 +195,7 @@ defmodule TableRex.Renderer.Text do
defp render_header_separator(
{table, meta, %{horizontal_style: horizontal_style} = opts, rendered}
)
when horizontal_style in [:all, :header] do
when horizontal_style in [:all, :header, :gfm] do
line =
render_line(
meta.table_width,
Expand Down Expand Up @@ -242,6 +251,10 @@ defmodule TableRex.Renderer.Text do

defp vertically_framed(lines, _, _), do: lines

defp render_bottom_frame({table, %Meta{render_frame_bottom?: false} = meta, opts, rendered}) do
{table, meta, opts, rendered}
end

defp render_bottom_frame({table, %Meta{render_horizontal_frame?: false} = meta, opts, rendered}) do
{table, meta, opts, rendered}
end
Expand Down
4 changes: 3 additions & 1 deletion lib/table_rex/renderer/text/meta.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ defmodule TableRex.Renderer.Text.Meta do
render_horizontal_frame?: false,
render_vertical_frame?: false,
render_column_separators?: false,
render_row_separators?: false
render_row_separators?: false,
render_frame_top?: true,
render_frame_bottom?: true

@doc """
Retrieves the "inner width" of the table, which is the full width minus any frame.
Expand Down
15 changes: 15 additions & 0 deletions test/table_rex/renderer/text_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,21 @@ defmodule TableRex.Renderer.TextTest do
"""
end

test "render in github flavored markdown format", %{table: table} do
{:ok, rendered} =
table
|> Table.put_title("")
|> Table.render(horizontal_style: :gfm)

assert rendered == """
| Artist | Track | Year |
+----------------+----------------------+------+
| Konflict | Cyanide | 1999 |
| Keaton & Hive | The Plague | 2003 |
| Vicious Circle | Welcome To Shanktown | 2007 |
"""
end

test "render with not title or header & horizontal style: frame", %{table: table} do
{:ok, rendered} =
table
Expand Down
Loading