binja 0.5.1 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -240,109 +240,141 @@ app.get('/user/:id', ({ params }) => templates.user({ id: params.id }))
240
240
 
241
241
  ---
242
242
 
243
- ## Filters
244
-
245
- ### String Filters
246
-
247
- | Filter | Example | Output |
248
- |--------|---------|--------|
249
- | `upper` | `{{ "hello"\|upper }}` | `HELLO` |
250
- | `lower` | `{{ "HELLO"\|lower }}` | `hello` |
251
- | `capitalize` | `{{ "hello"\|capitalize }}` | `Hello` |
252
- | `title` | `{{ "hello world"\|title }}` | `Hello World` |
253
- | `trim` | `{{ " hello "\|trim }}` | `hello` |
254
- | `truncatechars` | `{{ "hello world"\|truncatechars:5 }}` | `he...` |
255
- | `truncatewords` | `{{ "hello world foo"\|truncatewords:2 }}` | `hello world...` |
256
- | `slugify` | `{{ "Hello World!"\|slugify }}` | `hello-world` |
257
- | `striptags` | `{{ "<p>Hello</p>"\|striptags }}` | `Hello` |
258
- | `wordcount` | `{{ "hello world"\|wordcount }}` | `2` |
259
- | `center` | `{{ "hi"\|center:10 }}` | ` hi ` |
260
- | `ljust` | `{{ "hi"\|ljust:10 }}` | `hi ` |
261
- | `rjust` | `{{ "hi"\|rjust:10 }}` | ` hi` |
262
- | `cut` | `{{ "hello"\|cut:"l" }}` | `heo` |
263
- | `addslashes` | `{{ "it's"\|addslashes }}` | `it\'s` |
264
- | `stringformat` | `{{ 5\|stringformat:"03d" }}` | `005` |
265
-
266
- ### Number Filters
267
-
268
- | Filter | Example | Output |
269
- |--------|---------|--------|
270
- | `abs` | `{{ -5\|abs }}` | `5` |
271
- | `add` | `{{ 5\|add:3 }}` | `8` |
272
- | `floatformat` | `{{ 3.14159\|floatformat:2 }}` | `3.14` |
273
- | `filesizeformat` | `{{ 1048576\|filesizeformat }}` | `1.0 MB` |
274
- | `divisibleby` | `{{ 10\|divisibleby:2 }}` | `true` |
275
- | `get_digit` | `{{ 12345\|get_digit:2 }}` | `4` |
276
- | `widthratio` | `{% widthratio value max 100 %}` | Calculated ratio |
277
-
278
- ### List Filters
279
-
280
- | Filter | Example | Output |
281
- |--------|---------|--------|
282
- | `length` | `{{ items\|length }}` | `3` |
283
- | `first` | `{{ items\|first }}` | First item |
284
- | `last` | `{{ items\|last }}` | Last item |
285
- | `join` | `{{ items\|join:", " }}` | `a, b, c` |
286
- | `reverse` | `{{ items\|reverse }}` | Reversed list |
287
- | `sort` | `{{ items\|sort }}` | Sorted list |
288
- | `unique` | `{{ items\|unique }}` | Unique items |
289
- | `slice` | `{{ items\|slice:":2" }}` | First 2 items |
290
- | `batch` | `{{ items\|batch:2 }}` | Grouped by 2 |
291
- | `random` | `{{ items\|random }}` | Random item |
292
-
293
- ### Date Filters
294
-
295
- | Filter | Example | Output |
296
- |--------|---------|--------|
297
- | `date` | `{{ now\|date:"Y-m-d" }}` | `2024-01-15` |
298
- | `time` | `{{ now\|time:"H:i" }}` | `14:30` |
299
- | `timesince` | `{{ past\|timesince }}` | `2 days ago` |
300
- | `timeuntil` | `{{ future\|timeuntil }}` | `in 3 hours` |
243
+ ## Filters (84 Built-in)
244
+
245
+ binja includes **84 built-in filters** covering both Jinja2 and Django Template Language.
246
+
247
+ ### String Filters (26)
248
+
249
+ | Filter | Description | Example |
250
+ |--------|-------------|---------|
251
+ | `upper` | Uppercase | `{{ "hello"\|upper }}` `HELLO` |
252
+ | `lower` | Lowercase | `{{ "HELLO"\|lower }}` `hello` |
253
+ | `capitalize` | First letter uppercase | `{{ "hello"\|capitalize }}` `Hello` |
254
+ | `capfirst` | First char uppercase | `{{ "hello"\|capfirst }}` `Hello` |
255
+ | `title` | Title case | `{{ "hello world"\|title }}` `Hello World` |
256
+ | `trim` | Strip whitespace | `{{ " hi "\|trim }}` `hi` |
257
+ | `striptags` | Remove HTML tags | `{{ "<p>Hi</p>"\|striptags }}` `Hi` |
258
+ | `slugify` | URL-friendly slug | `{{ "Hello World!"\|slugify }}` `hello-world` |
259
+ | `truncatechars` | Truncate to N chars | `{{ "hello"\|truncatechars:3 }}` `hel...` |
260
+ | `truncatewords` | Truncate to N words | `{{ "a b c d"\|truncatewords:2 }}` `a b...` |
261
+ | `truncatechars_html` | Truncate preserving HTML | `{{ "<b>hi</b> world"\|truncatechars_html:5 }}` |
262
+ | `truncatewords_html` | Truncate words in HTML | `{{ "<p>a b c</p>"\|truncatewords_html:2 }}` |
263
+ | `wordcount` | Count words | `{{ "hello world"\|wordcount }}` `2` |
264
+ | `wordwrap` | Wrap at N chars | `{{ text\|wordwrap:40 }}` |
265
+ | `center` | Center in N chars | `{{ "hi"\|center:10 }}` → ` hi ` |
266
+ | `ljust` | Left justify | `{{ "hi"\|ljust:10 }}` → `hi ` |
267
+ | `rjust` | Right justify | `{{ "hi"\|rjust:10 }}` → ` hi` |
268
+ | `cut` | Remove substring | `{{ "hello"\|cut:"l" }}` → `heo` |
269
+ | `replace` | Replace substring | `{{ "hello"\|replace:"l","x" }}` → `hexxo` |
270
+ | `indent` | Indent lines | `{{ text\|indent:4 }}` |
271
+ | `linebreaks` | Newlines to `<p>/<br>` | `{{ text\|linebreaks }}` |
272
+ | `linebreaksbr` | Newlines to `<br>` | `{{ text\|linebreaksbr }}` |
273
+ | `linenumbers` | Add line numbers | `{{ code\|linenumbers }}` |
274
+ | `addslashes` | Escape quotes | `{{ "it's"\|addslashes }}` `it\'s` |
275
+ | `format` | sprintf-style format | `{{ "Hi %s"\|format:name }}` |
276
+ | `stringformat` | Python % format | `{{ 5\|stringformat:"03d" }}` `005` |
277
+
278
+ ### Number Filters (9)
279
+
280
+ | Filter | Description | Example |
281
+ |--------|-------------|---------|
282
+ | `abs` | Absolute value | `{{ -5\|abs }}` `5` |
283
+ | `int` | Convert to integer | `{{ "42"\|int }}` `42` |
284
+ | `float` | Convert to float | `{{ "3.14"\|float }}` `3.14` |
285
+ | `round` | Round number | `{{ 3.7\|round }}` `4` |
286
+ | `add` | Add number | `{{ 5\|add:3 }}` `8` |
287
+ | `divisibleby` | Check divisibility | `{{ 10\|divisibleby:2 }}` `true` |
288
+ | `floatformat` | Format decimal places | `{{ 3.14159\|floatformat:2 }}` `3.14` |
289
+ | `filesizeformat` | Human file size | `{{ 1048576\|filesizeformat }}` `1.0 MB` |
290
+ | `get_digit` | Get Nth digit | `{{ 12345\|get_digit:2 }}` `4` |
291
+
292
+ ### List/Array Filters (22)
293
+
294
+ | Filter | Description | Example |
295
+ |--------|-------------|---------|
296
+ | `length` | List length | `{{ items\|length }}` → `3` |
297
+ | `length_is` | Check length | `{{ items\|length_is:3 }}` `true` |
298
+ | `first` | First item | `{{ items\|first }}` |
299
+ | `last` | Last item | `{{ items\|last }}` |
300
+ | `join` | Join with separator | `{{ items\|join:", " }}` `a, b, c` |
301
+ | `slice` | Slice list | `{{ items\|slice:":2" }}` |
302
+ | `reverse` | Reverse list | `{{ items\|reverse }}` |
303
+ | `sort` | Sort list | `{{ items\|sort }}` |
304
+ | `unique` | Remove duplicates | `{{ items\|unique }}` |
305
+ | `batch` | Group into batches | `{{ items\|batch:2 }}` |
306
+ | `columns` | Split into columns | `{{ items\|columns:3 }}` |
307
+ | `dictsort` | Sort dict by key | `{{ dict\|dictsort }}` |
308
+ | `dictsortreversed` | Sort dict reversed | `{{ dict\|dictsortreversed }}` |
309
+ | `groupby` | Group by attribute | `{{ items\|groupby:"category" }}` |
310
+ | `random` | Random item | `{{ items\|random }}` |
311
+ | `list` | Convert to list | `{{ value\|list }}` |
312
+ | `make_list` | String to char list | `{{ "abc"\|make_list }}` → `['a','b','c']` |
313
+ | `map` | Map attribute | `{{ items\|map:"name" }}` |
314
+ | `select` | Filter by test | `{{ items\|select:"even" }}` |
315
+ | `reject` | Reject by test | `{{ items\|reject:"none" }}` |
316
+ | `selectattr` | Filter by attr test | `{{ items\|selectattr:"active" }}` |
317
+ | `rejectattr` | Reject by attr test | `{{ items\|rejectattr:"hidden" }}` |
318
+
319
+ ### Math Filters (4)
320
+
321
+ | Filter | Description | Example |
322
+ |--------|-------------|---------|
323
+ | `max` | Maximum value | `{{ items\|max }}` |
324
+ | `min` | Minimum value | `{{ items\|min }}` |
325
+ | `sum` | Sum of values | `{{ items\|sum }}` |
326
+ | `attr` | Get attribute | `{{ item\|attr:"name" }}` |
327
+
328
+ ### Date/Time Filters (4)
329
+
330
+ | Filter | Description | Example |
331
+ |--------|-------------|---------|
332
+ | `date` | Format date | `{{ now\|date:"Y-m-d" }}` → `2024-01-15` |
333
+ | `time` | Format time | `{{ now\|time:"H:i" }}` → `14:30` |
334
+ | `timesince` | Time since date | `{{ past\|timesince }}` → `2 days ago` |
335
+ | `timeuntil` | Time until date | `{{ future\|timeuntil }}` → `in 3 hours` |
301
336
 
