diff --git a/misc/conv b/misc/conv new file mode 100755 index 0000000..bcd0f01 --- /dev/null +++ b/misc/conv @@ -0,0 +1,21 @@ +#!/usr/bin/env node + +function main(args) +{ + // args + const path_transform = ((args.length >= 0) ? args.shift() : "transform.js"); + const path_data = ((args.length >= 0) ? args.shift() : "data.json"); + + // exec + const _fs = require("fs"); + const data = JSON.parse(_fs.readFileSync(path_data).toString()); + const transformation_code = _fs.readFileSync(path_transform).toString() + const sd = eval(transformation_code)(data); + + // output + const output = (JSON.stringify(sd, undefined, "\t") + "\n"); + process.stdout.write(output); +} + + +main(process.argv.slice(2)); diff --git a/misc/description.json b/misc/description.json deleted file mode 100644 index 50fb34a..0000000 --- a/misc/description.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "group": { - "attributes": [ - "members" - ] - }, - "section": { - "attributes": [ - "title", - "content" - ] - }, - "list": { - "attributes": [ - "items" - ] - }, - "text": { - "attributes": [ - "content" - ] - } -} diff --git a/misc/example-polyhedra.md b/misc/example-polyhedra.md deleted file mode 100644 index caf331b..0000000 --- a/misc/example-polyhedra.md +++ /dev/null @@ -1,36 +0,0 @@ -# Polyhedra - -## Tetrahedron - -- vertices: 4 -- faces: 4 -- edges: 6 - - -## Hexahedron - -- vertices: 8 -- faces: 6 -- edges: 12 - - -## Octahedron - -- vertices: 6 -- faces: 8 -- edges: 12 - - -## Dodecahedron - -- vertices: 20 -- faces: 12 -- edges: 30 - - -## Icosahedron - -- vertices: 12 -- faces: 20 -- edges: 30 - diff --git a/misc/example-polyhedra.sd.json b/misc/example-polyhedra.sd.json deleted file mode 100644 index 1c10b26..0000000 --- a/misc/example-polyhedra.sd.json +++ /dev/null @@ -1,207 +0,0 @@ -{ - "definitions": { - "tetrahedron": { - "kind": "section", - "data": { - "title": "Tetrahedron", - "content": { - "kind": "list", - "data": { - "items": [ - { - "kind": "text", - "data": { - "content": "vertices: 4" - } - }, - { - "kind": "text", - "data": { - "content": "faces: 4" - } - }, - { - "kind": "text", - "data": { - "content": "edges: 6" - } - } - ] - } - } - } - }, - "hexahedron": { - "kind": "section", - "data": { - "title": "Hexahedron", - "content": { - "kind": "list", - "data": { - "items": [ - { - "kind": "text", - "data": { - "content": "vertices: 6" - } - }, - { - "kind": "text", - "data": { - "content": "faces: 8" - } - }, - { - "kind": "text", - "data": { - "content": "edges: 12" - } - } - ] - } - } - } - }, - "octahedron": { - "kind": "section", - "data": { - "title": "Octahedron", - "content": { - "kind": "list", - "data": { - "items": [ - { - "kind": "text", - "data": { - "content": "vertices: 6" - } - }, - { - "kind": "text", - "data": { - "content": "faces: 8" - } - }, - { - "kind": "text", - "data": { - "content": "edges: 12" - } - } - ] - } - } - } - }, - "dodecahedron": { - "kind": "section", - "data": { - "title": "Dodecahedron", - "content": { - "kind": "list", - "data": { - "items": [ - { - "kind": "text", - "data": { - "content": "vertices: 20" - } - }, - { - "kind": "text", - "data": { - "content": "faces: 12" - } - }, - { - "kind": "text", - "data": { - "content": "edges: 30" - } - } - ] - } - } - } - }, - "icosahedron": { - "kind": "section", - "data": { - "title": "Icosahedron", - "content": { - "kind": "list", - "data": { - "items": [ - { - "kind": "text", - "data": { - "content": "vertices: 12" - } - }, - { - "kind": "text", - "data": { - "content": "faces: 20" - } - }, - { - "kind": "text", - "data": { - "content": "edges: 30" - } - } - ] - } - } - } - } - }, - "content": { - "title": "Polyhedra", - "main": { - "kind": "section", - "data": { - "title": "Polyhedra", - "content": { - "kind": "group", - "data": { - "members": [ - { - "kind": "reference", - "data": { - "name": "tetrahedron" - } - }, - { - "kind": "reference", - "data": { - "name": "hexahedron" - } - }, - { - "kind": "reference", - "data": { - "name": "octahedron" - } - }, - { - "kind": "reference", - "data": { - "name": "dodecahedron" - } - }, - { - "kind": "reference", - "data": { - "name": "icosahedron" - } - } - ] - } - } - } - } - - } -} - diff --git a/misc/example-simple.sd.json b/misc/example-simple.sd.json deleted file mode 100644 index f89df34..0000000 --- a/misc/example-simple.sd.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "kind": "group", - "data": { - "members": [ - { - "kind": "text", - "data": { - "content": "foo" - } - }, - { - "kind": "text", - "data": { - "content": "bar" - } - } - ] - } -} diff --git a/misc/examples/polyhedra/data.json b/misc/examples/polyhedra/data.json new file mode 100644 index 0000000..699f2f4 --- /dev/null +++ b/misc/examples/polyhedra/data.json @@ -0,0 +1,41 @@ +{ + "stock": { + "tetrahedron": { + "name": "Tetrahedron", + "vertices": 4, + "faces": 4, + "edges": 6 + }, + "hexahedron": { + "name": "Hexahedron", + "vertices": 8, + "faces": 6, + "edges": 12 + }, + "octahedron": { + "name": "Octahedron", + "vertices": 6, + "faces": 8, + "edges": 12 + }, + "dodecahedron": { + "name": "Dodecahedron", + "vertices": 20, + "faces": 12, + "edges": 30 + }, + "icosahedron": { + "name": "Icosahedron", + "vertices": 12, + "faces": 20, + "edges": 30 + } + }, + "order": [ + "tetrahedron", + "hexahedron", + "octahedron", + "dodecahedron", + "icosahedron" + ] +} diff --git a/misc/examples/polyhedra/transform-default.js b/misc/examples/polyhedra/transform-default.js new file mode 100644 index 0000000..af83420 --- /dev/null +++ b/misc/examples/polyhedra/transform-default.js @@ -0,0 +1,51 @@ +(data) => ({ + "definitions": { + }, + "content": { + "title": "Polyhedra", + "main": { + "kind": "section", + "data": { + "title": "Polyhedra", + "content": { + "kind": "group", + "data": { + "members": data.order.map( + name => ({ + "kind": "section", + "data": { + "title": data.stock[name].name, + "content": { + "kind": "list", + "data": { + "items": [ + { + "kind": "text", + "data": { + "content": ("vertices: " + data.stock[name].vertices.toFixed(0)), + } + }, + { + "kind": "text", + "data": { + "content": ("faces: " + data.stock[name].faces.toFixed(0)), + } + }, + { + "kind": "text", + "data": { + "content": ("edges: " + data.stock[name].edges.toFixed(0)), + } + }, + ] + } + } + } + }) + ) + } + } + } + } + } +}) diff --git a/misc/examples/polyhedra/transform-table.js b/misc/examples/polyhedra/transform-table.js new file mode 100644 index 0000000..6693bb9 --- /dev/null +++ b/misc/examples/polyhedra/transform-table.js @@ -0,0 +1,72 @@ +(data) => ({ + "definitions": { + }, + "content": { + "title": "Polyhedra", + "main": { + "kind": "section", + "data": { + "title": "Polyhedra", + "content": { + "kind": "table", + "data": { + "head": [ + { + "kind": "text", + "data": { + "content": "name" + } + }, + { + "kind": "text", + "data": { + "content": "vertices" + } + }, + { + "kind": "text", + "data": { + "content": "faces" + } + }, + { + "kind": "text", + "data": { + "content": "edges" + } + } + ], + "rows": data.order.map( + name => ([ + { + "kind": "text", + "data": { + "content": data.stock[name].name + } + }, + { + "kind": "code", + "data": { + "content": data.stock[name].vertices.toFixed(0) + } + }, + { + "kind": "code", + "data": { + "content": data.stock[name].faces.toFixed(0) + } + }, + { + "kind": "code", + "data": { + "content": data.stock[name].edges.toFixed(0) + } + } + ]) + ), + } + } + } + } + } +}) diff --git a/misc/notes.md b/misc/notes.md deleted file mode 100644 index 89b5d5c..0000000 --- a/misc/notes.md +++ /dev/null @@ -1,15 +0,0 @@ -- coproduct `element` - - group - - section - - list - - … -- coproduct `output` - - (markdown) - - html - - tex - - odt - - … -- coproduct `input` - - markdown - - diff --git a/source/main.ts b/source/main.ts index cd55061..23bab5e 100644 --- a/source/main.ts +++ b/source/main.ts @@ -1,8 +1,11 @@ +declare var console; + async function main(args_raw : Array) : Promise { // args const arg_handler : lib_plankton.args.class_handler = new lib_plankton.args.class_handler( { + /* "input": lib_plankton.args.class_argument.positional({ "index": 0, "type": lib_plankton.args.enum_type.string, @@ -11,6 +14,7 @@ async function main(args_raw : Array) : Promise "info": "path to input file", "name": "input", }), + */ "output": lib_plankton.args.class_argument.volatile({ "indicators_long": ["output"], "indicators_short": ["o"], @@ -46,7 +50,8 @@ async function main(args_raw : Array) : Promise } else { - const document_raw : string = lib_plankton.json.decode(await lib_plankton.file.read(args.input)) + const document_raw : string = lib_plankton.json.decode(await lib_plankton.file/*.read(args.input)*/.read_stdin()); + const document : type_document = type_document.from_raw(document_raw); // let object : type_object = object_make(document_raw["content"]["main"]); diff --git a/source/objects/implementations/code.ts b/source/objects/implementations/code.ts new file mode 100644 index 0000000..4e63031 --- /dev/null +++ b/source/objects/implementations/code.ts @@ -0,0 +1,30 @@ + +/** + */ +type type_object_code_data = { + content : string; +}; + + +/** + */ +class type_object_code implements type_object +{ + public readonly kind : string = "code"; + public readonly data : type_object_code_data; + public constructor(data : type_object_code_data) {this.data = data;} +} + + +/** + */ +object_kind_register( + "code", + (data) => ( + new type_object_code( + { + "content": data["content"], + } + ) + ) +); diff --git a/source/objects/implementations/link.ts b/source/objects/implementations/link.ts new file mode 100644 index 0000000..c21d6b7 --- /dev/null +++ b/source/objects/implementations/link.ts @@ -0,0 +1,32 @@ + +/** + */ +type type_object_link_data = { + target : string; + label : (null | string); +}; + + +/** + */ +class type_object_link implements type_object +{ + public readonly kind : string = "link"; + public readonly data : type_object_link_data; + public constructor(data : type_object_link_data) {this.data = data;} +} + + +/** + */ +object_kind_register( + "link", + (data) => ( + new type_object_link( + { + "target": data["target"], + "label": (data["label"] ?? null), + } + ) + ) +); diff --git a/source/objects/implementations/table.ts b/source/objects/implementations/table.ts new file mode 100644 index 0000000..f6536cf --- /dev/null +++ b/source/objects/implementations/table.ts @@ -0,0 +1,33 @@ + +/** + */ +type type_object_table_data = { + head : (null | Array); + rows : Array>; +}; + + +/** + * @todo check sanity (length of head and rows) + */ +class type_object_table implements type_object +{ + public readonly kind : string = "table"; + public readonly data : type_object_table_data; + public constructor(data : type_object_table_data) {this.data = data;} +} + + +/** + */ +object_kind_register( + "table", + (data) => ( + new type_object_table( + { + "head": data["head"], + "rows": data["rows"], + } + ) + ) +); diff --git a/source/outputs/implementations/html.ts b/source/outputs/implementations/html.ts index 3c8834f..357803b 100644 --- a/source/outputs/implementations/html.ts +++ b/source/outputs/implementations/html.ts @@ -36,6 +36,27 @@ class type_output_html implements type_output new lib_plankton.xml.class_node_text(content), ] ), + "code": ({"content": content}) => new lib_plankton.xml.class_node_complex( + "code", + { + "class": "sd-code", + }, + [ + new lib_plankton.xml.class_node_text(content), + ] + ), + "link": ({"target": target, "label": label}) => new lib_plankton.xml.class_node_complex( + "a", + { + "class": "sd-link", + "href": target, + }, + ( + (label === null) + ? [] + : [new lib_plankton.xml.class_node_text(label)] + ) + ), "group": ({"members": members}) => new lib_plankton.xml.class_node_complex( "div", { @@ -100,6 +121,81 @@ class type_output_html implements type_output ) ), ), + "table": ({"head": head, "rows": rows}) => new lib_plankton.xml.class_node_complex( + "table", + { + "class": "sd-table", + }, + ( + [] + .concat( + (head === null) + ? [] + : [ + new lib_plankton.xml.class_node_complex( + "thead", + {}, + [ + new lib_plankton.xml.class_node_complex( + "tr", + {}, + ( + head.map( + cell => new lib_plankton.xml.class_node_complex( + "th", + {}, + [ + this.render_object_internal( + cell, + { + "level": options.level, + "depth": (options.depth + 1), + } + ), + ] + ) + ) + ) + ), + ] + ), + ] + ) + .concat( + [ + new lib_plankton.xml.class_node_complex( + "tbody", + {}, + ( + rows.map( + row => new lib_plankton.xml.class_node_complex( + "tr", + {}, + ( + row.map( + cell => new lib_plankton.xml.class_node_complex( + "td", + {}, + [ + this.render_object_internal( + cell, + { + "level": options.level, + "depth": (options.depth + 1), + } + ), + ] + ) + ) + ) + ) + ) + ) + ) + ] + ) + ), + ), }, { "fallback": (object) => new lib_plankton.xml.class_node_complex( diff --git a/tools/makefile b/tools/makefile index ef34c32..93e952f 100644 --- a/tools/makefile +++ b/tools/makefile @@ -26,9 +26,12 @@ ${dir_temp}/sd-unlinked.js: \ ${dir_source}/base.ts \ ${dir_source}/objects/base.ts \ ${dir_source}/objects/implementations/text.ts \ + ${dir_source}/objects/implementations/code.ts \ + ${dir_source}/objects/implementations/link.ts \ ${dir_source}/objects/implementations/group.ts \ ${dir_source}/objects/implementations/section.ts \ ${dir_source}/objects/implementations/list.ts \ + ${dir_source}/objects/implementations/table.ts \ ${dir_source}/document.ts \ ${dir_source}/outputs/base.ts \ ${dir_source}/outputs/implementations/json.ts \