Skip to content
This repository has been archived by the owner on Jun 27, 2020. It is now read-only.

Commit

Permalink
quick save
Browse files Browse the repository at this point in the history
  • Loading branch information
rsdoiel committed Aug 4, 2016
1 parent 1cd3f67 commit e11a865
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 19 deletions.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,53 @@ That would be expressed on the command line as follows
Since we are leveraging Go's [text/template](https://golang.org/pkg/text/template/) the template itself
can be more than a simple substitution.

## Template blocks

The Go text templates support defining blocks and rendering them in conjuction with a main template. This is
also supported by *mkpage*. For each template encountered on the command line it is added to an array of templates
passed and parse by the text template package. This is then executed and output rendered by *mkpage*.

```shell
mkpage "content=string:Hello World" testdata/page.tmpl testdata/header.tmpl testdata/footer.tmpl
```

Here is what *page.tmpl* would look like

```go
{{template "header" . }}

{{.content}}

{{template "footer" . }}
```

The header and footer are then defined in their own template files (though they also could be combined into one).

*header.tmpl*

```go
{{define "header"}}This is the document header{{end}}
```

*footer.tmpl*

```go
{{define "footer"}}This is the footer{{end}}
```

In this example the output would look like

```text
This is the document header
Hello World
This is the footer
```




## Options

In additional to populating a template with values from data sources *mkpage* also includes the
Expand Down
16 changes: 8 additions & 8 deletions cmds/mkpage/mkpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"text/template"

// My package
"github.com/rsdoiel/mkpage"
Expand Down Expand Up @@ -124,7 +124,6 @@ func init() {

func main() {
var (
src []byte
err error
)

Expand All @@ -141,6 +140,7 @@ func main() {
license(os.Stdout, appName)
}

var templateSources []string
data := make(map[string]string)
args := flag.Args()
for i, arg := range args {
Expand All @@ -154,15 +154,15 @@ func main() {
data[pair[0]] = pair[1]
} else {
// Must be the template source
src, err = ioutil.ReadFile(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't read %s %s\n", arg, err)
os.Exit(1)
}
templateSources = append(templateSources, arg)
}
}
err = mkpage.MakePage(os.Stdout, string(src), data, useMarkdownProcessor)
tmpl, err := template.ParseFiles(templateSources...)
if err != nil {
fmt.Fprintf(os.Stderr, "Template parsing failed, %s\n", err)
os.Exit(1)
}
if err := mkpage.MakePage(os.Stdout, tmpl, data, useMarkdownProcessor); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
Expand Down
39 changes: 39 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,45 @@ <h1>mkpage</h1>
<p>Since we are leveraging Go&rsquo;s <a href="https://golang.org/pkg/text/template/">text/template</a> the template itself
can be more than a simple substitution.</p>

<h2>Template blocks</h2>

<p>The Go text templates support defining blocks and rendering them in conjuction with a main template. This is
also supported by <em>mkpage</em>. For each template encountered on the command line it is added to an array of templates
passed and parse by the text template package. This is then executed and output rendered by <em>mkpage</em>.</p>

<pre><code class="language-shell"> mkpage &quot;content=string:Hello World&quot; testdata/page.tmpl testdata/header.tmpl testdata/footer.tmpl
</code></pre>

<p>Here is what <em>page.tmpl</em> would look like</p>

<pre><code class="language-go"> {{template &quot;header&quot; . }}

{{.content}}

{{template &quot;footer&quot; . }}
</code></pre>

<p>The header and footer are then defined in their own template files (though they also could be combined into one).</p>

<p><em>header.tmpl</em></p>

<pre><code class="language-go"> {{define &quot;header&quot;}}This is the document header{{end}}
</code></pre>

<p><em>footer.tmpl</em></p>

<pre><code class="language-go"> {{define &quot;footer&quot;}}This is the footer{{end}}
</code></pre>

<p>In this example the output would look like</p>

<pre><code class="language-text"> This is the document header

Hello World

This is the footer
</code></pre>

<h2>Options</h2>

<p>In additional to populating a template with values from data sources <em>mkpage</em> also includes the
Expand Down
17 changes: 7 additions & 10 deletions mkpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
)

const (
Version = "v0.0.2"
Version = "v0.0.3"
)

// ResolveData takes a data map and reads in the files and URL sources as needed turning
Expand All @@ -58,7 +58,7 @@ func ResolveData(data map[string]string, useMarkdownProcessor bool) (map[string]
case strings.HasPrefix(val, "http://") == true || strings.HasPrefix(val, "https://") == true:
resp, err := http.Get(val)
if err != nil {
return out, err
return out, fmt.Errorf("Error from %s, %s", val, err)
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
Expand All @@ -69,9 +69,10 @@ func ResolveData(data map[string]string, useMarkdownProcessor bool) (map[string]
if contentTypes, ok := resp.Header["Content-Type"]; ok == true && isContentType(contentTypes, "application/json") == true {
var o interface{}
err := json.Unmarshal(buf, &o)
if err == nil {
out[key] = o
if err != nil {
return out, fmt.Errorf("Can't JSON decode %s", val)
}
out[key] = o
} else {
out[key] = string(buf)
}
Expand All @@ -80,7 +81,7 @@ func ResolveData(data map[string]string, useMarkdownProcessor bool) (map[string]
default:
buf, err := ioutil.ReadFile(val)
if err != nil {
return out, err
return out, fmt.Errorf("Can't read %s, %s", val, err)
}
ext := path.Ext(val)
if useMarkdownProcessor == true && strings.Compare(ext, ".md") == 0 {
Expand All @@ -94,14 +95,10 @@ func ResolveData(data map[string]string, useMarkdownProcessor bool) (map[string]
}

// MakePage applies the provided data to the template provided and renders to writer and returns an error if something goes wrong
func MakePage(wr io.Writer, templateSource string, keyValues map[string]string, useMarkdownProcessor bool) error {
func MakePage(wr io.Writer, tmpl *template.Template, keyValues map[string]string, useMarkdownProcessor bool) error {
data, err := ResolveData(keyValues, useMarkdownProcessor)
if err != nil {
return fmt.Errorf("Can't resolve data source %s", err)
}
tmpl, err := template.New("text").Parse(templateSource)
if err != nil {
return err
}
return tmpl.Execute(wr, data)
}
4 changes: 3 additions & 1 deletion mkpage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"path"
"strings"
"testing"
"text/template"

// 3rd Party Packages
"github.com/russross/blackfriday"
Expand Down Expand Up @@ -126,7 +127,8 @@ Weather: {{.weather.data.text}}
var buf bytes.Buffer
wr := io.Writer(&buf)

err := MakePage(wr, src, in, true)
tmpl := template.Must(template.New("text").Parse(src))
err := MakePage(wr, tmpl, in, true)
if err != nil {
t.Error(err)
t.FailNow()
Expand Down
1 change: 1 addition & 0 deletions testdata/footer.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{define "footer"}}This is the footer{{end}}
1 change: 1 addition & 0 deletions testdata/header.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{define "header"}}This is the document header{{end}}
5 changes: 5 additions & 0 deletions testdata/page.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{template "header" . }}

{{.content}}

{{template "footer" . }}

0 comments on commit e11a865

Please sign in to comment.