Implement toInt built in action

master v0.1.1
Jason T. Lenz 3 years ago
parent 3902da7abc
commit dea22af226

@ -188,13 +188,13 @@ advisable to avoid naming any elements in frontmatter starting with "pgot".
## Custom pgot actions
In addition to the standard [template
actions](https://golang.org/pkg/text/template/#hdr-Actions), there is currently
one custom template action defined by pgot (below). This is included more as a
simple example rather than a critical feature. For those interested in
implementing custom template actions the
[gotFuncs.go](/lenzj/pgot/src/branch/master/lib/gotFuncs.go) source
code demonstrates how custom template actions can be added.
In addition to the standard
[template actions](https://golang.org/pkg/text/template/#hdr-Actions), there are
currently two custom template actions defined by pgot (below). These are
included more as simple examples rather than critical features. For those
interested in implementing custom template actions the
[gotFuncs.go](/lenzj/pgot/src/branch/master/lib/gotFuncs.go) source code
demonstrates how custom template actions can be added.
```text
{{lnp "label" "url"}}
@ -204,6 +204,15 @@ lnp (link new page) generates the specified url as an html link which opens a
new page when the user selects the link. If label is nil (aka "") then the url
is displayed as the clickable link, otherwise the label itself is displayed.
```text
{{toInt value}}
```
toInt (to integer) is able to convert a value parameter to an integer. Multiple
types can be handled. For example a string representation of a number can be
converted. This can also convert the JSON default number type (float64) to an
integer.
## Compiling from source
### Dependencies

@ -1,13 +1,16 @@
package pgot
import (
"reflect"
"strconv"
"text/template"
)
var (
// This should include all publicly accessible custom got functions
funcMap = template.FuncMap{
"lnp": lnp,
"lnp": lnp,
"toInt": toInt,
}
)
@ -21,3 +24,72 @@ func lnp(label, url string) string {
return "<a href=\"" + url + "\" target=\"_blank\">" + label + "</a>"
}
}
// From html/template/content.go
// Copyright 2011 The Go Authors. All rights reserved.
// indirect returns the value, after dereferencing as many times
// as necessary to reach the base type (or nil).
func indirect(a interface{}) interface{} {
if a == nil {
return nil
}
if t := reflect.TypeOf(a); t.Kind() != reflect.Ptr {
// Avoid creating a reflect.Value if it's not a pointer.
return a
}
v := reflect.ValueOf(a)
for v.Kind() == reflect.Ptr && !v.IsNil() {
v = v.Elem()
}
return v.Interface()
}
// Leveraged from https://github.com/spf13/cast
// caste.go
// Copyright (c) 2014 Steve Francia
// toInt casts an interface to an int type.
func toInt(i interface{}) int {
i = indirect(i)
switch s := i.(type) {
case int:
return s
case int64:
return int(s)
case int32:
return int(s)
case int16:
return int(s)
case int8:
return int(s)
case uint:
return int(s)
case uint64:
return int(s)
case uint32:
return int(s)
case uint16:
return int(s)
case uint8:
return int(s)
case float64:
return int(s)
case float32:
return int(s)
case string:
v, err := strconv.ParseInt(s, 0, 0)
if err == nil {
return int(v)
}
panic("unable to cast variable to int")
case bool:
if s {
return 1
}
return 0
case nil:
return 0
default:
panic("unable to cast variable to int")
}
}

@ -14,8 +14,8 @@ import (
var Version string
func main() {
// Get application name as executed from command prompt
appName := filepath.Base(os.Args[0])
// Get application name as executed from command prompt
appName := filepath.Base(os.Args[0])
// Set up formatting for error messages
log.SetFlags(0)
@ -25,22 +25,22 @@ func main() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(),
"Usage: %s [OPTION]... [FILE]...\n"+
"Process a got (golang template) file and send transformed text to output.\n"+
"Options:\n", appName)
"Process a got (golang template) file and send transformed text to output.\n"+
"Options:\n", appName)
flag.PrintDefaults()
}
var oflag = flag.String("o", "-", "output `file` path")
var iflag = flag.String("i", "", "colon separated list of `paths` to search with pgotInclude")
var dflag = flag.String("d", "", "string of json frontmatter to include")
var vflag = flag.Bool("v", false, "display " + appName + " version")
var vflag = flag.Bool("v", false, "display "+appName+" version")
flag.Parse()
// Display application version if requested
if *vflag {
fmt.Println(appName + " " + Version)
os.Exit(0)
}
// Display application version if requested
if *vflag {
fmt.Println(appName + " " + Version)
os.Exit(0)
}
// Prepare input and output streams
var (

@ -194,13 +194,13 @@ advisable to avoid naming any elements in frontmatter starting with "pgot".
## Custom pgot actions
In addition to the standard [template
actions](https://golang.org/pkg/text/template/#hdr-Actions), there is currently
one custom template action defined by pgot (below). This is included more as a
simple example rather than a critical feature. For those interested in
implementing custom template actions the
[gotFuncs.go]({{.repLoc}}{{.rname}}/src/branch/master/lib/gotFuncs.go) source
code demonstrates how custom template actions can be added.
In addition to the standard
[template actions](https://golang.org/pkg/text/template/#hdr-Actions), there are
currently two custom template actions defined by pgot (below). These are
included more as simple examples rather than critical features. For those
interested in implementing custom template actions the
[gotFuncs.go](/lenzj/pgot/src/branch/master/lib/gotFuncs.go) source code
demonstrates how custom template actions can be added.
```text
{{`{{lnp "label" "url"}}`}}
@ -210,6 +210,15 @@ lnp (link new page) generates the specified url as an html link which opens a
new page when the user selects the link. If label is nil (aka "") then the url
is displayed as the clickable link, otherwise the label itself is displayed.
```text
{{`{{toInt value}}`}}
```
toInt (to integer) is able to convert a value parameter to an integer. Multiple
types can be handled. For example a string representation of a number can be
converted. This can also convert the JSON default number type (float64) to an
integer.
## Compiling from source
### Dependencies

@ -0,0 +1 @@
pgot: template: .:1:2: executing "." at <lnp>: wrong number of args for lnp: want 2 got 0

@ -0,0 +1 @@
pgot: template: .:1:2: executing "." at <toInt>: wrong number of args for toInt: want 1 got 4

@ -0,0 +1,3 @@
;;;
;;;
{{toInt "3.14" 4 5 6}}

@ -0,0 +1 @@
pgot: template: .:1:2: executing "." at <toInt "3.14">: error calling toInt: unable to cast variable to int

@ -0,0 +1,3 @@
;;;
;;;
{{toInt "3.14"}}

@ -0,0 +1,3 @@
;;;
;;;
{{toInt 4345}}

@ -0,0 +1,3 @@
;;;
;;;
{{toInt "3"}}
Loading…
Cancel
Save