webmcp
view framework/env/encode/mime/atom_token.lua @ 31:1cd9e69b85cb
add execute.load_chunk(args)
load_chunk loads the content of a lua file and returns the code.
It can construct the path name easily so you can put helper code in
seperate files for DRYness.
load_chunk loads the content of a lua file and returns the code.
It can construct the path name easily so you can put helper code in
seperate files for DRYness.
| author | Daniel Poelzleithner <poelzi@poelzi.org> | 
|---|---|
| date | Tue Oct 05 02:34:04 2010 +0200 (2010-10-05) | 
| parents | 9fdfb27f8e67 | 
| children | 
 line source
     1 function encode.mime.atom_token(str)
     2   local charset = "UTF-8"  -- TODO: support other charsets via locale system
     3   if
     4     string.find(str, "^[0-9A-Za-z!#%$%%&'%*%+%-/=%?%^_`{|}~]+$")
     5   then
     6     return str
     7   elseif
     8     string.find(str, "^[\t 0-9A-Za-z!#%$%%&'%*%+%-/=%?%^_`{|}~]+$")
     9   then
    10     return '"' .. str .. '"'
    11   elseif
    12     string.find(str, "^[\t -~]*$")
    13   then
    14     local parts = { '"' }
    15     for char in string.gmatch(str, ".") do
    16       if char == '"' or char == "\\" then
    17         parts[#parts+1] = "\\"
    18       end
    19       parts[#parts+1] = char
    20     end
    21     parts[#parts+1] = '"'
    22     return table.concat(parts)
    23   else
    24     local parts = { "=?", charset, "?Q?" }
    25     for char in string.gmatch(str, ".") do
    26       local byte = string.byte(char)
    27       if string.find(char, "^[0-9A-Za-z%.%-]$") then
    28         parts[#parts+1] = char
    29       else
    30         local byte = string.byte(char)
    31         if byte == 32 then
    32           parts[#parts+1] = "_"
    33         else
    34           parts[#parts+1] = string.format("=%02X", byte)
    35         end
    36       end
    37     end
    38     parts[#parts+1] = "?="
    39     return table.concat(parts)
    40   end
    41 end
