-
Notifications
You must be signed in to change notification settings - Fork 0
/
findstr2org.groovy
105 lines (98 loc) · 3.03 KB
/
findstr2org.groovy
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
// def submoduleList = "git ls-files --stage | grep 160000".execute().text
def allFiles = new ConfigObject()
def crlf = System.getProperty('line.separator');
String fs = System.getProperty('file.separator')
def readFiles;
readFiles = {
ConfigObject cfg
}
def debug(String text) {
// println "#D> $text"
}
System.in.readLines().each {
line ->
(line =~ /^(.*?):(.*)/).each {
lineMatch ->
def pathName = lineMatch[1];
def text = lineMatch[2]
def path = pathName.split(/(\\|\/)/)
// debug "pathName = $pathName"
// debug "text = $text"
// debug "path = $path"
//debug path
def cfg = path.inject(allFiles) {
root, dirname ->
// debug ">> $root, $dirname"
root.dir."$dirname"
}
def textLines = (cfg.textLines?:[])
textLines << text
cfg.textLines = textLines;
//debug cfg
}
}
//debug allFiles
String getProtocol(String fileName) {
def ext = fileName.replaceAll(/.*\./,'').toLowerCase();
// println "# ext = $ext"
def extList = ['html': 'file+emacs',
'pdf': 'file+sys',
'org': 'file',
//'properties': 'file+emacs',
'service': 'file+emacs',]
String protocol = extList.containsKey(ext)?extList[ext]:'file'
//println "# protocol=$protocol"
return protocol
};
String getEditLink(String path, String fileName) {
String protocol = getProtocol(fileName);
String link = "";
(protocol =~ /(.*)\+(.*)/).each {
m ->
if (m[2] == 'emacs') {
link += " ([[file+sys:$path$fileName][start]])"
}
}
return link;
}
def printDir(ConfigObject cfg, int level, String dirName, String path) {
debug "# in printDir(${cfg.keySet()}, $level, $dirName, $path)"
// debug "cfg=$cfg"
debug "cfg.dir.keySet() == ${cfg.dir.keySet()}"
debug "cfg.textLines == ${cfg.textLines}"
// This if statement consolidates empty directories
if (cfg.textLines.size() || cfg.dir.keySet().size() > 1) {
println "${'*' * level} $dirName"
cfg.textLines.each {
def search = "::/${it.replaceAll(/(?i)[^a-z0-9 ]/, '.')}/"
//println "path=$path"
String filePath = "$path".replaceAll(/[\/\\]$/, '');
println "- [[${getProtocol(dirName)}:$filePath$search][Edit]]${getEditLink('',filePath)}: $it"
}
debug "# cfg.dir=${cfg.dir.keySet()}"
cfg.dir.keySet().each {
dir ->
debug "# dir=$dir"
def dd = cfg.dir."$dir"
debug "dd=$dd"
String fs = System.getProperty('file.separator')
printDir(cfg.dir."$dir", level + 1, dir, "$path$dir$fs")
}
} else {
String fs = System.getProperty('file.separator')
cfg.dir.each {
subDirName, dirContents ->
printDir(dirContents, level, "$dirName$fs$subDirName",
"$path$subDirName$fs")
}
}
}
// #+STARTUP: showeverything
def printHeader = {
println """#+TITLE: Working Files
* Config :noexport:
#+OPTIONS: toc:nil num:nil html-preamble:nil html-postamble:nil ^:{}
""".replaceAll("\r?\n", System.getProperty('line.separator'))
}
printHeader()
printDir(allFiles, 1, "Files", "");