-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvimwikigraph.go
More file actions
307 lines (261 loc) · 6.91 KB
/
vimwikigraph.go
File metadata and controls
307 lines (261 loc) · 6.91 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package main
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/emicklei/dot"
)
const wiki_ext string = ".wiki"
const wikiref string = `\[\[([^\[\]]*)\]\]`
const markdownref string = `\[(.*)\]\((.*)\)`
type Wiki struct {
// Root directory of vimwiki structure
root string
// Connections from a file to its links
graph map[string][]string
// Directories to rename during processing
remap map[string]string
// Enable clustered plotting of files in sub directories
cluster bool
// When any path matches this string, it is ignored in the resulting
// graphs.
ignorePath string
// Contains all regular expressions to match links
wikilink *regexp.Regexp
markdownlink *regexp.Regexp
ignored *regexp.Regexp
}
func newWiki(dir string, remap map[string]string, cluster bool, ignore string) (*Wiki, error) {
wiki := Wiki{
root: dir,
remap: remap,
graph: make(map[string][]string),
ignorePath: ignore,
cluster: cluster,
}
err := wiki.CompileExpressions()
return &wiki, err
}
// Walk walks over all directories in wiki.root except for any directory
// contained in subDirToSkip.
func (wiki *Wiki) Walk(subDirToSkip []string) error {
err := filepath.Walk(wiki.root, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Printf("err %v", err)
return err
}
if info.IsDir() {
for _, s := range subDirToSkip {
if info.Name() == s {
fmt.Fprintf(os.Stderr, "skipping: %v\n", info.Name())
return filepath.SkipDir
}
}
return nil
}
if wiki.IgnorePath(path) {
return nil
}
return wiki.Add(path)
})
return err
}
func (wiki *Wiki) Insert(key, value string) {
// prevent (possibly many) duplicates
if unique(value, wiki.graph[key]) {
wiki.graph[key] = append(wiki.graph[key], value)
}
}
func (wiki *Wiki) Remap(dir, key, match string) (string, string) {
// joins current directory with link
match = filepath.Join(dir, match)
// apply remap naming, diary/file.wiki -> diary.wiki
for k, v := range wiki.remap {
if k == dir {
key = v
}
if strings.Contains(match, k) {
match = v
}
}
return key, match
}
// Compile compiles all regex to match links with
func (wiki *Wiki) CompileExpressions() error {
wikilink, err := regexp.Compile(wikiref)
if err != nil {
return err
}
wiki.wikilink = wikilink
markdownlink, err := regexp.Compile(markdownref)
if err != nil {
return err
}
wiki.markdownlink = markdownlink
if wiki.ignorePath != "" {
ignored, err := regexp.Compile(wiki.ignorePath)
if err != nil {
return err
}
wiki.ignored = ignored
}
return nil
}
// Links returns all links available in text.
func (wiki *Wiki) Links(text string) []string {
// wiki syntax
wikilinks := wiki.WikiLinks(text)
for i, m := range wikilinks {
wikilinks[i] = wiki.ParseWikiLinks(m)
}
// markdown syntax
markdownlinks := wiki.MarkdownLinks(text)
for i, m := range markdownlinks {
link := wiki.ParseMarkdownLinks(m)
if link != "" {
markdownlinks[i] = link
}
}
return append(wikilinks, markdownlinks...)
}
// WikiLinks matches on all vimwiki syntax links in text.
func (wiki *Wiki) WikiLinks(text string) []string {
return wiki.wikilink.FindAllString(text, -1)
}
// MarkdownLinks matches on all markdown syntax links in text.
func (wiki *Wiki) MarkdownLinks(text string) []string {
return wiki.markdownlink.FindAllString(text, -1)
}
// ParseMarkdownLinks extracts the filename from markdown syntax links.
func (wiki *Wiki) ParseMarkdownLinks(link string) string {
idx := strings.Index(link, "(")
link = link[idx:]
link = strings.Trim(link, "()")
ext := filepath.Ext(link)
if ext == ".md" || ext == ".wiki" {
return link
}
// assume it refers to a local markdown file
if ext == "" {
return link + ".md"
}
// if ext is anything else, we should probably skip the file
return ""
}
// ParseWikiLinks extracts the filename from vimwiki syntax links.
func (wiki *Wiki) ParseWikiLinks(link string) string {
// [[file]] -> dir/file.wiki
link = strings.Trim(link, "[]")
// split of description [[link|description]]
idx := strings.Index(link, "|")
if idx > 0 {
link = link[:idx]
}
ext := filepath.Ext(link)
if ext != ".md" && ext != ".wiki" {
link += ".wiki"
}
return link
}
func (wiki *Wiki) IgnorePath(path string) bool {
// When no regexes are provided to be ignored, always accpet the files
if wiki.ignored == nil {
return false
}
// Otherwise, return true if any match with the given regex is observed,
// in that case the link should not be added to the graph
return wiki.ignored.Match([]byte(path))
}
// Add adds path to the wiki.graph when it contains links to other files.
//
// Only the relative paths are considered between the passed path and wiki.root.
func (wiki *Wiki) Add(path string) error {
key, err := filepath.Rel(wiki.root, path)
if err != nil {
return err
}
dir := filepath.Dir(key) // current dir when in subdirectory
// initialise a node
if _, ok := wiki.graph[key]; !ok {
wiki.graph[key] = make([]string, 0)
}
// open file to find links
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
for _, link := range wiki.Links(scanner.Text()) {
// do not insert links to ignored paths
if wiki.IgnorePath(link) {
continue
}
// rename and/or collapse folders
key, link = wiki.Remap(dir, key, link)
// insert into the graph
wiki.Insert(key, link)
}
}
return scanner.Err()
}
// Dot converts wiki.graph into dot.Graph.
//
// Only nodes, and their connections, are drawn if their sum of edges
// is greater than the provided level. For `level = 0` all nodes
// are inserted.
//
// If wiki.cluster == true any nodes that correspond to a subdirectory are
// inserted in the corresponding subgraph of that subdirectory. By default, the
// visualisation will highlight these subgraphs.
func (wiki *Wiki) Dot(level int, opts ...dot.GraphOption) *dot.Graph {
graph := dot.NewGraph()
for _, opt := range opts {
opt.Apply(graph)
}
var a, b dot.Node
for k, val := range wiki.graph {
// skip nodes with less edges
if len(val) < level {
continue
}
// insert in subgraph if wiki and in subdirectory
// FIXME move into func?
dir, _ := filepath.Split(k)
if wiki.cluster && dir != "" {
subgraph := graph.Subgraph(dir, dot.ClusterOption{})
a = subgraph.Node(k)
} else {
a = graph.Node(k)
}
for _, v := range val {
// insert in subgraph if wiki and in subdirectory
dir, _ := filepath.Split(v)
if wiki.cluster && dir != "" {
subgraph := graph.Subgraph(dir, dot.ClusterOption{})
b = subgraph.Node(v)
} else {
b = graph.Node(v)
}
// only insert unique edges
if len(graph.FindEdges(a, b)) == 0 {
graph.Edge(a, b)
}
}
}
return graph
}
// unique returns true when s is not present in values
func unique(s string, vals []string) bool {
for _, v := range vals {
if s == v {
return false
}
}
return true
}