Add ReadDir function to list local files.
authorRussell Oliver <roliver8143@gmail.com>
Fri, 26 Jun 2015 10:23:37 +0000 (20:23 +1000)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 25 Jul 2015 19:56:38 +0000 (21:56 +0200)
Includes documentation.

docs/content/extras/localfiles.md [new file with mode: 0644]
docs/layouts/shortcodes/fileindex.html [new file with mode: 0644]
tpl/template_funcs.go
tpl/template_resources.go

diff --git a/docs/content/extras/localfiles.md b/docs/content/extras/localfiles.md
new file mode 100644 (file)
index 0000000..843082d
--- /dev/null
@@ -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/">}}
diff --git a/docs/layouts/shortcodes/fileindex.html b/docs/layouts/shortcodes/fileindex.html
new file mode 100644 (file)
index 0000000..132ed67
--- /dev/null
@@ -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>
index 258591314d4f6e09bf9e1039b37a66514b5364cb..fdf4d9f189f0bdc8c95cfef48ea126d5e5817a36 100644 (file)
@@ -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) },
 
index c413574d38f74d22bfe6a94e85659f778db45401..f49e7df6f7a531dd57ce844a210ea577ae6a811b 100644 (file)
@@ -21,6 +21,8 @@ import (
        "io/ioutil"
        "net/http"
        "net/url"
+       "os"
+       "path/filepath"
        "strings"
        "sync"
        "time"
@@ -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
+}