302
337
  #### Timezone Support
303
338
 
304
- All date/time operations respect the `timezone` option in Environment:
305
-
306
339
  ```typescript
307
340
  const env = new Environment({
308
- timezone: 'Europe/Rome' // All dates will be displayed in Rome timezone
309
- })
310
-
311
- // 2024-06-15 12:00 UTC = 2024-06-15 14:00 Rome (UTC+2)
312
- await env.renderString('{{ date|date:"Y-m-d H:i" }}', {
313
- date: new Date('2024-06-15T12:00:00Z')
341
+ timezone: 'Europe/Rome' // All dates in Rome timezone
314
342
  })
315
- // Output: 2024-06-15 14:00
316
-
317
- // {% now %} tag also uses the configured timezone
318
- await env.renderString('{% now "Y-m-d H:i" %}')
319
- // Output: current date/time in Rome timezone
320
343
  ```
321
344
 
322
- Common timezone values: `UTC`, `Europe/London`, `Europe/Rome`, `Europe/Paris`, `America/New_York`, `America/Los_Angeles`, `Asia/Tokyo`, `Asia/Shanghai`, `Australia/Sydney`
323
-
324
- ### Safety & Encoding
325
-
326
- | Filter | Example | Description |
327
- |--------|---------|-------------|
328
- | `escape` | `{{ html\|escape }}` | HTML escape |
329
- | `safe` | `{{ html\|safe }}` | Mark as safe (no escape) |
330
- | `urlencode` | `{{ url\|urlencode }}` | URL encode |
331
- | `iriencode` | `{{ url\|iriencode }}` | IRI encode (unicode-safe) |
332
- | `json` | `{{ data\|json }}` | JSON stringify |
333
- | `json_script` | `{{ data\|json_script:"id" }}` | Safe JSON in script tag |
334
- | `truncatechars_html` | `{{ html\|truncatechars_html:10 }}` | Truncate preserving HTML |
335
- | `truncatewords_html` | `{{ html\|truncatewords_html:5 }}` | Truncate words in HTML |
336
- | `urlizetrunc` | `{{ text\|urlizetrunc:15 }}` | URLs with truncated display |
337
-
338
- ### Default Values
339
-
340
- | Filter | Example | Output |
341
- |--------|---------|--------|
342
- | `default` | `{{ missing\|default:"N/A" }}` | `N/A` |
343
- | `default_if_none` | `{{ null\|default_if_none:"None" }}` | `None` |
344
- | `yesno` | `{{ true\|yesno:"Yes,No" }}` | `Yes` |
345
- | `pluralize` | `{{ count\|pluralize }}` | `s` or `` |
345
+ ### Safety & Encoding Filters (13)
346
+
347
+ | Filter | Description | Example |
348
+ |--------|-------------|---------|
349
+ | `escape` / `e` | HTML escape | `{{ html\|escape }}` |
350
+ | `forceescape` | Force HTML escape | `{{ html\|forceescape }}` |
351
+ | `safe` | Mark as safe | `{{ html\|safe }}` |
352
+ | `safeseq` | Mark sequence safe | `{{ items\|safeseq }}` |
353
+ | `escapejs` | JS string escape | `{{ text\|escapejs }}` |
354
+ | `urlencode` | URL encode | `{{ url\|urlencode }}` |
355
+ | `iriencode` | IRI encode | `{{ url\|iriencode }}` |
356
+ | `urlize` | URLs to links | `{{ text\|urlize }}` |
357
+ | `urlizetrunc` | URLs to links (truncated) | `{{ text\|urlizetrunc:15 }}` |
358
+ | `json` / `tojson` | JSON stringify | `{{ data\|json }}` |
359
+ | `json_script` | Safe JSON in script | `{{ data\|json_script:"id" }}` |
360
+ | `pprint` | Pretty print | `{{ data\|pprint }}` |
361
+ | `xmlattr` | Dict to XML attrs | `{{ attrs\|xmlattr }}` |
362
+
363
+ ### Default/Conditional Filters (4)
364
+
365
+ | Filter | Description | Example |
366
+ |--------|-------------|---------|
367
+ | `default` / `d` | Default value | `{{ missing\|default:"N/A" }}` |
368
+ | `default_if_none` | Default if null | `{{ val\|default_if_none:"None" }}` |
369
+ | `yesno` | Boolean to text | `{{ true\|yesno:"Yes,No" }}` → `Yes` |
370
+ | `pluralize` | Pluralize suffix | `{{ count\|pluralize }}` → `s` |
371
+
372
+ ### Misc Filters (2)
373
+
374
+ | Filter | Description | Example |
375
+ |--------|-------------|---------|
376
+ | `items` | Dict to pairs | `{% for k,v in dict\|items %}` |
377
+ | `unordered_list` | Nested list to HTML | `{{ items\|unordered_list }}` |
346
378
 
347
379
  ---
348
380