-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathstats.go
More file actions
204 lines (168 loc) · 5.14 KB
/
Copy pathstats.go
File metadata and controls
204 lines (168 loc) · 5.14 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
package xlog
import (
"context"
"fmt"
"io"
"os"
"regexp"
"sort"
"strings"
"github.com/emad-elsaid/xlog/markdown/ast"
)
// GardenStats holds statistics about the digital garden.
type GardenStats struct {
TotalPages int
TotalWords int
AvgWordsPerPage int
TotalLinks int
OrphanedPages int
HubPages []string
}
// Stats displays statistics about the digital garden.
// This is the CLI entry point.
func Stats(ctx context.Context) {
printStats(ctx, os.Stdout)
}
// calculateStats computes statistics across all pages.
// This function is testable as it doesn't perform I/O.
func calculateStats(ctx context.Context) GardenStats {
allPages := Pages(ctx)
totalPages := len(allPages)
if totalPages == 0 {
return GardenStats{}
}
totalWords := 0
linkCounts := make(map[string]int)
incomingLinks := make(map[string]int)
for _, p := range allPages {
select {
case <-ctx.Done():
return GardenStats{}
default:
content := p.Content()
totalWords += countWords(string(content))
// Extract implicit [[page]] links from markdown content using regex
// This approach avoids circular dependencies with extension packages
matches := findPageLinks(string(content), pageLinkPattern)
// Extract explicit markdown links from AST
_, tree := p.AST()
markdownLinks := FindAllInAST[*ast.Link](tree)
internalLinkCount := len(matches)
// Count page link references
for _, pageName := range matches {
incomingLinks[pageName]++
}
// Count explicit markdown links to internal pages
for _, link := range markdownLinks {
dest := string(link.Destination)
// Skip empty, external, and fragment-only links
if dest == "" || isExternalLink(dest) || strings.HasPrefix(dest, "#") {
continue
}
targetPageName := linkToPageName(dest)
incomingLinks[targetPageName]++
internalLinkCount++
}
// Store total outgoing links for this page
linkCounts[p.Name()] = internalLinkCount
}
}
avgWords := 0
if totalPages > 0 {
avgWords = totalWords / totalPages
}
// Calculate total links
totalLinks := 0
for _, count := range linkCounts {
totalLinks += count
}
// Find orphaned pages (no incoming links)
orphanedCount := 0
for _, p := range allPages {
if incomingLinks[p.Name()] == 0 {
orphanedCount++
}
}
// Find hub pages (top 3 pages with most incoming links)
hubPages := findHubPages(incomingLinks, 3)
return GardenStats{
TotalPages: totalPages,
TotalWords: totalWords,
AvgWordsPerPage: avgWords,
TotalLinks: totalLinks,
OrphanedPages: orphanedCount,
HubPages: hubPages,
}
}
// findHubPages returns the top N pages with the most incoming links.
func findHubPages(incomingLinks map[string]int, topN int) []string {
type pageLink struct {
name string
count int
}
var pages []pageLink
for name, count := range incomingLinks {
if count > 0 {
pages = append(pages, pageLink{name, count})
}
}
// Sort by count descending using stdlib sort for O(n log n) performance
sort.Slice(pages, func(i, j int) bool {
return pages[i].count > pages[j].count
})
// Take top N
result := []string{}
limit := topN
if len(pages) < limit {
limit = len(pages)
}
for i := 0; i < limit; i++ {
result = append(result, pages[i].name)
}
return result
}
// findPageLinks extracts page names from [[page]] syntax in markdown content.
func findPageLinks(content, pattern string) []string {
re := regexp.MustCompile(pattern)
matches := re.FindAllStringSubmatch(content, -1)
var pageNames []string
for _, match := range matches {
if len(match) > 1 {
pageNames = append(pageNames, match[1])
}
}
return pageNames
}
// countWords counts the number of words in text.
// Words are sequences of non-whitespace characters.
func countWords(text string) int {
if text == "" {
return 0
}
fields := strings.Fields(text)
return len(fields)
}
// printStats formats and writes statistics to the given writer.
func printStats(ctx context.Context, w io.Writer) {
stats := calculateStats(ctx)
_, _ = fmt.Fprintln(w)
_, _ = fmt.Fprintln(w, "═══════════════════════════════════════")
_, _ = fmt.Fprintln(w, " Digital Garden Statistics")
_, _ = fmt.Fprintln(w, "═══════════════════════════════════════")
_, _ = fmt.Fprintln(w)
_, _ = fmt.Fprintf(w, " Total Pages: %d\n", stats.TotalPages)
_, _ = fmt.Fprintf(w, " Total Words: %d\n", stats.TotalWords)
_, _ = fmt.Fprintf(w, " Average Words per Page: %d\n", stats.AvgWordsPerPage)
_, _ = fmt.Fprintf(w, " Total Links: %d\n", stats.TotalLinks)
_, _ = fmt.Fprintf(w, " Orphaned Pages: %d\n", stats.OrphanedPages)
_, _ = fmt.Fprintln(w)
if len(stats.HubPages) > 0 {
_, _ = fmt.Fprintln(w, " Hub Pages (most connected):")
for i, page := range stats.HubPages {
_, _ = fmt.Fprintf(w, " %d. %s\n", i+1, page)
}
_, _ = fmt.Fprintln(w)
}
_, _ = fmt.Fprintln(w, "═══════════════════════════════════════")
_, _ = fmt.Fprintln(w)
}