Mercurial Hosting > luan
comparison src/luan/modules/Html.luan @ 1712:36c28be6d432
improve html and bbcode
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 14 Jul 2022 22:14:21 -0600 |
parents | af45ed10aff6 |
children | f8eba0442956 |
comparison
equal
deleted
inserted
replaced
1711:05d14db623b6 | 1712:36c28be6d432 |
---|---|
1 require "java" | 1 require "java" |
2 local HtmlLuan = require "java:luan.modules.HtmlLuan" | 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" | 3 local URLEncoder = require "java:java.net.URLEncoder" |
5 local URLDecoder = require "java:java.net.URLDecoder" | 4 local URLDecoder = require "java:java.net.URLDecoder" |
6 local JsonToString = require "java:goodjava.json.JsonToString" | 5 local JsonToString = require "java:goodjava.json.JsonToString" |
7 local Luan = require "luan:Luan.luan" | 6 local Luan = require "luan:Luan.luan" |
8 local error = Luan.error | 7 local error = Luan.error |
14 | 13 |
15 | 14 |
16 local Html = {} | 15 local Html = {} |
17 | 16 |
18 Html.decode = HtmlLuan.decode | 17 Html.decode = HtmlLuan.decode |
19 Html.encode = HtmlLuan.encode | 18 local encode = HtmlLuan.encode |
19 Html.encode = encode | |
20 Html.javascript_encode = JsonToString.javascriptEncode | 20 Html.javascript_encode = JsonToString.javascriptEncode |
21 | |
22 local quote = HtmlLuan.quote | |
23 Html.quote = quote | |
24 | 21 |
25 function Html.parse(text,container_tags) | 22 function Html.parse(text,container_tags) |
26 text or error "text required" | 23 text or error "text required" |
27 container_tags = container_tags or {"script","style","textarea"} | 24 container_tags = container_tags or {"script","style","textarea"} |
28 return HtmlParser.toList(text,container_tags) | 25 return HtmlLuan.parse(text,container_tags) |
29 end | 26 end |
30 | 27 |
31 function Html.url_encode(s) | 28 function Html.url_encode(s) |
32 return URLEncoder.encode(s,"UTF-8") | 29 return URLEncoder.encode(s,"UTF-8") |
33 end | 30 end |
39 local function output_tag(tag) | 36 local function output_tag(tag) |
40 %><<%= tag.name %><% | 37 %><<%= tag.name %><% |
41 for name, value in pairs(tag.attributes) do | 38 for name, value in pairs(tag.attributes) do |
42 %> <%= name %><% | 39 %> <%= name %><% |
43 if value ~= true then | 40 if value ~= true then |
44 %>=<%= quote(value) %><% | 41 %>="<%= encode(value) %>"<% |
45 end | 42 end |
46 end | 43 end |
47 if tag.is_empty then | 44 if tag.is_empty then |
48 %>/<% | 45 %>/<% |
49 end | 46 end |
53 function Html.to_string(list) | 50 function Html.to_string(list) |
54 return output_of( function() | 51 return output_of( function() |
55 for _, obj in ipairs(list) do | 52 for _, obj in ipairs(list) do |
56 local tp = type(obj) | 53 local tp = type(obj) |
57 if tp == "string" then | 54 if tp == "string" then |
58 %><%= obj %><% | 55 %><%= encode(obj) %><% |
59 elseif tp == "table" then | 56 elseif tp == "table" then |
60 tp = obj.type | 57 tp = obj.type |
61 if tp == nil then | 58 if tp == nil then |
62 error "no type in element of table for 'Html.to_string'" | 59 error "no type in element of table for 'Html.to_string'" |
63 elseif tp == "comment" then | 60 elseif tp == "comment" then |