-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathmarkdown_writer.go
More file actions
107 lines (93 loc) · 2.62 KB
/
markdown_writer.go
File metadata and controls
107 lines (93 loc) · 2.62 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
package main
import (
"fmt"
"log"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/mmcdole/gofeed"
)
type MarkdownWriter struct {
FilePath string
}
func NewMarkdownWriter(cfg *Config, dateStr string) *MarkdownWriter {
if dateStr == "" {
dateStr = time.Now().Format("2006-01-02")
}
fname := cfg.MarkdownFilePrefix + dateStr + cfg.MarkdownFileSuffix + ".md"
return &MarkdownWriter{
FilePath: filepath.Join(cfg.MarkdownDirPath, fname),
}
}
func (w MarkdownWriter) WriteFeedHeaderRaw(title, articleURL string) string {
// Logic to reconstruct the Favicon HTML without a gofeed object
var src string
if strings.Contains(title, "Hacker News") {
src = "https://news.ycombinator.com/favicon.ico"
} else if articleURL == "" {
return fmt.Sprintf("\n### 🍵 %s\n", title)
} else {
u, err := url.Parse(articleURL)
if err != nil {
return fmt.Sprintf("\n### 🍵 %s\n", title)
}
// Google S2 Favicon service
src = "https://www.google.com/s2/favicons?sz=32&domain=" + u.Hostname()
}
faviconImg := fmt.Sprintf(`<img src="%s" width="32" height="32" />`, src)
return fmt.Sprintf("\n### %s %s\n", faviconImg, title)
}
func (w MarkdownWriter) Write(body string) {
f, err := os.OpenFile(w.FilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Printf("Error opening file %s: %v", w.FilePath, err)
return
}
defer f.Close()
if _, err := f.Write([]byte(body)); err != nil {
log.Printf("Error writing to file: %v", err)
}
}
func (w MarkdownWriter) WriteLink(title string, url string, newLine bool, readingTime string) string {
content := fmt.Sprintf("[%s](%s)", title, url)
if readingTime != "" {
content += fmt.Sprintf(" (%s)", readingTime)
}
if newLine {
content += " \n"
}
return content
}
func (w MarkdownWriter) WriteSummary(content string, newLine bool) string {
if content == "" {
return ""
}
if newLine {
content += " \n\n"
}
return content
}
func (w MarkdownWriter) WriteHeader(feed *gofeed.Feed) string {
favicon := w.getFaviconHTML(feed)
return fmt.Sprintf("\n### %s %s\n", favicon, feed.Title)
}
// Helper method specifically for MarkdownWriter
func (w MarkdownWriter) getFaviconHTML(s *gofeed.Feed) string {
var src string
// Hacker news is a special case
if strings.Contains(s.Title, "Hacker News") {
src = "https://news.ycombinator.com/favicon.ico"
} else if s.FeedLink == "" {
return "🍵"
} else {
u, err := url.Parse(s.FeedLink)
if err != nil {
// If URL parsing fails, just return emoji
return "🍵"
}
src = "https://www.google.com/s2/favicons?sz=32&domain=" + u.Hostname()
}
return fmt.Sprintf(`<img src="%s" width="32" height="32" />`, src)
}