1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-27 08:39:30 +00:00

[stdlib] add group text by tags, defined

This commit is contained in:
Étienne Fildadut 2024-11-17 16:07:46 +01:00
parent 49c9741349
commit 0aedf99a78
5 changed files with 105 additions and 14 deletions

View file

@ -5,6 +5,8 @@ local ArgumentTuple, Struct
local to_anselme = require("anselme.common.to_anselme") local to_anselme = require("anselme.common.to_anselme")
local group_text_by_tag_identifier
--- A Lua-friendly representation of an Anselme Text value. --- A Lua-friendly representation of an Anselme Text value.
-- They appear in both TextEventData and ChoiceEventData to represent the text that has to be shown. -- They appear in both TextEventData and ChoiceEventData to represent the text that has to be shown.
-- --
@ -191,11 +193,17 @@ Text = Runtime(Event) {
for _, text in event_buffer:iter(state) do for _, text in event_buffer:iter(state) do
table.insert(l, text:to_lua(state)) table.insert(l, text:to_lua(state))
end end
return l if state.scope:defined(group_text_by_tag_identifier) then
local tag_key = state.scope:get(group_text_by_tag_identifier)
return l:group_by(tag_key)
else
return { l }
end
end, end,
} }
package.loaded[...] = Text package.loaded[...] = Text
ArgumentTuple, Struct = ast.ArgumentTuple, ast.Struct ArgumentTuple, Struct = ast.ArgumentTuple, ast.Struct
group_text_by_tag_identifier = ast.Identifier:new("_group_text_by_tag")
return Text return Text

View file

@ -16,7 +16,20 @@ return {
if l:truthy() then if l:truthy() then
return Boolean:new(env:defined(state, s:to_identifier())) return Boolean:new(env:defined(state, s:to_identifier()))
else else
return Boolean:new(env:defined_in_current(state, s:to_identifier())) return Boolean:new(env:defined_in_current(state, s:to_symbol()))
end
end
},
{
--- Returns true if the variable named `var` is defined in in the current scope, false otherwise.
--
-- If `search parent` is true, this will also search in parent scopes of the current scope.
"defined", "(var::is string, search parent::is boolean=true)",
function(state, s, l)
if l:truthy() then
return Boolean:new(state.scope:defined(s:to_identifier()))
else
return Boolean:new(state.scope:defined_in_current(s:to_symbol()))
end end
end end
}, },

View file

