xcomponent 0.5.2__tar.gz → 0.6.1__tar.gz

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.
Files changed (84) hide show
  1. {xcomponent-0.5.2 → xcomponent-0.6.1}/CHANGELOG.md +10 -0
  2. {xcomponent-0.5.2 → xcomponent-0.6.1}/Cargo.lock +1 -1
  3. {xcomponent-0.5.2 → xcomponent-0.6.1}/Cargo.toml +1 -1
  4. {xcomponent-0.5.2 → xcomponent-0.6.1}/Justfile +7 -1
  5. {xcomponent-0.5.2 → xcomponent-0.6.1}/PKG-INFO +2 -2
  6. {xcomponent-0.5.2 → xcomponent-0.6.1}/docs/source/user/getting_started.md +47 -12
  7. {xcomponent-0.5.2 → xcomponent-0.6.1}/pyproject.toml +5 -3
  8. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/rust/context.rs +3 -0
  9. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/rust/expression/ast/eval.rs +9 -4
  10. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/rust/expression/ast/model.rs +4 -0
  11. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/rust/expression/ast/parse.rs +5 -0
  12. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/rust/expression/grammar.pest +31 -4
  13. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/rust/expression/parser.rs +8 -0
  14. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/rust/expression/tokens.rs +5 -0
  15. xcomponent-0.6.1/tests/unittests/conftest.py +41 -0
  16. {xcomponent-0.5.2 → xcomponent-0.6.1}/tests/unittests/expression/test_if_expr.py +19 -0
  17. xcomponent-0.6.1/tests/unittests/expression/test_let.py +26 -0
  18. xcomponent-0.6.1/tests/unittests/expression/test_str_method.py +22 -0
  19. xcomponent-0.6.1/tests/unittests/test_named_component.py +30 -0
  20. xcomponent-0.6.1/tests/unittests/test_render_attributes.py +141 -0
  21. xcomponent-0.6.1/tests/unittests/test_render_context.py +55 -0
  22. xcomponent-0.6.1/tests/unittests/test_render_globals.py +50 -0
  23. {xcomponent-0.5.2 → xcomponent-0.6.1}/tests/unittests/test_render_globals_obj.py +18 -19
  24. xcomponent-0.6.1/tests/unittests/test_render_globals_obj2.py +55 -0
  25. xcomponent-0.6.1/tests/unittests/test_render_nested_dict.py +24 -0
  26. xcomponent-0.6.1/tests/unittests/test_render_nested_node.py +58 -0
  27. xcomponent-0.6.1/tests/unittests/test_render_node.py +167 -0
  28. xcomponent-0.6.1/tests/unittests/test_render_script.py +22 -0
  29. {xcomponent-0.5.2 → xcomponent-0.6.1}/uv.lock +16 -6
  30. xcomponent-0.5.2/tests/unittests/test_named_component.py +0 -31
  31. xcomponent-0.5.2/tests/unittests/test_render_attributes.py +0 -158
  32. xcomponent-0.5.2/tests/unittests/test_render_context.py +0 -50
  33. xcomponent-0.5.2/tests/unittests/test_render_globals.py +0 -49
  34. xcomponent-0.5.2/tests/unittests/test_render_globals_obj2.py +0 -54
  35. xcomponent-0.5.2/tests/unittests/test_render_nested_dict.py +0 -22
  36. xcomponent-0.5.2/tests/unittests/test_render_nested_node.py +0 -56
  37. xcomponent-0.5.2/tests/unittests/test_render_node.py +0 -146
  38. xcomponent-0.5.2/tests/unittests/test_render_script.py +0 -19
  39. {xcomponent-0.5.2 → xcomponent-0.6.1}/.github/workflows/build-sdist.yml +0 -0
  40. {xcomponent-0.5.2 → xcomponent-0.6.1}/.github/workflows/build-wheels.yml +0 -0
  41. {xcomponent-0.5.2 → xcomponent-0.6.1}/.github/workflows/publish-doc.yml +0 -0
  42. {xcomponent-0.5.2 → xcomponent-0.6.1}/.github/workflows/publish-pypi.yml +0 -0
  43. {xcomponent-0.5.2 → xcomponent-0.6.1}/.github/workflows/release.yml +0 -0
  44. {xcomponent-0.5.2 → xcomponent-0.6.1}/.github/workflows/tests-artifacts.yml +0 -0
  45. {xcomponent-0.5.2 → xcomponent-0.6.1}/.github/workflows/tests.yml +0 -0
  46. {xcomponent-0.5.2 → xcomponent-0.6.1}/.gitignore +0 -0
  47. {xcomponent-0.5.2 → xcomponent-0.6.1}/.python-version +0 -0
  48. {xcomponent-0.5.2 → xcomponent-0.6.1}/LICENSE +0 -0
  49. {xcomponent-0.5.2 → xcomponent-0.6.1}/README.md +0 -0
  50. {xcomponent-0.5.2 → xcomponent-0.6.1}/docs/Makefile +0 -0
  51. {xcomponent-0.5.2 → xcomponent-0.6.1}/docs/source/conf.py +0 -0
  52. {xcomponent-0.5.2 → xcomponent-0.6.1}/docs/source/develop/.gitkeep +0 -0
  53. {xcomponent-0.5.2 → xcomponent-0.6.1}/docs/source/index.md +0 -0
  54. {xcomponent-0.5.2 → xcomponent-0.6.1}/docs/source/user/changelog.md +0 -0
  55. {xcomponent-0.5.2 → xcomponent-0.6.1}/scripts/get_version.py +0 -0
  56. {xcomponent-0.5.2 → xcomponent-0.6.1}/scripts/write_changelog.py +0 -0
  57. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/python/xcomponent/__init__.py +0 -0
  58. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/python/xcomponent/py.typed +0 -0
  59. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/python/xcomponent/service/__init__.py +0 -0
  60. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/python/xcomponent/service/catalog.py +0 -0
  61. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/python/xcomponent/xcore.pyi +0 -0
  62. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/rust/catalog/catalog.rs +0 -0
  63. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/rust/catalog/mod.rs +0 -0
  64. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/rust/expression/ast/mod.rs +0 -0
  65. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/rust/expression/mod.rs +0 -0
  66. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/rust/lib.rs +0 -0
  67. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/rust/markup/grammar.pest +0 -0
  68. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/rust/markup/mod.rs +0 -0
  69. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/rust/markup/parser.rs +0 -0
  70. {xcomponent-0.5.2 → xcomponent-0.6.1}/src/rust/markup/tokens.rs +0 -0
  71. {xcomponent-0.5.2 → xcomponent-0.6.1}/tests/__init__.py +0 -0
  72. {xcomponent-0.5.2 → xcomponent-0.6.1}/tests/unittests/__init__.py +0 -0
  73. {xcomponent-0.5.2 → xcomponent-0.6.1}/tests/unittests/expression/test_binary_eq.py +0 -0
  74. {xcomponent-0.5.2 → xcomponent-0.6.1}/tests/unittests/expression/test_binary_op.py +0 -0
  75. {xcomponent-0.5.2 → xcomponent-0.6.1}/tests/unittests/expression/test_binary_op_bool.py +0 -0
  76. {xcomponent-0.5.2 → xcomponent-0.6.1}/tests/unittests/expression/test_default_values.py +0 -0
  77. {xcomponent-0.5.2 → xcomponent-0.6.1}/tests/unittests/expression/test_field_access.py +0 -0
  78. {xcomponent-0.5.2 → xcomponent-0.6.1}/tests/unittests/expression/test_for_expr.py +0 -0
  79. {xcomponent-0.5.2 → xcomponent-0.6.1}/tests/unittests/expression/test_func.py +0 -0
  80. {xcomponent-0.5.2 → xcomponent-0.6.1}/tests/unittests/expression/test_nested.py +0 -0
  81. {xcomponent-0.5.2 → xcomponent-0.6.1}/tests/unittests/expression/test_render_comment.py +0 -0
  82. {xcomponent-0.5.2 → xcomponent-0.6.1}/tests/unittests/expression/test_types.py +0 -0
  83. {xcomponent-0.5.2 → xcomponent-0.6.1}/tests/unittests/expression/test_unary.py +0 -0
  84. {xcomponent-0.5.2 → xcomponent-0.6.1}/tests/unittests/test_core.py +0 -0
