From 68888bfc4ff93ed4d64e4fb4390c162aa697038c Mon Sep 17 00:00:00 2001 From: PgBiel <9021226+PgBiel@users.noreply.github.com> Date: Tue, 1 Aug 2023 23:55:36 -0300 Subject: [PATCH 01/60] fix negative numbers with two minus signs --- tests/strfmt-tests.typ | 2 +- typst-strfmt.typ | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/strfmt-tests.typ b/tests/strfmt-tests.typ index 1700cde..dcf6d9d 100644 --- a/tests/strfmt-tests.typ +++ b/tests/strfmt-tests.typ @@ -17,7 +17,7 @@ assert.eq(strfmt("{:08}|{0:0<8}|{0:0>8}|{0:0^8}", 120), "00000120|12000000|00000120|000120000") // test other kinds of affixes / fills and alignments - assert.eq(strfmt("{:a>8}, {:^20.10}, {:+05.4}, {}", "b", 5.5, 11, -4), "aaaaaaab, 5.5000000000 , +0011, -4") + assert.eq(strfmt("{:a>8}, {:^20.10}, {:+05.4}, {:07}, {other:06?}", "b", 5.5, 11, -4, other: -30.0), "aaaaaaab, 5.5000000000 , +0011, -000004, -030.0") // test base conversion (I) assert.eq(strfmt("{:b}, {0:05b}, {:#010b}, {:05x}, {:05X}, {:+#05X}, {2:x?}, {3:X?}", 5, 5, 27, 27, 27), "101, 00101, 0b00000101, 0001b, 0001B, +0x1B, 1b, 1B") diff --git a/typst-strfmt.typ b/typst-strfmt.typ index d4c0c72..ffb5123 100644 --- a/typst-strfmt.typ +++ b/typst-strfmt.typ @@ -1,3 +1,4 @@ +// typst-strfmt v0.1.1 (WIP) #let _strfmt_formatparser(s) = { if type(s) != "string" { panic("String format parsing internal error: String format parser given non-string.") @@ -336,6 +337,9 @@ parameter := argument '$' sign = "" } + // we'll add the sign back later! + replacement = calc.abs(replacement) + if spectype in ("e", "E") { let exponent-sign = if spectype == "E" { "E" } else { "e" } replacement = _strfmt_exp-format(calc.abs(replacement), exponent-sign: exponent-sign, precision: precision) From 69b70174c03a32ff9fc80a22c4b22557c1fc6b14 Mon Sep 17 00:00:00 2001 From: PgBiel <9021226+PgBiel@users.noreply.github.com> Date: Wed, 2 Aug 2023 01:19:18 -0300 Subject: [PATCH 02/60] fix precision on floats which are exact integers --- tests/strfmt-tests.typ | 3 +++ typst-strfmt.typ | 17 +++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/strfmt-tests.typ b/tests/strfmt-tests.typ index dcf6d9d..9811a43 100644 --- a/tests/strfmt-tests.typ +++ b/tests/strfmt-tests.typ @@ -34,6 +34,9 @@ // test taking width and precision from pos args / named args (.x$ / y$ notation) assert.eq(strfmt("{:.1$}; {woah:0france$.moment$}; {}; {2:a>1$}", 5.5399234, 9, "stringy", woah: 3.9, france: 7, moment: 2), "5.539923400; 0003.90; 9; aastringy") + // test weird precision cases + assert.eq(strfmt("{0:e} {0:+.9E} | {1:.3e} {1:.3} {2:.3} | {3:.4} {3:.4E} | {4:.2} {4:.3} {4:.5}", 124.2312, 50, 50.0, -0.02, 2.44454), "1.242312e2 +1.242312000E2 | 5.000e1 50 50.000 | -0.0200 -2.0000E-2 | 2.44 2.445 2.44454") + // test custom decimal separators (I) assert.eq(strfmt("{}; {:07e}; {}; {}; {:?}", 1.532, 45000, -5.6, "a.b", "c.d", fmt-decimal-separator: ","), "1,532; 004,5e4; -5,6; a.b; \"c.d\"") diff --git a/typst-strfmt.typ b/typst-strfmt.typ index ffb5123..94737f8 100644 --- a/typst-strfmt.typ +++ b/typst-strfmt.typ @@ -180,13 +180,22 @@ } let result = _strfmt_stringify(calc.round(float(num), digits: calc.min(50, precision))) let digits-match = result.match(regex("^\\d+\\.(\\d+)$")) + let digits-len-diff = 0 if digits-match != none and digits-match.captures.len() > 0 { + // get the digits capture group; its length will be digit amount let digits = digits-match.captures.first() - let digits-len-diff = precision - digits.len() - // add missing zeroes for precision - if digits-len-diff > 0 { - result += "0" * digits-len-diff + digits-len-diff = precision - digits.len() + } else if "." not in result { // 5.0 or something + // 0 digits! Difference will be exactly 'precision' + digits-len-diff = precision + } + + // add missing zeroes for precision + if digits-len-diff > 0 { + if "." not in result { + result += "." // decimal separator missing } + result += "0" * digits-len-diff } result From 2edd1de5cbd165fcb066fe3aadf117d5a460471a Mon Sep 17 00:00:00 2001 From: PgBiel <9021226+PgBiel@users.noreply.github.com> Date: Tue, 18 Jul 2023 23:30:25 -0300 Subject: [PATCH 03/60] organize README --- README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fc0885b..b8b9a04 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,13 @@ I intend to add a few extras over time, though. The first "extra" I've added so **Compatible with:** [Typst](https://github.com/typst/typst) v0.4.0 +## Table of Contents + +- [Usage](#usage) +- [Grammar](#grammar) +- [Changelog](#changelog) +- [License](#license) + ## Usage Download the `typst-strfmt.typ` file either from Releases or directly from the repository. Then, move it to your project's folder, and write at the top of your typst file(s): @@ -14,7 +21,7 @@ Download the `typst-strfmt.typ` file either from Releases or directly from the r #import "typst-strfmt.typ": strfmt ``` -That will give you access to the main function provided by this library (`strfmt`), which accepts a format string, followed by zero or more replacements to insert in that string (according to `{...}` formats inserted in that string), an optional `fmt-decimal-separator` parameter, and returns the formatted string, as described below. +Doing the above will give you access to the main function provided by this library (`strfmt`), which accepts a format string, followed by zero or more replacements to insert in that string (according to `{...}` formats inserted in that string), an optional `fmt-decimal-separator` parameter, and returns the formatted string, as described below. Its syntax is almost identical to Rust's `format!` (as specified here: https://doc.rust-lang.org/std/fmt/). You can escape formats by duplicating braces (`{{` and `}}` become `{` and `}`). Here's an example (see more examples in the file `tests/strfmt-tests.typ`): @@ -83,3 +90,7 @@ Note, however, that precision of type `.*` is not supported yet and will raise a ### v0.1.0 - Initial release, added `strfmt`. + +## License + +MIT license (see the `LICENSE` file). From 3132ad8c95f2a48bc936e57b395c62432ac93196 Mon Sep 17 00:00:00 2001 From: PgBiel <9021226+PgBiel@users.noreply.github.com> Date: Tue, 18 Jul 2023 23:46:25 -0300 Subject: [PATCH 04/60] add more versions and testing to README --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b8b9a04..bb8a35f 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A Typst library that brings convenient string formatting and interpolation throu I intend to add a few extras over time, though. The first "extra" I've added so far is the `fmt-decimal-separator: "string"` parameter, which lets you customize the decimal separator for decimal numbers (floats) inserted into strings. E.g. `strfmt("Result: {}", 5.8, fmt-decimal-separator: ",")` will return the string `"Result: 5,8"` (comma instead of dot). See more below. -**Compatible with:** [Typst](https://github.com/typst/typst) v0.4.0 +**Compatible with:** [Typst](https://github.com/typst/typst) v0.4.0, v0.5.0, v0.6.0 ## Table of Contents @@ -85,6 +85,14 @@ parameter := argument '$' Note, however, that precision of type `.*` is not supported yet and will raise an error. +## Testing + +Test with the following command (from the project root folder): + +```sh +cd tests && typst c strfmt-tests.typ +``` + ## Changelog ### v0.1.0 From a36acec61e41a39226f866530b5aa3b1d5b103d3 Mon Sep 17 00:00:00 2001 From: PgBiel <9021226+PgBiel@users.noreply.github.com> Date: Tue, 18 Jul 2023 23:47:01 -0300 Subject: [PATCH 05/60] fix testing command --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bb8a35f..a14c1f9 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ Note, however, that precision of type `.*` is not supported yet and will raise a Test with the following command (from the project root folder): ```sh -cd tests && typst c strfmt-tests.typ +cd tests && typst c strfmt-tests.typ --root .. ``` ## Changelog From 17dbdfb281da66b737b51d302088d15f47e59cb7 Mon Sep 17 00:00:00 2001 From: PgBiel <9021226+PgBiel@users.noreply.github.com> Date: Tue, 18 Jul 2023 23:51:18 -0300 Subject: [PATCH 06/60] improve testing docs wording --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a14c1f9..f32188c 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ Note, however, that precision of type `.*` is not supported yet and will raise a ## Testing -Test with the following command (from the project root folder): +If you wish to contribute, you may test this package with the following command (from the project root folder): ```sh cd tests && typst c strfmt-tests.typ --root .. From c6d110a072b7146bab9603390ecba0f22ad04870 Mon Sep 17 00:00:00 2001 From: PgBiel <9021226+PgBiel@users.noreply.github.com> Date: Wed, 19 Jul 2023 00:04:07 -0300 Subject: [PATCH 07/60] add typst.toml --- typst.toml | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 typst.toml diff --git a/typst.toml b/typst.toml new file mode 100644 index 0000000..9cb1239 --- /dev/null +++ b/typst.toml @@ -0,0 +1,8 @@ +[package] +name = "strfmt" +version = "0.1.0" +authors = ["PgBiel "] +license = "MIT" +description = "Convenient Rust-like string formatting in Typst" +entrypoint = "typst-strfmt.typ" +repository = "https://github.com/PgBiel/typst-strfmt" From ba51dec1d1d4fc425d6967fc0bf91704c00c9ebe Mon Sep 17 00:00:00 2001 From: PgBiel <9021226+PgBiel@users.noreply.github.com> Date: Tue, 1 Aug 2023 13:17:45 -0300 Subject: [PATCH 08/60] rename to oxifmt --- .gitignore | 2 +- README.md | 6 +++--- tests/strfmt-tests.typ | 2 +- typst-strfmt.typ => typst-oxifmt.typ | 0 typst.toml | 6 +++--- 5 files changed, 8 insertions(+), 8 deletions(-) rename typst-strfmt.typ => typst-oxifmt.typ (100%) diff --git a/.gitignore b/.gitignore index 59808ce..51b3b14 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -typst-strfmt.pdf +typst-oxifmt.pdf strfmt-tests.pdf diff --git a/README.md b/README.md index f32188c..26975e7 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# typst-strfmt (v0.1.0) +# typst-oxifmt (v0.1.0) A Typst library that brings convenient string formatting and interpolation through the `strfmt` function. Its syntax is taken directly from Rust's `format!` syntax, so read its page for more information (https://doc.rust-lang.org/std/fmt/). Only a few things aren't supported from the Rust syntax, such as the `p` (pointer) format type, or the `.*` precision specifier. @@ -15,10 +15,10 @@ I intend to add a few extras over time, though. The first "extra" I've added so ## Usage -Download the `typst-strfmt.typ` file either from Releases or directly from the repository. Then, move it to your project's folder, and write at the top of your typst file(s): +Download the `typst-oxifmt.typ` file either from Releases or directly from the repository. Then, move it to your project's folder, and write at the top of your typst file(s): ```js -#import "typst-strfmt.typ": strfmt +#import "typst-oxifmt.typ": strfmt ``` Doing the above will give you access to the main function provided by this library (`strfmt`), which accepts a format string, followed by zero or more replacements to insert in that string (according to `{...}` formats inserted in that string), an optional `fmt-decimal-separator` parameter, and returns the formatted string, as described below. diff --git a/tests/strfmt-tests.typ b/tests/strfmt-tests.typ index 9811a43..5b678c5 100644 --- a/tests/strfmt-tests.typ +++ b/tests/strfmt-tests.typ @@ -1,4 +1,4 @@ -#import "../typst-strfmt.typ": strfmt +#import "../typst-oxifmt.typ": strfmt #{ // test basics (sequential args, named args, pos args) diff --git a/typst-strfmt.typ b/typst-oxifmt.typ similarity index 100% rename from typst-strfmt.typ rename to typst-oxifmt.typ diff --git a/typst.toml b/typst.toml index 9cb1239..9983cfb 100644 --- a/typst.toml +++ b/typst.toml @@ -1,8 +1,8 @@ [package] -name = "strfmt" +name = "oxifmt" version = "0.1.0" authors = ["PgBiel "] license = "MIT" description = "Convenient Rust-like string formatting in Typst" -entrypoint = "typst-strfmt.typ" -repository = "https://github.com/PgBiel/typst-strfmt" +entrypoint = "typst-oxifmt.typ" +repository = "https://github.com/PgBiel/typst-oxifmt" From fdd334f1395d9c51748530a97e61edfaddb17e8b Mon Sep 17 00:00:00 2001 From: PgBiel <9021226+PgBiel@users.noreply.github.com> Date: Tue, 1 Aug 2023 13:22:00 -0300 Subject: [PATCH 09/60] MIT-0 license --- LICENSE | 29 ++++++++++++----------------- README.md | 2 +- typst.toml | 2 +- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/LICENSE b/LICENSE index 762c85e..a9cb23f 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,16 @@ -MIT License +MIT No Attribution Copyright (c) 2023 Pg Biel -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: +Permission is hereby granted, free of charge, to any person obtaining a copy of this +software and associated documentation files (the "Software"), to deal in the Software +without restriction, including without limitation the rights to use, copy, modify, +merge, publish, distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so. -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index 26975e7..8525205 100644 --- a/README.md +++ b/README.md @@ -101,4 +101,4 @@ cd tests && typst c strfmt-tests.typ --root .. ## License -MIT license (see the `LICENSE` file). +MIT-0 license (see the `LICENSE` file). diff --git a/typst.toml b/typst.toml index 9983cfb..4ae8b03 100644 --- a/typst.toml +++ b/typst.toml @@ -2,7 +2,7 @@ name = "oxifmt" version = "0.1.0" authors = ["PgBiel "] -license = "MIT" +license = "MIT-0" description = "Convenient Rust-like string formatting in Typst" entrypoint = "typst-oxifmt.typ" repository = "https://github.com/PgBiel/typst-oxifmt" From dccdec4d0233a9a4cb6ec6d34ce9239b061cbfb0 Mon Sep 17 00:00:00 2001 From: PgBiel <9021226+PgBiel@users.noreply.github.com> Date: Tue, 1 Aug 2023 13:47:56 -0300 Subject: [PATCH 10/60] add repository references to readme --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8525205..9cbba71 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ A Typst library that brings convenient string formatting and interpolation through the `strfmt` function. Its syntax is taken directly from Rust's `format!` syntax, so read its page for more information (https://doc.rust-lang.org/std/fmt/). Only a few things aren't supported from the Rust syntax, such as the `p` (pointer) format type, or the `.*` precision specifier. -I intend to add a few extras over time, though. The first "extra" I've added so far is the `fmt-decimal-separator: "string"` parameter, which lets you customize the decimal separator for decimal numbers (floats) inserted into strings. E.g. `strfmt("Result: {}", 5.8, fmt-decimal-separator: ",")` will return the string `"Result: 5,8"` (comma instead of dot). See more below. +A few extras (beyond the Rust-like syntax) will be added over time, though (feel free to drop suggestions at the repository: https://github.com/PgBiel/typst-oxifmt). The first "extra" so far is the `fmt-decimal-separator: "string"` parameter, which lets you customize the decimal separator for decimal numbers (floats) inserted into strings. E.g. `strfmt("Result: {}", 5.8, fmt-decimal-separator: ",")` will return the string `"Result: 5,8"` (comma instead of dot). See more below. **Compatible with:** [Typst](https://github.com/typst/typst) v0.4.0, v0.5.0, v0.6.0 @@ -10,6 +10,8 @@ I intend to add a few extras over time, though. The first "extra" I've added so - [Usage](#usage) - [Grammar](#grammar) +- [Issues and Contributing](#issues-and-contributing) +- [Testing](#testing) - [Changelog](#changelog) - [License](#license) @@ -85,6 +87,10 @@ parameter := argument '$' Note, however, that precision of type `.*` is not supported yet and will raise an error. +## Issues and Contributing + +Please report any issues or send any contributions (through pull requests) to the repository at https://github.com/PgBiel/typst-oxifmt + ## Testing If you wish to contribute, you may test this package with the following command (from the project root folder): From ebc1748e32134039a6699c9f6a61e1aa4e8788d9 Mon Sep 17 00:00:00 2001 From: PgBiel <9021226+PgBiel@users.noreply.github.com> Date: Wed, 2 Aug 2023 00:53:36 -0300 Subject: [PATCH 11/60] add examples section to README --- README.md | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 105 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9cbba71..748c810 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,9 @@ A few extras (beyond the Rust-like syntax) will be added over time, though (feel ## Table of Contents - [Usage](#usage) -- [Grammar](#grammar) + - [Formatting options](#formatting-options) + - [Examples](#examples) + - [Grammar](#grammar) - [Issues and Contributing](#issues-and-contributing) - [Testing](#testing) - [Changelog](#changelog) @@ -28,13 +30,19 @@ Doing the above will give you access to the main function provided by this libra Its syntax is almost identical to Rust's `format!` (as specified here: https://doc.rust-lang.org/std/fmt/). You can escape formats by duplicating braces (`{{` and `}}` become `{` and `}`). Here's an example (see more examples in the file `tests/strfmt-tests.typ`): ```js +#import "typst-oxifmt.typ": strfmt + let s = strfmt("I'm {}. I have {num} cars. I'm {0}. {} is {{cool}}.", "John", "Carl", num: 10) assert.eq(s, "I'm John. I have 10 cars. I'm John. Carl is {cool}.") ``` Note that `{}` extracts positional arguments after the string sequentially (the first `{}` extracts the first one, the second `{}` extracts the second one, and so on), while `{0}`, `{1}`, etc. will always extract the first, the second etc. positional arguments after the string. Additionally, `{bananas}` will extract the named argument "bananas". -You can use `{:spec}` to customize your output. See the Rust docs linked above for more info, but here's a summary: +### Formatting options + +You can use `{:spec}` to customize your output. See the Rust docs linked above for more info, but a summary is below. + +(You may also want to check out the examples at [Examples](#examples).) - Adding a `?` at the end of `spec` (that is, writing e.g. `{0:?}`) will call `repr()` to stringify your argument, instead of `str()`. Note that this only has an effect if your argument is a string, an integer, a float or a `label()` / `