From 73b79c6aa1a6d1894cb6cfa3e5e80bd46492006b Mon Sep 17 00:00:00 2001 From: Christopher Hase Date: Thu, 27 Mar 2025 13:27:14 +0100 Subject: [PATCH] create html part 8 --- broker.ts | 109 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 94 insertions(+), 15 deletions(-) diff --git a/broker.ts b/broker.ts index b85190e..e7a0a4b 100644 --- a/broker.ts +++ b/broker.ts @@ -61,26 +61,105 @@ async function sleep() { }, 3600000); } - - -function html(input: string): string { - - // Zerlege den String in einzelne Zeilen - const lines = input.split("\n"); - - // Bearbeite jede Zeile (z.B. trimme Leerzeichen und füge einen Prefix hinzu) - //const processedLines = lines.map(line => `> ${line.trim()}`); - const processedLines = lines.map(line => processLine(line)); - - // Füge die Zeilen wieder zu einem String zusammen - return processedLines.join("\n"); +//node structure for the Parse Tree +interface Node { + type: "Root" | "Hexagram" | "Judgement" | "Images" | "ChangingLines" ; + child?: Node; + value?: string; + //level?: number; // Für Header } +function html(inputText: string): string { + // Parse Tree erstellen & HTML generieren + const parseTree = parse(inputText); + const htmlOutput = render(parseTree); + + return htmlOutput; +} + +//function parse(input: string): string { +function parse(input: string): Node { + + console.log("Parse input text"); + + const root: Node = { type: "Root"}; //, child: {} + var currentNode: Node = root; + + const lines = input.split("\n"); + + for (const line of lines) { + if (line.startsWith("Hexagram")) { + const hexagram: Node = { type: "Hexagram"}; + currentNode.child = hexagram; + currentNode = hexagram; + currentNode.value = "

" + line + "

"; //TODO: formattierung + } + if (line.startsWith("Judgement")) { + const judgement: Node = { type: "Judgement"}; + currentNode.child = judgement; + currentNode = judgement; + currentNode.value = "

" + line + "

"; //TODO: formattierung + } + if (line.startsWith("Images")) { + const images: Node = { type: "Images"}; + currentNode.child = images; + currentNode = images; + currentNode.value = "

" + line + "

"; //TODO: formattierung + } + if (line.startsWith("changingLines") && currentNode.type != "ChangingLines") { + const changingLines: Node = { type: "ChangingLines"}; + currentNode.child = changingLines; + currentNode = changingLines; + currentNode.value = line + "
"; //TODO: formattierung + } + else { + currentNode.value?.concat(line + "
"); + } + } + + // Bearbeite jede Zeile + //const processedLines = lines.map(line => processLine(line)); + //processedLines.join("\n"); + + return root; +} + +function render(node: Node): string { + + console.log("Render node " + node.type); + + var outputHTML: string = ""; + + // type: "Root" | "Hexagram" | "Judgement" | "Images" | "ChangingLines" ; + switch (node.type) { + case "Root": + return render(node.child!); + //case "Hexagram": + default: + outputHTML = "

" + node.value + "

"; + outputHTML.concat(render(node.child!)); + return outputHTML; + + } + + //return ""; +} + +/* //TODO: process every line... -function processLine(input: string): string { +function processLine(inputLine: string): string { + + if (inputLine.startsWith('Hexagram')) { + return processLineHexagram(inputLine); + } //return input + "\n"; //"

${content}

" - return "

" + input + "

"; + return "

" + inputLine + "

"; } + +function processLineHexagram(inputLine: string): string { + + return "

" + inputLine + "

"; +}*/ \ No newline at end of file