diff options
| author | KatolaZ <katolaz@freaknet.org> | 2018-01-10 02:49:20 +0000 | 
|---|---|---|
| committer | KatolaZ <katolaz@freaknet.org> | 2018-01-10 02:49:20 +0000 | 
| commit | b3ca06d08f4bba7af522a085e942d523c697a684 (patch) | |
| tree | ac1fe3ed403300d02a30222a987d7a1af250d56c | |
first commit
| -rw-r--r-- | d1pkgweb.go | 108 | ||||
| -rw-r--r-- | deb822/deb822_decl.go | 7 | ||||
| -rw-r--r-- | deb822/package.go | 108 | ||||
| -rw-r--r-- | deb822/parse.go | 80 | 
4 files changed, 303 insertions, 0 deletions
| diff --git a/d1pkgweb.go b/d1pkgweb.go new file mode 100644 index 0000000..d416e27 --- /dev/null +++ b/d1pkgweb.go @@ -0,0 +1,108 @@ +package main + +import ( +	"bufio" +	"d1pkgweb/deb822" +	"fmt" +	"os" +) + +var templ = `<html> +<body> +<title>{{.Name}}-{{.Version}}</title> +<style type="text/css"> +body { +      margin-top: 10px; +      line-height:1.6; +      font-size:20px; +} + +div.pkgname{ +      font-size: 150%; +      margin-top: 40px; +      margin-left: 40px; +      border-bottom: 2px solid #444444; +} + +div.description{ +      font-size: 100%; +      margin-left: 20px; +      margin-top: 20px; +      margin-bottom: 20px; +      border-bottom: 2px solid #aaaaaa; +} + +div.long_description{ +     font-size: 100%; +     width: 600px; +     margin-left: 40px; +} + +div.dep_list{ +     margin-top:20px; +     margin-left: 20px; + +} + +li.dep_item{ +     margin-left: 35px; +} + +</style> +<div class="pkgname">{{.Name}} {{.Version}}</div> +<div class="description"> +{{.Description}} +</div> +<div class="long_description"> +{{.LongDescription}} +</div> + +<hr> +<div class="dep_list"> +Depends: +<ul> +{{range .Depends}}<li class="dep_item">{{ . }}</li> +{{else}}<div>No depends</div>{{end}} +</ul> +</div> +<div class="maintainer"> +Maintainer: {{.Maintainer}} +</div> +</body> +</html> +` + +func main() { + +	args := os.Args + +	if len(args) < 2 { +		fmt.Printf("Usage: %s <filein>\n", args[0]) +		return +	} + +	fnames := args[1:] +	for _, fname := range fnames { +		f, err := os.Open(fname) +		if err != nil { +			defer f.Close() +		} +		if err != nil { +			fmt.Printf("Error opening file %s\n", fname) + +		} else { +			r := bufio.NewScanner(f) + +			if r != nil { +				for s, err := deb822.ScanStanza(r); s["Package"] != ""; s, err = deb822.ScanStanza(r) { +					if err == nil { +						deb822.Stanza2HtmlPage(s, templ, ".") +					} else { +						fmt.Printf("error: %s\n", err) +					} +					//WriteFiles(s, num, "./files/") +				} +			} +		} +	} +} diff --git a/deb822/deb822_decl.go b/deb822/deb822_decl.go new file mode 100644 index 0000000..82dcdbb --- /dev/null +++ b/deb822/deb822_decl.go @@ -0,0 +1,7 @@ +package deb822 + +/*Stanza map containing the values of fields in an RFC822 stanza */ +type Stanza map[string]string + +/*Stanzas array of Stanza*/ +type Stanzas []map[string]string diff --git a/deb822/package.go b/deb822/package.go new file mode 100644 index 0000000..df444ea --- /dev/null +++ b/deb822/package.go @@ -0,0 +1,108 @@ +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 +} diff --git a/deb822/parse.go b/deb822/parse.go new file mode 100644 index 0000000..a8a02e6 --- /dev/null +++ b/deb822/parse.go @@ -0,0 +1,80 @@ +package deb822 + +import ( +	"bufio" +	"fmt" +	"strings" +) + +/*ParseStream parses a stream and returns an array of maps, where +/* each map contains all the fields of an RFC822 stanza */ +func ParseStream(stream *bufio.Scanner) (Stanzas, int) { + +	s := make(Stanzas, 10) +	curSize := 10 +	for i := 0; i < 10; i++ { +		s[i] = make(map[string]string) +	} +	curStanza := 0 +	curField := "" +	for stream.Scan() { +		if strings.TrimSpace(stream.Text()) == "" { +			if curField == "" { +				continue +			} +			curStanza++ +			curField = "" +			fmt.Printf("==== New stanza ==== (Nr. %d/%d)\n", curStanza, curSize) +			if curStanza >= curSize { +				newS := make(Stanzas, curSize*2) +				for i := curSize; i < curSize*2; i++ { +					newS[i] = make(map[string]string) +				} +				curSize *= 2 +				copy(newS, s) +				s = newS +			} +			continue +		} + +		//fmt.Printf("line: %s\n", stream.Text()) +		field := strings.SplitN(stream.Text(), ":", 2) +		//fmt.Printf("field: %s\n", field) +		if len(field) > 1 { +			/* This is a new field*/ +			curField = field[0] +			s[curStanza][curField] = strings.TrimSpace(field[1]) +		} else { +			/* this is the continuation of the last field */ +			s[curStanza][curField] += "\n" + field[0] +		} +	} +	return s, curStanza +} + +/*ScanStanza reads a single RFC822 stanza from a stream and returns +/* it as a map */ +func ScanStanza(stream *bufio.Scanner) (Stanza, error) { + +	s := make(Stanza) + +	curField := "" +	for stream.Scan() { +		if strings.TrimSpace(stream.Text()) == "" { +			return s, nil +		} + +		field := strings.SplitN(stream.Text(), ":", 2) +		if len(field) > 1 { +			/* This is a new field*/ +			curField = field[0] +			s[curField] = strings.TrimSpace(field[1]) +		} else { +			/* this is the continuation of the last field */ +			s[curField] += "\n" + field[0] +		} +	} + +	err := stream.Err() +	return s, err +} | 
