Mercurial > prosody-hg
comparison util/xtemplate.lua @ 13394:6debd8dd12ab
util.xtemplate: Adopt {-path-} syntax to strip preceding and/or trailing whitespace
Seen in some other template languages
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sat, 09 Dec 2023 14:57:41 +0100 |
| parents | d10957394a3c |
| children | 1f93e4f78c53 |
comparison
equal
deleted
inserted
replaced
| 13393:f72ca74de456 | 13394:6debd8dd12ab |
|---|---|
| 6 local st = require("prosody.util.stanza"); | 6 local st = require("prosody.util.stanza"); |
| 7 | 7 |
| 8 local function render(template, root, escape, filters) | 8 local function render(template, root, escape, filters) |
| 9 escape = escape or st.xml_escape; | 9 escape = escape or st.xml_escape; |
| 10 | 10 |
| 11 return (s_gsub(template, "%b{}", function(block) | 11 return (s_gsub(template, "(%s*)(%b{})(%s*)", function(pre_blank, block, post_blank) |
| 12 local inner = s_sub(block, 2, -2); | 12 local inner = s_sub(block, 2, -2); |
| 13 if inner:sub(1, 1) == "-" then | |
| 14 pre_blank = ""; | |
| 15 inner = inner:sub(2); | |
| 16 end | |
| 17 if inner:sub(-1, -1) == "-" then | |
| 18 post_blank = ""; | |
| 19 inner = inner:sub(1, -2); | |
| 20 end | |
| 13 local path, pipe, pos = s_match(inner, "^([^|]+)(|?)()"); | 21 local path, pipe, pos = s_match(inner, "^([^|]+)(|?)()"); |
| 14 if not (type(path) == "string") then return end | 22 if not (type(path) == "string") then return end |
| 15 local value | 23 local value |
| 16 if path == "." then | 24 if path == "." then |
| 17 value = root; | 25 value = root; |
| 72 pipe, pos = s_match(inner, "^(|?)()", p); | 80 pipe, pos = s_match(inner, "^(|?)()", p); |
| 73 end | 81 end |
| 74 | 82 |
| 75 if type(value) == "string" then | 83 if type(value) == "string" then |
| 76 if not is_escaped then value = escape(value); end | 84 if not is_escaped then value = escape(value); end |
| 77 return value | 85 return pre_blank .. value .. post_blank |
| 78 elseif st.is_stanza(value) then | 86 elseif st.is_stanza(value) then |
| 79 value = value:get_text(); | 87 value = value:get_text(); |
| 80 if value then return escape(value) end | 88 if value then return pre_blank .. escape(value) .. post_blank end |
| 81 end | 89 end |
| 82 return "" | 90 return pre_blank .. post_blank |
| 83 end)) | 91 end)) |
| 84 end | 92 end |
| 85 | 93 |
| 86 return { render = render } | 94 return { render = render } |
