From 69da1ff223f2553aefbe86ec86c2015014c21d55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Reuh=20Fildadut?= Date: Mon, 20 May 2024 23:43:07 +0200 Subject: [PATCH] [stdlib] add tag(text, tags) --- anselme/ast/Text.lua | 18 +++++++++++++++--- anselme/ast/Translatable.lua | 2 +- anselme/stdlib/text.lua | 6 ++++++ ideas.md | 8 -------- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/anselme/ast/Text.lua b/anselme/ast/Text.lua index 4599290..018a073 100644 --- a/anselme/ast/Text.lua +++ b/anselme/ast/Text.lua @@ -1,7 +1,7 @@ local class = require("anselme.lib.class") local ast = require("anselme.ast") local Event, Runtime = ast.abstract.Event, ast.abstract.Runtime -local ArgumentTuple +local ArgumentTuple, Struct local to_anselme = require("anselme.common.to_anselme") @@ -29,7 +29,8 @@ TextEventData = class { end, } -local Text = Runtime(Event) { +local Text +Text = Runtime(Event) { type = "text", list = nil, -- { { String, tag Struct }, ... } @@ -41,6 +42,17 @@ local Text = Runtime(Event) { table.insert(self.list, { str, tags }) end, + with_tags = function(self, tags) + local r = Text:new() + for _, e in ipairs(self.list) do + local t = Struct:new() + t:include(e[2]) + t:include(tags) + self:insert(e[1], t) + end + return r + end, + traverse = function(self, fn, ...) for _, e in ipairs(self.list) do fn(e[1], ...) @@ -73,6 +85,6 @@ local Text = Runtime(Event) { } package.loaded[...] = Text -ArgumentTuple = ast.ArgumentTuple +ArgumentTuple, Struct = ast.ArgumentTuple, ast.Struct return Text diff --git a/anselme/ast/Translatable.lua b/anselme/ast/Translatable.lua index add771e..ed635d0 100644 --- a/anselme/ast/Translatable.lua +++ b/anselme/ast/Translatable.lua @@ -17,7 +17,7 @@ local Translatable = ast.abstract.Node { self.context = Struct:new() self.context:set(String:new("source"), String:new(self.expression.source)) self.context:set(String:new("file"), String:new(self.expression.source:match("^([^%:]*)"))) - -- TODO: add parent script/function to context + -- TODO: add parent script/function name to context - should be more stable than source position end, _format = function(self, ...) diff --git a/anselme/stdlib/text.lua b/anselme/stdlib/text.lua index 001546a..f591e15 100644 --- a/anselme/stdlib/text.lua +++ b/anselme/stdlib/text.lua @@ -28,6 +28,12 @@ return { return Nil:new() end }, + { + "tag", "(txt::is text, tags::is struct)", + function(state, text, tags) + return text:with_tags(tags) + end + }, -- choice { diff --git a/ideas.md b/ideas.md index 401958c..09e1a9c 100644 --- a/ideas.md +++ b/ideas.md @@ -9,14 +9,6 @@ Documentation: * tutorial * standard library ---- - -Standard library. - -* Text and string manipulation would make sense, but that would require a full UTF-8/Unicode support library like https://github.com/starwing/luautf8. -> might favor bad practices for translation - - retag/add tags -* And in general, clean up everything. - # Can be done later Translation.