generated from typst-community/typst-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use valkyrie:0.2.1 to parse validate input
- Loading branch information
1 parent
323b20c
commit 7d83c65
Showing
9 changed files
with
241 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#import "@preview/valkyrie:0.2.1" as z |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#import "deps.typ": * | ||
|
||
#let columns-schema = z.array( | ||
z.dictionary((: | ||
key: z.any(), | ||
header: z.content(optional: true), | ||
display: z.function(optional: true), | ||
fill: z.color(optional: true), | ||
align: z.alignment(default: start), | ||
gutter: z.length(optional: true), | ||
width: z.any(optional: true), | ||
)) + (: | ||
handle-descendents: (self, it, ctx: z.z-ctx(), scope: ()) => { | ||
|
||
if (it.len() == 0 and self.optional) { | ||
return none | ||
} | ||
|
||
// Check `it` if strict | ||
if (ctx.strict == true) { | ||
for (key, value) in it { | ||
if (key not in self.dictionary-schema) { | ||
return (self.fail-validation)( | ||
self, | ||
it, | ||
ctx: ctx, | ||
scope: scope, | ||
message: "Unknown key `" + key + "` in dictionary", | ||
) | ||
} | ||
} | ||
} | ||
|
||
if "children" in it { | ||
it.children = z.parse( | ||
it.children, | ||
z.array( | ||
self, | ||
z.coerce.array | ||
), | ||
ctx: ctx | ||
) | ||
} | ||
|
||
for (key, schema) in self.dictionary-schema { | ||
|
||
let entry = ( | ||
schema.validate | ||
)( | ||
schema, | ||
it.at(key, default: none), // implicitly handles missing entries | ||
ctx: ctx, | ||
scope: (..scope, str(key)) | ||
) | ||
|
||
if (entry != none or ctx.remove-optional-none == false) { | ||
it.insert(key, entry) | ||
} | ||
|
||
} | ||
return it | ||
}, | ||
) | ||
) |
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#import "/src/lib.typ" as dining-table | ||
|
||
#let data = ( | ||
( | ||
date: datetime.today(), | ||
particulars: lorem(05), | ||
ledger: [JRS123] + dining-table.note.make[Hello World], | ||
amount: (unit: $100$, decimal: $00$), | ||
total: (unit: $99$, decimal: $00$), | ||
), | ||
)*7 | ||
|
||
#import "@preview/typpuccino:0.1.0" | ||
#let bg-fill-1 = typpuccino.latte.base | ||
#let bg-fill-2 = typpuccino.latte.mantle | ||
|
||
#let example = ( | ||
( | ||
key: "date", | ||
header: align(left)[Date], | ||
display: (it)=>it.display(auto), | ||
fill: bg-fill-1, | ||
align: start, | ||
gutter: 0.5em, | ||
), | ||
( | ||
key: "particulars", | ||
header: text(tracking: 5pt)[Particulars], | ||
width: 1fr, | ||
gutter: 0.5em, | ||
), | ||
( | ||
key: "ledger", | ||
header: [Ledger], | ||
fill: bg-fill-2, | ||
width: 2cm, | ||
gutter: 0.5em, | ||
), | ||
( | ||
header: align(center)[Amount], | ||
fill: bg-fill-1, | ||
gutter: 0.5em, | ||
hline: arguments(stroke: dining-table.lightrule), | ||
children: ( | ||
( | ||
key: "amount.unit", | ||
header: align(left)[£], | ||
width: 5em, | ||
align: right, | ||
vline: arguments(stroke: dining-table.lightrule), | ||
gutter: 0em, | ||
), | ||
( | ||
key: "amount.decimal", | ||
header: align(right, text(number-type: "old-style")[.00]), | ||
align: left | ||
), | ||
) | ||
), | ||
( | ||
header: align(center)[Total], | ||
gutter: 0.5em, | ||
hline: arguments(stroke: dining-table.lightrule), | ||
children: ( | ||
( | ||
key: "total.unit", | ||
header: align(left)[£], | ||
width: 5em, | ||
align: right, | ||
vline: arguments(stroke: dining-table.lightrule), | ||
gutter: 0em, | ||
), | ||
( | ||
key: "total.decimal", | ||
header: align(right, text(number-type: "old-style")[.00]), | ||
align: left | ||
), | ||
) | ||
), | ||
) | ||
|
||
#set text(size: 11pt) | ||
#set page(height: auto, margin: 1em) | ||
#dining-table.make(columns: example, | ||
data: data, | ||
notes: dining-table.note.display-list | ||
) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#import "/src/deps.typ": * | ||
#import "/src/validation.typ" | ||
#import "/src/lib.typ" as dining-table | ||
|
||
#import "@preview/typpuccino:0.1.0" | ||
#let bg-fill-1 = typpuccino.latte.base | ||
#let bg-fill-2 = typpuccino.latte.mantle | ||
|
||
#let example = ( | ||
( | ||
key: "date", | ||
header: align(left)[Date], | ||
display: (it)=>it.display(auto), | ||
fill: bg-fill-1, | ||
align: start, | ||
gutter: 0.5em, | ||
), | ||
( | ||
key: "particulars", | ||
header: text(tracking: 5pt)[Particulars], | ||
width: 1fr, | ||
gutter: 0.5em, | ||
), | ||
( | ||
key: "ledger", | ||
header: [Ledger], | ||
fill: bg-fill-2, | ||
width: 2cm, | ||
gutter: 0.5em, | ||
), | ||
( | ||
header: align(center)[Amount], | ||
fill: bg-fill-1, | ||
gutter: 0.5em, | ||
hline: arguments(stroke: dining-table.lightrule), | ||
children: ( | ||
( | ||
key: "amount.unit", | ||
header: align(left)[£], | ||
width: 5em, | ||
align: right, | ||
vline: arguments(stroke: dining-table.lightrule), | ||
gutter: 0em, | ||
), | ||
( | ||
key: "amount.decimal", | ||
header: align(right, text(number-type: "old-style")[.00]), | ||
align: left | ||
), | ||
) | ||
), | ||
( | ||
header: align(center)[Total], | ||
gutter: 0.5em, | ||
hline: arguments(stroke: dining-table.lightrule), | ||
children: ( | ||
( | ||
key: "total.unit", | ||
header: align(left)[£], | ||
width: 5em, | ||
align: right, | ||
vline: arguments(stroke: dining-table.lightrule), | ||
gutter: 0em, | ||
), | ||
( | ||
key: "total.decimal", | ||
header: align(right, text(number-type: "old-style")[.00]), | ||
align: left | ||
), | ||
) | ||
), | ||
) | ||
|
||
#set text(size: 11pt) | ||
#set page(height: auto, margin: 1em) | ||
|
||
|
||
#z.parse(example, validation.columns-schema, ctx: z.z-ctx(remove-optional-none: true)) |