1
0
mirror of https://github.com/thebaer/tildes.git synced 2018-07-20 07:15:21 +00:00

Find local tildizens with a Code directory

This commit is contained in:
Matt Baer 2015-01-25 22:39:16 -05:00
parent c57c0b353f
commit faccca5464
2 changed files with 112 additions and 0 deletions

84
code.go Normal file
View File

@ -0,0 +1,84 @@
package main
import (
"os"
"fmt"
"time"
"bufio"
"strings"
"path/filepath"
"text/template"
)
func main() {
fmt.Println("Starting...")
generate(findProjects())
}
type User struct {
Name string
Projects []Project
}
type Project struct {
Name string
Path string
}
func findProjects() map[string]User {
files, _ := filepath.Glob("/home/*/Code/*")
users := make(map[string]User)
for _, path := range files {
pparts := strings.Split(path, "/")
uname := pparts[2]
proj := &Project{Name: filepath.Base(path), Path: strings.Replace(path, "/home/", "~", -1)}
u, exists := users[uname]
if !exists {
fmt.Printf("Found Code for ~%s.\n", uname)
projs := []Project{*proj}
u = User{Name: uname, Projects: projs}
} else {
u.Projects = append(u.Projects, *proj)
}
users[uname] = u
}
return users
}
type Page struct {
Host string
Users map[string]User
Updated string
UpdatedForHumans string
}
func generate(users map[string]User) {
fmt.Println("Generating page.")
f, err := os.Create(os.Getenv("HOME") + "/public_html/code.html")
if err != nil {
panic(err)
}
defer f.Close()
w := bufio.NewWriter(f)
template, err := template.ParseFiles("templates/code.html")
if err != nil {
panic(err)
}
// Extra page data
host, _ := os.Hostname()
curTime := time.Now()
updatedReadable := curTime.UTC().Format(time.RFC1123)
updated := curTime.UTC().Format(time.RFC3339)
// Generate the page
page := &Page{Host: host, UpdatedForHumans: updatedReadable, Updated: updated, Users: users}
template.ExecuteTemplate(w, "code", page)
w.Flush()
fmt.Println("DONE!")
}

28
templates/code.html Normal file
View File

@ -0,0 +1,28 @@
{{define "code"}}
<html>
<head>
<title>Code on {{.Host}}</title>
<link type="text/css" rel="stylesheet" href="tilde.css" />
</head>
<body id="hello">
<h1>Code on {{.Host}}</h1>
<p>Last Updated: <time datetime="{{.Updated}}">{{.UpdatedForHumans}}</time></p>
{{range .Users}}
<h2><a href="/~{{.Name}}/">~{{.Name}}</a></h2>
<div class="links">
<ul class="links">
{{range .Projects}}
<li><strong>{{.Name}}</strong> &middot; {{.Path}}</li>
{{end}}
</ul>
</div>
{{end}}
</body>
</html>
{{end}}