@@ -1,3 +1,13 @@
1
+ ## 0.6.1 - 2025-06-13
2
+
3
+ * Authorize full markup inside statement instead of self closed element only.
4
+ * Upgrade sphinx version.
5
+
6
+ ## 0.6.0 - 2025-06-06
7
+
8
+ * Add a let keyword in order to set variable in expression.
9
+ * Add support of string methods on expression.
10
+
1
11
  ## 0.5.2 - 2025-06-03
2
12
 
3
13
  * Hotfix component havin attributes'name containing python reserved keyword.
@@ -595,7 +595,7 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
595
595
 
596
596
  [[package]]
597
597
  name = "xcore"
598
- version = "0.5.2"
598
+ version = "0.6.1"
599
599
  dependencies = [
600
600
  "env_logger",
601
601
  "html-escape",
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "xcore"
3
- version = "0.5.2"
3
+ version = "0.6.1"
4
4
  authors = ["Guillaume Gauvrit <guillaume@gauvr.it>"]
5
5
  edition = "2021"
6
6
  description = "JSX-like for python using maturin/pyo3 and pest."
@@ -7,7 +7,13 @@ develop:
7
7
  uv run maturin develop
8
8
 
9
9
  install:
10
- uv sync --group dev
10
+ uv sync --group dev --frozen
11
+
12
+ update:
13
+ uv sync --group dev --group docs
14
+
15
+ upgrade:
16
+ uv sync --group dev --group docs --upgrade
11
17
 
12
18
  doc:
13
19
  uv sync --group dev --group docs
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xcomponent
3
- Version: 0.5.2
3
+ Version: 0.6.1
4
4
  Classifier: License :: OSI Approved :: MIT License
5
5
  Classifier: Programming Language :: Python :: 3
6
6
  Classifier: Programming Language :: Python :: 3.11
@@ -15,7 +15,7 @@ Classifier: Typing :: Typed
15
15
  Requires-Dist: furo>=2024.5.6 ; extra == 'docs'
16
16
  Requires-Dist: linkify-it-py>=2.0.3,<3 ; extra == 'docs'
17
17
  Requires-Dist: myst-parser>=4.0.0,<5 ; extra == 'docs'
18
- Requires-Dist: sphinx>=7.0.1,<8 ; extra == 'docs'
18
+ Requires-Dist: sphinx>=8.0.0,<9 ; extra == 'docs'
19
19
  Requires-Dist: sphinx-autodoc2>=0.5.0,<1 ; extra == 'docs'
20
20
  Provides-Extra: docs
21
21
  License-File: LICENSE
@@ -228,7 +228,6 @@ assert HelloWebPage(globals={"title": "my title", "description": ""}) == (
228
228
  )
229
229
  ```
230
230
 
231
-
232
231
  The description is passed as an empty string and is mandatory.
233
232
  The access to `globals.description` will raise a KeyError if it is not present.
234
233
 
@@ -241,7 +240,7 @@ proper documentation.
241
240
 
242
241
  Everything in the Markup that has been enclosed by curly braces is an expression.
243
242
  An expression enables the dynamic rendering of a page, using variables,
244
- `if` statements, `for` statements, and operators.
243
+ `if` statements, `for` statements, `let` statements, and operators.
245
244
 
246
245
  ### Types
247
246
 
@@ -268,8 +267,8 @@ String objects can be enclosed by double quotes or single quotes.
268
267
  Boolean values are `true` and `false`, like in JavaScript, Rust, and many languages,
269
268
  except Python.
270
269
 
271
- None value is render has an empty string for a XNode, and completely remove
272
- the rendering of attributes.
270
+ None value is render has an empty string for a XNode, and is used to remove
271
+ the rendering of tag attributes.
273
272
 
274
273
  ### Functions
275
274
 
@@ -287,6 +286,20 @@ def HelloWorld(name: str) -> str:
287
286
 
288
287
  When the functions are called, the simple types are cast to Python types.
289
288
 
289
+ ````{node}
290
+
291
+ It is possible to call the str methods directly in the XComponent expression.
292
+
293
+ The capitalized function was a simple exemple for function explanation.
294
+
295
+ ```python
296
+ @catalog.component
297
+ def HelloWorld(name: str) -> str:
298
+ return """<>{"Hello " + name.capitalize()}</>"""
299
+ ```
300
+
301
+ ````
302
+
290
303
  ### List Index
291
304
 
292
305
  To access an index in a list, the `[]` must be used.
@@ -327,7 +340,6 @@ between booleans and integers are permitted.
327
340
 
328
341
  Strings can be multiplied by an integer, like in Python, to produce a repeated string.
329
342
 
330
-
331
343
  #### Binary Operators
332
344
 
333
345
  All types support Python truthy/falsey values.
@@ -336,15 +348,12 @@ The operators include `and` and `or`. The `not` operator is used to reverse a co
336
348
 
337
349
  Also, Python functions can be registered to mitigate or implement complex binary operations.
338
350
 
339
-
340
351
  #### Comparison Operators
341
352
 
342
353
  | `==` | `!=` | `>=` | `<=` | `>` | `<` |
343
354
  | ------ | ---------- | --------------------- | ------------------ | ------------ | --------- |
344
355
  | equals | not equals | greater than or equal | less than or equal | greater than | less than |
345
356
 
346
-
347
-
348
357
  #### Priority
349
358
 
350
359
  The priority of operators follows the mathematical order.
@@ -352,8 +361,7 @@ Multiplication and division have the highest priority, followed by addition and
352
361
  subtraction, then greater than or equal to and less than or equal to, next are
353
362
  equals and not equals, followed by the and operator, and finally the or operator.
354
363
 
355
- The parenthesis, such as **( *condition* )** can be used to override the priority.
356
-
364
+ The parenthesis, such as **( _condition_ )** can be used to override the priority.
357
365
 
358
366
  ### If Syntax
359
367
 
@@ -371,7 +379,7 @@ Example:
371
379
  { if elements > 0 { <Elements elements={elements} /> } else { <Empty/>} }
372
380
  ```
373
381
 
374
- Blocks can contain other expressions or self-closed markup only as a design choice.
382
+ Blocks can contain other expressions or markup.
375
383
 
376
384
  ### For Syntax
377
385
 
@@ -387,7 +395,7 @@ Example:
387
395
  {for k in my_dict { <Item key={k} value={my_dict[k]}/>}}
388
396
  ```
389
397
 
390
- Blocks can contain other expressions or self-closed markup only as a design choice.
398
+ Blocks can contain other expressions or markup.
391
399
 
392
400
  ````{important}
393
401
  this is not python code, you can do
@@ -398,3 +406,30 @@ this is not python code, you can do
398
406
  }
399
407
  ```
400
408
  ````
409
+
410
+ ### Let Syntax
411
+
412
+ The `let` keywork let you declare new constant in the component.
413
+
414
+ The expression cannot have multiple statement separated by indentation or semicolum.
415
+ Since the expression language is minimal, an other expression can be used to display
416
+ a variable that has been set, or reuse it to do a condition.
417
+ The variable will be local to the component.
418
+ Says differently, the `let` keyword declare constant scoped by component.
419
+
420
+ The `let` syntax is like the Rust syntax.
421
+ It means that the binary expression is affected to the variable.
422
+
423
+ Example:
424
+
425
+ ```jsx
426
+ {/* simple let statement */}
427
+ { let x = 42 }
428
+ {/* display the value */}
429
+ {x}
430
+
431
+ {/* if statement with else */}
432
+ {let y = if (x >= 1) {"Yes"} else {"No"}}
433
+ {/* display the value */}
434
+ {y}
435
+ ```
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "xcomponent"
3
- version = "0.5.2"
3
+ version = "0.6.1"
4
4
  description = "A component base template engige - inspired by JSX"
5
5
  readme = "README.md"
6
6
  authors = [{ name = "Guillaume Gauvrit", email = "guillaume@gauvr.it" }]
@@ -34,7 +34,7 @@ docs = [
34
34
  "furo >=2024.5.6",
35
35
  "linkify-it-py >=2.0.3,<3",
36
36
  "myst-parser >=4.0.0,<5",
37
- "sphinx >=7.0.1,<8",
37
+ "sphinx >=8.0.0,<9",
38
38
  "sphinx-autodoc2 >=0.5.0,<1",
39
39
  ]
40
40
 
@@ -48,11 +48,13 @@ docs = [
48
48
  "furo >=2024.5.6",
49
49
  "linkify-it-py >=2.0.3,<3",
50
50
  "myst-parser >=4.0.0,<5",
51
- "sphinx >=7.0.1,<8",
51
+ "sphinx >=8.0.0,<9",
52
52
  "sphinx-autodoc2 >=0.5.0,<1",
53
53
  ]
54
54
 
55
55
  [tool.pyright]
56
+ autoSearchPaths = false
57
+ extraPaths = ["src/python"]
56
58
  include = ["src/python", "tests"]
57
59
  typeCheckingMode = "strict"
58
60
  reportMissingTypeStubs = false
@@ -302,6 +302,9 @@ impl RenderContext {
302
302
  d.insert(key, value);
303
303
  self.stack.push(d);
304
304
  }
305
+ pub fn insert_current(&mut self, key: LiteralKey, value: Literal) {
306
+ self.stack.last_mut().unwrap().insert(key, value);
307
+ }
305
308
  pub fn get(&self, key: &LiteralKey) -> Option<&Literal> {
306
309
  self.stack.iter().rev().find_map(|scope| scope.get(key))
307
310
  }
@@ -264,10 +264,10 @@ pub fn eval_ast<'py>(
264
264
  let item = o.obj().getattr(py, field)?.into_bound(py);
265
265
  Literal::downcast(py, item)
266
266
  }
