diff --git a/ascii-tree-factory/ascii-tree-factory.qml b/ascii-tree-factory/ascii-tree-factory.qml new file mode 100644 index 0000000..672d1ff --- /dev/null +++ b/ascii-tree-factory/ascii-tree-factory.qml @@ -0,0 +1,70 @@ +import QtQml 2.0 +import QOwnNotesTypes 1.0 + +Script { + /** + * Sarà eseguito quando il motore di scripting parte + */ + function init() { + script.registerCustomAction("ascii-tree-factory", "Generate ASCII tree from path"); + } + + function customActionInvoked(identifier) { + switch (identifier) { + case "ascii-tree-factory": { + var selection = script.noteTextEditSelectedText(); + // break selection strings at line ends + var lines = selection.split("\n"); + // initialize tree object + var tree = {}; + //for each line in selection + lines.forEach(function(line){ + // break line at slashes + var path = line.split("/"); + var current = tree; + // for each segment + path.forEach(function(subpath){ + // if the key doesn't have descendants, attach an empty object. + if (!current[subpath]){ + current[subpath] = {}; + } + // else move to the next level + current = current[subpath]; + }); + }); + // uncomment for troubleshooting + // script.log(JSON.stringify(tree)); + + // Start rendering the codeblock with the "graphical" tree + var codeBlockTree = `\`\`\`\n`; + // init an array to keep track if each level is the last one at that depth + var lastLevel = []; + // recursive function to print the tree + function printTree(tree, level){ + lastLevel.push(false); + let keys = Object.keys(tree); + for (var k=0; k < keys.length; k++){ + if (k == keys.length - 1){ + lastLevel[level]=true; + } + // preparing the string that will be printed before the current key + let previousLevelsRendering = ""; + for (var l=0; l `Scripting`. + - Click on `Add script... > Add local script`. + - Select the `ASCII-Tree-Factory.qml` file in the script folder. +3. **Activate the Plugin**: + - Go back to QOwnNotes. + - In the `Scripting` settings, ensure that `ASCII-Tree-Factory.qml` is listed and checked. + +## Usage + +1. **Select Text**: Highlight the text in your note that represents the path structure you want to convert into an ASCII tree. +2. **Run the Plugin**: + - Right-click on the selected text. + - Choose `Custom Actions` > `Generate ASCII tree from path`. + +The plugin will replace the selected text (single or multi-line) with an ASCII tree representation. + +## Example + +### Input + +``` +root/folder1/file1.txt +root/folder2/file2.txt +root/folder2/subfolder1/file3.txt +``` + +### Output + +``` +root + ├─folder1 + │ └─file1.txt + └─folder2 + └─file2.txt + └─subfolder1 + └─file3.txt +``` + +## Contributing + +If you have suggestions or improvements, feel free to fork the repository and submit a pull request. + +## ToDo + +- [ ] Generalize settings such as item separator (default is `/`) +- [ ] Provide aesthetic options to the tree generation +- [ ] Look for libraries that can render good looking and customizable tree structures (any idea???) + +## License + +This project is licensed under the MIT License. See the `LICENSE` file for details. + +--- + +Enjoy using the ASCII Tree Factory plugin to enhance your note-taking experience with QOwnNotes!