Mercurial Hosting > luan
comparison core/src/luan/modules/Html.luan @ 625:a3c1e11fb6aa
rewrite much of Html to be more understandable;
add Lucene html_highlighter();
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 12 Jan 2016 23:52:56 -0700 |
parents | 195a64f948f2 |
children | ca169567ce07 |
comparison
equal
deleted
inserted
replaced
624:8281a248c47e | 625:a3c1e11fb6aa |
---|---|
1 java() | 1 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" | |
5 local Luan = require "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" | |
11 local output_of = Io.output_of or error() | |
12 | |
3 | 13 |
4 local M = {} | 14 local M = {} |
5 | 15 |
6 M.encode = HtmlLuan.encode | 16 M.encode = HtmlLuan.encode |
7 M.parse = HtmlLuan.parse | |
8 M.to_string = HtmlLuan.to_string | |
9 | 17 |
18 local quote = HtmlLuan.quote | |
19 M.quote = quote | |
10 | 20 |
11 | 21 function M.parse(text,container_tags) |
12 -- extras | 22 text or error "text required" |
13 | 23 container_tags = container_tags or {"script","style","textarea"} |
14 local Luan = require "luan:Luan" | 24 return HtmlParser.toList(text,container_tags) |
15 local ipairs = Luan.ipairs | 25 end |
16 local type = Luan.type | |
17 local Io = require "luan:Io" | |
18 local URLEncoder = require "java:java.net.URLEncoder" | |
19 | 26 |
20 function M.url_encode(s) | 27 function M.url_encode(s) |
21 return URLEncoder.encode(s,"UTF-8") | 28 return URLEncoder.encode(s,"UTF-8") |
22 end | 29 end |
23 | 30 |
24 function M.process_url_tags(html) | 31 local function output_tag(tag) |
25 for i, v in ipairs(html) do | 32 %><<%= tag.name %><% |
26 if type(v) == "table" and v.type == "tag" then | 33 local attributes = tag.attributes |
27 if v.name == "url" then | 34 for name, value in pairs(attributes) do |
28 local url = v.attributes.url or html[i+1] | 35 %> <%= name %><% |
29 v.name = "a" | 36 if value ~= true then |
30 v.attributes.url = nil | 37 %>=<%= quote(value) %><% |
31 v.attributes.href = url | 38 end |
32 elseif v.name == "/url" then | 39 end |
33 v.name = "/a" | 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'") | |
34 end | 71 end |
35 end | 72 end |
36 end | 73 end ) |
37 end | 74 end |
38 | 75 |
39 function M.add_nofollow(html) | |
40 for i, v in ipairs(html) do | |
41 if type(v) == "table" and v.type == "tag" and v.name == "a" then | |
42 v.attributes.rel = "nofollow" | |
43 end | |
44 end | |
45 end | |
46 | |
47 | |
48 return M | 76 return M |