From 81961177e7b09de83a39710ca44b12bd9e2767c2 Mon Sep 17 00:00:00 2001 From: Aire-One Date: Sun, 30 Oct 2022 16:35:12 +0100 Subject: [PATCH] feat(scraper): implement function parameters --- src/awesomewm.d.tl/entity/Function_Info.tl | 6 ++-- src/awesomewm.d.tl/generator/snippets.tl | 2 +- src/awesomewm.d.tl/scraper/module_doc.tl | 32 +++++++++++++++++++++- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/awesomewm.d.tl/entity/Function_Info.tl b/src/awesomewm.d.tl/entity/Function_Info.tl index 4b9c1e1..f856539 100644 --- a/src/awesomewm.d.tl/entity/Function_Info.tl +++ b/src/awesomewm.d.tl/entity/Function_Info.tl @@ -6,7 +6,7 @@ local record Function_Info Function_Info: Function_Info record Parameter name: string - type: string + types: List end name: string @@ -27,10 +27,10 @@ local __Function_Info: metatable = { end, } -function Function_Info:append_parameter(name: string, type: string) +function Function_Info:append_parameter(name: string, types: List) self.parameters:append { name = name, - type = type, + types = types, } end diff --git a/src/awesomewm.d.tl/generator/snippets.tl b/src/awesomewm.d.tl/generator/snippets.tl index e3871ae..f284c09 100644 --- a/src/awesomewm.d.tl/generator/snippets.tl +++ b/src/awesomewm.d.tl/generator/snippets.tl @@ -30,7 +30,7 @@ function snippets.render_anonymous_function_signature(item: Function_Info.Functi local tmpl_args = { function_name = item.name, function_parameter = item.parameters:map(function(param: Function_Info.Parameter): string - return snippets.render_typed_variable(param.name, List({param.type})) -- TODO : add support for multiple types + return snippets.render_typed_variable(param.name, param.types) end):concat(", "), function_return = item.return_types:concat(", "), } diff --git a/src/awesomewm.d.tl/scraper/module_doc.tl b/src/awesomewm.d.tl/scraper/module_doc.tl index 495ba58..5d93513 100644 --- a/src/awesomewm.d.tl/scraper/module_doc.tl +++ b/src/awesomewm.d.tl/scraper/module_doc.tl @@ -4,6 +4,7 @@ local logger = require "logger" local Module_Doc = require "entity.Module_Doc" local scan = require "web_sanitize.query.scan_html" local scraper_utils = require "scraper.utils" +local stringx = require "pl.stringx" local utils = require "utils" local log = logger.log("scraper") @@ -12,10 +13,36 @@ local function extract_node_text(node: scan.HTMLNode): string return utils.sanitize_string(node:inner_text()) end +local function parse_parameter_types(parameter_type: string): List + if parameter_type == "" then + return List({ "any" }) + end + + return stringx.split(parameter_type, " or "):map(utils.sanitize_string) +end + local function extract_function_name(function_name_node: scan.HTMLNode): string return function_name_node and ((function_name_node.attr.name as string):gsub(".*:", "")) end +local function extract_function_parameters(function_parameters_node: scan.HTMLNode): { Function_Info.Parameter } + local query_selectors = { + name = "span.parameter", + types = "span.types" + } + + return scraper_utils.scrape_tuples( + function_parameters_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]), + types = parse_parameter_types(extract_node_text(nodes[query_selectors.types])), + } + end + ) +end + local function extract_function_return_types(function_return_types_node: scan.HTMLNode): { string } if not function_return_types_node then return {} @@ -30,17 +57,20 @@ end local function extract_section_functions(dl: string): { Function_Info.Function_Info } local query_selectors = { function_name = "dt a", + function_parameters = "dd table", function_return_type = "dd ol", } return scraper_utils.scrape_tuples( dl, - { query_selectors.function_name, query_selectors.function_return_type }, + { query_selectors.function_name, query_selectors.function_parameters, query_selectors.function_return_type }, function(nodes: { string : scan.HTMLNode | nil }): Function_Info.Function_Info local function_info = Function_Info() function_info.name = extract_function_name(nodes[query_selectors.function_name]) + function_info.parameters = + List(extract_function_parameters(nodes[query_selectors.function_parameters])) function_info.return_types = List( extract_function_return_types( nodes[query_selectors.function_return_type]