Browse Source

refactor: use mapping for html tags

decentral1se 2 years ago
parent
commit
5439d101d7
No known key found for this signature in database GPG Key ID: 3789458B3D0C410
  1. 57
      distribusi.go

57
distribusi.go

@ -102,6 +102,39 @@ var htmlBody = `
</html
`
// fileType is a file type identifiers, such as "image or "audio".
type fileType = string
// subType is a more specific file type identifier comparsed to fileType such as "gif and "mp4".
type subType = string
// htmlTag is an appropriate html tag element for a specific fileType & subType.
type htmlTag = string
// htmlTags is a mimetype to htmlTag mapping for generation purposes.
var htmlTags = map[fileType]map[subType]htmlTag{
"text": {
"html": "<section id=\"%s\">%s</section>",
"generic": "<pre>%s</pre>",
},
"image": {
"thumbnail": "<figure><a href=\"%s\"><img class='thumbnail' src='data:image/jpg;base64,%s'></a><figcaption>%s</figcaption></figure>",
"generic": "<figure><a href=\"%s\"><img class='%s' loading='lazy' src=\"%s\"/></a><figcaption>%s</figcaption></figure>",
},
"application": {
"pdf": "<object data=\"%s\" class='pdf' type='application/pdf'><embed src=\"%s\" type='application/pdf'/></object>",
},
"audio": {
"generic": "<audio controls> <source src=\"%s\" type='audio/%s'> Your browser does not support the audio element. </audio>",
},
"video": {
"generic": "<video controls> <source src=\"%s\" type='video/%s'> </video>",
},
"unknown": {
"generic": "<a class='%s' href=\"%s\">%s</a>",
},
}
// main is the command-line entrypoint.
func main() {
if Version == "" {
@ -552,10 +585,11 @@ func getHref(c *cli.Context, fpath string, mtype string) (bool, string, error) {
if err != nil {
return unknown, href, err
}
if stype == "html" {
href = fmt.Sprintf("<section id=\"%s\">%s</section>", fname, trimFinalNewline(fcontents))
href = fmt.Sprintf(htmlTags[ftype][stype], fname, trimFinalNewline(fcontents))
} else {
href = fmt.Sprintf("<pre>%s</pre>", trimFinalNewline(fcontents))
href = fmt.Sprintf(htmlTags[ftype]["generic"], trimFinalNewline(fcontents))
}
} else if ftype == "image" {
caption, err := getCaption(c, fpath)
@ -564,33 +598,30 @@ func getHref(c *cli.Context, fpath string, mtype string) (bool, string, error) {
}
if stype == "gif" {
href = fmt.Sprintf("<figure><a href=\"%s\"><img class='%s' loading='lazy' src=\"%s\" /></a><figcaption>%s</figcaption></figure>", fname, stype, fname, caption)
href = fmt.Sprintf(htmlTags[ftype]["generic"], fname, stype, fname, caption)
} else {
thumb, err := genThumb(c, fpath, caption)
if err != nil {
logrus.Debugf("failed to generate thumbnail for %s, showing original image", fpath)
href = fmt.Sprintf("<figure><a href=\"%s\"><img class='%s' loading='lazy' src=\"%s\"/></a><figcaption>%s</figcaption></figure>", fname, stype, fname, caption)
href = fmt.Sprintf(htmlTags[ftype]["generic"], fname, stype, fname, caption)
} else {
href = fmt.Sprintf(
"<figure><a href=\"%s\"><img class='thumbnail' src='data:image/jpg;base64,%s'></a><figcaption>%s</figcaption></figure>",
fname, thumb, caption,
)
href = fmt.Sprintf(htmlTags[ftype]["thumbnail"], fname, thumb, caption)
}
}
} else if ftype == "application" {
if stype == "pdf" {
href = fmt.Sprintf("<object data=\"%s\" class='pdf' type='application/pdf'><embed src=\"%s\" type='application/pdf'/></object>", fname, fname)
href = fmt.Sprintf(htmlTags[ftype][stype], fname, fname)
} else {
unknown = true
href = fmt.Sprintf("<a class='%s' href=\"%s\">%s</a>", stype, fname, fname)
href = fmt.Sprintf(htmlTags["unknown"]["generic"], stype, fname, fname)
}
} else if ftype == "audio" {
href = fmt.Sprintf("<audio controls> <source src=\"%s\" type='audio/%s'> Your browser does not support the audio element. </audio>", fname, stype)
href = fmt.Sprintf(htmlTags[ftype]["generic"], fname, stype)
} else if ftype == "video" {
href = fmt.Sprintf("<video controls> <source src=\"%s\" type='video/%s'> </video>", fname, stype)
href = fmt.Sprintf(htmlTags[ftype]["generic"], fname, stype)
} else {
unknown = true
href = fmt.Sprintf("<a class='%s' href=\"%s\">%s</a>", stype, fname, fname)
href = fmt.Sprintf(htmlTags["unknown"]["generic"], stype, fname, fname)
}
logrus.Debugf("generated href for %s: %s", fname, href)

Loading…
Cancel
Save