91 lines
1.8 KiB
Go
91 lines
1.8 KiB
Go
|
|
package progdoc
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"log"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"strings"
|
||
|
|
)
|
||
|
|
|
||
|
|
type Option struct {
|
||
|
|
configSitemap func(sm *siteMap) error
|
||
|
|
}
|
||
|
|
|
||
|
|
func Meta(meta SiteMeta) Option {
|
||
|
|
return Option{
|
||
|
|
configSitemap: func(sm *siteMap) error {
|
||
|
|
sm.Meta = &meta
|
||
|
|
return nil
|
||
|
|
},
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func SourceFile(path, file string) Option {
|
||
|
|
return Option{
|
||
|
|
configSitemap: func(sm *siteMap) error {
|
||
|
|
_, err := os.Stat(file)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
// TODO: support things other than markdown
|
||
|
|
src, err := inferSourceFromFilename(file)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
log.Printf("Page '%s' -> %s", path, file)
|
||
|
|
sm.Pages = append(sm.Pages, sitePage{
|
||
|
|
Path: path,
|
||
|
|
Source: stdLayoutSource{MainSource: src},
|
||
|
|
})
|
||
|
|
|
||
|
|
return nil
|
||
|
|
},
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func SourceDir(basePath, dir string) Option {
|
||
|
|
return Option{
|
||
|
|
configSitemap: func(sm *siteMap) error {
|
||
|
|
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if !info.IsDir() && info.Mode().IsRegular() {
|
||
|
|
src, err := inferSourceFromFilename(path)
|
||
|
|
if err != nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
relPath, err := filepath.Rel(dir, strings.TrimSuffix(path, filepath.Ext(path)))
|
||
|
|
if err != nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
targetPath := filepath.Join(basePath, relPath)
|
||
|
|
|
||
|
|
log.Printf("Page '%s' -> %s", targetPath, path)
|
||
|
|
sm.Pages = append(sm.Pages, sitePage{
|
||
|
|
Path: relPath,
|
||
|
|
Source: stdLayoutSource{MainSource: src},
|
||
|
|
})
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
})
|
||
|
|
},
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func inferSourceFromFilename(fname string) (pageSource, error) {
|
||
|
|
absName, err := filepath.Abs(fname)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
if strings.HasSuffix(absName, ".md") {
|
||
|
|
return mdSource{MDFile: absName}, nil
|
||
|
|
}
|
||
|
|
return nil, errors.New("unsupported file type")
|
||
|
|
}
|