Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draw nodes with at least l edges. #5

Merged
merged 1 commit into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[vimwiki](https://github.com/vimwiki/vimwiki) directory and builds a
graph between the encountered files and their internal references. The
code supports vimwiki-style links `[[link]]`, `[[link|description]]`
and markdown-style links `(description)[link]`. The graph is
and markdown-style links `(description)[link]`. The graph is
converted to the DOT language
using [`dot`](https://github.com/emicklei/dot). The results can then be
visualised with [graphviz](https://www.graphviz.org/about/), e.g. using
Expand All @@ -20,6 +20,11 @@ providing new insights.

`-cluster`: cluster subdirectories as subgraphs

`-l`: only nodes with at least `l` edges are inserted. The inserted nodes are
inserted with all their edges. Thus, nodes with less than `l` edges can appear
when they are connected to other nodes that do satisfy the requirement.
For `-l 0`, all nodes are inserted.

## Examples
To illustrate `/example/` contains some `.wiki` files and also a
diary, `/example/diary/*.wiki`. Running `vimwikigraph` produces the
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func main() {

cluster := flag.Bool("cluster", false, "cluster nodes in sub directories")
diary := flag.Bool("diary", false, "collapse all diary entries under a single `diary.wiki` node")
level := flag.Int("l", 1, "draw only edges from nodes with at least level number of edges")
flag.Parse()

// remap any path that contains `diary` into `diary.wiki`
Expand All @@ -49,7 +50,7 @@ func main() {
}

// convert to a dot-graph for visualisation
g := wiki.Dot(dot.Directed)
g := wiki.Dot(*level, dot.Directed)
g.Attr("rankdir", "LR")
g.Write(os.Stdout)
}
10 changes: 7 additions & 3 deletions vimwikigraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,14 @@ func (wiki *Wiki) Add(path string) error {

// 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(opts ...dot.GraphOption) *dot.Graph {
func (wiki *Wiki) Dot(level int, opts ...dot.GraphOption) *dot.Graph {
graph := dot.NewGraph()
for _, opt := range opts {
opt.Apply(graph)
Expand All @@ -223,8 +227,8 @@ func (wiki *Wiki) Dot(opts ...dot.GraphOption) *dot.Graph {

for k, val := range wiki.graph {

// only draw nodes with connection
if len(val) == 0 {
// skip nodes with less edges
if len(val) < level {
continue
}

Expand Down
39 changes: 39 additions & 0 deletions vimwikigraph_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package main

import (
"fmt"
"os"
"testing"

"github.com/emicklei/dot"
)

type match struct {
Expand Down Expand Up @@ -193,3 +197,38 @@ func TestMatchParseWikiLinks(t *testing.T) {
}
}
}

func TestNodeConnectionLevel(t *testing.T) {
os.Chdir(".")
dir, _ := os.Executable()
t.Log(dir)
wiki := newWiki("example", make(map[string]string), false)

// build 0 < i < 10 entries with each i connections, where the current
// nodes are not considered as connections
wiki.graph = make(map[string][]string)
for i := 0; i < 10; i++ {
k := fmt.Sprintf("%d", i)
wiki.graph[k] = make([]string, 0, 0)
for j := 0; j < i; j++ {
wiki.graph[k] = append(wiki.graph[k], fmt.Sprintf("%d00", j))
}
}

// level zero: 9 entries + 10 connections: 19 nodes in total
// each increment of the level should draw one node less
for l := 0; l < 10; l++ {
g := wiki.Dot(l, dot.Directed)
nconn := len(g.FindNodes())
exp := 19 - l
if nconn != exp {
t.Errorf("For level %v: exp: %v, got %v", l, exp, nconn)
}
}

// higher than maximum connectivity should result in zero nodes
nconn := len(wiki.Dot(10, dot.Directed).FindNodes())
if nconn > 0 {
t.Errorf("Expected 0 connections, got %v", nconn)
}
}