Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ReadDir function to list local files #1204

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions docs/content/extras/localfiles.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
aliases:
- /doc/localfiles/
date: 2015-06-12
menu:
main:
parent: extras
next: /community/mailing-list
notoc: true
prev: /extras/urls
title: Traversing Local Files
weight: 110
---

## Traversing Local Files

Hugo includes a way to traverse local files.
This is done using the 'ReadDir' function.

## Using ReadDir

ReadDir takes a single string input that is relative to the root directory of the site. It returns an array of [os.FileInfo](https://golang.org/pkg/os/#FileInfo)

Let's create a shortcode to build a file index with links using ReadDir.

'fileindex.html'

<table style="width=100%">
<th>Size in bytes</th>
<th>Name</th>
{{$dir := .Get "dir"}}
{{ $url := .Get "baseurl" }}

{{ $files := ReadDir $dir }}
{{ range $files }}
<tr>
<td>{{.Size}}</td>
<td>
<a href="{{$url}}{{.Name | urlize }}"> {{.Name}}</a>
</td>
</tr>
{{ end }}
</table>

Now lets use it to list the css files used on this site

{{</* fileindex dir="static/css" baseurl="/css/" */>}}

Is rendered as:

{{< fileindex dir="static/css/" baseurl="/css/">}}
16 changes: 16 additions & 0 deletions docs/layouts/shortcodes/fileindex.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<table style="width=100%">
<th>Size in bytes</th>
<th>Name</th>
{{$dir := .Get "dir"}}
{{ $url := .Get "baseurl" }}

{{ $files := ReadDir $dir }}
{{ range $files }}
<tr>
<td>{{.Size}}</td>
<td>
<a href="{{$url}}{{.Name | urlize }}"> {{.Name}}</a>
</td>
</tr>
{{ end }}
</table>
1 change: 1 addition & 0 deletions tpl/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,7 @@ func init() {
"dateFormat": DateFormat,
"getJSON": GetJSON,
"getCSV": GetCSV,
"ReadDir": ReadDir,
"seq": helpers.Seq,
"getenv": func(varName string) string { return os.Getenv(varName) },

Expand Down
24 changes: 24 additions & 0 deletions tpl/template_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -263,3 +265,25 @@ func GetCSV(sep string, urlParts ...string) [][]string {
}
return d
}

func ReadDir(path string) []os.FileInfo {
wd := ""
p := ""
if viper.GetString("WorkingDir") != "" {
wd = viper.GetString("WorkingDir")
}
if strings.Contains(path, "..") {
jww.ERROR.Printf("Path contains parent directory marker ..\n", path)
return nil
}

p = filepath.Clean(path)
p = filepath.Join(wd, p)

list, err := ioutil.ReadDir(p)
if err != nil {
jww.ERROR.Printf("Failed to read Directory %s with error message %s", path, err)
return nil
}
return list
}