diff 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
line wrap: on
line diff
--- a/core/src/luan/modules/Html.luan	Thu Jan 07 18:46:07 2016 -0700
+++ b/core/src/luan/modules/Html.luan	Tue Jan 12 23:52:56 2016 -0700
@@ -1,48 +1,76 @@
 java()
 local HtmlLuan = require "java:luan.modules.HtmlLuan"
+local HtmlParser = require "java:luan.modules.parsers.Html"
+local URLEncoder = require "java:java.net.URLEncoder"
+local Luan = require "luan:Luan"
+local error = Luan.error
+local ipairs = Luan.ipairs or error()
+local pairs = Luan.pairs or error()
+local type = Luan.type or error()
+local Io = require "luan:Io"
+local output_of = Io.output_of or error()
+
 
 local M = {}
 
 M.encode = HtmlLuan.encode
-M.parse = HtmlLuan.parse
-M.to_string = HtmlLuan.to_string
 
-
+local quote = HtmlLuan.quote
+M.quote = quote
 
--- extras
-
-local Luan = require "luan:Luan"
-local ipairs = Luan.ipairs
-local type = Luan.type
-local Io = require "luan:Io"
-local URLEncoder = require "java:java.net.URLEncoder"
+function M.parse(text,container_tags)
+	text or error "text required"
+	container_tags = container_tags or {"script","style","textarea"}
+	return HtmlParser.toList(text,container_tags)
+end
 
 function M.url_encode(s)
 	return URLEncoder.encode(s,"UTF-8")
 end
 
-function M.process_url_tags(html)
-	for i, v in ipairs(html) do
-		if type(v) == "table" and v.type == "tag" then
-			if v.name == "url" then
-				local url = v.attributes.url or html[i+1]
-				v.name = "a"
-				v.attributes.url = nil
-				v.attributes.href = url
-			elseif v.name == "/url" then
-				v.name = "/a"
-			end
+local function output_tag(tag)
+	%><<%= tag.name %><%
+	local attributes = tag.attributes
+	for name, value in pairs(attributes) do
+		%> <%= name %><%
+		if value ~= true then
+			%>=<%= quote(value) %><%
 		end
 	end
+	if tag.is_empty then
+		%>/<%
+	end
+	%>><%
 end
 
-function M.add_nofollow(html)
-	for i, v in ipairs(html) do
-		if type(v) == "table" and v.type == "tag" and v.name == "a" then
-			v.attributes.rel = "nofollow"
+function M.to_string(list)
+	return output_of( function()
+		for _, obj in ipairs(list) do
+			local tp = type(obj)
+			if tp == "string" then
+				%><%= obj %><%
+			elseif tp == "table" then
+				tp = obj.type
+				if tp == nil then
+					error "no type in element of table for 'Html.to_string'"
+				elseif tp == "comment" then
+					%><!--<%= obj.text %>--><%
+				elseif tp == "cdata" then
+					%><![CDATA[<%= obj.text %>]]><%
+				elseif tp == "tag" then
+					output_tag(obj)
+				elseif tp == "container" then
+					local tag = obj.tag
+					output_tag(tag)
+					%><%= obj.text %></<%= tag.name %>><%
+				else
+					error "invalid element type for 'Html.to_string'"
+				end
+			else
+				error("invalid value ("..tp..") in list for 'Html.to_string'")
+			end
 		end
-	end
+	end )
 end
 
-
 return M