Adding some embedded short codes (including code highlighting)

This commit is contained in:
spf13 2014-01-09 17:33:20 -05:00
parent 13b067b506
commit 3fd6c1a24e
3 changed files with 201 additions and 149 deletions

View file

@ -109,7 +109,10 @@ func FindEnd(str string, name string) (int, int) {
}
func GetTemplate(name string, t bundle.Template) *template.Template {
return t.Lookup("shortcodes/" + name + ".html")
if x := t.Lookup("shortcodes/" + name + ".html"); x != nil {
return x
}
return t.Lookup("_internal/shortcodes/" + name + ".html")
}
func StripShortcodes(stringToParse string) string {
@ -216,6 +219,9 @@ func SplitParams(in string) (name string, par2 string) {
func ShortcodeRender(tmpl *template.Template, data *ShortcodeWithPage) string {
buffer := new(bytes.Buffer)
tmpl.Execute(buffer, data)
err := tmpl.Execute(buffer, data)
if err != nil {
fmt.Println("error processing shortcode", tmpl.Name(), "\n ERR:", err)
}
return buffer.String()
}

View file

@ -0,0 +1,45 @@
// Copyright © 2013 Steve Francia <spf@spf13.com>.
//
// Licensed under the Simple Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://opensource.org/licenses/Simple-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bundle
type Tmpl struct {
Name string
Data string
}
func (t *GoHtmlTemplate) EmbedShortcodes() {
const k = "shortcodes"
t.AddInternalTemplate(k, "highlight.html", `{{ $lang := index .Params 0 }}{{ highlight .Inner $lang }}`)
t.AddInternalTemplate(k, "test.html", `This is a simple Test`)
t.AddInternalTemplate(k, "figure.html", `<!-- image -->
<figure {{ if isset .Params "class" }}class="{{ index .Params "class" }}"{{ end }}>
{{ if isset .Params "link"}}<a href="{{ index .Params "link"}}">{{ end }}
<img src="{{ index .Params "src" }}" {{ if or (isset .Params "alt") (isset .Params "caption") }}alt="{{ if isset .Params "alt"}}{{ index .Params "alt"}}{{else}}{{ index .Params "caption" }}{{ end }}"{{ end }} />
{{ if isset .Params "link"}}</a>{{ end }}
{{ if or (or (isset .Params "title") (isset .Params "caption")) (isset .Params "attr")}}
<figcaption>{{ if isset .Params "title" }}
<h4>{{ index .Params "title" }}</h4>{{ end }}
{{ if or (isset .Params "caption") (isset .Params "attr")}}<p>
{{ index .Params "caption" }}
{{ if isset .Params "attrlink"}}<a href="{{ index .Params "attrlink"}}"> {{ end }}
{{ index .Params "attr" }}
{{ if isset .Params "attrlink"}}</a> {{ end }}
</p> {{ end }}
</figcaption>
{{ end }}
</figure>
<!-- image -->`)
}

View file

@ -173,6 +173,7 @@ func NewTemplate() Template {
}
func (t *GoHtmlTemplate) LoadEmbedded() {
t.EmbedShortcodes()
}
func (t *GoHtmlTemplate) AddInternalTemplate(prefix, name, tpl string) error {