@ -1,6 +1,8 @@
-- TODO: doc in other language -- TODO: doc in other language
return [[ return [[
:@format = stdlib.format
:@bloc attaché = stdlib.attached block :@bloc attaché = stdlib.attached block
:@afficher = stdlib.print :@afficher = stdlib.print
@ -83,4 +85,6 @@ return [[
:@persister = stdlib.persist :@persister = stdlib.persist
:@écrire choix = stdlib.write choice :@écrire choix = stdlib.write choice
:@grouper texte par tag = stdlib.group text by tag
]] ]]

View file

@ -9,6 +9,9 @@ local translation_manager = require("anselme.state.translation_manager")
local tag_manager = require("anselme.state.tag_manager") local tag_manager = require("anselme.state.tag_manager")
local resume_manager = require("anselme.state.resume_manager") local resume_manager = require("anselme.state.resume_manager")
local group_text_by_tag_identifier = Identifier:new("_group_text_by_tag")
local group_text_by_tag_symbol = group_text_by_tag_identifier:to_symbol { exported = true }
return { return {
-- text -- text
{ {
@ -41,6 +44,37 @@ return {
end end
}, },
{
--- Cause future text events to be each split into separate text event every time the value of the tag with the key `tag_key` changes.
--
-- For example, with the following Anselme script:
-- ```
-- group text by tag("speaker")
-- speaker: "John" #
-- | A
-- | B
-- speaker: "Lana" #
-- | C
-- speaker: "John" #
-- | D
-- ```
-- will produce three separate text events instead of one:
-- * the first with the texts "A" and "B"; both with the tag `speaker="John"`
-- * the second with the text "C"; with the tag `speaker="Lana"`
-- * the last with the text "D"; wiith the tag `speaker="John"`
--
-- This setting affect will affect the whole state.
"group text by tag", "(tag::is string)",
function(state, tag)
if not state.scope:defined(group_text_by_tag_identifier) then
state.scope:define(group_text_by_tag_symbol, tag)
else
state.scope:set(group_text_by_tag_identifier, tag)
end
return Nil:new()
end
},
-- choice -- choice
{ {
--- Write a choice event to the event buffer using this text and `fn` as the function to call if the choice is selected. --- Write a choice event to the event buffer using this text and `fn` as the function to call if the choice is selected.

View file

@ -650,19 +650,43 @@ _defined at line 21 of [anselme/stdlib/string.lua](../anselme/stdlib/string.lua)
Concatenate two texts, returning a new text value. Concatenate two texts, returning a new text value.
_defined at line 16 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"_+_", "(a::is text, b::is text)",` _defined at line 19 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"_+_", "(a::is text, b::is text)",`
### txt::is text ! ### txt::is text !
Write a text event in the event buffer using this text. Write a text event in the event buffer using this text.
_defined at line 30 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"_!", "(txt::is text)",` _defined at line 33 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"_!", "(txt::is text)",`
### tag (txt::is text, tags::is struct) ### tag (txt::is text, tags::is struct)
Create and return a new text from `text`, with the tags from `tags` added. Create and return a new text from `text`, with the tags from `tags` added.
_defined at line 38 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"tag", "(txt::is text, tags::is struct)",` _defined at line 41 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"tag", "(txt::is text, tags::is struct)",`
### group text by tag (tag::is string)
Cause future text events to be each split into separate text event every time the value of the tag with the key `tag_key` changes.
For example, with the following Anselme script:
```
group text by tag("speaker")
speaker: "John" #
| A
| B
speaker: "Lana" #
| C
speaker: "John" #
| D
```
will produce three separate text events instead of one:
* the first with the texts "A" and "B"; both with the tag `speaker="John"`
* the second with the text "C"; with the tag `speaker="Lana"`
* the last with the text "D"; wiith the tag `speaker="John"`
This setting affect will affect the whole state.
_defined at line 67 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"group text by tag", "(tag::is string)",`
### write choice (text::is text, fn=attached block(keep return=true, default=($()()))) ### write choice (text::is text, fn=attached block(keep return=true, default=($()())))
@ -678,13 +702,13 @@ write choice(| Choice |, $42)
If we are currently resuming to an anchor contained in `fn`, `fn` is directly called and the current choice event buffer will be discarded on flush, simulating the choice event buffer being sent to the host game and this choice being selected. If we are currently resuming to an anchor contained in `fn`, `fn` is directly called and the current choice event buffer will be discarded on flush, simulating the choice event buffer being sent to the host game and this choice being selected.
_defined at line 57 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"write choice", "(text::is text, fn=attached block(keep return=true, default=($()())))",` _defined at line 91 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"write choice", "(text::is text, fn=attached block(keep return=true, default=($()())))",`
### original -> translated ### original -> translated
Add a translation so `original` is replaced with `translated`. Add a translation so `original` is replaced with `translated`.
_defined at line 74 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"_->_", "(original::is(\"quote\"), translated::is(\"quote\"))",` _defined at line 108 of [anselme/stdlib/text.lua](../anselme/stdlib/text.lua):_ `"_->_", "(original::is(\"quote\"), translated::is(\"quote\"))",`
# Symbols # Symbols
@ -1135,23 +1159,31 @@ If `search parent` is true, this will also search in parent scopes of the enviro
_defined at line 14 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"defined", "(env::is environment, var::is string, search parent::is boolean=false)",` _defined at line 14 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"defined", "(env::is environment, var::is string, search parent::is boolean=false)",`
### defined (var::is string, search parent::is boolean=true)
Returns true if the variable named `var` is defined in in the current scope, false otherwise.
If `search parent` is true, this will also search in parent scopes of the current scope.
_defined at line 27 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"defined", "(var::is string, search parent::is boolean=true)",`
### c::is environment . s::is string ### c::is environment . s::is string
Gets the variable named `s` defined in the environment `c`. Gets the variable named `s` defined in the environment `c`.
_defined at line 26 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"_._", "(c::is environment, s::is string)",` _defined at line 39 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"_._", "(c::is environment, s::is string)",`
### c::is environment . s::is string = v ### c::is environment . s::is string = v
Sets the variable named `s` defined in the environment `c` to `v`. Sets the variable named `s` defined in the environment `c` to `v`.
_defined at line 35 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"_._", "(c::is environment, s::is string) = v",` _defined at line 48 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"_._", "(c::is environment, s::is string) = v",`
### c::is environment . s::is symbol = v ### c::is environment . s::is symbol = v
Define a new variable `s` in the environment `c` with the value `v`. Define a new variable `s` in the environment `c` with the value `v`.
_defined at line 45 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"_._", "(c::is environment, s::is symbol) = v",` _defined at line 58 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"_._", "(c::is environment, s::is symbol) = v",`
### import (env::is environment, symbol tuple::is tuple) ### import (env::is environment, symbol tuple::is tuple)
@ -1164,7 +1196,7 @@ import(env, [:a, :b])
:b = env.b :b = env.b
``` ```
_defined at line 63 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"import", "(env::is environment, symbol tuple::is tuple)",` _defined at line 76 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"import", "(env::is environment, symbol tuple::is tuple)",`
### import (env::is environment, symbol::is symbol) ### import (env::is environment, symbol::is symbol)
@ -1176,14 +1208,14 @@ import(env, :a)
:a = env.a :a = env.a
``` ```
_defined at line 79 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"import", "(env::is environment, symbol::is symbol)",` _defined at line 92 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"import", "(env::is environment, symbol::is symbol)",`
### load (path::is string) ### load (path::is string)
Load an Anselme script from a file and run it. Load an Anselme script from a file and run it.
Returns the environment containing the exported variables from the file. Returns the environment containing the exported variables from the file.
_defined at line 89 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"load", "(path::is string)",` _defined at line 102 of [anselme/stdlib/environment.lua](../anselme/stdlib/environment.lua):_ `"load", "(path::is string)",`
# Typed values # Typed values
@ -1336,4 +1368,4 @@ _defined at line 14 of [anselme/stdlib/wrap.lua](../anselme/stdlib/wrap.lua):_ `
--- ---
_file generated at 2024-11-09T16:02:36Z_ _file generated at 2024-11-17T15:00:50Z_