2013-08-30 20:18:05 -04:00
|
|
|
package target
|
|
|
|
|
|
|
|
import (
|
2013-09-01 12:24:03 -04:00
|
|
|
"fmt"
|
2013-08-30 20:18:05 -04:00
|
|
|
"io"
|
2013-09-01 12:56:58 -04:00
|
|
|
"os"
|
2013-09-01 12:24:03 -04:00
|
|
|
"path"
|
2013-09-01 12:56:58 -04:00
|
|
|
"path/filepath"
|
2013-08-30 20:18:05 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Publisher interface {
|
|
|
|
Publish(string, io.Reader) error
|
|
|
|
}
|
2013-09-01 12:24:03 -04:00
|
|
|
|
|
|
|
type Translator interface {
|
|
|
|
Translate(string) (string, error)
|
|
|
|
}
|
|
|
|
|
2013-09-03 23:52:50 -04:00
|
|
|
type Output interface {
|
|
|
|
Publisher
|
|
|
|
Translator
|
|
|
|
}
|
|
|
|
|
2013-09-01 12:24:03 -04:00
|
|
|
type Filesystem struct {
|
|
|
|
UglyUrls bool
|
|
|
|
DefaultExtension string
|
2013-09-01 12:56:58 -04:00
|
|
|
PublishDir string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *Filesystem) Publish(path string, r io.Reader) (err error) {
|
|
|
|
|
|
|
|
translated, err := fs.Translate(path)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-09-13 00:18:13 -04:00
|
|
|
return writeToDisk(translated, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeToDisk(translated string, r io.Reader) (err error) {
|
|
|
|
path, _ := filepath.Split(translated)
|
2013-09-05 01:28:59 -04:00
|
|
|
ospath := filepath.FromSlash(path)
|
2013-09-01 12:56:58 -04:00
|
|
|
|
2013-09-05 01:28:59 -04:00
|
|
|
if ospath != "" {
|
|
|
|
err = os.MkdirAll(ospath, 0764) // rwx, rw, r
|
|
|
|
if err != nil {
|
2013-09-20 20:03:43 -04:00
|
|
|
panic(err)
|
2013-09-05 01:28:59 -04:00
|
|
|
}
|
2013-09-01 12:56:58 -04:00
|
|
|
}
|
|
|
|
|
2013-09-05 01:28:59 -04:00
|
|
|
file, err := os.Create(translated)
|
2013-09-01 12:56:58 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(file, r)
|
|
|
|
return
|
2013-09-01 12:24:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *Filesystem) Translate(src string) (dest string, err error) {
|
2013-09-01 12:56:58 -04:00
|
|
|
if src == "/" {
|
2013-09-05 12:57:25 -04:00
|
|
|
if fs.PublishDir != "" {
|
|
|
|
return path.Join(fs.PublishDir, "index.html"), nil
|
|
|
|
}
|
2013-09-01 12:56:58 -04:00
|
|
|
return "index.html", nil
|
|
|
|
}
|
2013-09-01 12:24:03 -04:00
|
|
|
|
|
|
|
dir, file := path.Split(src)
|
|
|
|
ext := fs.extension(path.Ext(file))
|
|
|
|
name := filename(file)
|
2013-09-05 01:28:59 -04:00
|
|
|
if fs.PublishDir != "" {
|
|
|
|
dir = path.Join(fs.PublishDir, dir)
|
|
|
|
}
|
2013-09-01 12:24:03 -04:00
|
|
|
|
2013-09-25 00:24:49 -04:00
|
|
|
if fs.UglyUrls || file == "index.html" {
|
2013-09-03 23:52:50 -04:00
|
|
|
return path.Join(dir, fmt.Sprintf("%s%s", name, ext)), nil
|
|
|
|
}
|
|
|
|
|
2013-09-01 12:24:03 -04:00
|
|
|
return path.Join(dir, name, fmt.Sprintf("index%s", ext)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *Filesystem) extension(ext string) string {
|
2013-09-03 23:52:50 -04:00
|
|
|
switch ext {
|
|
|
|
case ".md", ".rst": // TODO make this list configurable. page.go has the list of markup types.
|
|
|
|
return ".html"
|
|
|
|
}
|
|
|
|
|
2013-09-01 12:24:03 -04:00
|
|
|
if ext != "" {
|
|
|
|
return ext
|
|
|
|
}
|
|
|
|
|
|
|
|
if fs.DefaultExtension != "" {
|
|
|
|
return fs.DefaultExtension
|
|
|
|
}
|
|
|
|
|
|
|
|
return ".html"
|
|
|
|
}
|
|
|
|
|
|
|
|
func filename(f string) string {
|
|
|
|
ext := path.Ext(f)
|
|
|
|
if ext == "" {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
return f[:len(f)-len(ext)]
|
|
|
|
}
|