feat(scraper): extract record from parameters

This commit is contained in:
Aire-One 2022-11-06 18:25:58 +01:00
parent aa42419a54
commit db49da6cfc
1 changed files with 50 additions and 31 deletions

View File

@ -1,6 +1,7 @@
local Function_Info = require "entity.Function_Info"
local List = require "pl.List"
local logger = require "logger"
local Map = require "pl.Map"
local Module_Doc = require "entity.Module_Doc"
local scan = require "web_sanitize.query.scan_html"
local scraper_utils = require "scraper.utils"
@ -32,47 +33,65 @@ local function extract_item_name(item_name_node: scan.HTMLNode): string
return item_name_node and ((item_name_node.attr.name as string):gsub("^.*[%.:]", ""))
end
local function extract_function_parameter_Parameters(tr_node: scan.HTMLNode): { Function_Info.Parameter }
local query_selectors = {
name = "span.parameter",
types = "span.types"
}
return scraper_utils.scrape_tuples(
tr_node:outer_html(),
{ query_selectors.name, query_selectors.types },
function(nodes: { string : scan.HTMLNode | nil }): Function_Info.Parameter
return {
name = extract_node_text(nodes[query_selectors.name] as scan.HTMLNode),
types = parse_parameter_types(extract_node_text(nodes[query_selectors.types] as scan.HTMLNode)),
}
end)
end
local function extract_function_parameters(function_parameters_node: scan.HTMLNode): { Function_Info.Parameter }
local current_record_parameter: string = nil
local current_record_parameter: Type_Info.Type_Info | nil = nil
return scraper_utils.scrape(
function_parameters_node:outer_html(),
"tr",
function(line_node: scan.HTMLNode): Function_Info.Parameter
if line_node.attr ~= nil and line_node.attr.class == "see_also_sublist" then
log:debug("subtable found, skipping")
local parameters = extract_function_parameter_Parameters(line_node)
if #parameters == 0 then
return nil
elseif #parameters ~= 1 then
log:error(logger.message_with_metadata("Expected 1 parameter by <tr> node",
{ len = #parameters, line_node = line_node, parameters = parameters }))
error("Expected 1 parameter by <tr> node")
end
local name, types = parameters[1].name, parameters[1].types
if line_node.attr ~= nil and line_node.attr.class == "see_also_sublist" and current_record_parameter then
local record_parameter = current_record_parameter as Type_Info.Type_Info
if not record_parameter.record_entries then
record_parameter.record_entries = Map()
end
(record_parameter.record_entries as Map<string, List<Type_Info.Type_Info>>):set(name, types)
return nil
end
local query_selectors = {
name = "span.parameter",
types = "span.types"
if #types == 1 and types[1].name == "table" then
local record_name = utils.capitalize(name)
current_record_parameter = Type_Info(record_name)
return {
name = name,
types = List({ current_record_parameter }),
}
end
return {
name = name,
types = types,
}
return scraper_utils.scrape_tuples(
line_node:outer_html(),
{ query_selectors.name, query_selectors.types },
function(nodes: { string : scan.HTMLNode | nil }): Function_Info.Parameter
local name = extract_node_text(nodes[query_selectors.name] as scan.HTMLNode)
local types = parse_parameter_types(extract_node_text(nodes[query_selectors.types] as scan.HTMLNode))
if #types == 1 and types[1].name == "table" then
local record_name = utils.capitalize(name)
current_record_parameter = record_name
return {
name = name,
types = List({ Type_Info(record_name) }),
}
end
return {
name = name,
types = types,
}
end
)[1]
end
)
end)
end
local function extract_function_return_types(function_return_types_node: scan.HTMLNode): { string }