commands: Import Octopress image tag in Jekyll importer - hugo - [fork] hugo port for 9front
 (HTM) git clone git@git.drkhsh.at/hugo.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 5f3ad1c31985450fab8d6772e9cbfcb57cf5cc53
 (DIR) parent 8cd3ea565ae57a8e77d0b0935472944a0643b29d
 (HTM) Author: Stefan Buynov <stefan.buynov@gmail.com>
       Date:   Sat, 22 Apr 2017 23:35:52 +0300
       
       commands: Import Octopress image tag in Jekyll importer
       
       
       Diffstat:
         M commands/import_jekyll.go           |      45 +++++++++++++++++++++++++++++++
         M commands/import_jekyll_test.go      |      20 ++++++++++++++++++++
       
       2 files changed, 65 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/commands/import_jekyll.go b/commands/import_jekyll.go
       @@ -528,5 +528,50 @@ func convertJekyllContent(m interface{}, content string) string {
                        content = replace.re.ReplaceAllString(content, replace.replace)
                }
        
       +        replaceListFunc := []struct {
       +                re      *regexp.Regexp
       +                replace func(string) string
       +        }{
       +                // Octopress image tag: http://octopress.org/docs/plugins/image-tag/
       +                {regexp.MustCompile(`{%\s+img\s*(.*?)\s*%}`), replaceImageTag},
       +        }
       +
       +        for _, replace := range replaceListFunc {
       +                content = replace.re.ReplaceAllStringFunc(content, replace.replace)
       +        }
       +
                return content
        }
       +
       +func replaceImageTag(match string) string {
       +        r := regexp.MustCompile(`{%\s+img\s*(\p{L}*)\s+([\S]*/[\S]+)\s+(\d*)\s*(\d*)\s*(.*?)\s*%}`)
       +        result := bytes.NewBufferString("{{< figure ")
       +        parts := r.FindStringSubmatch(match)
       +        // Index 0 is the entire string, ignore
       +        replaceOptionalPart(result, "class", parts[1])
       +        replaceOptionalPart(result, "src", parts[2])
       +        replaceOptionalPart(result, "width", parts[3])
       +        replaceOptionalPart(result, "height", parts[4])
       +        // title + alt
       +        part := parts[5]
       +        if len(part) > 0 {
       +                splits := strings.Split(part, "'")
       +                lenSplits := len(splits)
       +                if lenSplits == 1 {
       +                        replaceOptionalPart(result, "title", splits[0])
       +                } else if lenSplits == 3 {
       +                        replaceOptionalPart(result, "title", splits[1])
       +                } else if lenSplits == 5 {
       +                        replaceOptionalPart(result, "title", splits[1])
       +                        replaceOptionalPart(result, "alt", splits[3])
       +                }
       +        }
       +        result.WriteString(">}}")
       +        return result.String()
       +
       +}
       +func replaceOptionalPart(buffer *bytes.Buffer, partName string, part string) {
       +        if len(part) > 0 {
       +                buffer.WriteString(partName + "=\"" + part + "\" ")
       +        }
       +}
 (DIR) diff --git a/commands/import_jekyll_test.go b/commands/import_jekyll_test.go
       @@ -97,6 +97,26 @@ func TestConvertJekyllContent(t *testing.T) {
                        {map[interface{}]interface{}{},
                                "{% highlight go %}\nvar s int\n{% endhighlight %}",
                                "{{< highlight go >}}\nvar s int\n{{< / highlight >}}"},
       +
       +                // Octopress image tag
       +                {map[interface{}]interface{}{},
       +                        "{% img http://placekitten.com/890/280 %}",
       +                        "{{< figure src=\"http://placekitten.com/890/280\" >}}"},
       +                {map[interface{}]interface{}{},
       +                        "{% img left http://placekitten.com/320/250 Place Kitten #2 %}",
       +                        "{{< figure class=\"left\" src=\"http://placekitten.com/320/250\" title=\"Place Kitten #2\" >}}"},
       +                {map[interface{}]interface{}{},
       +                        "{% img right http://placekitten.com/300/500 150 250 'Place Kitten #3' %}",
       +                        "{{< figure class=\"right\" src=\"http://placekitten.com/300/500\" width=\"150\" height=\"250\" title=\"Place Kitten #3\" >}}"},
       +                {map[interface{}]interface{}{},
       +                        "{% img right http://placekitten.com/300/500 150 250 'Place Kitten #4' 'An image of a very cute kitten' %}",
       +                        "{{< figure class=\"right\" src=\"http://placekitten.com/300/500\" width=\"150\" height=\"250\" title=\"Place Kitten #4\" alt=\"An image of a very cute kitten\" >}}"},
       +                {map[interface{}]interface{}{},
       +                        "{% img http://placekitten.com/300/500 150 250 'Place Kitten #4' 'An image of a very cute kitten' %}",
       +                        "{{< figure src=\"http://placekitten.com/300/500\" width=\"150\" height=\"250\" title=\"Place Kitten #4\" alt=\"An image of a very cute kitten\" >}}"},
       +                {map[interface{}]interface{}{},
       +                        "{% img right /placekitten/300/500 'Place Kitten #4' 'An image of a very cute kitten' %}",
       +                        "{{< figure class=\"right\" src=\"/placekitten/300/500\" title=\"Place Kitten #4\" alt=\"An image of a very cute kitten\" >}}"},
                }
        
                for _, data := range testDataList {