267
- _ => Err(PyErr::new::<pyo3::exceptions::PyTypeError, _>(format!(
268
- "Cannot access field '{}' on non-object",
269
- field
270
- ))),
267
+ _ => {
268
+ let item = base.into_py(py).getattr(field)?;
269
+ Literal::downcast(py, item)
270
+ }
271
271
  }
272
272
  }
273
273
 
@@ -401,6 +401,11 @@ pub fn eval_ast<'py>(
401
401
  ))),
402
402
  }
403
403
  }
404
+ AST::LetStatement { ident, expr } => {
405
+ let value = eval_ast(py, expr, catalog, context)?;
406
+ context.insert_current(LiteralKey::Str(ident.clone()), value);
407
+ Ok(Literal::Str("".to_string()))
408
+ }
404
409
  }
405
410
  }
406
411
 
@@ -32,4 +32,8 @@ pub enum AST {
32
32
  iterable: Box<AST>,
33
33
  body: Box<AST>,
34
34
  },
35
+ LetStatement {
36
+ ident: String,
37
+ expr: Box<AST>,
38
+ },
35
39
  }
@@ -54,6 +54,11 @@ pub fn token_to_ast(tok: &ExpressionToken, min_prec: u8) -> Result<AST, PyErr> {
54
54
  iterable: token_to_ast(iterable, min_prec).map(|x| Box::new(x))?,
55
55
  body: token_to_ast(body, min_prec).map(|x| Box::new(x))?,
56
56
  }),
