From c51f6d12b0a014d9a6209dc6d7488cfa3f77b05d Mon Sep 17 00:00:00 2001 From: Fenris Wolf Date: Mon, 23 Feb 2026 20:16:38 +0100 Subject: [PATCH] [mod] --- misc/sd.schema.json | 234 ++++++++++++++++++++- readme.md | 28 +++ source/objects/implementations/group.ts | 4 +- source/objects/implementations/list.ts | 4 +- source/objects/implementations/table.ts | 14 +- source/outputs/implementations/html.ts | 24 +++ source/outputs/implementations/markdown.ts | 84 +++++++- 7 files changed, 379 insertions(+), 13 deletions(-) create mode 100644 readme.md diff --git a/misc/sd.schema.json b/misc/sd.schema.json index 123a07e..57c9ec4 100644 --- a/misc/sd.schema.json +++ b/misc/sd.schema.json @@ -1,7 +1,233 @@ { - "anyOf": [ - { - "type": " + "definitions": { + "doc_object": { + "anyOf": [ + { + "type": "object", + "reqired": [ + "kind", + "data" + ], + "properties": { + "kind": { + "type": "string", + "enum": ["code"] + }, + "data": { + "type": "object", + "reqired": [ + "content" + ], + "properties": { + "content": { + "type": "string" + } + } + } + } + }, + { + "type": "object", + "reqired": [ + "kind", + "data" + ], + "properties": { + "kind": { + "type": "string", + "enum": ["text"] + }, + "data": { + "type": "object", + "reqired": [ + "content" + ], + "properties": { + "content": { + "type": "string" + } + } + } + } + }, + { + "type": "object", + "reqired": [ + "kind", + "data" + ], + "properties": { + "kind": { + "type": "string", + "enum": ["link"] + }, + "data": { + "type": "object", + "reqired": [ + "target", + "label" + ], + "properties": { + "target": { + "type": "string" + }, + "label": { + "nullable": true, + "type": "string" + } + } + } + } + }, + { + "type": "object", + "reqired": [ + "kind", + "data" + ], + "properties": { + "kind": { + "type": "string", + "enum": ["section"] + }, + "data": { + "type": "object", + "required": [ + "title", + "content" + ], + "properties": { + "title": { + "type": "string" + }, + "content": { + "$ref": "#/definitions/doc_object" + } + } + } + } + }, + { + "type": "object", + "reqired": [ + "kind", + "data" + ], + "properties": { + "kind": { + "type": "string", + "enum": ["group"] + }, + "data": { + "type": "object", + "required": [ + "members" + ], + "properties": { + "members": { + "type": "array", + "items": { + "$ref": "#/definitions/doc_object" + } + } + } + } + } + }, + { + "type": "object", + "reqired": [ + "kind", + "data" + ], + "properties": { + "kind": { + "type": "string", + "enum": ["list"] + }, + "data": { + "type": "object", + "required": [ + "items" + ], + "properties": { + "items": { + "type": "array", + "items": { + "$ref": "#/definitions/doc_object" + } + } + } + } + } + }, + { + "type": "object", + "reqired": [ + "kind", + "data" + ], + "properties": { + "kind": { + "type": "string", + "enum": ["table"] + }, + "data": { + "type": "object", + "required": [ + "head", + "rows" + ], + "properties": { + "head": { + "nullable": true, + "type": "array", + "items": { + "$ref": "#/definitions/doc_object" + } + }, + "rows": { + "type": "array", + "items": { + "type": "array", + "items": { + "$ref": "#/definitions/doc_object" + } + } + } + } + } + } + } + ] } - ] + }, + "type": "object", + "required": [ + "definitions", + "content" + ], + "properties": { + "definitions": { + "type": "object", + "additionalProperties": { + "$ref": "#/definitions/doc_object" + } + }, + "content": { + "type": "object", + "required": [ + "title", + "main" + ], + "properties": { + "title": { + "type": "string" + }, + "main": { + "$ref": "#/definitions/doc_object" + } + } + } + } } diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..4063a03 --- /dev/null +++ b/readme.md @@ -0,0 +1,28 @@ +# docdef + +zum Erzeugen von Dokumenten anhand von JSON-Daten + + +## Nutzung + +Am beigefügten Beispiel `polyhedra`: + +```sh +misc/conv misc/examples/polyhedra/transform-default.js misc/examples/polyhedra/data.json | build/sd -o markdown > /tmp/polyhedra-default.md +``` + +Der erste Teil (vor der Pipe) transformiert die rohen Daten in ein ein JSON-Dokument im `sd`-Dialekt. `build/sd -o markdown` liest dieses Dokument von der Standard-Eingabe und erzeugt die Ausgabe im Markdown-Format. + +Folgende Abwandlungen sind möglich: + +```sh +# Markdown-Tabelle +misc/conv misc/examples/polyhedra/transform-table.js misc/examples/polyhedra/data.json | build/sd -o markdown > /tmp/polyhedra-table.md + +# HTML-Liste +misc/conv misc/examples/polyhedra/transform-default.js misc/examples/polyhedra/data.json | build/sd -o html > /tmp/polyhedra-default.html + +# HTML-Tabelle +misc/conv misc/examples/polyhedra/transform-table.js misc/examples/polyhedra/data.json | build/sd -o html > /tmp/polyhedra-table.html +``` + diff --git a/source/objects/implementations/group.ts b/source/objects/implementations/group.ts index dbcbc3c..c7f9a01 100644 --- a/source/objects/implementations/group.ts +++ b/source/objects/implementations/group.ts @@ -2,7 +2,9 @@ /** */ type type_object_group_data = { - members : type_object; + members : Array< + type_object + >; }; diff --git a/source/objects/implementations/list.ts b/source/objects/implementations/list.ts index 5895742..0c95f88 100644 --- a/source/objects/implementations/list.ts +++ b/source/objects/implementations/list.ts @@ -2,7 +2,9 @@ /** */ type type_object_list_data = { - items : Array; + items : Array< + type_object + >; }; diff --git a/source/objects/implementations/table.ts b/source/objects/implementations/table.ts index f6536cf..da76b21 100644 --- a/source/objects/implementations/table.ts +++ b/source/objects/implementations/table.ts @@ -2,8 +2,18 @@ /** */ type type_object_table_data = { - head : (null | Array); - rows : Array>; + head : ( + null + | + Array< + type_object + > + ); + rows : Array< + Array< + type_object + > + >; }; diff --git a/source/outputs/implementations/html.ts b/source/outputs/implementations/html.ts index 1543649..783aee6 100644 --- a/source/outputs/implementations/html.ts +++ b/source/outputs/implementations/html.ts @@ -57,6 +57,30 @@ class type_output_html implements type_output : [new lib_plankton.xml.class_node_text(label)] ) ), + "image": ({"url": url, "label": label}) => new lib_plankton.xml.class_node_complex( + "img", + { + "class": "sd-image", + "alt": label, + "src": url, + }, + [] + ), + "paragraph": ({"content": content}) => new lib_plankton.xml.class_node_complex( + "p", + { + "class": "sd-paragraph", + }, + [ + this.render_object_internal( + content, + { + "level": options.level, + "depth": (options.depth + 1), + } + ), + ] + ), "section": ({"title": title, "content": content}) => new lib_plankton.xml.class_node_complex( "section", { diff --git a/source/outputs/implementations/markdown.ts b/source/outputs/implementations/markdown.ts index 72a4aac..6a4938f 100644 --- a/source/outputs/implementations/markdown.ts +++ b/source/outputs/implementations/markdown.ts @@ -48,13 +48,33 @@ class type_output_markdown implements type_output "label": label, } ), + "image": ({"url": url, "label": label}) => lib_plankton.string.coin( + "![{{label}}]({{url}})", + { + "url": url, + "label": label, + } + ), + "paragraph": ({"content": content}) => lib_plankton.string.coin( + "{{content}}\n\n", + { + "content": this.render_object_internal( + content, + { + "depth": (options.depth + 1), + "headline_level": options.headline_level, + "list_level": options.list_level, + } + ), + } + ), "section": ({"title": title, "content": content}) => lib_plankton.string.coin( "{{head}}\n\n{{body}}\n\n\n", { "head": lib_plankton.string.coin( "{{prefix}} {{value}}", { - "prefix": "#".repeat(options.level + 1), + "prefix": "#".repeat(options.headline_level + 1), "value": title, } ), @@ -62,7 +82,7 @@ class type_output_markdown implements type_output content, { "depth": (options.depth + 1), - "headline_level": (options.level + 1), + "headline_level": (options.headline_level + 1), "list_level": options.list_level, } ), @@ -78,7 +98,7 @@ class type_output_markdown implements type_output x, { "depth": options.depth, - "headline_level": options.level, + "headline_level": options.headline_level, "list_level": options.list_level } ) @@ -103,7 +123,7 @@ class type_output_markdown implements type_output item, { "depth": (options.depth + 1), - "headline_level": options.level, + "headline_level": options.headline_level, "list_level": (options.list_level + 1), } ), @@ -117,8 +137,62 @@ class type_output_markdown implements type_output } ), "table": ({"head": head, "rows": rows}) => lib_plankton.string.coin( - "(unhandled)", + "{{head}}\n{{separation}}\n{{body}}\n\n", { + "head": lib_plankton.string.coin( + "| {{entries}} |", + { + "entries": ( + head + .map( + (object) => this.render_object_internal( + object, + { + "depth": (options.depth + 1), + "headline_level": options.headline_level, + "list_level": options.list_level, + } + ), + ) + .join(" | ") + ), + } + ), + "separation": lib_plankton.string.coin( + "| {{entries}} |", + { + "entries": ( + head + .map((object) => "-- ") + .join(" | ") + ), + } + ), + "body": ( + rows + .map( + row => lib_plankton.string.coin( + "| {{entries}} |", + { + "entries": ( + row + .map( + (object) => this.render_object_internal( + object, + { + "depth": (options.depth + 1), + "headline_level": options.headline_level, + "list_level": options.list_level, + } + ), + ) + .join(" | ") + ), + } + ) + ) + .join("\n") + ), } ), },