Skip to content

Commit

Permalink
Handle page tags (#10)
Browse files Browse the repository at this point in the history
* add tags to page struct

* update README.md
  • Loading branch information
kondoumh authored Aug 21, 2021
1 parent f846831 commit c54d33c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 17 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ If you want replace the output username to something else, create the `<WorkDir>
}
```

IF you want extract tags from links of the page, create the `<WorkDir>/<project name>_tags.csv` file as follows. If the links or related page titles of the page match those words,they will be added as tag attributes.

```
word1,word2,word3
```

To generate graph image as SVG format, specify -m(--image) flag.

```
Expand Down
10 changes: 10 additions & 0 deletions cmd/cmd_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,13 @@ func CheckProject(projectName string) {
os.Exit(1)
}
}

// Contains will check if a string is present in a slice
func Contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
42 changes: 35 additions & 7 deletions cmd/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/mamezou-tech/sbgraph/pkg/file"

Expand All @@ -13,11 +14,12 @@ import (
)

type page struct {
ID string `json:"id"`
Title string `json:"title"`
Author string `json:"author"`
Views int `json:"views"`
Linked int `json:"linked"`
ID string `json:"id"`
Title string `json:"title"`
Author string `json:"author"`
Views int `json:"views"`
Linked int `json:"linked"`
Tags []string `json:"tags"`
}

type user struct {
Expand Down Expand Up @@ -110,15 +112,17 @@ func buildGraph(cmd *cobra.Command) {
graph := createGraph()
var pGraph projectGraph
pNodes := map[string]int{}
tags := readTagsFile(projectName, config.WorkDir)
for _, p := range pages {
gid := graph.AddNode(p.Title)
pNodes[p.ID] = gid
pGraph.Pages = append(pGraph.Pages, page{p.ID, p.Title, p.Author.ID, p.Views, p.Linked})
tags := extractTags(&p,tags)
pGraph.Pages = append(pGraph.Pages, page{p.ID, p.Title, p.Author.ID, p.Views, p.Linked, tags})
}
uNodes := map[string]int{}
if includeUser {
var authors types.Authors
if (file.Exists(projectName + "_authors.json", config.WorkDir)) {
if file.Exists(projectName+"_authors.json", config.WorkDir) {
err := authors.ReadFrom(projectName, config.WorkDir)
CheckErr(err)
}
Expand Down Expand Up @@ -184,6 +188,30 @@ func buildGraph(cmd *cobra.Command) {
}
}

func readTagsFile(projectName string, dir string) []string {
if !file.Exists(projectName+"_tags.csv", dir) {
return []string{}
}
bytes, err := file.ReadBytes(projectName+"_tags.csv", dir)
CheckErr(err)
return strings.Split(string(bytes), ",")
}

func extractTags(page *types.Page, tagLinks []string) []string {
tags := []string{}
for _, l := range page.Related.Links {
if Contains(tagLinks, l.Title) && !Contains(tags, l.Title) {
tags = append(tags, l.Title)
}
}
for _, t := range page.Tags {
if Contains(tagLinks, t) && !Contains(tags, t) {
tags = append(tags, t)
}
}
return tags
}

func createGraph() graphviz.Graph {
graph := graphviz.Graph{}
graph.MakeDirected()
Expand Down
21 changes: 11 additions & 10 deletions pkg/types/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import (

// Page represents a Scrapbox page
type Page struct {
ID string `json:"id"`
Title string `json:"title"`
Created int32 `json:"created"`
Updated int32 `json:"updated"`
Pin int64 `json:"pin"`
Views int `json:"views"`
Linked int `json:"linked"`
Author User `json:"user"`
Collaborators []User `json:"collaborators"`
Image string `json:"image"`
ID string `json:"id"`
Title string `json:"title"`
Created int32 `json:"created"`
Updated int32 `json:"updated"`
Pin int64 `json:"pin"`
Views int `json:"views"`
Linked int `json:"linked"`
Author User `json:"user"`
Collaborators []User `json:"collaborators"`
Image string `json:"image"`
Tags []string `json:"links"`
Related struct {
Links []struct {
ID string `json:"id"`
Expand Down

0 comments on commit c54d33c

Please sign in to comment.