Skip to content

Commit b3ff06c

Browse files
committed
Rehash dedent() function in Message type class
1 parent 016dcda commit b3ff06c

File tree

1 file changed

+43
-39
lines changed

1 file changed

+43
-39
lines changed

fluent/resource.lua

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ local tablex = require("pl.tablex")
44

55
local node_types = {}
66
local node_to_type
7-
local dedent
87

98
local FluentNode = class({
109
discardable = false,
@@ -33,7 +32,13 @@ local FluentNode = class({
3332
self.value = node
3433
else
3534
if not self.elements then self.elements = {} end
36-
table.insert(self.elements, node)
35+
if #self.elements >= 1 then
36+
if not self.elements[#self.elements]:append(node) then
37+
table.insert(self.elements, node)
38+
end
39+
else
40+
table.insert(self.elements, node)
41+
end
3742
end
3843
end,
3944

@@ -100,36 +105,54 @@ node_types.Pattern = class({
100105
_base = FluentNode,
101106
_init = function (self, node)
102107
self:super(node)
103-
-- TODO: merge sequential mergables in elements
104-
-- TODO: move dedent to here after merge?
108+
-- D(self.elements)
109+
self:dedent()
110+
end,
111+
dedent = function (self)
112+
local mindent = function(node)
113+
local indents = {}
114+
if type(node.value) == "string" then
115+
for indent in string.gmatch(node.value, "\n *%S") do
116+
table.insert(indents, #indent-2)
117+
end
118+
end
119+
return tablex.reduce(math.min, indents) or 0
120+
end
121+
local striplen = tablex.reduce(math.min, tablex.imap(mindent, self.elements)) or 0
122+
local i, strippref = 0, "\n"
123+
while i < striplen do
124+
strippref = strippref .. " "
125+
i = i + 1
126+
end
127+
local strip = function(node, key, len)
128+
if type(node.value) == "string" then
129+
local value = string.gsub(node.value, "\r\n", "\n")
130+
if len >= 1 then
131+
self.elements[key].value = string.gsub(value, strippref, "\n")
132+
end
133+
end
134+
end
135+
tablex.foreachi(self.elements, strip, striplen)
105136
end,
106137
format = function (self, parameters)
107-
local value = #self.elements >= 2 and dedent() or self.elements[1].value
108-
-- Todo parse elements and actually format a value
138+
local value = #self.elements >= 2 and "TODO" or self.elements[1].value
139+
-- TODO: parse elements and actually format a value
109140
return value, parameters
110141
end
111142
})
112-
-- local lasttype = "none"
113-
-- for key, value in ipairs(stuff) do
114-
-- if lasttype == value.id then
115-
-- ast.elements[#ast.elements] = ast.elements[#ast.elements].value .. value.value
116-
-- else
117-
-- table.insert(ast.elements, self(value))
118-
-- lasttype = value.id
119-
-- end
120-
-- end
121-
-- for key, value in ipairs(ast.elements) do
122-
-- if key == "value" then
123-
-- ast.elements[key] = dedent(value)
124-
-- end
125-
-- end
126143

127144
node_types.TextElement = class({
128145
appendable = true,
129146
_base = FluentNode,
130147
_init = function (self, node)
131148
node.id = "TextElement"
132149
self:super(node)
150+
end,
151+
__add = function (self, node)
152+
if self:is_a(node:is_a()) and self.appendable and node.appendable then
153+
self.value = (self.value or "") .. "\n" .. (node.value or "")
154+
return self
155+
end
133156
end
134157
})
135158

@@ -235,25 +258,6 @@ node_to_type = function (node)
235258
end
236259
end
237260

238-
dedent = function (content)
239-
local min
240-
for indent in string.gmatch(content, "\n *%S") do
241-
min = min and math.min(min, #indent) or #indent
242-
end
243-
local common = function(shortest)
244-
local i = 0
245-
local s = ""
246-
while i < shortest do
247-
s = s .. " "
248-
i = i + 1
249-
end
250-
return s
251-
end
252-
local sp = common(min-2)
253-
local rep = string.gsub(content, "\n"..sp, "\n")
254-
return rep
255-
end
256-
257261
local FluentResource = class({
258262
type = "Resource",
259263
index = {},

0 commit comments

Comments
 (0)