Skip to content

Commit

Permalink
Added Ascii Tree Factory (#243)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
77nnit authored Oct 31, 2024
1 parent 1645cd9 commit 8480b42
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
70 changes: 70 additions & 0 deletions ascii-tree-factory/ascii-tree-factory.qml
Original file line number Diff line number Diff line change
@@ -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<level; l++){
// for each previous level print a "│ " if it's not the last key at that level
previousLevelsRendering += lastLevel[l] ? " " : "";
}
// put together the string to be printed accounting for first level and last key at that level
codeBlockTree += `${(level==0) ? keys[k] : previousLevelsRendering +(lastLevel[level]?"└─" :"├─" )+keys[k]}\n`;
printTree(tree[keys[k]], level + 1);
}
}
// calling the recursive function
printTree(tree, 0);
// closing the codeblock
codeBlockTree += `\`\`\``;
script.noteTextEditWrite(codeBlockTree);
break;
}
}
}
}
10 changes: 10 additions & 0 deletions ascii-tree-factory/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "ASCII Tree Factory",
"identifier": "ascii-tree-factory",
"script": "ascii-tree-factory.qml",
"authors": ["@77nnit"],
"platforms": ["windows"],
"version": "0.1.0",
"minAppVersion": "24.10.5",
"description" : "This script converts forward-slashes-separated lines to an ASCII tree representation, replacing the selected text."
}
65 changes: 65 additions & 0 deletions ascii-tree-factory/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

## Overview

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.

## Manual Installation

1. **Download the Plugin**: Save the `ASCII-Tree-Factory.qml` file to your local machine.
2. **Add to QOwnNotes**:
- Open QOwnNotes.
- Navigate to `Settings` > `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!

0 comments on commit 8480b42

Please sign in to comment.