Skip to content

Commit

Permalink
[improve] Number recognition in tables (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mc-Zen authored Nov 4, 2024
1 parent f5e1291 commit 7701362
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
15 changes: 9 additions & 6 deletions src/parsing.typ
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

/// Converts a content value into a string if it contains only text nodes.
/// Otherwise, `none` is returned.
#let content-to-stringw(x) = {
#let content-to-string-table(x) = {
let prefix = none
let suffix = none
if x.has("text") { return (x.text, prefix, suffix) }
Expand All @@ -41,9 +41,9 @@
}

#let nonum = highlight
#assert.eq(content-to-stringw[alpha ], none)
#assert.eq(content-to-stringw[#nonum[€]12], ("12", [€], none))
#assert.eq(content-to-stringw[#nonum[€]12.43#nonum[#footnote[1]]], ("12.43", [€], footnote[1]))
#assert.eq(content-to-string-table[alpha ], none)
#assert.eq(content-to-string-table[#nonum[€]12], ("12", [€], none))
#assert.eq(content-to-string-table[#nonum[€]12.43#nonum[#footnote[1]]], ("12.43", [€], footnote[1]))

/// Converts a number into a string if the input is either
/// - an integer or a float,
Expand All @@ -64,15 +64,18 @@
return result.replace(",", ".").replace(sym.minus, "-")
}

#let number-to-stringw(number) = {
#let number-to-string-table(number) = {
let result
if type(number) == str { result = number }
else if type(number) in (int, float) { result = str(number) }
else if type(number) == content { result = content-to-stringw(number) }
else if type(number) == content { result = content-to-string-table(number) }
else { result = none }
if result == none { return none }
if type(result) != array { result = (result, none, none) }
result.at(0) = result.at(0).replace(",", ".").replace(sym.minus, "-")
if result.len() == 0 or result.at(0).at(0) not in "0123456789+-." {
return none
}
return result
}

Expand Down
6 changes: 3 additions & 3 deletions src/ztable.typ
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#import "num.typ": num, number-to-stringw
#import "num.typ": num, number-to-string-table
#import "state.typ": num-state

// #let ptable-counter = counter("__pillar-table__")

#let is-normal-cell(cell, format, default: none) = {
format.at(cell.x, default: default) == none or number-to-stringw(cell.body) == none
format.at(cell.x, default: default) == none or number-to-string-table(cell.body) == none
}

#let call-num(cell, format, col-widths: auto, default: none, state: auto) = context{
let (numeral, prefix, suffix) = number-to-stringw(cell.body)
let (numeral, prefix, suffix) = number-to-string-table(cell.body)
let cell-fmt = format.at(cell.x, default: default)
let args = if type(cell-fmt) == dictionary { cell-fmt } else { () }
num(numeral, prefix: prefix, suffix: suffix, state: state, align: (col-widths: col-widths, col: cell.x), ..args)
Expand Down
2 changes: 1 addition & 1 deletion tests/tables/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
zero.ztable(
columns: 2,
format: (auto, (decimal-separator: ",", fixed: 2)),
[ Long title], [ Defg ],
[Long title], [Defg],
table.hline(),
"2.3", "3422",
"10", "101",
Expand Down

0 comments on commit 7701362

Please # to comment.