You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lua_ical.lua 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. --[[
  2. Copyright (c) 2019, Vsevolod Stakhov <vsevolod@highsecure.ru>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ]]--
  13. local l = require 'lpeg'
  14. local wsp = l.P" "
  15. local crlf = l.P"\r"^-1 * l.P"\n"
  16. local eol = (crlf * #crlf) + (crlf - (crlf^-1 * wsp))
  17. local name = l.C((l.P(1) - (l.P":"))^1) / function(v) return (v:gsub("[\n\r]+%s","")) end
  18. local value = l.C((l.P(1) - eol)^0) / function(v) return (v:gsub("[\n\r]+%s","")) end
  19. local elt = name * ":" * wsp^0 * value * eol
  20. local exports = {}
  21. local function ical_txt_values(input)
  22. local control={n='\n', r='\r'}
  23. local escaper = l.Ct((elt / function(_,b) return (b:gsub("\\(.)", control)) end)^1)
  24. local values = escaper:match(input)
  25. if not values then
  26. return nil
  27. end
  28. return table.concat(values, "\n")
  29. end
  30. --[[[
  31. -- @function lua_ical.ical_txt_values(input)
  32. -- Returns all values from ical as a plain text. Names are completely ignored.
  33. --]]
  34. exports.ical_txt_values = ical_txt_values
  35. return exports