Skip to content

Commit 8480b42

Browse files
authored
Added Ascii Tree Factory (#243)
* Added Ascii Tree Factory * Update readme.md the `l` got lost in the `.qml` extension... * Added ToDo section to readme file * Update readme.md * Update info.json Build was failing for inconsistent identifier * Update info.json * Update and rename ASCII-Tree-Factory.qml to ascii-tree-factory.qml * Update info.json Removed Linux and MacOs as not tested * Update ascii-tree-factory.qml regression bug due to changing identifier * Update info.json
1 parent 1645cd9 commit 8480b42

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import QtQml 2.0
2+
import QOwnNotesTypes 1.0
3+
4+
Script {
5+
/**
6+
* Sarà eseguito quando il motore di scripting parte
7+
*/
8+
function init() {
9+
script.registerCustomAction("ascii-tree-factory", "Generate ASCII tree from path");
10+
}
11+
12+
function customActionInvoked(identifier) {
13+
switch (identifier) {
14+
case "ascii-tree-factory": {
15+
var selection = script.noteTextEditSelectedText();
16+
// break selection strings at line ends
17+
var lines = selection.split("\n");
18+
// initialize tree object
19+
var tree = {};
20+
//for each line in selection
21+
lines.forEach(function(line){
22+
// break line at slashes
23+
var path = line.split("/");
24+
var current = tree;
25+
// for each segment
26+
path.forEach(function(subpath){
27+
// if the key doesn't have descendants, attach an empty object.
28+
if (!current[subpath]){
29+
current[subpath] = {};
30+
}
31+
// else move to the next level
32+
current = current[subpath];
33+
});
34+
});
35+
// uncomment for troubleshooting
36+
// script.log(JSON.stringify(tree));
37+
38+
// Start rendering the codeblock with the "graphical" tree
39+
var codeBlockTree = `\`\`\`\n`;
40+
// init an array to keep track if each level is the last one at that depth
41+
var lastLevel = [];
42+
// recursive function to print the tree
43+
function printTree(tree, level){
44+
lastLevel.push(false);
45+
let keys = Object.keys(tree);
46+
for (var k=0; k < keys.length; k++){
47+
if (k == keys.length - 1){
48+
lastLevel[level]=true;
49+
}
50+
// preparing the string that will be printed before the current key
51+
let previousLevelsRendering = "";
52+
for (var l=0; l<level; l++){
53+
// for each previous level print a "│ " if it's not the last key at that level
54+
previousLevelsRendering += lastLevel[l] ? " " : "";
55+
}
56+
// put together the string to be printed accounting for first level and last key at that level
57+
codeBlockTree += `${(level==0) ? keys[k] : previousLevelsRendering +(lastLevel[level]?"└─" :"├─" )+keys[k]}\n`;
58+
printTree(tree[keys[k]], level + 1);
59+
}
60+
}
61+
// calling the recursive function
62+
printTree(tree, 0);
63+
// closing the codeblock
64+
codeBlockTree += `\`\`\``;
65+
script.noteTextEditWrite(codeBlockTree);
66+
break;
67+
}
68+
}
69+
}
70+
}

ascii-tree-factory/info.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "ASCII Tree Factory",
3+
"identifier": "ascii-tree-factory",
4+
"script": "ascii-tree-factory.qml",
5+
"authors": ["@77nnit"],
6+
"platforms": ["windows"],
7+
"version": "0.1.0",
8+
"minAppVersion": "24.10.5",
9+
"description" : "This script converts forward-slashes-separated lines to an ASCII tree representation, replacing the selected text."
10+
}

ascii-tree-factory/readme.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
## Overview
3+
4+
The ASCII Tree Factory plugin for QOwnNotes allows users to generate an ASCII tree representation from a given path structure. This can be particularly useful for visualizing directory structures or any hierarchical data within your notes.
5+
6+
## Manual Installation
7+
8+
1. **Download the Plugin**: Save the `ASCII-Tree-Factory.qml` file to your local machine.
9+
2. **Add to QOwnNotes**:
10+
- Open QOwnNotes.
11+
- Navigate to `Settings` > `Scripting`.
12+
- Click on `Add script... > Add local script`.
13+
- Select the `ASCII-Tree-Factory.qml` file in the script folder.
14+
3. **Activate the Plugin**:
15+
- Go back to QOwnNotes.
16+
- In the `Scripting` settings, ensure that `ASCII-Tree-Factory.qml` is listed and checked.
17+
18+
## Usage
19+
20+
1. **Select Text**: Highlight the text in your note that represents the path structure you want to convert into an ASCII tree.
21+
2. **Run the Plugin**:
22+
- Right-click on the selected text.
23+
- Choose `Custom Actions` > `Generate ASCII tree from path`.
24+
25+
The plugin will replace the selected text (single or multi-line) with an ASCII tree representation.
26+
27+
## Example
28+
29+
### Input
30+
31+
```
32+
root/folder1/file1.txt
33+
root/folder2/file2.txt
34+
root/folder2/subfolder1/file3.txt
35+
```
36+
37+
### Output
38+
39+
```
40+
root
41+
├─folder1
42+
│ └─file1.txt
43+
└─folder2
44+
└─file2.txt
45+
└─subfolder1
46+
└─file3.txt
47+
```
48+
49+
## Contributing
50+
51+
If you have suggestions or improvements, feel free to fork the repository and submit a pull request.
52+
53+
## ToDo
54+
55+
- [ ] Generalize settings such as item separator (default is `/`)
56+
- [ ] Provide aesthetic options to the tree generation
57+
- [ ] Look for libraries that can render good looking and customizable tree structures (any idea???)
58+
59+
## License
60+
61+
This project is licensed under the MIT License. See the `LICENSE` file for details.
62+
63+
---
64+
65+
Enjoy using the ASCII Tree Factory plugin to enhance your note-taking experience with QOwnNotes!

0 commit comments

Comments
 (0)