From 0e26fe8078a816809f43e0477b3a7799c21cb0d8 Mon Sep 17 00:00:00 2001 From: Fenris Wolf Date: Thu, 8 Feb 2024 11:10:07 +0100 Subject: [PATCH] [add] element:section --- source/elements/implementations/list.ts | 30 ++++++++++++ source/elements/implementations/section.ts | 32 +++++++++++++ source/outputs/base.ts | 54 ++++++++++++++++++++++ source/outputs/implementations/html.ts | 37 ++++++++++++++- source/outputs/implementations/json.ts | 13 ++++++ tools/makefile | 2 + 6 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 source/elements/implementations/list.ts create mode 100644 source/elements/implementations/section.ts diff --git a/source/elements/implementations/list.ts b/source/elements/implementations/list.ts new file mode 100644 index 0000000..90bf32a --- /dev/null +++ b/source/elements/implementations/list.ts @@ -0,0 +1,30 @@ + +/** + */ +type type_element_list_data = { + items : Array; +}; + + +/** + */ +class type_element_list implements type_element +{ + public readonly kind : string = "list"; + public readonly data : type_element_list_data; + public constructor(data : type_element_list_data) {this.data = data;} +} + + +/** + */ +element_kind_register( + "list", + (data) => ( + new type_element_list( + { + "items": data["items"], + } + ) + ) +); diff --git a/source/elements/implementations/section.ts b/source/elements/implementations/section.ts new file mode 100644 index 0000000..942759c --- /dev/null +++ b/source/elements/implementations/section.ts @@ -0,0 +1,32 @@ + +/** + */ +type type_element_section_data = { + title : string; + content : type_element; +}; + + +/** + */ +class type_element_section implements type_element +{ + public readonly kind : string = "section"; + public readonly data : type_element_section_data; + public constructor(data : type_element_section_data) {this.data = data;} +} + + +/** + */ +element_kind_register( + "section", + (data) => ( + new type_element_section( + { + "title": data["title"], + "content": data["content"], + } + ) + ) +); diff --git a/source/outputs/base.ts b/source/outputs/base.ts index 8b5e080..8205ec3 100644 --- a/source/outputs/base.ts +++ b/source/outputs/base.ts @@ -13,3 +13,57 @@ interface type_output extends lib_plankton.call.type_coproduct } + +/** + */ +let output_kind_pool : Record< + string, + ( + ( + data : any, + make : ((raw : any) => type_output) + ) + => + type_output + ) +> = {}; + + +/** + */ +function output_kind_register( + name : string, + factory : ( + (data : any, make : ((raw : any) => type_output)) + => + type_output + ) +) : void +{ + if (name in output_kind_pool) + { + throw (new Error("kind '" + name + "' already registered")); + } + else + { + output_kind_pool[name] = factory; + } +} + + +/** + */ +function output_make( + raw : any +) : type_output +{ + if (! (raw["kind"] in output_kind_pool)) + { + throw (new Error("kind '" + raw["kind"] + "' not registered")); + } + else + { + return output_kind_pool[raw["kind"]](raw["data"], output_make); + } +} + diff --git a/source/outputs/implementations/html.ts b/source/outputs/implementations/html.ts index 82c99e8..67952df 100644 --- a/source/outputs/implementations/html.ts +++ b/source/outputs/implementations/html.ts @@ -30,10 +30,32 @@ class type_output_html implements type_output + "\n" ), + "section": ({"title": title, "content": content}) => ( + "
\n" + + + ( + "
" + + + title + + + "
" + ) + + + this.render_element(content) + + + "
\n" + ), + "list": ({"items": items}) => ( + "
    \n" + + + items.map(x => this.render_element(x)).map(x => ("
  • " + x + "
  • \n")).join("") + + + "
\n" + ), }, { "fallback": (element) => ( - "
"
+					"
"
 					+
 					JSON.stringify(element, undefined, "    ")
 					+
@@ -44,3 +66,16 @@ class type_output_html implements type_output
 	}
 	
 }
+
+
+/**
+ */
+output_kind_register(
+	"html",
+	(data, sub) => (
+		new type_output_html(
+			{
+			}
+		)
+	)
+);
diff --git a/source/outputs/implementations/json.ts b/source/outputs/implementations/json.ts
index 93a6eb7..84a0b61 100644
--- a/source/outputs/implementations/json.ts
+++ b/source/outputs/implementations/json.ts
@@ -24,3 +24,16 @@ class type_output_json implements type_output
 	}
 	
 }
+
+
+/**
+ */
+output_kind_register(
+	"json",
+	(data, sub) => (
+		new type_output_json(
+			{
+			}
+		)
+	)
+);
diff --git a/tools/makefile b/tools/makefile
index 73caec7..93a6c27 100644
--- a/tools/makefile
+++ b/tools/makefile
@@ -27,6 +27,8 @@ ${dir_temp}/sd-unlinked.js: \
 		${dir_source}/elements/base.ts \
 		${dir_source}/elements/implementations/text.ts \
 		${dir_source}/elements/implementations/group.ts \
+		${dir_source}/elements/implementations/section.ts \
+		${dir_source}/elements/implementations/list.ts \
 		${dir_source}/outputs/base.ts \
 		${dir_source}/outputs/implementations/json.ts \
 		${dir_source}/outputs/implementations/html.ts \