57
+
58
+ ExpressionToken::LetExpression { ident, expr } => Ok(AST::LetStatement {
59
+ ident: ident.clone(),
60
+ expr: token_to_ast(expr, min_prec).map(|x| Box::new(x))?,
61
+ }),
57
62
  // Comment produce a Noop
58
63
  ExpressionToken::Noop => Ok(AST::Literal(Literal::Str("".to_string()))),
59
64
  _ => Err(PySyntaxError::new_err(format!(
@@ -1,8 +1,8 @@
1
- expression = { for_expression | if_expression | binary_expression | comment_expression }
1
+ expression = { for_expression | if_expression | let_expression | binary_expression | comment_expression }
2
2
 
3
3
  binary_expression = { comment_expression* ~ whitespace* ~ term ~ (whitespace* ~ operator ~ whitespace* ~ term)* }
4
4
 
5
- term = _{ for_expression | if_expression | unary_expression | postfix }
5
+ term = _{ for_expression | if_expression | let_expression | unary_expression | postfix }
6
6
 
7
7
  unary_expression = { "not" ~ whitespace+ ~ binary_expression }
8
8
 
@@ -21,14 +21,13 @@ pos_arg = { expression }
21
21
  kw_arg = { ident ~ whitespace* ~ "=" ~ whitespace* ~ expression }
22
22
 
23
23
  primary = _{ "(" ~ expression ~ ")" | component | string | integer | boolean | ident }
24
- component = { "<" ~ (!"/>" ~ ANY)* ~ "/>" }
25
24
 
26
25
  operator = { "+" | "-" | "*" | "/" | "and" | "or" | "==" | "!=" | ">=" | "<=" | ">" | "<" }
27
26
  boolean = { "true" | "false" }
28
27
  integer = @{ (ASCII_DIGIT | "_")+ }
29
28
  string = @{ "\"" ~ (escape_sequence | !("\"" | "\\") ~ ANY)* ~ "\"" }
30
29
 
31
- keyword = { "for" | "in" | "if" | "else" | "not" | "and" | "or" }
30
+ keyword = { "for" | "in" | "if" | "else" | "not" | "and" | "or" | "let" }
32
31
 
33
32
  keyword_spaced = { keyword ~ (whitespace+ | ".") }
34
33
  ident = { !keyword_spaced ~ ident_raw }
@@ -43,6 +42,34 @@ if_expression = {
43
42
 
44
43
  for_expression = { "for" ~ whitespace+ ~ ident ~ whitespace+ ~ "in" ~ whitespace+ ~ expression ~ whitespace* ~ block }
45
44
 
45
+ let_expression = { "let" ~ whitespace+ ~ ident ~ whitespace* ~ "=" ~ whitespace* ~ expression }
46
+
46
47
  block = { whitespace* ~"{" ~ whitespace* ~ expression ~ whitespace* ~ "}" ~ whitespace* }
47
48
  comment_expression = _{ "/*" ~ comment ~ "*/" }
48
49
  comment = { (!"*/" ~ ANY)* }
50
+
51
+ component = { markup_element | markup_fragment }
52
+ markup_node = _{ markup_element | markup_fragment | markup_comment | markup_text }
53
+ markup_element = _{ markup_self_closing_element | markup_normal_element }
54
+
55
+ markup_normal_element = {
56
+ markup_open_tag ~ markup_node* ~ markup_close_tag
57
+ }
58
+
59
+ markup_self_closing_element = _{ "<" ~ markup_ident ~ (whitespace* ~ markup_attribute)* ~ whitespace* ~ "/>" }
60
+ markup_open_tag = _{ "<" ~ markup_ident ~ (whitespace* ~ markup_attribute)* ~ whitespace* ~ ">" }
61
+ markup_close_tag = @{ "</" ~ markup_ident ~ ">" }
62
+
63
+ markup_fragment = _{ markup_open_fragment ~ markup_node* ~ markup_close_fragment }
64
+ markup_open_fragment = _{ "<>" }
65
+ markup_close_fragment = _{ "</>" }
66
+ markup_attribute = _{ markup_attr_name ~ ("=" ~ markup_attr_value)? }
67
+ markup_comment = _{ "<!--" ~ (!"-->" ~ ANY)* ~ "-->" }
68
+ markup_expression = _{ "{" ~ (markup_expression | (!"}" ~ ANY))* ~ "}" }
69
+
70
+ markup_ident = @{ ASCII_ALPHANUMERIC ~ (ASCII_ALPHANUMERIC | "_" )* }
71
+ markup_attr_name = @{ (ASCII_ALPHA | "_" ) ~ (ASCII_ALPHANUMERIC | "_" | "-" | ":" | "." )* }
72
+ markup_attr_value = @{ markup_string_literal_quote | markup_string_literal_doublequote | markup_expression }
73
+ markup_string_literal_doublequote = _{ "\"" ~ (!"\"" ~ ANY)* ~ "\"" }
74
+ markup_string_literal_quote = _{ "'" ~ (!"'" ~ ANY)* ~ "'" }
75
+ markup_text = @{ (!("<" | "{") ~ ANY)+ }
@@ -115,6 +115,14 @@ fn parse_expression_token(pair: Pair<Rule>) -> Result<ExpressionToken, String> {
115
115
  body,
116
116
  })
117
117
  }
118
+ Rule::let_expression => {
119
+ let mut inner = pair.into_inner();
120
+ let ident = inner.next().unwrap().as_str().to_string();
121
+ let expr_expr = inner.next().unwrap();
122
+ let expr = Box::new(parse_expression_token(expr_expr)?);
123
+
124
+ Ok(ExpressionToken::LetExpression { ident, expr })
125
+ }
118
126
  Rule::ident => {
119
127
  let content = pair.as_str();
120
128
  debug!("Pushing ident {}", content);
@@ -148,6 +148,10 @@ pub enum ExpressionToken {
148
148
  iterable: Box<ExpressionToken>,
149
149
  body: Box<ExpressionToken>,
150
150
  },
151
+ LetExpression {
152
+ ident: String,
153
+ expr: Box<ExpressionToken>,
154
+ },
151
155
  Noop,
152
156
  }
153
157
 
@@ -200,6 +204,7 @@ impl std::fmt::Display for ExpressionToken {
200
204
  iterable,
201
205
  body,
202
206
  } => write!(f, "for {} in {} {{ {} }}", ident, iterable, body),
207
+ ExpressionToken::LetExpression { ident, expr } => write!(f, "let {} = {}", ident, expr),
203
208
  ExpressionToken::Noop => write!(f, ""), // ??
204
209
  }
205
210
  }
