Implement module section types "Signals" (#18) #23

Merged
Aire-One merged 5 commits from feat/#18 into master 2022-10-23 21:39:59 +02:00
6 changed files with 49 additions and 26 deletions

View File

@ -9,6 +9,7 @@ local record Module_Doc
constructors: List<Function_Info.Function_Info> constructors: List<Function_Info.Function_Info>
methods: List<Function_Info.Function_Info> methods: List<Function_Info.Function_Info>
static_functions: List<Function_Info.Function_Info> static_functions: List<Function_Info.Function_Info>
signals: List<string>
end end
local __Module_Doc: metatable<Module_Doc> = { local __Module_Doc: metatable<Module_Doc> = {

View File

@ -1,17 +1,14 @@
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 utils = require "utils" local stringx = require "pl.stringx"
local template = require "pl.template" local template = require "pl.template"
local utils = require "utils"
local snippets = {} local snippets = {}
function snippets.indent(str: string, level: number): string function snippets.indent(str: string, level: number): string
level = level or 1 level = level or 1
local ret = "" return stringx.rstrip(stringx.indent(str, level, string.rep(" ", 3)))
for line in str:gmatch("[^\n]+") do
ret = ret .. string.rep(" ", level * 3) .. line .. "\n"
end
return ret
end end
function snippets.render_typed_variable(name: string, types: List<string>): string function snippets.render_typed_variable(name: string, types: List<string>): string
@ -47,4 +44,22 @@ function snippets.render_record_functions(items: List<Function_Info.Function_Inf
end):concat("\n") end):concat("\n")
end end
function snippets.render_enum(name: string, values: List<string>): string
local tmpl = [[
enum $(name)
$(indent(body))
end
]]
local tmpl_args = {
name = name,
body = values:map(function(value: string): string
return string.format('"%s"', value)
end):concat("\n"),
indent = snippets.indent,
}
return utils.do_or_fail(template.substitute, tmpl, tmpl_args)
end
return snippets return snippets

View File

@ -8,15 +8,11 @@ local snippets = require "generator.snippets"
local tmpl = [[ local tmpl = [[
-- Auto generated file (Do not manually edit this file!) -- Auto generated file (Do not manually edit this file!)
# if module.signals then local record $(mod_name)
local enum signals # if #module.signals then
# for _, signal in ipairs(module.signals) do $(snippets.indent(snippets.render_enum("Signal", module.signals)))
"$(signal.name)"
# end
end
# end -- /signals # end -- /signals
local record $(mod_name)
# if #module.methods then # if #module.methods then
-- Object methods -- Object methods
$(snippets.indent(snippets.render_record_functions(module.methods))) $(snippets.indent(snippets.render_record_functions(module.methods)))

View File

@ -8,6 +8,10 @@ local utils = require "utils"
local log = logger.log("scraper") local log = logger.log("scraper")
local function extract_node_text(node: scan.HTMLNode): string
return utils.sanitize_string(node:inner_text())
end
local function extract_function_name(function_name_node: scan.HTMLNode): string local function extract_function_name(function_name_node: scan.HTMLNode): string
return function_name_node and ((function_name_node.attr.name as string):gsub(".*:", "")) return function_name_node and ((function_name_node.attr.name as string):gsub(".*:", ""))
end end
@ -20,9 +24,7 @@ local function extract_function_return_types(function_return_types_node: scan.HT
local selector = "span.types .type" local selector = "span.types .type"
local html = function_return_types_node:outer_html() local html = function_return_types_node:outer_html()
return scraper_utils.scrape(html, selector, function(node: scan.HTMLNode): string return scraper_utils.scrape(html, selector, extract_node_text)
return utils.sanitize_string(node:inner_text())
end)
end end
local function extract_section_functions(dl: string): { Function_Info.Function_Info } local function extract_section_functions(dl: string): { Function_Info.Function_Info }
@ -50,6 +52,12 @@ local function extract_section_functions(dl: string): { Function_Info.Function_I
) )
end end
local function extract_section_signal(dl: string): { string }
local selector = "dt strong"
return scraper_utils.scrape(dl, selector, extract_node_text)
end
local module = {} local module = {}
function module.get_doc_from_page(html: string): Module_Doc.Module_Doc function module.get_doc_from_page(html: string): Module_Doc.Module_Doc
@ -80,7 +88,7 @@ function module.get_doc_from_page(html: string): Module_Doc.Module_Doc
elseif section_name == "Object methods" then elseif section_name == "Object methods" then
module_doc.methods = List(extract_section_functions(dl_html)) module_doc.methods = List(extract_section_functions(dl_html))
elseif section_name == "Signals" then elseif section_name == "Signals" then
log:warn("Not implemented: Signals") module_doc.signals = List(extract_section_signal(dl_html))
else else
error("Unknown section name: " .. section_name) error("Unknown section name: " .. section_name)
end end

View File

@ -832,6 +832,9 @@ local record pl
rpartition: function(string, string): string, string, string rpartition: function(string, string): string, string, string
at: function(string, number): string at: function(string, number): string
indent: function(string, number, string): string
dedent: function(string): string
lines: function(string): function(): string lines: function(string): function(): string
title: function(string): string title: function(string): string

View File

@ -1,14 +1,14 @@
local record Scanner local record Scanner
record HTMLNode record HTMLNode
tag: string tag: string
type: string | nil type: string | nil
num: number num: number
self_closing: boolean | nil self_closing: boolean | nil
attr: table attr: table
outer_html: function(self: HTMLNode): string outer_html: function(self: HTMLNode): string
inner_html: function(self: HTMLNode): string inner_html: function(self: HTMLNode): string
inner_text: function(self: HTMLNode): string inner_text: function(self: HTMLNode): string
-- TODO : add replacement methods -- TODO : add replacement methods
end end
record NodeStack record NodeStack