diff --git a/.gitmodules b/.gitmodules index a924ca1..8024ca7 100644 --- a/.gitmodules +++ b/.gitmodules @@ -61,3 +61,9 @@ [submodule "vendor/github.com/ddliu/motto"] path = vendor/github.com/ddliu/motto url = https://github.com/ddliu/motto +[submodule "vendor/github.com/disintegration/imaging"] + path = vendor/github.com/disintegration/imaging + url = https://github.com/disintegration/imaging +[submodule "vendor/golang.org/x/image"] + path = vendor/golang.org/x/image + url = https://go.googlesource.com/image diff --git a/helper/render.go b/helper/render.go index 3b3b06c..791400a 100644 --- a/helper/render.go +++ b/helper/render.go @@ -27,6 +27,36 @@ func BackToRoot(curNavPath string) string { return tmpPath } +// ResolveNavPath fixes nav target relative to current navigation path +func ResolveNavPath(target string) string { + curNavPath := (*currentContext)["CurrentPath"].(string) + if strings.HasPrefix(target, "/") { + target = BackToRoot(curNavPath) + target + } + target = path.Clean(target) + return target +} + +// ResolveOutputPath fixes output directory relative to current navigation path +func ResolveOutputPath(target string) string { + if strings.HasPrefix(target, "/") { + target = config.Config.Directories.Output + "/" + target + } else { + target = currentTreeNodeConfig.OutputPath + "/" + target + } + return path.Clean(target) +} + +// ResolveInputPath fixes input directory relative to current navigation path +func ResolveInputPath(target string) string { + if strings.HasPrefix(target, "/") { + target = config.Config.Directories.Input + "/" + target + } else { + target = currentTreeNodeConfig.InputPath + "/" + target + } + return path.Clean(target) +} + // SetTemplateDir sets base directory for searching template files func SetTemplateDir(dir string) { templateDir = dir diff --git a/helper/template_filters.go b/helper/template_filters.go index f16161b..23eaa16 100644 --- a/helper/template_filters.go +++ b/helper/template_filters.go @@ -1,11 +1,15 @@ package helper import ( + "errors" + "fmt" "io/ioutil" "path" + "strconv" "strings" "github.com/ddliu/motto" + "github.com/disintegration/imaging" "github.com/flosch/pongo2" _ "github.com/flosch/pongo2-addons" _ "github.com/robertkrimen/otto/underscore" @@ -16,6 +20,17 @@ func init() { if err != nil { panic(err) } + + newFilters := map[string]pongo2.FilterFunction{ + "image_resize": ImageResizeFilter, + "relative_path": RelativePathFilter, + } + for name, fn := range newFilters { + err := pongo2.RegisterFilter(name, fn) + if err != nil { + panic(err) + } + } } // MarkdownFilter is a pongo2 filter, which converts markdown to html @@ -29,6 +44,88 @@ func MarkdownFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pong nil } +type imageParams struct { + width int + height int + filename string +} + +func parseImageParams(str string) (*imageParams, error) { + if str == "" { + return nil, errors.New("missing image parameters") + } + p := imageParams{} + for _, s := range strings.Split(str, ",") { + e := strings.Split(s, "=") + if len(e) < 2 { + return nil, fmt.Errorf("invalid image parameter: %s", s) + } + var err error + switch e[0] { + case "w": + p.width, err = strconv.Atoi(e[1]) + case "h": + p.height, err = strconv.Atoi(e[1]) + case "f": + p.filename = e[1] + default: + return nil, fmt.Errorf("invalid image parameter: %s", s) + } + if err != nil { + return nil, fmt.Errorf("could not convert image parameter to correct value type for '%s': %s", s, err) + } + } + return &p, nil +} + +// ImageResizeFilter read the image url and resize parameters and saves the resized image +// param: w=WITDH,h=HEIGHT +func ImageResizeFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) { + imgSource := in.String() + p, err := parseImageParams(param.String()) + if err != nil { + return nil, &pongo2.Error{ + Sender: "filter:image_resize", + OrigError: err, + } + } + + imgSource = ResolveInputPath(imgSource) + if p.filename == "" { + p.filename = fmt.Sprintf("%dx%d_%s", p.width, p.height, path.Base(imgSource)) + } + imgTarget := ResolveOutputPath(p.filename) + Log.Noticef("resizing image from %s to %s", imgSource, imgTarget) + + img, err := imaging.Open(imgSource, imaging.AutoOrientation(true)) + if err != nil { + return nil, &pongo2.Error{ + Sender: "filter:image_resize", + OrigError: fmt.Errorf("could not open image '%s': %s", imgSource, err), + } + } + img = imaging.Resize(img, p.width, p.height, imaging.Lanczos) + + err = imaging.Save(img, imgTarget) + if err != nil { + return nil, &pongo2.Error{ + Sender: "filter:image_resize", + OrigError: fmt.Errorf("could save image '%s': %s", imgTarget, err), + } + } + + return pongo2.AsValue(ResolveNavPath(p.filename)), nil +} + +// RelativePathFilter returns the relative path to navpoint based on current nav +func RelativePathFilter(in, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) { + return pongo2.AsValue( + ResolveNavPath( + in.String(), + ), + ), nil +} + // RegisterFilters reads a directory and register filters from files within it func RegisterFilters(dir string) { files, err := ioutil.ReadDir(dir) diff --git a/test.rest b/test.rest index e54ca3d..39fd120 100644 --- a/test.rest +++ b/test.rest @@ -1 +1 @@ -POST https://cockpit.basiscms.de/api/collections/get/mark2webBlog?filter[published]=true&sort[date]=-1&limit=1&skip=1 \ No newline at end of file +GET https://mark2web.basiscms.de/api/collections/get/mark2webBlog?filter[published]=true&sort[date]=-1&limit=1&skip=0 \ No newline at end of file diff --git a/vendor/github.com/disintegration/imaging b/vendor/github.com/disintegration/imaging new file mode 160000 index 0000000..5362c13 --- /dev/null +++ b/vendor/github.com/disintegration/imaging @@ -0,0 +1 @@ +Subproject commit 5362c131d56305ce787e79a5b94ffc956df00d62 diff --git a/vendor/golang.org/x/image b/vendor/golang.org/x/image new file mode 160000 index 0000000..0694c2d --- /dev/null +++ b/vendor/golang.org/x/image @@ -0,0 +1 @@ +Subproject commit 0694c2d4d067f97ebef574d63a763ee8ab559da7 diff --git a/website/content/de/01_Navigation/02_Installation/bild.jpg b/website/content/de/01_Navigation/02_Installation/bild.jpg new file mode 100644 index 0000000..627f349 Binary files /dev/null and b/website/content/de/01_Navigation/02_Installation/bild.jpg differ diff --git a/website/project-files/css/main.css b/website/project-files/css/main.css index 467f21b..cf3f969 100755 --- a/website/project-files/css/main.css +++ b/website/project-files/css/main.css @@ -24,7 +24,10 @@ hr { display:block; height:1px; border:0; border:none; padding:0;} input, select {vertical-align:middle;} input, select, textarea {-webkit-appearance: none; border-radius: 0;} .clear {clear:both; margin: 0; padding:0;} -img {line-height:0;} +img { + line-height:0; + max-width: 100%; +} /*-------------------------------------------------------------- # General Settings diff --git a/website/templates/base.html b/website/templates/base.html index 0c5214e..d74134b 100755 --- a/website/templates/base.html +++ b/website/templates/base.html @@ -29,7 +29,7 @@