Skip to content

Commit

Permalink
✨ Add support to OffsetArrays
Browse files Browse the repository at this point in the history
Closes #110
  • Loading branch information
ronisbr committed Sep 3, 2022
1 parent e2679fb commit ea9823f
Show file tree
Hide file tree
Showing 14 changed files with 828 additions and 45 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
[compat]
Crayons = "4.0"
Formatting = "0.4"
OffsetArrays = "1"
Reexport = "0.2, 1"
StringManipulation = "0.3"
Tables = "0.2, 1"
julia = "1"

[extras]
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
test = ["OffsetArrays", "Test"]
46 changes: 25 additions & 21 deletions src/backends/html/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,10 @@ function _pt_html(

cell_str = _parse_cell_html(
cell_data,
allow_html_in_cells = allow_html_in_cells,
compact_printing = compact_printing,
limit_printing = limit_printing,
renderer = Val(:print)
allow_html_in_cells=allow_html_in_cells,
compact_printing=compact_printing,
limit_printing=limit_printing,
renderer=Val(:print)
)

empty!(properties)
Expand Down Expand Up @@ -382,34 +382,38 @@ function _pt_html(
ir = _get_data_row_index(ptable, i)
jr = _get_data_column_index(ptable, j)

# Notice that `(ir, jr)` are the indices of the printed
# data. It means that it refers to the ir-th data row and
# jr-th data column that will be printed. We need to convert
# those indices to the actual indices in the input table.
tir, tjr = _convert_axes(ptable.data, ir, jr)

for f in formatters.x
cell_data = f(cell_data, ir, jr)
cell_data = f(cell_data, tir, tjr)
end

# Apply highlighters.
for h in highlighters
if h.f(_getdata(ptable), tir, tjr)
merge!(style, Dict(h.fd(h, _getdata(ptable), tir, tjr)))
break
end
end
end

cell_str = _parse_cell_html(
cell_data;
allow_html_in_cells = allow_html_in_cells,
cell_first_line_only = cell_first_line_only,
compact_printing = compact_printing,
limit_printing = limit_printing,
linebreaks = linebreaks,
renderer = renderer
allow_html_in_cells=allow_html_in_cells,
cell_first_line_only=cell_first_line_only,
compact_printing=compact_printing,
limit_printing=limit_printing,
linebreaks=linebreaks,
renderer=renderer
)

empty!(properties)
properties["class"] = cell_class

if is_original_data
# Apply highlighters.
for h in highlighters
if h.f(_getdata(ptable), ir, jr)
merge!(style, Dict(h.fd(h, _getdata(ptable), ir, jr)))
break
end
end
end

_aprintln(
buf,
_create_html_tag(html_row_tag, cell_str; properties, style),
Expand Down
12 changes: 9 additions & 3 deletions src/backends/latex/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,15 @@ function _pt_latex(
jr = _get_data_column_index(ptable, j)

if column_id == :__ORIGINAL_DATA__
# Notice that `(ir, jr)` are the indices of the printed
# data. It means that it refers to the ir-th data row and
# jr-th data column that will be printed. We need to convert
# those indices to the actual indices in the input table.
tir, tjr = _convert_axes(ptable.data, ir, jr)

# Apply the formatters.
for f in formatters.x
cell_data = f(cell_data, ir, jr)
cell_data = f(cell_data, tir, tjr)
end

cell_str = _parse_cell_latex(
Expand All @@ -243,8 +249,8 @@ function _pt_latex(

# Apply highlighters.
for h in highlighters
if h.f(_getdata(ptable), ir, jr)
cell_str = h.fd(_getdata(ptable), ir, jr, cell_str)::String
if h.f(_getdata(ptable), tir, tjr)
cell_str = h.fd(_getdata(ptable), tir, tjr, cell_str)::String
break
end
end
Expand Down
9 changes: 8 additions & 1 deletion src/backends/text/fill.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,15 @@ function _fill_matrix_data!(
cell_data_type = typeof(cell_data)

# Apply the formatters.

# Notice that `(ir, jr)` are the indices of the printed data. It
# means that it refers to the ir-th data row and jr-th data
# column that will be printed. We need to convert those indices
# to the actual indices in the input table.
tir, tjr = _convert_axes(ptable.data, ir, jr)

for f in formatters.x
cell_data = f(cell_data, ir, jr)
cell_data = f(cell_data, tir, tjr)
end

# Render the cell.
Expand Down
9 changes: 7 additions & 2 deletions src/backends/text/print_cell.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,15 @@ function _process_data_cell_text(
alignment::Symbol,
(@nospecialize highlighters::Ref{Any})
)
# Notice that `(i, j)` are the indices of the printed data. It means that it
# refers to the ith data row and jth data column that will be printed. We
# need to convert those indices to the actual indices in the input table.
ti, tj = _convert_axes(ptable.data, i, j)

# Check for highlighters.
for h in highlighters.x
if h.f(_getdata(ptable), i, j)
crayon = h.fd(h, _getdata(ptable), i, j)::Crayon
if h.f(_getdata(ptable), ti, tj)
crayon = h.fd(h, _getdata(ptable), ti, tj)::Crayon
break
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/private.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function _preprocess_vec_or_mat(
header::Union{Nothing, AbstractVector, Tuple}
)
if header === nothing
pheader = (["Col. " * string(i) for i = 1:size(data,2)],)
pheader = (["Col. " * string(i) for i in axes(data, 2)],)
elseif header isa AbstractVector
pheader = (header,)
else
Expand Down Expand Up @@ -202,7 +202,7 @@ function _print_info(
if show_row_number
_add_column!(
ptable,
1:size(data)[1],
axes(data)[1] |> collect,
[row_number_column_title];
alignment = row_number_alignment,
id = :row_number
Expand Down
84 changes: 69 additions & 15 deletions src/processed_table/get.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,35 @@
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

"""
_convert_axes(data::Any, i::Int)
Convert the axes from the printed table data `i` to the axes in `data`.
!!! note
`i` refers to the i-th element that will be printed.
_convert_axes(data::Any, i::Int, j::Int)
Convert the axes from the printed table data `(i, j)` to the axes in `data`.
!!! note
`(i, j)` refers to the i-th data row and j-th data column that will be
printed.
"""
function _convert_axes(data::Any, i::Int)
ax = axes(data)
ti = first(ax[1]) + i - 1
return ti
end

function _convert_axes(data::Any, i::Int, j::Int)
ax = axes(data)
ti = first(ax[1]) + i - 1
tj = (length(ax) == 1) ? 1 : first(ax[2]) + j - 1
return ti, tj
end

"""
_get_cell_alignemnt(ptable::ProcessedTable, i::Int, j::Int)
Expand All @@ -23,12 +52,15 @@ function _get_cell_alignment(ptable::ProcessedTable, i::Int, j::Int)
header_alignment_override = nothing

# Get the cell index in the original table.
jr = _get_data_column_index(ptable, j)
jr = _get_data_column_index(ptable, j)

# Get the data index inside the header.
ajr = _convert_axes(ptable.header[i], jr)

# Search for alignment overrides in this cell.
for f in ptable._header_cell_alignment
header_alignment_override =
f(ptable.header, i, jr)::Union{Nothing, Symbol}
f(ptable.header, i, ajr)::Union{Nothing, Symbol}

if _is_alignment_valid(header_alignment_override)
return header_alignment_override
Expand Down Expand Up @@ -71,10 +103,13 @@ function _get_cell_alignment(ptable::ProcessedTable, i::Int, j::Int)
ir = _get_data_row_index(ptable, i)
jr = _get_data_column_index(ptable, j)

# Get the data index inside the table.
air, ajr = _convert_axes(ptable.data, ir, jr)

# Search for alignment overrides in this cell.
for f in ptable._data_cell_alignment
alignment_override =
f(_getdata(ptable.data), ir, jr)::Union{Nothing, Symbol}
f(_getdata(ptable.data), air, ajr)::Union{Nothing, Symbol}

if _is_alignment_valid(alignment_override)
return alignment_override
Expand Down Expand Up @@ -149,27 +184,35 @@ _getdata(ptable::ProcessedTable) = _getdata(ptable.data)
Get the element `(i, j)` if `ptable`. This function always considers the
additional columns and the header.
Notice that `i` and `j` must be related to the row and column that will be
printed. The actual index inside the data is handle internally in this function.
"""
function _get_element(ptable::ProcessedTable, i::Int, j::Int)
Δc = length(ptable._additional_data_columns)

# Check if we need to return an additional column or the real data.
if j Δc

if i ptable._num_header_rows
l = length(ptable._additional_header_columns[j])
aj = _convert_axes(ptable._additional_header_columns, j)
hj = ptable._additional_header_columns[aj]
l = length(hj)

if i l
return ptable._additional_header_columns[j][i]
ai = _convert_axes(hj, i)
return hj[ai]
else
return ""
end
else
id = _get_data_row_index(ptable, i)
aj = _convert_axes(ptable._additional_data_columns, j)
dj = ptable._additional_data_columns[aj]
id = _get_data_row_index(ptable, i)
aid = _convert_axes(dj, id)

ptable_additional_data_columns_j = ptable._additional_data_columns[j]

if isassigned(ptable_additional_data_columns_j, id)
return ptable_additional_data_columns_j[id]
if isassigned(dj, aid)
return dj[aid]
else
return _UNDEFINED_CELL
end
Expand All @@ -178,12 +221,20 @@ function _get_element(ptable::ProcessedTable, i::Int, j::Int)
jd = _get_data_column_index(ptable, j)

if i ptable._num_header_rows
return ptable.header[i][jd]
hi = ptable.header[i]

# Get the data index inside the header.
ajd = _convert_axes(hi, jd)

return hi[ajd]
else
id = _get_data_row_index(ptable, i)

if isassigned(ptable.data, id, jd)
return ptable.data[id, jd]
# Get the data index inside the table.
aid, ajd = _convert_axes(ptable.data, id, jd)

if isassigned(ptable.data, aid, ajd)
return ptable.data[aid, ajd]
else
return _UNDEFINED_CELL
end
Expand All @@ -204,8 +255,11 @@ function _get_header_element(ptable::ProcessedTable, j::Int)
if j Δc
return ptable._additional_header_columns[j]
else
jd = _get_data_column_index(ptable, j - Δc)
return ptable.header[jd]
# Get the data index inside the header.
jd = _get_data_column_index(ptable, j - Δc)
ajd = _convert_axes(first(ptable.header), jd)

return ptable.header[ajd]
end
end

Expand Down
4 changes: 4 additions & 0 deletions test/html_backend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ end
include("./html_backend/markdown.jl")
end

@testset "OffsetArrays" begin
include("./html_backend/offset_arrays.jl")
end

@testset "Renderers" begin
include("./html_backend/renderers.jl")
end
Expand Down
Loading

0 comments on commit ea9823f

Please sign in to comment.