Mercurial Hosting > luan
comparison src/luan/modules/Html.luan @ 775:1a68fc55a80c
simplify dir structure
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 26 Aug 2016 14:36:40 -0600 |
parents | core/src/luan/modules/Html.luan@ca169567ce07 |
children | bae2d0c2576c |
comparison
equal
deleted
inserted
replaced
774:3e30cf310e56 | 775:1a68fc55a80c |
---|---|
1 java() | |
2 local HtmlLuan = require "java:luan.modules.HtmlLuan" | |
3 local HtmlParser = require "java:luan.modules.parsers.Html" | |
4 local URLEncoder = require "java:java.net.URLEncoder" | |
5 local Luan = require "luan:Luan.luan" | |
6 local error = Luan.error | |
7 local ipairs = Luan.ipairs or error() | |
8 local pairs = Luan.pairs or error() | |
9 local type = Luan.type or error() | |
10 local Io = require "luan:Io.luan" | |
11 local output_of = Io.output_of or error() | |
12 | |
13 | |
14 local M = {} | |
15 | |
16 M.encode = HtmlLuan.encode | |
17 | |
18 local quote = HtmlLuan.quote | |
19 M.quote = quote | |
20 | |
21 function M.parse(text,container_tags) | |
22 text or error "text required" | |
23 container_tags = container_tags or {"script","style","textarea"} | |
24 return HtmlParser.toList(text,container_tags) | |
25 end | |
26 | |
27 function M.url_encode(s) | |
28 return URLEncoder.encode(s,"UTF-8") | |
29 end | |
30 | |
31 local function output_tag(tag) | |
32 %><<%= tag.name %><% | |
33 local attributes = tag.attributes | |
34 for name, value in pairs(attributes) do | |
35 %> <%= name %><% | |
36 if value ~= true then | |
37 %>=<%= quote(value) %><% | |
38 end | |
39 end | |
40 if tag.is_empty then | |
41 %>/<% | |
42 end | |
43 %>><% | |
44 end | |
45 | |
46 function M.to_string(list) | |
47 return output_of( function() | |
48 for _, obj in ipairs(list) do | |
49 local tp = type(obj) | |
50 if tp == "string" then | |
51 %><%= obj %><% | |
52 elseif tp == "table" then | |
53 tp = obj.type | |
54 if tp == nil then | |
55 error "no type in element of table for 'Html.to_string'" | |
56 elseif tp == "comment" then | |
57 %><!--<%= obj.text %>--><% | |
58 elseif tp == "cdata" then | |
59 %><![CDATA[<%= obj.text %>]]><% | |
60 elseif tp == "tag" then | |
61 output_tag(obj) | |
62 elseif tp == "container" then | |
63 local tag = obj.tag | |
64 output_tag(tag) | |
65 %><%= obj.text %></<%= tag.name %>><% | |
66 else | |
67 error "invalid element type for 'Html.to_string'" | |
68 end | |
69 else | |
70 error("invalid value ("..tp..") in list for 'Html.to_string'") | |
71 end | |
72 end | |
73 end ) | |
74 end | |
75 | |
76 return M |