Add feed generating capabilities #1
| @ -5,6 +5,7 @@ | |||||||
|     <meta charset="UTF-8"> |     <meta charset="UTF-8"> | ||||||
|     <link rel="icon" href="/favicon.ico"> |     <link rel="icon" href="/favicon.ico"> | ||||||
|     <link rel="stylesheet" href="blog.css"> |     <link rel="stylesheet" href="blog.css"> | ||||||
|  |     <link rel="alternative" type="application/rss+xml" href="blog.xml"> | ||||||
| 	</head> | 	</head> | ||||||
| 	<body> | 	<body> | ||||||
|     <table> |     <table> | ||||||
|  | |||||||
							
								
								
									
										19
									
								
								feed.tmpl.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								feed.tmpl.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,19 @@ | |||||||
|  | <?xml version="1.0" encoding="utf-8"?> | ||||||
|  | <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> | ||||||
|  |   <channel> | ||||||
|  |     <title>tilde.town blog</title> | ||||||
|  |     <description>web log of tilde town</description> | ||||||
|  |     <link>https://tilde.town/blog.html</link> | ||||||
|  |     <atom:link rel="self" type="application/rss+xml" href="https://tilde.town/blog.xml"/> | ||||||
|  |     {{ range .News }} | ||||||
|  |     <item> | ||||||
|  |       <title>{{.Title}}</title> | ||||||
|  |       <pubDate>{{.Pubdate}}</pubDate> | ||||||
|  |       <description> | ||||||
|  |         <![CDATA[{{.Content}}]]> | ||||||
|  |       </description> | ||||||
|  |       <guid isPermalink="false">{{.Pubdate}}-{{.Title}}</guid> | ||||||
|  |     </item> | ||||||
|  |     {{ end }} | ||||||
|  |   </channel> | ||||||
|  | </rss> | ||||||
| @ -6,5 +6,6 @@ set -e | |||||||
| 
 | 
 | ||||||
| cd /town/src/tilde.town | cd /town/src/tilde.town | ||||||
| /usr/bin/go run genblog.go > blog.html | /usr/bin/go run genblog.go > blog.html | ||||||
|  | /usr/bin/go run genfeed.go > blog.xml | ||||||
| /usr/bin/go run genusers.go > users.html | /usr/bin/go run genusers.go > users.html | ||||||
| /bin/cp index.html blog.html users.html blog.css style.css /var/www/tilde.town/ | /bin/cp index.html blog.html blog.xml users.html blog.css style.css /var/www/tilde.town/ | ||||||
|  | |||||||
							
								
								
									
										83
									
								
								genfeed.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								genfeed.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,83 @@ | |||||||
|  | package main | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"bytes" | ||||||
|  | 	_ "embed" | ||||||
|  | 	"encoding/json" | ||||||
|  | 	"fmt" | ||||||
|  | 	"os" | ||||||
|  | 	"os/exec" | ||||||
|  | 	"text/template" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | const statsPath = "/usr/local/bin/stats" | ||||||
|  | 
 | ||||||
|  | //go:embed feed.tmpl.xml | ||||||
|  | var feedTmpl string | ||||||
|  | 
 | ||||||
|  | type newsEntry struct { | ||||||
|  | 	Title   string // Title of entry | ||||||
|  | 	Pubdate string // Human readable date | ||||||
|  | 	Content string // HTML of entry | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type tildeData struct { | ||||||
|  | 	News        []newsEntry | ||||||
| 
					
					acdw marked this conversation as resolved
					
				 | |||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func _main() error { | ||||||
|  | 	data, err := stats() | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
| 
					
					acdw marked this conversation as resolved
					
				 
				
					
						vilmibm
						commented  can delete can delete | |||||||
|  | 
 | ||||||
| 
					
					acdw marked this conversation as resolved
					
				 
				
					
						vilmibm
						commented  can delete can delete | |||||||
|  | 	type tmplData struct { | ||||||
|  | 		News   []newsEntry | ||||||
|  | 	} | ||||||
| 
					
					acdw marked this conversation as resolved
					
				 
				
					
						vilmibm
						commented  can delete can delete | |||||||
|  | 
 | ||||||
|  | 	td := &tmplData{ | ||||||
| 
					
					acdw marked this conversation as resolved
					
				 
				
					
						vilmibm
						commented  can delete these three functions can delete these three functions | |||||||
|  | 		News:   data.News, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	t, err := template.New("feed").Parse(feedTmpl) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return fmt.Errorf("failed to parse the feed template: %w", err) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	out := bytes.Buffer{} | ||||||
|  | 	if err = t.Execute(&out, td); err != nil { | ||||||
|  | 		return fmt.Errorf("failed to render feed template: %w", err) | ||||||
|  | 	} | ||||||
| 
					
					acdw marked this conversation as resolved
					
				 
				
					
						vilmibm
						commented  can delete can delete | |||||||
|  | 
 | ||||||
|  | 	fmt.Println(out.String()) | ||||||
|  | 
 | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
| 
					
					acdw marked this conversation as resolved
					
				 
				
					
						vilmibm
						commented  can delete can delete | |||||||
|  | 
 | ||||||
|  | func stats() (*tildeData, error) { | ||||||
|  | 	sout := bytes.Buffer{} | ||||||
| 
					
					acdw marked this conversation as resolved
					
				 
				
					
						vilmibm
						commented  can delete can delete | |||||||
|  | 	cmd := exec.Command(statsPath) | ||||||
|  | 	cmd.Stdout = &sout | ||||||
| 
					
					acdw marked this conversation as resolved
					
				 
				
					
						vilmibm
						commented  can delete this func can delete this func | |||||||
|  | 
 | ||||||
|  | 	err := cmd.Run() | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	var data tildeData | ||||||
|  | 
 | ||||||
|  | 	err = json.Unmarshal(sout.Bytes(), &data) | ||||||
|  | 	if err != nil { | ||||||
| 
					
					acdw marked this conversation as resolved
					
				 
				
					
						vilmibm
						commented  can delete this for loop can delete this for loop | |||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return &data, nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func main() { | ||||||
|  | 	err := _main() | ||||||
|  | 	if err != nil { | ||||||
|  | 		fmt.Fprintf(os.Stderr, "error: %s\n", err) | ||||||
|  | 		os.Exit(1) | ||||||
|  | 	} | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	
this struct can be deleted