-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormatter.ahk
180 lines (162 loc) · 4.4 KB
/
Formatter.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/******************************************
* @file Formatter.ahk
* @description: A class to format data to different formats
* @author hsayed
* @date 2025/01/25
* @version 1.0.0
*******************************************/
class Formatter
{
__New(data?)
{
this.data := IsSet(data) ? data : ""
}
GetKeys(_map)
{
keys := []
for key, value in _map
{
keys.Push(key)
}
return keys
}
ToTreeMap(data, indent := "", isRoot := true) {
static branch := "├── ", indentSymbol := "│ ", end := "└── "
result := ""
keys := this.GetKeys(data) ; Get all keys (which are folder names or file names)
itemCount := keys.Length ; Get the number of items at the current level
current := 0
for index, key in keys {
current++
isLastItem := (current = itemCount)
; Determine the next level of indentation based on whether this is the last item
nextIndent := isRoot ? "" : (isLastItem ? indent . " " : indent . indentSymbol)
; Determine the correct prefix to use
prefix := (isRoot && !isLastItem) ? branch : (isRoot ? end : (isLastItem ? end : branch))
; If it's the root level and the first item, don't add extra indentation
if (!isRoot || current > 1)
result .= indent
if (isRoot)
result .= key . "`n"
else
result .= prefix . key . "`n"
if (type(data[key]) = "Map") ; Check if the current item is a Map (directory)
result .= this.ToTreeMap(data[key], nextIndent, false)
}
return result
}
ToMarkdownTaskList(withNumbers := false)
{
result := ""
if (this.data = "")
return ""
if (this.data is Array)
{
for each, item in this.data
{
result .= "- [ ] " (withNumbers ? each "_" item : item) "`n"
}
}
else
{
result := "- [ ] " this.data "`n"
}
return result
}
ToMarkdownList(withNumbers := false)
{
result := ""
if (this.data = "")
return ""
if (this.data is Array)
{
for each, item in this.data
{
result .= "- " (withNumbers ? each "_" item : item) "`n"
}
}
else
{
result := "- " this.data "`n"
}
return result
}
ToMarkdownCode(lang := "###", withNumbers := false)
{
result := ""
if (this.data = "")
return ""
if (this.data is Array)
{
for each, item in this.data
{
result .= (withNumbers ? each "_" item : item) "`n``````" lang "`n`n```````n"
}
}
else
{
result .= (withNumbers ? each "_" item : item) "`n``````" lang "`n`n```````n"
}
return result
}
}
; Example Usage:
ExampleUsage() {
; Example 1: Tree Map
folders := Map(
"Root", Map(
"Folder1", Map(
"SubFolder1", Map(),
"SubFolder2", Map()
),
"Folder2", Map(
"SubFolder3", Map()
)
)
)
treeFormatter := Formatter()
MsgBox(treeFormatter.ToTreeMap(folders))
/*
Output:
Root
├── Folder1
│ ├── SubFolder1
│ └── SubFolder2
└── Folder2
└── SubFolder3
*/
; Example 2: Markdown Task List
tasks := ["Buy groceries", "Do laundry", "Write code"]
taskFormatter := Formatter(tasks)
MsgBox(taskFormatter.ToMarkdownTaskList())
/*
Output:
- [ ] Buy groceries
- [ ] Do laundry
- [ ] Write code
*/
; Example 3: Markdown List
items := ["Apple", "Banana", "Orange"]
listFormatter := Formatter(items)
MsgBox(listFormatter.ToMarkdownList(true))
/*
Output:
- 1_Apple
- 2_Banana
- 3_Orange
*/
; Example 4: Markdown Code
codeSnippets := ["console.log('Hello')", "console.log('World')"]
codeFormatter := Formatter(codeSnippets)
MsgBox(codeFormatter.ToMarkdownCode("javascript"))
/*
Output:
console.log('Hello')
```javascript
```
console.log('World')
```javascript
```
*/
}
ExampleUsage()