@@ -0,0 +1,41 @@
1
+ import re
2
+ from collections.abc import Iterator, Mapping
3
+
4
+ import pytest
5
+ from bs4 import BeautifulSoup, PageElement # type: ignore
6
+
7
+ from xcomponent import Catalog
8
+
9
+
10
+ @pytest.fixture()
11
+ def catalog() -> Catalog:
12
+ return Catalog()
13
+
14
+
15
+ @pytest.fixture()
16
+ def globals() -> Mapping[str, str]:
17
+ return {
18
+ "RADIO_DIV_CLASS": "RADIO_DIV_CLASS",
19
+ "RADIO_INPUT_CLASS": "RADIO_INPUT_CLASS",
20
+ "RADIO_LABEL_CLASS": "RADIO_LABEL_CLASS",
21
+ }
22
+
23
+
24
+ @pytest.fixture()
25
+ def soup_rendered(
26
+ catalog: Catalog, template_string: str, globals: Mapping[str, str]
27
+ ) -> Iterator[PageElement]:
28
+ rendered = catalog.render(template_string.strip(), globals=globals)
29
+ soup = BeautifulSoup(rendered, "html.parser")
30
+ try:
31
+ yield next(soup.children) # type: ignore
32
+ except Exception as exc:
33
+ # Display the error in the assertion is a compromise
34
+ yield exc # type: ignore
35
+
36
+
37
+ @pytest.fixture()
38
+ def soup_expected(expected_string: str) -> PageElement:
39
+ expected_string = re.sub(r">\s+<", "><", expected_string).strip()
40
+ expected_soup = BeautifulSoup(expected_string, features="html.parser")
41
+ return next(expected_soup.children) # type: ignore
@@ -30,6 +30,21 @@ def IfListStmt(a: list[str], b: str) -> str:
30
30
  return """<p>{if a { for x in a { a } } else { b } }</p>"""
