Skip to content

Commit

Permalink
properly parse link files
Browse files Browse the repository at this point in the history
  • Loading branch information
lucat1 committed May 22, 2022
1 parent 24d2ce7 commit 986f0ef
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
7 changes: 3 additions & 4 deletions header.gohtml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<!DOCTYPE html>
<html lang='en'>
<html lang="en">
<head>
<meta name='viewport' content='width=device-width'>
<meta name="viewport" content="width=device-width">
<style>{{ .Stylesheet }}</style>
<title>Index of {{ .FullPath }}</title>
</head>
<body>
<h1>
Index of
{{ if not (eq (len .Root.Name) 0) }}/<a href={{ .Root.URL }}>{{ .Root.Name }}</a>{{ end }}/{{ range $i,$a := .Parts }}<a href="{{ .URL }}">{{ .Name }}</a>/{{ end }}
Index of {{ if not (eq (len .Root.Name) 0) }}/<a href="{{ .Root.URL }}">{{ .Root.Name }}</a>{{ end }}/{{ range $i,$a := .Parts }}<a href="{{ .URL }}">{{ .Name }}</a>/{{ end }}
</h1>
<hr>
<div class="g">
55 changes: 33 additions & 22 deletions statik.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,28 +138,33 @@ func generate(m *minify.M, dir string, parts []string) bool {

out := new(bytes.Buffer)
// Generate the header and the double dots back anchor when appropriate
p, url := []Dir{}, ""
for _, part := range parts {
url = path.Join(url, part)
p = append(p, Dir{Name: part, URL: withBaseURL(url)})
}
gen(header, Header{
Root: Dir{
Name: strings.TrimPrefix(strings.TrimSuffix(baseURL.Path, "/"), "/"),
URL: baseURL.String(),
},
Parts: p,
FullPath: path.Join(baseURL.Path+rel) + "/",
Stylesheet: template.CSS(style),
}, out)
if len(parts) != 0 {
gen(line, Line{
IsDir: true,
Name: "..",
URL: withBaseURL(path.Join(rel, "..")),
Size: humanize.Bytes(0),
{
p, url := []Dir{}, ""
for _, part := range parts {
url = path.Join(url, part)
p = append(p, Dir{Name: part, URL: withBaseURL(url)})
}
gen(header, Header{
Root: Dir{
Name: strings.TrimPrefix(strings.TrimSuffix(baseURL.Path, "/"), "/"),
URL: baseURL.String(),
},
Parts: p,
FullPath: path.Join(baseURL.Path+rel) + "/",
Stylesheet: template.CSS(style),
}, out)
}
// Populte the back line
{
if len(parts) != 0 {
gen(line, Line{
IsDir: true,
Name: "..",
URL: withBaseURL(path.Join(rel, "..")),
Size: humanize.Bytes(0),
}, out)
}
}

for _, entry := range entries {
pth := path.Join(dir, entry.Name())
Expand All @@ -179,11 +184,17 @@ func generate(m *minify.M, dir string, parts []string) bool {
data.Name = data.Name[:len(data.Name)-len(linkSuffix)]
data.Size = humanize.Bytes(0)

url, err := ioutil.ReadFile(pth)
raw, err := ioutil.ReadFile(pth)
if err != nil {
log.Fatalf("Could not read link file: %s\n%s\n", pth, err)
}
data.URL = string(url)
rawStr := string(raw)
u, err := url.Parse(rawStr[:len(rawStr)-1])
if err != nil {
log.Fatalf("Could not parse URL in file: %s\nThe value is: %s\n%s\n", pth, raw, err)
}

data.URL = u.String()
gen(line, data, out)
continue
}
Expand Down

0 comments on commit 986f0ef

Please sign in to comment.