mark2web/assets.go

53 lines
1.4 KiB
Go
Raw Normal View History

2019-03-18 13:34:52 +01:00
package mark2web
2019-02-28 12:13:28 +01:00
import (
2019-03-18 15:14:41 +01:00
"log"
"path"
"regexp"
2019-02-28 12:13:28 +01:00
"strings"
2019-03-18 13:34:52 +01:00
"gitbase.de/apairon/mark2web/helper"
2019-02-28 12:13:28 +01:00
cpy "github.com/otiai10/copy"
)
2019-02-28 15:13:59 +01:00
// ProcessAssets copies the assets from input to output dir
2019-02-28 12:13:28 +01:00
func ProcessAssets() {
2019-03-18 15:14:41 +01:00
switch Config.Assets.Action {
2019-02-28 12:13:28 +01:00
case "copy":
2019-03-18 15:14:41 +01:00
from := Config.Assets.FromPath
to := Config.Assets.ToPath
2019-02-28 12:13:28 +01:00
if !strings.HasPrefix(from, "/") {
2019-03-18 15:14:41 +01:00
from = Config.Directories.Input + "/" + from
2019-02-28 12:13:28 +01:00
}
if !strings.HasPrefix(to, "/") {
2019-03-18 15:14:41 +01:00
to = Config.Directories.Output + "/" + to
2019-02-28 12:13:28 +01:00
}
2019-03-18 13:34:52 +01:00
helper.Log.Noticef("copying assets from '%s' to '%s'", from, to)
2019-02-28 12:13:28 +01:00
err := cpy.Copy(from, to)
if err != nil {
2019-03-18 13:34:52 +01:00
helper.Log.Panicf("could not copy assets from '%s' to '%s': %s", from, to, err)
2019-02-28 12:13:28 +01:00
}
}
}
2019-03-18 15:14:41 +01:00
// fixAssetsPath replaces assets path based on current path
func fixAssetsPath(str, curNavPath string) string {
if find := Config.Assets.FixTemplate.Find; find != "" {
helper.Log.Debugf("fixing assets paths for path '%s'", curNavPath)
repl := Config.Assets.FixTemplate.Replace
toPath := Config.Assets.ToPath
bToRoot := helper.BackToRoot(curNavPath)
regex, err := regexp.Compile(find)
if err != nil {
log.Panicf("could not compile regexp '%s' for assets path: %s", find, err)
}
repl = bToRoot + toPath + "/" + repl
repl = path.Clean(repl) + "/"
helper.Log.Debugf("new assets paths: %s", repl)
return regex.ReplaceAllString(str, repl)
}
return str
}