31
31
 
32
32
 
33
+ @catalog.component
34
+ def Haha():
35
+ return "<p>Ha ha!</p>"
36
+
37
+
38
+ @catalog.component
39
+ def IfSelfClosedComponent(a: bool) -> str:
40
+ return """<>{if a { <Haha /> } }</>"""
41
+
42
+
43
+ @catalog.component
44
+ def IfComponent(a: bool) -> str:
45
+ return """<>{if a { <p>Ha ha!</p> } }</>"""
46
+
47
+
33
48
  @pytest.mark.parametrize(
34
49
  "result,expected",
35
50
  [
@@ -43,6 +58,10 @@ def IfListStmt(a: list[str], b: str) -> str:
43
58
  pytest.param(IfNotElseStmt(False, "No", "Yes"), "<p>No</p>"),
44
59
  pytest.param(IfListStmt(["a"], "b"), "<p>a</p>"),
45
60
  pytest.param(IfListStmt([], "b"), "<p>b</p>"),
61
+ pytest.param(IfSelfClosedComponent(True), "<p>Ha ha!</p>"),
62
+ pytest.param(IfSelfClosedComponent(False), ""),
63
+ pytest.param(IfComponent(True), "<p>Ha ha!</p>"),
64
+ pytest.param(IfComponent(False), ""),
46
65
  ],
47
66
  )
