aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Arakaki Giovanini <mail@gabrielgio.me>2023-09-03 16:01:20 +0200
committerGabriel Arakaki Giovanini <mail@gabrielgio.me>2023-09-03 16:01:20 +0200
commita21602a450217333a27419d8168865b21fae6e7e (patch)
treebcb454130105de02dd97864dc1c977bf30044c38
parent3b21449468a1b20b3ff706fe00a04556a804e627 (diff)
downloadapkdoc-a21602a450217333a27419d8168865b21fae6e7e.tar.gz
apkdoc-a21602a450217333a27419d8168865b21fae6e7e.tar.bz2
apkdoc-a21602a450217333a27419d8168865b21fae6e7e.zip
feat: Add option for inputing template
Now a template file is required to run the cli command. That gives the user an option to provide its own template file. An default will be provided later.
-rw-r--r--example.txt8
-rw-r--r--main.go18
-rw-r--r--parser.go (renamed from parser/parser.go)53
-rw-r--r--template.go44
4 files changed, 114 insertions, 9 deletions
diff --git a/example.txt b/example.txt
new file mode 100644
index 0000000..8c5c0eb
--- /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 fe3c34f..314ecc8 100644
--- a/main.go
+++ b/main.go
@@ -5,19 +5,18 @@ import (
"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 @@ func main() {
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 @@ func main() {
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
index 62b0d85..344efca 100644
--- a/parser/parser.go
+++ b/parser.go
@@ -1,4 +1,4 @@
-package parser
+package main
import (
"fmt"
@@ -35,6 +35,57 @@ func (e *Entry) FomartLink(format string) string {
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 {
return &v
}
diff --git a/template.go b/template.go
new file mode 100644
index 0000000..503af64
--- /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")
+ }
+}