%
function split_used_by(users)
local as_params = {}
local as_returns = {}
local as_fields = {}
for key, user in ipairs(users) do
if #user.as_params > 0 then
table.insert(as_params, {
type_name=user.type_name,
as_params=user.as_params
})
end
if #user.as_return > 0 then
table.insert(as_returns, {
type_name=user.type_name,
as_returns=user.as_return
})
end
if #user.as_fields > 0 then
table.insert(as_fields, {
type_name=user.type_name,
as_fields=user.as_fields
})
end
end
return as_params,as_returns,as_fields
end
function render_used_by(users)
local as_params,as_returns,as_fields = split_used_by(users)
local function render(to_render, name, field_name)
if #to_render > 0 then
%>
<%=name%>
<%
for _k, used_in_type in ipairs(to_render) do
for _k, used_by in ipairs(used_in_type[field_name]) do
%>
<%
end
function render(str)
%><%=str-%><%
end
function is_string_starting_with(str,start)
return string.sub(str,1,string.len(start))==start
end
function get_string_from(str,from)
return string.sub(str,from,string.len(str))
end
local function headingToSize(heading)
return "is-" .. get_string_from(heading, 1)
end
function render_markdown(markdown)
local code_to_compile = nil
local skip_rust_example = false
local transformer = function(toTransform)
if toTransform:IsStart() then
local start = toTransform:GetStartOrNil()
if start:IsCodeBlock() then
local block = start:GetCodeBlockOrNil()
local fence = block:GetFencedOrNil()
if fence == "teal_lua" then
code_to_compile = "local " .. library_name .. " = require(\""..definition_file_folder.."." .. library_name .."\")"
return
elseif is_string_starting_with(fence, "rs") or is_string_starting_with(fence,"rust") then
skip_rust_example = true
return
end
elseif start:IsLink() then
local link_type,to,title = start:GetLinkOrNil()
if is_string_starting_with(to,"#") then
to = create_link(get_string_from(to,1))
elseif not is_string_starting_with(to,"http") then
to = create_link(to)
end
return markdown_event_creator.NewStartFrom(start.NewLinkFrom(link_type,to,title))
elseif start:IsHeading() then
local heading_level, fragment, classes = start:GetHeadingOrNil()
table.insert(classes,"subtitle")
table.insert(classes,headingToSize(heading_level))
return markdown_event_creator.NewStartFrom(
markdown_tag_creator.NewHeadingFrom(heading_level,fragment,classes)
)
end
elseif code_to_compile ~= nil and toTransform:IsText() then
local text = toTransform:GetTextOrNil()
code_to_compile = code_to_compile .. '\n'.. text
return
elseif skip_rust_example and toTransform:IsText() then
return
elseif skip_rust_example and toTransform:IsEnd() then
local blockEnd = toTransform:GetEndOrNil()
if blockEnd:IsCodeBlock() then
local codeBlock = blockEnd:GetCodeBlockOrNil()
local fence = codeBlock:GetFencedOrNil() --or ""
if is_string_starting_with(fence, "rs") or is_string_starting_with(fence,"rust") then
skip_rust_example = false
return
end
end
elseif code_to_compile ~= nil and toTransform:IsEnd() then
local blockEnd = toTransform:GetEndOrNil()
if blockEnd:IsCodeBlock() then
local codeBlock = blockEnd:GetCodeBlockOrNil()
if codeBlock:IsFenced() and codeBlock:GetFencedOrNil() == "teal_lua" then
local tl = require("tl")
local env = tl.init_env(false,false,true)
local output,result = tl.gen(code_to_compile,env)
if #result.syntax_errors > 0 then
print("Syntax errors found at code:")
print(code_to_compile)
print("Errors:")
for k,v in ipairs(result.syntax_errors) do
print("Syntax error found at:" .. v.msg .. ' x=' .. tostring(v.x) .. ' y=' ..tostring(v.y))
end
end
if #result.type_errors > 0 then
print("Type errors found at code:")
print(code_to_compile)
print("Errors:")
for k,v in ipairs(result.type_errors) do
print("Type error found at:" .. v.msg .. ' x=' .. tostring(v.x) .. ' y=' ..tostring(v.y))
end
end
local teal_code = code_to_compile
code_to_compile = nil
return {
markdown_event_creator.NewHtmlFrom(
[[
<%
end
function renderTealTypeOf(type_parts)
for k,v in ipairs(type_parts) do
local name = v:GetSymbolOrNil()
if name then
render(name)
else
name = v:GetTypeOrNil()
if name.type_kind == "Builtin" or name.type_kind == "Generic" then
render(name.name)
else
%>
<%= name.name -%>
<%
end
end
end
end
function render_record_part(header_name, record, parts)
function render_part(part_name, record)
for x, member in pairs(dedupe_by(record[part_name],function(x)return x.name end)) do
%>
<%= member.name -%>: <%renderTealTypeOf(member.signature or member.teal_type) %>
<%
for _,name in ipairs(parts) do
render_part(name,record)
end
%>
<%
end
function render_definition_files()
for k,v in pairs(definition_config) do
%>
<%=k%>
<%
end
end
local page_name
local type_name
local type_members
local used_by
local t = page:GetTypeOrNil()
local index = page:GetIndexPageOrNil()
local custom_page = page:GetCustomPageOrNil()
if t then
type_name = t.type_name
type_members = t.type_members
used_by = t.used_by
elseif index then
type_name = index.type_name
type_members = index.type_members
elseif custom_page then
page_name = custom_page.name
end
page_name = page_name or type_name
%>
<%= page_name-%>
<%= page_name -%>
<%
if page:IsType() then
%>
<%
end
%>
<%
local custom_page = page:GetCustomPageOrNil()
if custom_page then
render_markdown(custom_page.markdown_content)
else
if page:GetType() then
render_used_by(used_by)
end
local record = type_members:GetRecordOrNil()
if record then
%>
Type doc:
<% render_markdown(record.type_doc or "") %>
<%
render_record_part("Fields",record,{"fields","static_fields"})
render_record_part("Methods",record, {
"methods",
"mut_methods","functions",
"mut_functions",
"meta_method",
"meta_method_mut",
"meta_function",
"meta_function_mut"
})
end
local enum = type_members:GetEnumOrNil()
if enum then
%>
Type doc:
<% render_markdown(enum.type_doc or "") %>
Variants
<% for _, variant in ipairs(enum.variants) do %>
"<%= variant %>"
<% end %>
<%
end
if index then
%>
Globals:
<% for _, global_instance in ipairs(globals) do %>
global <%= global_instance.name %> : <% renderTealTypeOf(global_instance.teal_type) %>
<%
if global_instance.doc then
%>
<%
render_markdown(global_instance.doc)
%>
<%
end
%>
<% end %>
<%
end
if index then
%>
Types:
<%
for _, teal_type in ipairs(all_types) do
local record = teal_type:GetRecordOrNil()
if record and not record.should_be_inlined then
%>