Skip to content

Commit

Permalink
add link support
Browse files Browse the repository at this point in the history
  • Loading branch information
lucat1 committed May 2, 2022
1 parent 0b5c428 commit 5c32f24
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions statik.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ var (
baseDir, outDir string
baseUrl *url.URL = nil

include, exclude *regexp.Regexp = nil, nil
empty, recursive, sortEntries bool
include, exclude *regexp.Regexp = nil, nil
empty, recursive, sortEntries, converLinks bool
)

func bytes(b int64) string {
Expand Down Expand Up @@ -60,9 +60,13 @@ func header(rel string) string {
return str
}

func line(name, path string, modTime time.Time, size int64) string {
func line(name, path string, modTime time.Time, size int64, link bool) string {
space := strings.Repeat(" ", nameSpace-len(name))
return fmt.Sprintf("<a href=\"%s\">%s</a>%s %-"+dateSpace+"s %-"+sizeSpace+"s\n", join(path), name, space, modTime.Format(formatLayout), bytes(size))
url := path
if !link {
url = join(path)
}
return fmt.Sprintf("<a href=\"%s\">%s</a>%s %-"+dateSpace+"s %-"+sizeSpace+"s\n", url, name, space, modTime.Format(formatLayout), bytes(size))
}

func footer(date time.Time) string {
Expand Down Expand Up @@ -126,9 +130,19 @@ func generate(dir string) bool {
if pth == outDir {
continue
}

if strings.HasSuffix(pth, ".link") {
url, err := ioutil.ReadFile(pth)
if err != nil {
log.Fatalf("Could not read link file: %s\n%s\n", pth, err)
}
content += line(entry.Name()[:len(entry.Name())-5], string(url), entry.ModTime(), 0, true)
continue
}

// Only list directories when recursing and only those which are not empty
if !entry.IsDir() || recursive && generate(pth) {
content += line(entry.Name(), path.Join(rel, entry.Name()), entry.ModTime(), entry.Size())
content += line(entry.Name(), path.Join(rel, entry.Name()), entry.ModTime(), entry.Size(), true)
}

// Copy all files over to the web root
Expand All @@ -155,6 +169,7 @@ func main() {
emp := flag.Bool("empty", false, "Whether to list empty directories")
s := flag.Bool("sort", true, "Sort files A-z and by type")
b := flag.String("b", "http://localhost", "The base URL")
l := flag.Bool("l", false, "Convert .link files to anchor tags")
flag.Parse()

args := flag.Args()
Expand All @@ -174,6 +189,7 @@ func main() {
log.Println("\tExclude:\t", *e)
log.Println("\tRecursive:\t", *r)
log.Println("\tEmpty:\t\t", *emp)
log.Println("\tConvert links:\t\t", *l)
log.Println("\tSource:\t\t", src)
log.Println("\tDestination:\t", dest)
log.Println("\tBase URL:\t", *b)
Expand All @@ -188,6 +204,7 @@ func main() {
recursive = *r
empty = *emp
sortEntries = *s
converLinks = *l

var wd string
if !filepath.IsAbs(src) || !filepath.IsAbs(dest) {
Expand Down

0 comments on commit 5c32f24

Please sign in to comment.