diff options
author | Laria Carolin Chabowski <laria@laria.me> | 2017-10-14 16:04:26 +0200 |
---|---|---|
committer | Laria Carolin Chabowski <laria@laria.me> | 2017-10-14 20:58:18 +0200 |
commit | 369d2c3e395903f6aff1d1869a81290d8bc994fa (patch) | |
tree | 4c3629906ebbfc8833af10c8f963f23619ecf00c /projects | |
download | code.laria.me-369d2c3e395903f6aff1d1869a81290d8bc994fa.tar.gz code.laria.me-369d2c3e395903f6aff1d1869a81290d8bc994fa.tar.bz2 code.laria.me-369d2c3e395903f6aff1d1869a81290d8bc994fa.zip |
Initial commit
Diffstat (limited to 'projects')
-rw-r--r-- | projects/project.go | 144 | ||||
-rw-r--r-- | projects/readme.go | 133 |
2 files changed, 277 insertions, 0 deletions
diff --git a/projects/project.go b/projects/project.go new file mode 100644 index 0000000..85239c9 --- /dev/null +++ b/projects/project.go @@ -0,0 +1,144 @@ +package projects + +import ( + "encoding/json" + "html/template" + "log" + "math" + "os" + "path" + "strings" +) + +type Link struct { + Title string + Href string + Class string `json:",omitempty"` +} + +type Project struct { + Name string `json:"-"` + + Title string + Description string + Shortdesc string + GoGet string `json:",omitempty"` + ReadmePath string `json:",omitempty"` + Git string `json:",omitempty"` // local git repo + License string `json:",omitempty"` + Links []Link + Tags []string + + dir string `json:"-"` + FormattedReadme template.HTML `json:"-"` +} + +func LoadProject(name string) (p Project, err error) { + f, err := os.Open(name) + if err != nil { + return p, err + } + defer f.Close() + + dec := json.NewDecoder(f) + if err := dec.Decode(&p); err != nil { + return p, err + } + + p.dir = path.Dir(name) + p.formatReadme() + p.Name = strings.Split(path.Base(name), ".")[0] + + return +} + +type ProjectRepo struct { + Dir string + Projects map[string]Project + Tags map[string]map[string]struct{} + Tagcloud map[string]TagcloudElem +} + +const TAGCLOUD_SIZE_CATEGORIES = 5 + +type TagcloudElem struct { + Tag string + Size int +} + +func NewProjectRepo(dir string) *ProjectRepo { + return &ProjectRepo{ + Dir: dir, + Projects: make(map[string]Project), + Tags: make(map[string]map[string]struct{}), + Tagcloud: make(map[string]TagcloudElem), + } +} + +func (repo *ProjectRepo) updateTags() { + tags := make(map[string]map[string]struct{}) + tagcounts := make(map[string]int) + maxcount := 0 + + for name, proj := range repo.Projects { + for _, tag := range proj.Tags { + if _, ok := tags[tag]; !ok { + tags[tag] = make(map[string]struct{}) + } + tags[tag][name] = struct{}{} + + tagcount := tagcounts[tag] + 1 + tagcounts[tag] = tagcount + if tagcount > maxcount { + maxcount = tagcount + } + } + } + + tagcloud := make(map[string]TagcloudElem) + if maxcount > 0 { + for tag, count := range tagcounts { + tagcloud[tag] = TagcloudElem{ + Tag: tag, + Size: int(math.Ceil((float64(count) / float64(maxcount)) * TAGCLOUD_SIZE_CATEGORIES)), + } + } + } + + repo.Tags = tags + repo.Tagcloud = tagcloud +} + +func (repo *ProjectRepo) ScanProjects() { + d, err := os.Open(repo.Dir) + if err != nil { + log.Printf("Failed scanning projects: Could not open dir: %s", err) + return + } + defer d.Close() + + names, err := d.Readdirnames(0) + if err != nil { + log.Printf("Failed scanning projects: Could not readdir: %s", err) + return + } + + projects := make(map[string]Project) + + for _, fname := range names { + if path.Ext(fname) != ".json" { + continue + } + + p, err := LoadProject(path.Join(repo.Dir, fname)) + if err != nil { + log.Printf("Skip project file %s: %s", fname, err) + continue + } + + projects[p.Name] = p + } + + repo.Projects = projects + repo.updateTags() +} diff --git a/projects/readme.go b/projects/readme.go new file mode 100644 index 0000000..2fda69b --- /dev/null +++ b/projects/readme.go @@ -0,0 +1,133 @@ +package projects + +import ( + "bytes" + "github.com/libgit2/git2go" + "html" + "html/template" + "io" + "log" + "os" + "os/exec" + "path" + "strings" +) + +type ReadmeFormat string + +const ( + ReadmeMarkdown ReadmeFormat = "markdown" + ReadmePlain ReadmeFormat = "plain" +) + +func (f ReadmeFormat) Format(raw []byte) template.HTML { + switch f { + case ReadmeMarkdown: + return formatMarkdown(raw) + default: + return template.HTML("<pre><code>" + html.EscapeString(string(raw)) + "</code></pre>") + } +} + +func formatMarkdown(raw []byte) template.HTML { + p := exec.Command("markdown") + + buf := new(bytes.Buffer) + + p.Stdin = bytes.NewReader(raw) + p.Stdout = buf + + if err := p.Run(); err != nil { + log.Printf("Failed formatting markdown readme: %s", err) + return "<strong>Failed formatting markdown :(</strong>" + } + + return template.HTML(buf.Bytes()) +} + +func gitReadme(gitpath string) (raw []byte, name string, err error) { + repo, err := git.OpenRepository(gitpath) + if err != nil { + return raw, name, err + } + + master_tree_obj, err := repo.RevparseSingle("master:") // Root tree of commit at top of master + if err != nil { + return raw, name, err + } + + master_tree, err := master_tree_obj.AsTree() + if err != nil { + return raw, name, err + } + + var inner_err error = nil + err = master_tree.Walk(func(_ string, entry *git.TreeEntry) int { + if !(strings.HasPrefix(strings.ToLower(entry.Name), "readme") && entry.Type == git.ObjectBlob) { + return 1 + } + + name = entry.Name + blob, err := repo.LookupBlob(entry.Id) + if err == nil { + raw = blob.Contents() + } else { + inner_err = err + } + return -1 + }) + + if err == nil && inner_err != nil { + err = inner_err + } + + return +} + +func (p *Project) formatReadme() { + readme_name := "" + var readme_raw []byte + if p.ReadmePath != "" { + fullpath := p.ReadmePath + if !path.IsAbs(fullpath) { + fullpath = path.Join(p.dir, fullpath) + } + fullpath = path.Clean(fullpath) + + f, err := os.Open(fullpath) + if err != nil { + log.Printf("Could not get readme for %s: failed opening %s: %s", p.Name, fullpath, err) + return + } + defer f.Close() + + buf := new(bytes.Buffer) + if _, err := io.Copy(buf, f); err != nil { + log.Printf("Could not get readme for %s: failed reading %s: %s", p.Name, fullpath, err) + return + } + + readme_raw = buf.Bytes() + readme_name = fullpath + } else if len(p.Git) > 0 { + var err error + readme_raw, readme_name, err = gitReadme(p.Git) + if err != nil { + log.Printf("Could not get readme for %s from git: %s", p.Name, err) + return + } + if readme_name == "" { + return + } + } else { + return + } + + format := ReadmePlain + switch strings.ToLower(path.Ext(readme_name)) { + case ".md", ".mkd", ".markdown": + format = ReadmeMarkdown + } + + p.FormattedReadme = format.Format(readme_raw) +} |