diff --git a/src/awesomewm.d.tl/entity/Variable_Info.tl b/src/awesomewm.d.tl/entity/Variable_Info.tl index 22855fd..f376458 100644 --- a/src/awesomewm.d.tl/entity/Variable_Info.tl +++ b/src/awesomewm.d.tl/entity/Variable_Info.tl @@ -7,6 +7,8 @@ local record Variable_Info name: string types: List + + constraints: List end local __Variable_Info: metatable = { diff --git a/src/awesomewm.d.tl/generator/snippets.tl b/src/awesomewm.d.tl/generator/snippets.tl index c82b815..510a945 100644 --- a/src/awesomewm.d.tl/generator/snippets.tl +++ b/src/awesomewm.d.tl/generator/snippets.tl @@ -45,12 +45,6 @@ function snippets.render_record_functions(items: List): string - return items:map(function(item: Variable_Info.Variable_Info): string - return snippets.render_typed_variable(item.name, item.types) - end):concat("\n") -end - function snippets.render_enum(name: string, values: List): string local tmpl = [[ enum $(name) @@ -69,4 +63,18 @@ end return utils.do_or_fail(template.substitute, tmpl, tmpl_args) end +function snippets.render_record_properties(items: List): string + return items:map(function(item: Variable_Info.Variable_Info): string + if not item.constraints or #item.constraints == 0 then + return snippets.render_typed_variable(item.name, item.types) + end + + local enum_type = utils.capitalize(item.name) + return string.format( + "%s%s", + snippets.render_enum(enum_type, item.constraints), + snippets.render_typed_variable(item.name, List({ enum_type }))) + end):concat("\n") +end + return snippets diff --git a/src/awesomewm.d.tl/scraper/module_doc.tl b/src/awesomewm.d.tl/scraper/module_doc.tl index 738ce2d..74b8905 100644 --- a/src/awesomewm.d.tl/scraper/module_doc.tl +++ b/src/awesomewm.d.tl/scraper/module_doc.tl @@ -55,18 +55,12 @@ local function extract_function_return_types(function_return_types_node: scan.HT return scraper_utils.scrape(html, selector, extract_node_text) end -local function extract_property_types(property_summary_type: scan.HTMLNode, property_constraint_node: scan.HTMLNode): List - -- local constraint_types = scraper_utils.scrape( - -- property_constraint_node:outer_html(), - -- "tr.see_also_sublist", - -- extract_node_text - -- ) - - -- if #constraint_types == 0 then - return parse_parameter_types(extract_node_text(property_summary_type)) - -- end - - -- return constraint_types +local function extract_property_constraints(property_constraint_node: scan.HTMLNode): { string } + return scraper_utils.scrape( + property_constraint_node:outer_html(), + "tr.see_also_sublist", + extract_node_text + ) end local function extract_section_functions(dl: string): { Function_Info.Function_Info } @@ -111,9 +105,15 @@ local function extract_section_variables(dl: string): { Variable_Info.Variable_I local variable_info = Variable_Info() variable_info.name = extract_item_name(nodes[query_selectors.variable_name]) - variable_info.types = extract_property_types( - nodes[query_selectors.variable_summary_type], - nodes[query_selectors.variable_property_constraint]) + variable_info.types = parse_parameter_types(extract_node_text(nodes[query_selectors.variable_summary_type])) + + if variable_info.types:contains("string") then + variable_info.constraints = List(extract_property_constraints(nodes[query_selectors.variable_property_constraint])):map( + function(constraint: string): string + return (constraint:gsub(""", "")) + end + ) + end return variable_info end @@ -139,7 +139,7 @@ function module.get_doc_from_page(html: string, module_name: string): Module_Doc end local module_doc = Module_Doc() - module_doc.record_name = module_name:gsub(".*%.", ""):gsub("^%l", string.upper) + module_doc.record_name = utils.capitalize((module_name:gsub(".*%.", ""))) for i = 1, #nodes:get("h2.section-header") do local h2 = nodes:get("h2.section-header")[i] diff --git a/src/awesomewm.d.tl/utils.tl b/src/awesomewm.d.tl/utils.tl index 32cfe5e..f4debbb 100644 --- a/src/awesomewm.d.tl/utils.tl +++ b/src/awesomewm.d.tl/utils.tl @@ -39,6 +39,10 @@ function utils.sanitize_string(s: string): string return (stringx.strip(web_sanitize.extract_text(s))) end +function utils.capitalize(s: string): string + return (s:gsub("^%l", string.upper)) +end + -- At some point, we should probably write a wrapper to make penlight's function work with pcalls. function utils.do_or_fail(func: function(...: any): (T | nil, string), ...: any): T local logger = require "logger"