tmplfn

package module
v0.0.0-...-7a15a69 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2017 License: BSD-3-Clause Imports: 11 Imported by: 1

README

tmplfn

A Go package to provide a common set of template extensions for working with Markdown integrations.

Documentation

Overview

*

  • tmplfn are a collection common of functions for use with Go's template
  • packages and with Markdown processor packages. *
  • @author R. S. Doiel, <rsdoiel@gmail.com> *
  • Copyright (c) 2016, R. S. Doiel
  • All rights reserved. *
  • Redistribution and use in source and binary forms, with or without
  • modification, are permitted provided that the following conditions are met: *
  • * Redistributions of source code must retain the above copyright notice, this
  • list of conditions and the following disclaimer. *
  • * Redistributions in binary form must reproduce the above copyright notice,
  • this list of conditions and the following disclaimer in the documentation
  • and/or other materials provided with the distribution. *
  • * Neither the name of findfile nor the names of its
  • contributors may be used to endorse or promote products derived from
  • this software without specific prior written permission. *
  • THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  • AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  • IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  • DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  • FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  • DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  • SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  • CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  • OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  • OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Index

Constants

This section is empty.

Variables

View Source
var (
	// TimeMap provides a common set of time/date related functions for use in text/template or html/template
	TimeMap = template.FuncMap{
		"year": func(s string) string {
			var (
				dt  time.Time
				err error
			)
			if s == "now" {
				dt = time.Now()
			} else {
				dt, err = time.Parse("2006-01-02", normalizeDate(s))
				if err != nil {
					return ""
				}
			}
			return dt.Format("2006")
		},
		"rfc3339": func(s string) string {
			var (
				dt  time.Time
				err error
			)
			if s == "now" {
				dt = time.Now()
			} else {
				dt, err = time.Parse("2006-01-02", normalizeDate(s))
				if err != nil {
					return ""
				}
			}
			return dt.Format(time.RFC3339)
		},
		"rfc1123": func(s string) string {
			var (
				dt  time.Time
				err error
			)
			if s == "now" {
				dt = time.Now()
			} else {
				dt, err = time.Parse("2006-01-02", normalizeDate(s))
				if err != nil {
					return ""
				}
			}
			return dt.Format(time.RFC1123)
		},
		"rfc1123z": func(s string) string {
			var (
				dt  time.Time
				err error
			)
			if s == "now" {
				dt = time.Now()
			} else {
				dt, err = time.Parse("2006-01-02", normalizeDate(s))
				if err != nil {
					return ""
				}
			}
			return dt.Format(time.RFC1123Z)
		},
		"rfc822z": func(s string) string {
			var (
				dt  time.Time
				err error
			)
			if s == "now" {
				dt = time.Now()
			} else {
				dt, err = time.Parse("2006-01-02", normalizeDate(s))
				if err != nil {
					return ""
				}
			}
			return dt.Format(time.RFC822Z)
		},
		"rfc822": func(s string) string {
			var (
				dt  time.Time
				err error
			)
			if s == "now" {
				dt = time.Now()
			} else {
				dt, err = time.Parse("2006-01-02", normalizeDate(s))
				if err != nil {
					return ""
				}
			}
			return dt.Format(time.RFC822)
		},
		"datefmt": func(dt, outputFmtYMD, outputFmtYM, outputFmtY string) string {
			var (
				inputFmt  string
				outputFmt string
			)

			switch {
			case len(dt) == 4:
				inputFmt = "2006"
				outputFmt = outputFmtY
			case len(dt) > 4 && len(dt) <= 7:
				inputFmt = "2006-01"
				outputFmt = outputFmtYM
			default:
				inputFmt = "2006-01-02"
				outputFmt = outputFmtYMD
			}

			d, err := time.Parse(inputFmt, dt)
			if err != nil {
				return fmt.Sprintf("%s, %s", dt, err.Error())
			}
			return d.Format(outputFmt)
		},
	}

	PageMap = template.FuncMap{
		"nl2p": func(s string) string {
			return strings.Replace(strings.Replace(s, "\n\n", "<p>", -1), "\n", "<br />", -1)
		},
		"contains": func(s, substring string) bool {
			return strings.Contains(s, substring)
		},
		"title": func(s string) string {
			return strings.Title(s)
		},
		"add": func(a, b int) int {
			return a + b
		},
		"sub": func(a, b int) int {
			return a - b
		},
		"arraylength": func(a []string) int {
			return len(a)
		},
		"mapsize": func(m map[string]string) int {
			return len(m)
		},
		"prevPage": func(from, size, max int) int {
			next := from - size
			if next < 0 {
				return 0
			}
			return next
		},
		"nextPage": func(from, size, max int) int {
			next := from + size
			if next > max {
				return from
			}
			return next
		},
		"getType": func(t interface{}) string {
			switch tp := t.(type) {
			default:
				return fmt.Sprintf("%T", tp)
			}
		},
		"asList": func(li []interface{}, sep string) string {
			var l []string
			for _, item := range li {
				l = append(l, fmt.Sprintf("%s", item))
			}
			return strings.Join(l, sep)
		},
		"synopsis": func(s string) string {
			return doc.Synopsis(s)
		},
		"encodeURIComponent": func(s string) string {
			u, err := url.Parse(s)
			if err != nil {
				log.Printf("Bad encoding request: %s, %s\n", s, err)
				return ""
			}
			return strings.Replace(u.String(), "&", "%26", -1)
		},
		"stringify": func(data interface{}, prettyPrint bool) string {
			if prettyPrint == true {
				if buf, err := json.MarshalIndent(data, "", "\t"); err == nil {
					return string(buf)
				}
			} else if buf, err := json.Marshal(data); err == nil {
				return string(buf)
			}
			return ""
		},
		"codeblock": func(src string, start int, end int, hint string) string {
			result := []string{}
			lines := strings.Split(src, "\n")
			if start < 1 {
				start = 0
			}
			if end < 1 {
				end = len(lines)
			}
			if (end - start) > 0 {
				result = append(result, fmt.Sprintf("```%s", hint))
			}
			for i, line := range lines[start:end] {
				if len(line) > 0 {
					result = append(result, fmt.Sprintf("    %s", line))
				} else if i > 0 && i < (end-1) {
					result = append(result, "")
				}
			}
			if len(result) > 0 {
				result = append(result, "```")
			}
			return strings.Join(result, "\n")
		},
	}
)

Functions

func Assemble

func Assemble(tmplFuncs template.FuncMap, templateFilenames ...string) (*template.Template, error)

Assemble support a very simple template setup of an outer HTML file with a content include along with common template functions.

func AssemblePage

func AssemblePage(htmlFilename, includeFilename string, tmplFuncs template.FuncMap) (*template.Template, error)

AssemblePage support a very simple template setup of an outer HTML file with a content include along with common template functions.

func AssembleString

func AssembleString(tmplFuncs template.FuncMap, src string) (*template.Template, error)

AssembleString like Assemble but using a string as a source for the template

func Join

func Join(maps ...template.FuncMap) template.FuncMap

Join take one or more func maps and returns an aggregate one.

func Page

func Page(filename string, tmplFuncs template.FuncMap) (*template.Template, error)

Page generate a template struct with Time and Page functions attach.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL