docdef/source/outputs/implementations/html.ts

205 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-02-08 10:41:17 +01:00
/**
*/
class type_output_html implements type_output<string>
{
public readonly kind : string = "html";
public readonly data : {};
public constructor(data : {}) {this.data = data;}
/**
*/
2024-02-08 18:10:46 +01:00
private render_object_internal(
object : type_object,
2024-02-08 16:57:51 +01:00
options : {
level ?: int;
2024-02-08 18:10:46 +01:00
depth ?: int;
2024-02-08 16:57:51 +01:00
} = {}
) : lib_plankton.xml.class_node
2024-02-08 10:41:17 +01:00
{
2024-02-08 16:57:51 +01:00
options = Object.assign(
{
"level": 0,
2024-02-08 18:10:46 +01:00
"depth": 0,
2024-02-08 16:57:51 +01:00
},
options
);
return lib_plankton.call.distinguish<lib_plankton.xml.class_node>(
2024-02-08 18:10:46 +01:00
object,
2024-02-08 10:41:17 +01:00
{
2024-02-08 16:57:51 +01:00
"text": ({"content": content}) => new lib_plankton.xml.class_node_complex(
"span",
{
"class": "sd-text",
},
[
new lib_plankton.xml.class_node_text(content),
]
2024-02-08 10:41:17 +01:00
),
2024-02-08 16:57:51 +01:00
"group": ({"members": members}) => new lib_plankton.xml.class_node_complex(
"div",
{
"class": "sd-group",
},
2024-02-08 11:10:07 +01:00
(
2024-02-08 16:57:51 +01:00
members
.map(
2024-02-08 18:10:46 +01:00
x => this.render_object_internal(
2024-02-08 16:57:51 +01:00
x,
{
"depth": options.depth,
"level": options.level,
}
)
)
2024-02-08 11:10:07 +01:00
)
),
2024-02-08 16:57:51 +01:00
"section": ({"title": title, "content": content}) => new lib_plankton.xml.class_node_complex(
"section",
{
"class": "sd-section",
},
[
new lib_plankton.xml.class_node_complex(
("h" + (options.level + 1).toFixed(0)),
{},
[
new lib_plankton.xml.class_node_text(title),
]
),
2024-02-08 18:10:46 +01:00
this.render_object_internal(
2024-02-08 16:57:51 +01:00
content,
{
"level": (options.level + 1),
2024-02-08 18:10:46 +01:00
"depth": (options.depth + 1),
2024-02-08 16:57:51 +01:00
}
),
]
),
"list": ({"items": items}) => new lib_plankton.xml.class_node_complex(
"ul",
{
"class": "sd-list",
},
(
items
.map(
item => new lib_plankton.xml.class_node_complex(
"li",
{},
[
2024-02-08 18:10:46 +01:00
this.render_object_internal(
2024-02-08 16:57:51 +01:00
item,
{
"level": options.level,
"depth": (options.depth + 1),
}
),
]
)
)
),
2024-02-08 11:10:07 +01:00
),
2024-02-08 10:41:17 +01:00
},
{
2024-02-08 18:10:46 +01:00
"fallback": (object) => new lib_plankton.xml.class_node_complex(
2024-02-08 16:57:51 +01:00
"pre",
{
"class": "sd-unhandled",
2024-02-08 18:10:46 +01:00
"rel": object.kind,
2024-02-08 16:57:51 +01:00
},
[
new lib_plankton.xml.class_node_text(
2024-02-08 18:10:46 +01:00
JSON.stringify(object, undefined, " "),
2024-02-08 16:57:51 +01:00
)
]
2024-02-08 10:41:17 +01:00
),
}
);
}
2024-02-08 16:57:51 +01:00
/**
* @implementation
*/
2024-02-08 18:10:46 +01:00
public render_object(
object : type_object
2024-02-08 16:57:51 +01:00
) : string
{
2024-02-08 18:10:46 +01:00
return this.render_object_internal(object).compile();
}
/**
* @implementation
*/
public render_document(
document : type_document
) : string
{
return lib_plankton.string.coin(
"<!DOCTYPE html>\n{{html}}",
{
"html": new lib_plankton.xml.class_node_complex(
"html",
{},
[
new lib_plankton.xml.class_node_complex(
"head",
{
},
[
new lib_plankton.xml.class_node_complex(
"meta",
{
"charset": "utf-8",
},
[
]
),
new lib_plankton.xml.class_node_complex(
"title",
{
},
[
new lib_plankton.xml.class_node_text(
document.content_title
),
]
),
]
),
new lib_plankton.xml.class_node_complex(
"body",
{
},
[
this.render_object_internal(
document.content_main,
{
"depth": 1,
}
),
]
)
]
).compile()
}
);
2024-02-08 16:57:51 +01:00
}
2024-02-08 10:41:17 +01:00
}
2024-02-08 11:10:07 +01:00
/**
*/
output_kind_register(
"html",
(data, sub) => (
new type_output_html(
{
}
)
)
);