diff --git a/example.txt b/example.txt
new file mode 100644
index 0000000000000000000000000000000000000000..8c5c0eb742c679931b7cacb13d419e64e9411413
--- /dev/null
+++ b/example.txt
@@ -0,0 +1,8 @@
+# Apks Alpine 3.18
+
+{{ range $e := . }}
+## {{ $e.Name }}
+
+{{ range $name, $value := ($e.Properties) }}- **{{$name}}**: {{ $value }}
+{{ end }}
+{{ end }}
diff --git a/main.go b/main.go
index fe3c34ff9c8a0fac6d77e6b41d7ebb7897ae4bbf..314ecc8ad5f43e74b6670920e62dd853335e1e7d 100644
--- a/main.go
+++ b/main.go
@@ -5,19 +5,18 @@ "archive/tar"
"bufio"
"compress/gzip"
"errors"
- "fmt"
"io"
"net/http"
"os"
- "git.sr.ht/~gabrielgio/apkdoc/parser"
flag "github.com/spf13/pflag"
)
func main() {
url := flag.StringP("url", "u", "", "Url to the APKINDEX.tar.gz")
output := flag.StringP("output", "o", "index.md", "Output path")
- repositoryFormat := flag.StringP("repository-format", "f", "https://git.sr.ht/~gabrielgio/apkbuilds/tree/%s/item/apks/%s", "Template to build repository link")
+ templateType := flag.StringP("template-type", "p", "text", "Template system to be used, options: html, text")
+ templateFile := flag.StringP("template-file", "t", "text", "Template file to be used")
flag.Parse()
tarStream, err := fechIndex(*url)
@@ -47,13 +46,13 @@ }
s := bufio.NewScanner(tr)
- entries := make([]*parser.Entry, 0)
+ entries := make([]*Entry, 0)
lines := make([]string, 0)
for s.Scan() {
l := s.Text()
if l == "" {
- entry := parser.Parse(lines)
+ entry := Parse(lines)
entries = append(entries, entry)
lines = make([]string, 0)
} else {
@@ -66,13 +65,16 @@ if err != nil {
panic("Error openning output file: " + err.Error())
}
- for _, e := range entries {
- fmt.Fprintln(outputFile, e.FomartLink(*repositoryFormat))
+ tmpl, err := GetTemplate(*templateType, *templateFile)
+ if err != nil {
+ panic("Error loading template file: " + err.Error())
}
+
+ tmpl.Execute(outputFile, entries)
}
func getOutputFile(output string) (*os.File, error) {
- if output == "" {
+ if output != "" {
outputFile, err := os.Create(output)
if err != nil {
return nil, err
diff --git a/parser/parser.go b/parser.go
rename from parser/parser.go
rename to parser.go
index 62b0d855ba36b390d7eb5c6bcbe76294cad99bc5..344efcab3bf3b5e1388d95237d1bbbbb00e3f62a 100644
--- a/parser/parser.go
+++ b/parser.go
@@ -1,4 +1,4 @@
-package parser
+package main
import (
"fmt"
@@ -33,6 +33,57 @@
func (e *Entry) FomartLink(format string) string {
c := strings.Replace(*e.Commit, "-dirty", "", -1)
return fmt.Sprintf(format, c, *e.Origin)
+}
+
+func (e *Entry) Properties() map[string]string {
+ p := make(map[string]string)
+
+ p["checksum"] = e.Checksum
+ p["version"] = e.Version
+ p["name"] = e.Name
+ p["package size"] = strconv.Itoa(e.PackageSize)
+ p["installed size"] = strconv.Itoa(e.InstalledSize)
+ p["description"] = e.Description
+ p["url"] = e.Url
+ p["license"] = e.License
+
+ if e.Architecture != nil {
+ p["architecture"] = *e.Architecture
+ }
+
+ if e.Origin != nil {
+ p["origin"] = *e.Origin
+ }
+
+ if e.Maintainer != nil {
+ p["maintainer"] = *e.Maintainer
+ }
+
+ if e.BuildTime != nil {
+ p["build time"] = e.BuildTime.String()
+ }
+
+ if e.Commit != nil {
+ p["commit"] = *e.Commit
+ }
+
+ if e.ProviderPriority != nil {
+ p["provider priority"] = strconv.Itoa(*e.ProviderPriority)
+ }
+
+ if len(e.Dependencies) > 0 {
+ p["dependencies"] = strings.Join(e.Dependencies, " ")
+ }
+
+ if len(e.Provides) > 0 {
+ p["provides"] = strings.Join(e.Provides, " ")
+ }
+
+ if len(e.InstallIf) > 0 {
+ p["install if"] = strings.Join(e.InstallIf, " ")
+ }
+
+ return p
}
func ptr[T any](v T) *T {
diff --git a/template.go b/template.go
new file mode 100644
index 0000000000000000000000000000000000000000..503af64338f8d2e30b3e37390cf9e54a642b2e44
--- /dev/null
+++ b/template.go
@@ -0,0 +1,44 @@
+package main
+
+import (
+ html "html/template"
+ "io"
+ "os"
+ text "text/template"
+)
+
+type Templater interface {
+ Execute(wr io.Writer, data any) error
+}
+
+var (
+ templateFunc = map[string]any{
+ "DerefI": func(i *int) int { return *i },
+ "DerefS": func(i *string) string { return *i },
+ }
+)
+
+func GetTemplate(templateType, filePath string) (Templater, error) {
+ file, err := os.Open(filePath)
+ if err != nil {
+ return nil, err
+ }
+
+ tmpl, err := io.ReadAll(file)
+ if err != nil {
+ return nil, err
+ }
+
+ switch templateType {
+ case "text":
+ return text.New("text").
+ Funcs(templateFunc).
+ Parse(string(tmpl))
+ case "html":
+ return html.New("html").
+ Funcs(templateFunc).
+ Parse(string(tmpl))
+ default:
+ panic("Invalid template-type")
+ }
+}