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 Function_Info = require "entity.Function_Info"
local List = require "pl.List" local List = require "pl.List"
local logger = require "logger" local logger = require "logger"
local Map = require "pl.Map"
local Module_Doc = require "entity.Module_Doc" local Module_Doc = require "entity.Module_Doc"
local scan = require "web_sanitize.query.scan_html" local scan = require "web_sanitize.query.scan_html"
local scraper_utils = require "scraper.utils" local scraper_utils = require "scraper.utils"
@ -32,36 +33,57 @@ local function extract_item_name(item_name_node: scan.HTMLNode): string
return item_name_node and ((item_name_node.attr.name as string):gsub("^.*[%.:]", "")) return item_name_node and ((item_name_node.attr.name as string):gsub("^.*[%.:]", ""))
end end
local function extract_function_parameters(function_parameters_node: scan.HTMLNode): { Function_Info.Parameter } local function extract_function_parameter_Parameters(tr_node: scan.HTMLNode): { Function_Info.Parameter }
local current_record_parameter: string = 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")
return nil
end
local query_selectors = { local query_selectors = {
name = "span.parameter", name = "span.parameter",
types = "span.types" types = "span.types"
} }
return scraper_utils.scrape_tuples( return scraper_utils.scrape_tuples(
line_node:outer_html(), tr_node:outer_html(),
{ query_selectors.name, query_selectors.types }, { query_selectors.name, query_selectors.types },
function(nodes: { string : scan.HTMLNode | nil }): Function_Info.Parameter function(nodes: { string : scan.HTMLNode | nil }): Function_Info.Parameter
local name = extract_node_text(nodes[query_selectors.name] as scan.HTMLNode) return {
local types = parse_parameter_types(extract_node_text(nodes[query_selectors.types] as scan.HTMLNode)) 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: Type_Info.Type_Info | nil = nil
return scraper_utils.scrape(
function_parameters_node:outer_html(),
"tr",
function(line_node: scan.HTMLNode): Function_Info.Parameter
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
if #types == 1 and types[1].name == "table" then if #types == 1 and types[1].name == "table" then
local record_name = utils.capitalize(name) local record_name = utils.capitalize(name)
current_record_parameter = record_name current_record_parameter = Type_Info(record_name)
return { return {
name = name, name = name,
types = List({ Type_Info(record_name) }), types = List({ current_record_parameter }),
} }
end end
@ -69,10 +91,7 @@ local function extract_function_parameters(function_parameters_node: scan.HTMLNo
name = name, name = name,
types = types, types = types,
} }
end end)
)[1]
end
)
end end
local function extract_function_return_types(function_return_types_node: scan.HTMLNode): { string } local function extract_function_return_types(function_return_types_node: scan.HTMLNode): { string }