Repository navigation
Add opt-in escaping, exact decimals, streaming render and a DocC catalog - #12
Merged
Merged
Conversation
…log.
- TemplateOptions.escaping turns on escaping for HTML (.html) or any
format (Escaping { ... }). Value tags ending in | raw or | escape are
written as they are.
- TemplateValue.decimal keeps Decimal values exact through arithmetic,
@sum, @AvG, rounding and formatting. It's boxed so other values stay
as small as before.
- render(_:into:) streams output to a TextOutputStream in 16 KB pieces.
- Formatters are made on first use and shared by templates with the same
locale, time zone and currency, so creating a template is about 30%
faster.
- A DocC catalog with articles on syntax, functions and rendering, and a
.spi.yml so Swift Package Index builds the docs.
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.
Before: HTML templates had to remember
| escapeon every value,Decimalprices were turned intoDouble(so they could pick up floating-point error), long output was always built as one big String, creating a template rebuilt every date and number formatter, and there were no browsable docs.After:
TemplateOptions.escapingis.noneby default, since templates aren't assumed to be HTML..htmlescapes& < > " 'in every value tag.Escaping { ... }takes your own transform for other formats. A value tag ending in| rawis written as it is, and one ending in| escapeisn't escaped twice. Template text andlogtags are never escaped.TemplateValue.decimal(Decimal)case.Decimalvalues fromEncodabletypes orTemplateValue(any:)stay exact through+ - * /, comparisons,@sum,@avg,round/floor/ceil/abs, andnumber/percent/currency/format. Mixing in aDoublefalls back toDouble. The case isindirect, so other values stay as small as before and rendering doesn't slow down.render(_:features:into:), plus theobject:andencoding:variants, write to anyTextOutputStreamin roughly 16 KB pieces. Rendering into aStringappends to it.DMTemplates.docchas a landing page and three articles (template syntax, functions and formatting, rendering), and builds without warnings..spi.ymllets Swift Package Index host it.Source compatibility: code that switches exhaustively over
TemplateValueneeds a.decimalcase.How: value tags record whether their last call is
raworescape(Expr.functionnow carries the function's name), andProgram.runwrites into a byte buffer that a streaming caller flushes once it reaches the chunk size.Tests: 116 pass on Linux with Swift 6.0.3, including new suites for escaping, decimals and streaming.
Generated by Claude Code