share/Makefile.am: Reorganize blocks, comments
[libquvi-scripts.git] / share / lua / common / quvi / html.lua
blob5da94ed08d49e61162f06dfc23638ab4fc547528
1 -- libquvi-scripts
2 -- Copyright (C) 2011 Toni Gundogdu <legatvs@gmail.com>
3 --
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
5 --
6 -- This library is free software; you can redistribute it and/or
7 -- modify it under the terms of the GNU Lesser General Public
8 -- License as published by the Free Software Foundation; either
9 -- version 2.1 of the License, or (at your option) any later version.
11 -- This library is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 -- Lesser General Public License for more details.
16 -- You should have received a copy of the GNU Lesser General Public
17 -- License along with this library; if not, write to the Free Software
18 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 -- 02110-1301 USA
23 -- A modularized version of <http://www.hpelbers.org/lua/utf8>
25 -- convert numeric html entities to utf8
26 -- example: &#8364; -> €
29 local M = {}
30 local char = string.char
32 function M.tail(n, k)
33 local u, r=''
34 for i=1,k do
35 n,r = math.floor(n/0x40), n%0x40
36 u = char(r+0x80) .. u
37 end
38 return u,n
39 end
41 function M.to_utf8(a)
42 local n, r, u = tonumber(a)
43 if n<0x80 then -- 1 byte
44 return char(n)
45 elseif n<0x800 then -- 2 byte
46 u,n = M.tail(n, 1)
47 return char(n+0xc0) .. u
48 elseif n<0x10000 then -- 3 byte
49 u,n = M.tail(n, 2)
50 return char(n+0xe0) .. u
51 elseif n<0x200000 then -- 4 byte
52 u,n = M.tail(n, 3)
53 return char(n+0xf0) .. u
54 elseif n<0x4000000 then -- 5 byte
55 u,n = M.tail(n, 4)
56 return char(n+0xf8) .. u
57 else -- 6 byte
58 u,n = M.tail(n, 5)
59 return char(n+0xfc) .. u
60 end
61 end
63 --[[
64 for s in io.lines() do
65 local o = s:gsub('&#(%d+);', M.to_utf8)
66 print(o)
67 end
68 ]]--
70 return M
72 -- vim: set ts=2 sw=2 tw=72 expandtab: