Skip to content

Commit 5f3ad1c

Browse files
buynovbep
authored andcommitted
commands: Import Octopress image tag in Jekyll importer
1 parent 8cd3ea5 commit 5f3ad1c

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

‎commands/import_jekyll.go‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,5 +528,50 @@ func convertJekyllContent(m interface{}, content string) string {
528528
content = replace.re.ReplaceAllString(content, replace.replace)
529529
}
530530

531+
replaceListFunc := []struct {
532+
re *regexp.Regexp
533+
replace func(string) string
534+
}{
535+
// Octopress image tag: http://octopress.org/docs/plugins/image-tag/
536+
{regexp.MustCompile(`{%\s+img\s*(.*?)\s*%}`), replaceImageTag},
537+
}
538+
539+
for _, replace := range replaceListFunc {
540+
content = replace.re.ReplaceAllStringFunc(content, replace.replace)
541+
}
542+
531543
return content
532544
}
545+
546+
func replaceImageTag(match string) string {
547+
r := regexp.MustCompile(`{%\s+img\s*(\p{L}*)\s+([\S]*/[\S]+)\s+(\d*)\s*(\d*)\s*(.*?)\s*%}`)
548+
result := bytes.NewBufferString("{{< figure ")
549+
parts := r.FindStringSubmatch(match)
550+
// Index 0 is the entire string, ignore
551+
replaceOptionalPart(result, "class", parts[1])
552+
replaceOptionalPart(result, "src", parts[2])
553+
replaceOptionalPart(result, "width", parts[3])
554+
replaceOptionalPart(result, "height", parts[4])
555+
// title + alt
556+
part := parts[5]
557+
if len(part) > 0 {
558+
splits := strings.Split(part, "'")
559+
lenSplits := len(splits)
560+
if lenSplits == 1 {
561+
replaceOptionalPart(result, "title", splits[0])
562+
} else if lenSplits == 3 {
563+
replaceOptionalPart(result, "title", splits[1])
564+
} else if lenSplits == 5 {
565+
replaceOptionalPart(result, "title", splits[1])
566+
replaceOptionalPart(result, "alt", splits[3])
567+
}
568+
}
569+
result.WriteString(">}}")
570+
return result.String()
571+
572+
}
573+
func replaceOptionalPart(buffer *bytes.Buffer, partName string, part string) {
574+
if len(part) > 0 {
575+
buffer.WriteString(partName + "=\"" + part + "\" ")
576+
}
577+
}

‎commands/import_jekyll_test.go‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,26 @@ func TestConvertJekyllContent(t *testing.T) {
9797
{map[interface{}]interface{}{},
9898
"{% highlight go %}\nvar s int\n{% endhighlight %}",
9999
"{{< highlight go >}}\nvar s int\n{{< / highlight >}}"},
100+
101+
// Octopress image tag
102+
{map[interface{}]interface{}{},
103+
"{% img http://placekitten.com/890/280 %}",
104+
"{{< figure src=\"http://placekitten.com/890/280\" >}}"},
105+
{map[interface{}]interface{}{},
106+
"{% img left http://placekitten.com/320/250 Place Kitten #2 %}",
107+
"{{< figure class=\"left\" src=\"http://placekitten.com/320/250\" title=\"Place Kitten #2\" >}}"},
108+
{map[interface{}]interface{}{},
109+
"{% img right http://placekitten.com/300/500 150 250 'Place Kitten #3' %}",
110+
"{{< figure class=\"right\" src=\"http://placekitten.com/300/500\" width=\"150\" height=\"250\" title=\"Place Kitten #3\" >}}"},
111+
{map[interface{}]interface{}{},
112+
"{% img right http://placekitten.com/300/500 150 250 'Place Kitten #4' 'An image of a very cute kitten' %}",
113+
"{{< 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\" >}}"},
114+
{map[interface{}]interface{}{},
115+
"{% img http://placekitten.com/300/500 150 250 'Place Kitten #4' 'An image of a very cute kitten' %}",
116+
"{{< figure src=\"http://placekitten.com/300/500\" width=\"150\" height=\"250\" title=\"Place Kitten #4\" alt=\"An image of a very cute kitten\" >}}"},
117+
{map[interface{}]interface{}{},
118+
"{% img right /placekitten/300/500 'Place Kitten #4' 'An image of a very cute kitten' %}",
119+
"{{< figure class=\"right\" src=\"/placekitten/300/500\" title=\"Place Kitten #4\" alt=\"An image of a very cute kitten\" >}}"},
100120
}
101121

102122
for _, data := range testDataList {

0 commit comments

Comments
 (0)