Browse Source

including counts of how many pages media is referenced on

Stephen Searles 1 year ago
parent
commit
e75784599e
1 changed files with 47 additions and 2 deletions
  1. 47
    2
      media.go

+ 47
- 2
media.go View File

@@ -1,13 +1,48 @@
1 1
 package caddyhugo
2 2
 
3 3
 import (
4
+	"bytes"
4 5
 	"fmt"
5 6
 	"io"
6 7
 	"net/http"
8
+	"net/url"
9
+	"path"
10
+
11
+	"github.com/PuerkitoBio/goquery"
12
+	"github.com/gohugoio/hugo/hugolib"
7 13
 
8 14
 	"git.stephensearles.com/stephen/caddy-hugo2/media"
9 15
 )
10 16
 
17
+func (ch *CaddyHugo) ReferencedMedia() map[string]map[*hugolib.Page]struct{} {
18
+	found := map[string]map[*hugolib.Page]struct{}{}
19
+
20
+	for _, page := range ch.HugoSites.Pages() {
21
+		r := bytes.NewBufferString(string(page.Render()))
22
+		doc, err := goquery.NewDocumentFromReader(r)
23
+		if err != nil {
24
+			continue
25
+		}
26
+		doc.Find("img,video").Map(func(i int, s *goquery.Selection) string {
27
+			u, ok := s.Attr("src")
28
+			if ok {
29
+				u = path.Base(u)
30
+				if ud, err := url.QueryUnescape(u); err == nil {
31
+					u = ud
32
+				}
33
+				if m := found[u]; m == nil {
34
+					found[u] = make(map[*hugolib.Page]struct{})
35
+				}
36
+				found[u][page] = struct{}{}
37
+			}
38
+			return ""
39
+		})
40
+
41
+	}
42
+
43
+	return found
44
+}
45
+
11 46
 func (ch *CaddyHugo) uploadMedia(w http.ResponseWriter, r *http.Request) (int, error) {
12 47
 	if ch.Media == nil {
13 48
 		http.NotFound(w, r)
@@ -49,6 +84,8 @@ func (ch *CaddyHugo) serveMediaPage(w http.ResponseWriter, r *http.Request) (int
49 84
 		return 404, nil
50 85
 	}
51 86
 
87
+	referenced := ch.ReferencedMedia()
88
+
52 89
 	io.WriteString(w, `<html>
53 90
 	<head><style>
54 91
 	iframe { height: 100%; }
@@ -87,12 +124,20 @@ func (ch *CaddyHugo) serveMediaPage(w http.ResponseWriter, r *http.Request) (int
87 124
 				fmt.Fprintf(w, `<div class="img">error rendering %q: %v</div>`, m.Name, err)
88 125
 				continue
89 126
 			}
127
+
128
+			refs := len(referenced[m.Name])
129
+			plural := "s"
130
+			if refs == 1 {
131
+				plural = ""
132
+			}
133
+			refLine := fmt.Sprintf("included on %d page%s", refs, plural)
134
+
90 135
 			switch m.Type {
91 136
 			case media.TypeImage:
92
-				fmt.Fprintf(w, `<div class="img"><img width=%d height=%d src=%q data-filename=%q /><br /><input type="text" readonly value=%q /><span class="copy">&#x1F4CB;</span></div>`, size.Dx(), size.Dy(), m.ThumbPath(size), m.Name, m.ThumbPath(size))
137
+				fmt.Fprintf(w, `<div class="img"><img width=%d height=%d src=%q data-filename=%q /><br /><input type="text" readonly value=%q /><span class="copy">&#x1F4CB;</span><br />%s</div>`, size.Dx(), size.Dy(), m.ThumbPath(size), m.Name, m.ThumbPath(size), refLine)
93 138
 			case media.TypeVideo:
94 139
 				// TODO: onmouseover sucks for mobile
95
-				fmt.Fprintf(w, `<div class="img"><video width=%d height=%d src=%q data-filename=%q onmouseover="this.play()" onmouseout="this.pause();this.currentTime=0;"></video><br /><input type="text" readonly value=%q /><span class="copy">&#x1F4CB;</span></div>`, size.Dx(), size.Dy(), m.ThumbPath(size), m.Name, m.ThumbPath(size))
140
+				fmt.Fprintf(w, `<div class="img"><video width=%d height=%d src=%q data-filename=%q onmouseover="this.play()" onmouseout="this.pause();this.currentTime=0;"></video><br /><input type="text" readonly value=%q /><span class="copy">&#x1F4CB;</span><br />%s</div>`, size.Dx(), size.Dy(), m.ThumbPath(size), m.Name, m.ThumbPath(size), refLine)
96 141
 			}
97 142
 		}
98 143
 	}

Loading…
Cancel
Save