From 07091eb708f324b918a819fda40da6d28df4b7c3 Mon Sep 17 00:00:00 2001 From: juan Date: Tue, 22 Jul 2025 23:12:34 -0400 Subject: [PATCH] data structure for augment capabilities --- lib/structs.dart | 100 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 97 insertions(+), 3 deletions(-) diff --git a/lib/structs.dart b/lib/structs.dart index e99c0d2..6f0f0e7 100644 --- a/lib/structs.dart +++ b/lib/structs.dart @@ -1,6 +1,7 @@ //data structures import 'dart:typed_data'; +import 'package:markdown/markdown.dart' as md; class GetThreadResponse { final int id; @@ -157,6 +158,94 @@ class AugmentTree { AugmentTree? parent; String ogTag = ''; String numbering = ''; + Map hirarchyDict = { + "h1": 1, + "h2": 2, + "h3": 3, + "h4": 4, + "h5": 5, + "h6": 6, + "p": 8, + "ul": 8, + "li": 8, + }; + + AugmentTree(); + + AugmentTree.fromMD(String rawMD) { + //makes raw MD into an augmentTree + print("started markdown2tree"); + final List nakedList = md.Document().parseLines(rawMD.split( + '\n')); //emails md is the index of the email in the thread, since this only handles one thus it shall be removed + // AugmentTree zoomTreeRoot = AugmentTree(); + for (var node in nakedList) { + //maybe do an add function, but isn't this it? + if (node is md.Element) { + AugmentTree temp = AugmentTree(); + temp.data = node.textContent; + temp.ogTag = node.tag; + if (hirarchyDict.containsKey(node.tag)) { + // make this O(1) + _add2Tree(this, node); + // print(node); + } + } + } + this.addNumbering(); + } + + void _add2Tree(AugmentTree tree, md.Element node2add) { + // adds node to its corresponding place + AugmentTree newNode = AugmentTree(); + newNode.setData(node2add.textContent); + newNode.ogTag = node2add.tag; + // cases, + //1. a node that comes is lower than the root.children last, if so it goes beneath it + if (tree.children.isEmpty) { + // new level to be created when totally empty + tree.children.add(newNode); + newNode.parent = tree; + } else if (tree.children.isNotEmpty && + tree.children.last.ogTag.isNotEmpty) { + if ((hirarchyDict[node2add.tag] ?? + -1) < // e.g. new node is h1 and old is h2, heapify + (hirarchyDict[tree.children.last.ogTag] ?? -1)) { + //have to figure out the borthers + //assuming it all goes right + if ((hirarchyDict[node2add.tag] ?? -1) == -1 || + (hirarchyDict[tree.children.last.ogTag] ?? -1) == -1) { + print( + 'failed and got -1 at _add2Tree \n ${hirarchyDict[node2add.tag] ?? -1} < ${hirarchyDict[tree.children.last.ogTag] ?? -1}'); + return; + } else if (tree.children.last.parent == null) { + // becomes the new top level + for (AugmentTree brother in tree.children) { + brother.parent = newNode; + } + tree.children = [newNode]; + } else { + newNode.parent = tree; + tree.children.add(newNode); + } + } else if ((hirarchyDict[node2add.tag] ?? + -1) > // go down e.g. new node is h3 and old is h2 or something + (hirarchyDict[tree.children.last.ogTag] ?? -1)) { + if ((hirarchyDict[node2add.tag] ?? -1) == -1 || + (hirarchyDict[tree.children.last.ogTag] ?? -1) == -1) { + print( + 'failed and got -1 at _add2Tree \n ${hirarchyDict[node2add.tag] ?? -1} > ${hirarchyDict[tree.children.last.ogTag] ?? -1}'); + print("-1 ${tree.children.last.ogTag}"); + return; + } + + _add2Tree(tree.children.last, node2add); + } else if ((hirarchyDict[node2add.tag] ?? -1) == + (hirarchyDict[tree.children.last.ogTag] ?? -1)) { + tree.children.add(newNode); + newNode.parent = tree; + } + } + } void setData(String data) { this.data = data; @@ -176,7 +265,7 @@ class AugmentTree { parentIsLettered = false; } else { parentIsLettered = prefix.runes.last >= 'a'.runes.first && - prefix.runes.last <= 'z'.runes.first; + prefix.runes.last <= 'z'.runes.first; } if (prefix.isEmpty) { @@ -197,14 +286,19 @@ class AugmentTree { } } +//perhaps make a struct that builds augment tree, since its so complex and needs to be like recursive -class MarkdownParsed{ +class MarkdownParsed { + //struct for holding the MD given in endpoint //not used final String text; MarkdownParsed({required this.text}); - factory MarkdownParsed.fromJson(Map json){ + factory MarkdownParsed.fromJson(Map json) { return MarkdownParsed( text: json['md'] ?? '', ); } } +//should make an md to tree class/struct + +// make a for loop of rows with markdown