Skip to content

Commit

Permalink
Merge pull request #823 from haozhu233/issue821
Browse files Browse the repository at this point in the history
`md_table_parser()` assumed pipe characters were always in the same column
  • Loading branch information
dmurdoch authored Feb 16, 2024
2 parents 0842199 + 518eaf6 commit a9c509a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
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 = '[email protected]', 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

0 comments on commit a9c509a

Please sign in to comment.