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

md_table_parser() assumed pipe characters were always in the same column #823

Merged
merged 1 commit into from
Feb 16, 2024
Merged
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: kableExtra
Type: Package
Title: Construct Complex Table with 'kable' and Pipe Syntax
Version: 1.4.0.2
Version: 1.4.0.3
Authors@R: c(
person('Hao', 'Zhu', email = 'haozhu233@gmail.com', role = c('aut', 'cre'),
comment = c(ORCID = '0000-0002-3386-6076')),
Expand Down
21 changes: 15 additions & 6 deletions R/util.R
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,13 @@ line_separator <- function(line, idx_matrix) {
})))
}

separator_indices <- function(line) {
separator_indices <- which(strsplit(line, '')[[1]] == '|')
cell_start_indices <- separator_indices[-length(separator_indices)] + 1
cell_end_indices <- separator_indices[-1] - 1
matrix(c(cell_start_indices, cell_end_indices), ncol=2)
}

md_table_parser <- function(md_table) {
# It seems that if there is a caption, the second row is definitely an empty
# string
Expand All @@ -324,11 +331,7 @@ md_table_parser <- function(md_table) {
tbody_lines <- md_table[3:length(md_table)]

# Analyze separator line
separator_indices <- which(strsplit(separator_line, '')[[1]] == '|')
cell_start_indices <- separator_indices[-length(separator_indices)] + 1
cell_end_indices <- separator_indices[-1] - 1
cell_indices <- matrix(c(cell_start_indices, cell_end_indices), ncol=2)

cell_indices <- separator_indices(separator_line)
alignment_raw <- line_separator(separator_line, cell_indices)
alignment <- sapply(alignment_raw, function(x) {
if (grepl("^:-+$", x)) 'l' else if (grepl("^:-+:$", x)) 'c' else 'r'
Expand All @@ -338,8 +341,14 @@ md_table_parser <- function(md_table) {
n_rows <- length(tbody_lines)

# thead and tbody
# Each line may have different indices if double-width
# characters are used (issue #821)
cell_indices <- separator_indices(thead_line)
header_row <- line_separator(thead_line, cell_indices)
body_rows <- sapply(tbody_lines, line_separator, cell_indices)
body_rows <- sapply(tbody_lines, function(line) {
cell_indices <- separator_indices(line)
line_separator(line, cell_indices)
})
table_matrix <- matrix(body_rows, ncol = n_cols, byrow = TRUE)

return(kbl(table_matrix, col.names=header_row, align=alignment,
Expand Down
4 changes: 3 additions & 1 deletion inst/NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
kableExtra 1.4.0.2
kableExtra 1.4.0.3
--------------------------------------------------------------------------------

Bug Fixes:
Expand All @@ -7,6 +7,8 @@ Bug Fixes:
that had no header (#812).
* Fixed a bug in `row_spec()` which added extra
line breaks when `extra_latex_after` was specified (#815).
* Fixed a bug in `kable_styling()` which didn't parse
"pipe" tables containing multibyte characters properly (#821).


kableExtra 1.4.0
Expand Down
Loading