summaryrefslogtreecommitdiffstats
path: root/hugolib/shortcode.go
diff options
context:
space:
mode:
authorspf13 <steve.francia@gmail.com>2013-07-04 11:32:55 -0400
committerspf13 <steve.francia@gmail.com>2013-07-04 11:32:55 -0400
commit6e16449e5fca5df192de04430af26a46ea9ceb2d (patch)
treeefa5ce31f86a99b4a8f410fda0784dd4cc8f9a92 /hugolib/shortcode.go
parent50a1d6f3f155ab837310e00ffb309a9199773c73 (diff)
adding hugo
Diffstat (limited to 'hugolib/shortcode.go')
-rw-r--r--hugolib/shortcode.go131
1 files changed, 131 insertions, 0 deletions
diff --git a/hugolib/shortcode.go b/hugolib/shortcode.go
new file mode 100644
index 000000000..fac6f5a2e
--- /dev/null
+++ b/hugolib/shortcode.go
@@ -0,0 +1,131 @@
+// 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 hugolib
+
+import (
+ "bytes"
+ "fmt"
+ "html/template"
+ "strings"
+ "unicode"
+)
+
+var _ = fmt.Println
+
+type ShortcodeFunc func([]string) string
+
+type Shortcode struct {
+ Name string
+ Func ShortcodeFunc
+}
+
+type ShortcodeWithPage struct {
+ Params interface{}
+ Page *Page
+}
+
+type Shortcodes map[string]ShortcodeFunc
+
+func ShortcodesHandle(stringToParse string, p *Page, t *template.Template) string {
+ posStart := strings.Index(stringToParse, "{{%")
+ if posStart > 0 {
+ posEnd := strings.Index(stringToParse[posStart:], "%}}") + posStart
+ if posEnd > posStart {
+ name, par := SplitParams(stringToParse[posStart+3 : posEnd])
+ params := Tokenize(par)
+ var data = &ShortcodeWithPage{Params: params, Page: p}
+ newString := stringToParse[:posStart] + ShortcodeRender(name, data, t) + ShortcodesHandle(stringToParse[posEnd+3:], p, t)
+ return newString
+ }
+ }
+ return stringToParse
+}
+
+func StripShortcodes(stringToParse string) string {
+ posStart := strings.Index(stringToParse, "{{%")
+ if posStart > 0 {
+ posEnd := strings.Index(stringToParse[posStart:], "%}}") + posStart
+ if posEnd > posStart {
+ newString := stringToParse[:posStart] + StripShortcodes(stringToParse[posEnd+3:])
+ return newString
+ }
+ }
+ return stringToParse
+}
+
+func Tokenize(in string) interface{} {
+ first := strings.Fields(in)
+ var final = make([]string, 0)
+ var keys = make([]string, 0)
+ inQuote := false
+ start := 0
+
+ for i, v := range first {
+ index := strings.Index(v, "=")
+
+ if !inQuote {
+ if index > 1 {
+ keys = append(keys, v[:index])
+ v = v[index+1:]
+ }
+ }
+
+ if !strings.HasPrefix(v, "&ldquo;") && !inQuote {
+ final = append(final, v)
+ } else if inQuote && strings.HasSuffix(v, "&rdquo;") && !strings.HasSuffix(v, "\\\"") {
+ first[i] = v[:len(v)-7]
+ final = append(final, strings.Join(first[start:i+1], " "))
+ inQuote = false
+ } else if strings.HasPrefix(v, "&ldquo;") && !inQuote {
+ if strings.HasSuffix(v, "&rdquo;") {
+ final = append(final, v[7:len(v)-7])
+ } else {
+ start = i
+ first[i] = v[7:]
+ inQuote = true
+ }
+ }
+
+ // No closing "... just make remainder the final token
+ if inQuote && i == len(first) {
+ final = append(final, first[start:len(first)]...)
+ }
+ }
+
+ if len(keys) > 0 {
+ var m = make(map[string]string)
+ for i, k := range keys {
+ m[k] = final[i]
+ }
+
+ return m
+ }
+
+ return final
+}
+
+func SplitParams(in string) (name string, par2 string) {
+ i := strings.IndexFunc(strings.TrimSpace(in), unicode.IsSpace)
+ if i < 1 {
+ return strings.TrimSpace(in), ""
+ }
+
+ return strings.TrimSpace(in[:i+1]), strings.TrimSpace(in[i+1:])
+}
+
+func ShortcodeRender(name string, data *ShortcodeWithPage, t *template.Template) string {
+ buffer := new(bytes.Buffer)
+ t.ExecuteTemplate(buffer, "shortcodes/"+name+".html", data)
+ return buffer.String()
+}