feat: get element inner text (--text option)
							parent
							
								
									bf8adcd9b9
								
							
						
					
					
						commit
						d2b8adbf70
					
				
							
								
								
									
										75
									
								
								html.lua
								
								
								
								
							
							
						
						
									
										75
									
								
								html.lua
								
								
								
								
							| 
						 | 
					@ -39,6 +39,62 @@ local VOID_TAGS = {
 | 
				
			||||||
	wbr = true,
 | 
						wbr = true,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					local INLINE_TAGS = {
 | 
				
			||||||
 | 
						-- Text formatting
 | 
				
			||||||
 | 
						a = true,
 | 
				
			||||||
 | 
						abbr = true,
 | 
				
			||||||
 | 
						b = true,
 | 
				
			||||||
 | 
						bdi = true,
 | 
				
			||||||
 | 
						bdo = true,
 | 
				
			||||||
 | 
						cite = true,
 | 
				
			||||||
 | 
						code = true,
 | 
				
			||||||
 | 
						data = true,
 | 
				
			||||||
 | 
						dfn = true,
 | 
				
			||||||
 | 
						em = true,
 | 
				
			||||||
 | 
						i = true,
 | 
				
			||||||
 | 
						kbd = true,
 | 
				
			||||||
 | 
						mark = true,
 | 
				
			||||||
 | 
						q = true,
 | 
				
			||||||
 | 
						ruby = true,
 | 
				
			||||||
 | 
						s = true,
 | 
				
			||||||
 | 
						samp = true,
 | 
				
			||||||
 | 
						small = true,
 | 
				
			||||||
 | 
						span = true,
 | 
				
			||||||
 | 
						strong = true,
 | 
				
			||||||
 | 
						sub = true,
 | 
				
			||||||
 | 
						sup = true,
 | 
				
			||||||
 | 
						time = true,
 | 
				
			||||||
 | 
						u = true,
 | 
				
			||||||
 | 
						var = true,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						-- Interactive elements
 | 
				
			||||||
 | 
						button = true,
 | 
				
			||||||
 | 
						label = true,
 | 
				
			||||||
 | 
						select = true,
 | 
				
			||||||
 | 
						textarea = true,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						-- Media/content
 | 
				
			||||||
 | 
						img = true,
 | 
				
			||||||
 | 
						picture = true,
 | 
				
			||||||
 | 
						map = true,
 | 
				
			||||||
 | 
						object = true,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						-- Line break
 | 
				
			||||||
 | 
						br = true,
 | 
				
			||||||
 | 
						wbr = true,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						-- Forms
 | 
				
			||||||
 | 
						input = true,
 | 
				
			||||||
 | 
						output = true,
 | 
				
			||||||
 | 
						progress = true,
 | 
				
			||||||
 | 
						meter = true,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						-- Scripting
 | 
				
			||||||
 | 
						script = true,
 | 
				
			||||||
 | 
						noscript = true,
 | 
				
			||||||
 | 
						template = true,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function M.make_dom_element( tag_name, parent_elem )
 | 
					function M.make_dom_element( tag_name, parent_elem )
 | 
				
			||||||
	local o = {
 | 
						local o = {
 | 
				
			||||||
| 
						 | 
					@ -86,6 +142,23 @@ function M.make_dom_element( tag_name, parent_elem )
 | 
				
			||||||
			for _, child in ipairs(self.children or {}) do
 | 
								for _, child in ipairs(self.children or {}) do
 | 
				
			||||||
				child:foreach( fn )
 | 
									child:foreach( fn )
 | 
				
			||||||
			end
 | 
								end
 | 
				
			||||||
 | 
							end,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							inner_text = function(self)
 | 
				
			||||||
 | 
								if self.tag_name == ":text" then
 | 
				
			||||||
 | 
									return self.content
 | 
				
			||||||
 | 
								end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								local text = ""
 | 
				
			||||||
 | 
								for _, child in ipairs(self.children) do
 | 
				
			||||||
 | 
									text = text .. child:inner_text()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if not INLINE_TAGS[child.tag_name] then
 | 
				
			||||||
 | 
										text = text .. "\n"
 | 
				
			||||||
 | 
									end
 | 
				
			||||||
 | 
								end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								return text
 | 
				
			||||||
		end
 | 
							end
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -524,7 +597,7 @@ function M.clean_text_nodes(node)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	node.content = trim( node.content:gsub("%s+", " ") )
 | 
						node.content = node.content:gsub("%s+", " ")
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										15
									
								
								main.lua
								
								
								
								
							
							
						
						
									
										15
									
								
								main.lua
								
								
								
								
							| 
						 | 
					@ -33,16 +33,19 @@ end
 | 
				
			||||||
local FLAGS = {
 | 
					local FLAGS = {
 | 
				
			||||||
	FIRST_ONLY = {},
 | 
						FIRST_ONLY = {},
 | 
				
			||||||
	NO_PRINT_ERRORS = {},
 | 
						NO_PRINT_ERRORS = {},
 | 
				
			||||||
 | 
						INNER_TEXT = {},
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
local LONGHAND_FLAGS = {
 | 
					local LONGHAND_FLAGS = {
 | 
				
			||||||
	["first-only"] = FLAGS.FIRST_ONLY,
 | 
						["first-only"] = FLAGS.FIRST_ONLY,
 | 
				
			||||||
	["quiet"] = FLAGS.NO_PRINT_ERRORS
 | 
						["quiet"] = FLAGS.NO_PRINT_ERRORS,
 | 
				
			||||||
 | 
						["text"] = FLAGS.INNER_TEXT,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
local SHORTHAND_FLAGS = {
 | 
					local SHORTHAND_FLAGS = {
 | 
				
			||||||
	["1"] = FLAGS.FIRST_ONLY,
 | 
						["1"] = FLAGS.FIRST_ONLY,
 | 
				
			||||||
	["q"] = FLAGS.NO_PRINT_ERRORS,
 | 
						["q"] = FLAGS.NO_PRINT_ERRORS,
 | 
				
			||||||
 | 
						["t"] = FLAGS.INNER_TEXT,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -210,6 +213,12 @@ end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if flags[FLAGS.FIRST_ONLY] then
 | 
					if flags[FLAGS.FIRST_ONLY] then
 | 
				
			||||||
	if #elements > 0 then
 | 
						if #elements > 0 then
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if flags[FLAGS.INNER_TEXT] then
 | 
				
			||||||
 | 
								logger.print( elements[1]:inner_text() )
 | 
				
			||||||
 | 
								return 0
 | 
				
			||||||
 | 
							end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		logger.print( HTML.tostring( elements[1] ) )
 | 
							logger.print( HTML.tostring( elements[1] ) )
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -217,5 +226,9 @@ if flags[FLAGS.FIRST_ONLY] then
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
for _, el in ipairs(elements) do
 | 
					for _, el in ipairs(elements) do
 | 
				
			||||||
 | 
							if flags[FLAGS.INNER_TEXT] then
 | 
				
			||||||
 | 
								logger.print( el:inner_text() )
 | 
				
			||||||
 | 
							else
 | 
				
			||||||
			logger.print( HTML.tostring(el) )
 | 
								logger.print( HTML.tostring(el) )
 | 
				
			||||||
 | 
							end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue