Repository navigation
feat(format): show exact fractions the same way in every output - #677
Merged
Merged
Conversation
`@milk{1 5/8%cup}` read `1.625 cup` in the terminal and on the recipe page,
while `5/8 cup` read `0.625 cup` in the terminal and `5/8 cup` on the page,
and the shopping list page showed `1 5/8 cup` (cooklang#672).
Every human-readable output now goes through `cooklang_format::number`:
the terminal, Markdown, LaTeX, Typst, schema.org text, the CLI shopping list
and its JSON strings, the step labels of the picture dialog, and the web
pages, which already did. `format_number` gives a mixed number when the value
is exactly one of the fractions it knows (halves, thirds, quarters, eighths):
`1 5/8`, `2 1/3`. Any other value stays a decimal, so nothing is rounded to
the nearest fraction (the bug in cooklang#432). Ranges read `1/2 - 1` everywhere.
JSON and YAML recipe output keep the number, and the Cooklang output is
unchanged. The shopping list page's own formatter already followed these
rules; it now says so.
Closes cooklang#672
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Fractions suit cups and spoons, not a scale: `100.625 g` read `100 5/8 g`, and the web pages already showed `0.5 kg` as `1/2 kg`. As in cooklang's own unit database (`[fractions]` in its units.toml: metric, time and temperature off, imperial on), metric units, times and °C/°F now always read in decimals. Cups, spoons, pounds, counts and units not in that list (cloves, pinch) keep the fractions. A bare `c` or `f` stays a fraction unit, since `c` is also a cup. `format_quantity_value` takes the quantity and applies its unit's rule; every caller with a unit at hand uses it, and the shopping list page's JavaScript has the same list. `format_quantity` and `format_number` still give fractions, for numbers without a unit such as servings. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #672.
The same recipe showed different numbers depending on where you looked. There were three formatters: the web pages used
format_number, the shopping list page used its own copy in JavaScript, and every other output used cooklang'sDisplay.cook recipe(terminal, Markdown, LaTeX, Typst, schema.org)1.625 cup,0.625 cup,0.75 cup1 5/8 cup,5/8 cup,3/4 cup1.625 cup,5/8 cup,3/4 cup1 5/8 cup,5/8 cup,3/4 cup1 5/8 cup,5/8 cup,3/4 cupcook shopping-list1.625 cup,0.625 cup,0.75 cup1 5/8 cup,5/8 cup,3/4 cup1.625Grams, litres, times and temperatures stay in decimals:
@flour{100.625%g}on the recipe page100.625 g100.625 g(not100 5/8 g)@butter{0.5%kg}on the recipe page1/2 kg0.5 kg@butter{0.5%kg}in the terminal0.5 kg0.5 kgWhat changes
One rule, in
cooklang_format::number:1 5/8,2 1/3. Until now the Rust formatter gave up above 1 on purpose, while the shopping list's JavaScript did not.5/8scaled ×1.5 is0.938, not1.[fractions]in itsunits.toml: metric, time and temperature off, imperial on). No unit, cups, spoons, pounds and units not in that list (cloves, pinch) keep the fractions. A barecorfkeeps them too, becausecis also a cup.format_quantity_value(new) takes the quantity and applies its unit's rule.format_quantity_with_unit(new) renders1 5/8 cupand100.625 g.grouped_quantity_fmtuses it, which covers every ingredient list.Ranges are still kept as text (
1/2-1 cup, shown as written) because range values are off in the parser (#597). Once they are on, they will read1/2 - 1in every output, as the web pages already do.Unchanged:
-f cooklangoutput stays valid Cooklang.formatNumberalready gave mixed numbers. It now has the same unit list, and both copies point at each other.format_numberandformat_quantitystill give fractions. They are for numbers without a unit, such as servings and numeric metadata.On the questions in the issue
2 7/16→2.438).Worth knowing
cooklang-formatis published, and its human-readable output changes.feat:lets release-please bump it.@./x{2 1/2%servings}rather than2.5. Values below 1 were already written as1/2. I checked that a menu reads both the same.2.5%servingsputs 3 servings' worth on the shopping list, and2 1/2does the same; whole servings scale correctly. This happens on main too. I will file it separately if you agree it is a bug.Tests
crates/format/src/number.rs:1.6,0.33,0.938,2.438);format_quantity_with_unit;tests/fractions_test.rs(new, 6 tests): the issue's recipe throughcook recipe(terminal and Markdown), scaled ×1.5, throughcook shopping-list, and JSON keeping the number. Also grams and litres staying decimals in the recipe and the shopping list. Everycookit runs goes throughwith_isolated_config.*1.5 cups* milkbecomes*1 1/2 cups* milkin the Markdown output) and a menu scaling unit test (1/2× 3 is now1 1/2).cargo fmt --check,cargo clippy --workspace --all-targetsandcargo test --workspaceare clean.shopping-list-copy; the shopping-list specs share.shopping-list, and they pass with one worker (18/18).22.5 g,112.5 mland1.125 l, alongside2 1/4 tbsp.🤖 Generated with Claude Code