package deb822
import (
"fmt"
"html/template"
"io"
"log"
"os"
"regexp"
"strings"
)
type Package struct {
Name string
Version string
Description string
LongDescription string
Depends []string
Maintainer string
Filename string
}
var regexpRemove = regexp.MustCompile("(DEVUAN/|DEBIAN/|[0-9]+:)")
var regexpDots = regexp.MustCompilePOSIX("(^[[:blank:]]+.[[:blank:]]*$)")
func NewPackage(s Stanza) (Package, error) {
var p Package
if s["Package"] == "" || s["Version"] == "" {
return p, fmt.Errorf("empty package")
}
p.Name = s["Package"]
p.Version = regexpRemove.ReplaceAllString(s["Version"], "")
descr := strings.SplitN(s["Description"], "\n", 2)
p.Description = descr[0]
if len(descr) > 1 {
p.LongDescription = regexpDots.ReplaceAllString(descr[1], "")
}
p.Maintainer = s["Maintainer"]
p.Depends = strings.Split(s["Depends"], ",")
return p, nil
}
func PrintPackage(p Package, templ string, out io.Writer) {
t, err := template.New("webpage").Parse(templ)
if err != nil {
log.Fatal("Error in template")
}
t.Execute(out, p)
}
/*Stanza2HtmlPage Render the html webpage of a package and save it in the
/* corresponding "pool" directory.
*/
func Stanza2HtmlPage(s Stanza, templ string, baseDir string) error {
fname := s["Filename"]
if fname == "" {
return fmt.Errorf("No Filename provided")
}
p, err := NewPackage(s)
if err != nil {
log.Fatal("empty package!!!")
}
nameVersion := fmt.Sprintf("%s_%s", p.Name, p.Version)
fmt.Printf(" nameVersion: %s\n", nameVersion)
fmt.Printf(" baseDir: %s\n", baseDir)
fname = regexpRemove.ReplaceAllString(fname, "")
fmt.Printf(" fname: %s\n", fname)
/// FIXME!!!! ERROR IN DETECTION OF PACKAGE NAME!!!!
dirName := fmt.Sprintf("%s/%s", baseDir, strings.Split(fname, nameVersion)[0])
err = os.MkdirAll(dirName, 0755)
if err == nil {
fmt.Printf(" dirName: %s\n Package: %s\n Version: %s\n", dirName, p.Name, p.Version)
htmlFile := fmt.Sprintf("%s%s_%s.html", dirName, p.Name, p.Version)
fmt.Printf("Creating file: %s\n---------\n", htmlFile)
if f, err := os.Open(htmlFile); err == nil {
f.Close()
fmt.Printf("File %s exists -- skipping\n", htmlFile)
return err
}
file, err := os.Create(htmlFile)
if err != nil {
err = fmt.Errorf("Error creating file: %s", err)
return err
} else {
PrintPackage(p, templ, file)
file.Close()
return nil
}
} else {
err = fmt.Errorf("Error creating path: %s", err)
return err
}
return nil
}