48
67
  def test_if(result: str, expected: str):
@@ -0,0 +1,26 @@
1
+ import pytest
2
+
3
+ from xcomponent import Catalog
4
+
5
+
6
+ @pytest.fixture(autouse=True)
7
+ def components(catalog: Catalog):
8
+ @catalog.component
9
+ def DummyInt() -> str:
10
+ return """<>{let a = 42}{a}</>"""
11
+
12
+ @catalog.component
13
+ def YesNo(a: int) -> str:
14
+ return """<>{let b = if (a >= 1) {"Yes"} else {"No"}}{b}</>"""
15
+
16
+
17
+ @pytest.mark.parametrize(
18
+ "template_string,expected",
19
+ [
20
+ pytest.param("<DummyInt />", "42", id="simple let"),
21
+ pytest.param("<YesNo a={0} />", "No", id="let if"),
22
+ pytest.param("<YesNo a={2} />", "Yes", id="let else"),
23
+ ],
24
+ )
25
+ def test_nested(catalog: Catalog, template_string: str, expected: str):
26
+ assert catalog.render(template_string) == expected
@@ -0,0 +1,22 @@
1
+ from xcomponent import Catalog
2
+
3
+ import pytest
4
+
5
+
6
+ @pytest.fixture(autouse=True)
7
+ def components(catalog: Catalog):
8
+ @catalog.component
9
+ def Dummy() -> str:
10
+ return '<>{"dummy-str".replace("-", "_")}</>'
11
+
12
+ return Dummy
13
+
14
+
15
+ @pytest.mark.parametrize(
16
+ "template_string,expected",
17
+ [
18
+ pytest.param("<Dummy />", "dummy_str", id="replace"),
19
+ ],
20
+ )
21
+ def test_render_nested_property(catalog: Catalog, template_string: str, expected: str):
22
+ assert catalog.render(template_string) == expected
@@ -0,0 +1,30 @@
1
+ import pytest
2
+
3
+ from xcomponent import Catalog
4
+
5
+
6
+ @pytest.fixture(autouse=True)
7
+ def components(catalog: Catalog):
8
+ @catalog.component(name="HelloWorld1")
9
+ def hello_world(name="world"):
10
+ return "<>Hello {name}</>"
11
+
12
+ @catalog.component()
13
+ def HelloWorld2(name="world"):
14
+ return "<>Hello {name}</>"
15
+
16
+ @catalog.component
17
+ def HelloWorld3(name="world"):
18
+ return "<>Hello {name}</>"
19
+
20
+
21
+ @pytest.mark.parametrize(
22
+ "markup",
23
+ [
24
+ pytest.param("<HelloWorld1/>", id="explicit name"),
25
+ pytest.param("<HelloWorld2/>", id="called implicit"),
26
+ pytest.param("<HelloWorld3/>", id="implicit"),
27
+ ],
28
+ )
29
+ def test_named_component(catalog: Catalog, markup: str):
30
+ assert catalog.render(markup) == "Hello world"