Merge pull request 'Implement module section types "Signals" (#18)' (#23) from feat/#18 into master
ci/woodpecker/push/lint Pipeline was successful Details
ci/woodpecker/push/build Pipeline was successful Details

Reviewed-on: #23
This commit is contained in:
Aire-One 2022-10-23 21:39:57 +02:00
commit b4d6aa5228
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>
methods: List<Function_Info.Function_Info>
static_functions: List<Function_Info.Function_Info>
signals: List<string>
end
local __Module_Doc: metatable<Module_Doc> = {

View File

@ -1,17 +1,14 @@
local Function_Info = require "entity.Function_Info"
local List = require "pl.List"
local utils = require "utils"
local stringx = require "pl.stringx"
local template = require "pl.template"
local utils = require "utils"
local snippets = {}
function snippets.indent(str: string, level: number): string
level = level or 1
local ret = ""
for line in str:gmatch("[^\n]+") do
ret = ret .. string.rep(" ", level * 3) .. line .. "\n"
end
return ret
return stringx.rstrip(stringx.indent(str, level, string.rep(" ", 3)))
end
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
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

View File

@ -8,15 +8,11 @@ local snippets = require "generator.snippets"
local tmpl = [[
-- Auto generated file (Do not manually edit this file!)
# if module.signals then
local enum signals
# for _, signal in ipairs(module.signals) do
"$(signal.name)"
# end
end
local record $(mod_name)
# if #module.signals then
$(snippets.indent(snippets.render_enum("Signal", module.signals)))
# end -- /signals
local record $(mod_name)
# if #module.methods then
-- Object 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 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
return function_name_node and ((function_name_node.attr.name as string):gsub(".*:", ""))
end
@ -20,9 +24,7 @@ local function extract_function_return_types(function_return_types_node: scan.HT
local selector = "span.types .type"
local html = function_return_types_node:outer_html()
return scraper_utils.scrape(html, selector, function(node: scan.HTMLNode): string
return utils.sanitize_string(node:inner_text())
end)
return scraper_utils.scrape(html, selector, extract_node_text)
end
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
local function extract_section_signal(dl: string): { string }
local selector = "dt strong"
return scraper_utils.scrape(dl, selector, extract_node_text)
end
local module = {}
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
module_doc.methods = List(extract_section_functions(dl_html))
elseif section_name == "Signals" then
log:warn("Not implemented: Signals")
module_doc.signals = List(extract_section_signal(dl_html))
else
error("Unknown section name: " .. section_name)
end

View File

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

View File

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