mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
importer: fix jekyll import highlight options
This commit is contained in:
parent
5068681707
commit
ab9214768d
2 changed files with 49 additions and 1 deletions
|
@ -25,6 +25,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/parser/metadecoders"
|
"github.com/gohugoio/hugo/parser/metadecoders"
|
||||||
|
|
||||||
|
@ -545,7 +546,6 @@ func convertJekyllContent(m interface{}, content string) string {
|
||||||
}{
|
}{
|
||||||
{regexp.MustCompile("(?i)<!-- more -->"), "<!--more-->"},
|
{regexp.MustCompile("(?i)<!-- more -->"), "<!--more-->"},
|
||||||
{regexp.MustCompile(`\{%\s*raw\s*%\}\s*(.*?)\s*\{%\s*endraw\s*%\}`), "$1"},
|
{regexp.MustCompile(`\{%\s*raw\s*%\}\s*(.*?)\s*\{%\s*endraw\s*%\}`), "$1"},
|
||||||
{regexp.MustCompile(`{%\s*highlight\s*(.*?)\s*%}`), "{{< highlight $1 >}}"},
|
|
||||||
{regexp.MustCompile(`{%\s*endhighlight\s*%}`), "{{< / highlight >}}"},
|
{regexp.MustCompile(`{%\s*endhighlight\s*%}`), "{{< / highlight >}}"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -559,6 +559,7 @@ func convertJekyllContent(m interface{}, content string) string {
|
||||||
}{
|
}{
|
||||||
// Octopress image tag: http://octopress.org/docs/plugins/image-tag/
|
// Octopress image tag: http://octopress.org/docs/plugins/image-tag/
|
||||||
{regexp.MustCompile(`{%\s+img\s*(.*?)\s*%}`), replaceImageTag},
|
{regexp.MustCompile(`{%\s+img\s*(.*?)\s*%}`), replaceImageTag},
|
||||||
|
{regexp.MustCompile(`{%\s*highlight\s*(.*?)\s*%}`), replaceHighlightTag},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, replace := range replaceListFunc {
|
for _, replace := range replaceListFunc {
|
||||||
|
@ -568,6 +569,50 @@ func convertJekyllContent(m interface{}, content string) string {
|
||||||
return content
|
return content
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func replaceHighlightTag(match string) string {
|
||||||
|
r := regexp.MustCompile(`{%\s*highlight\s*(.*?)\s*%}`)
|
||||||
|
parts := r.FindStringSubmatch(match)
|
||||||
|
lastQuote := rune(0)
|
||||||
|
f := func(c rune) bool {
|
||||||
|
switch {
|
||||||
|
case c == lastQuote:
|
||||||
|
lastQuote = rune(0)
|
||||||
|
return false
|
||||||
|
case lastQuote != rune(0):
|
||||||
|
return false
|
||||||
|
case unicode.In(c, unicode.Quotation_Mark):
|
||||||
|
lastQuote = c
|
||||||
|
return false
|
||||||
|
default:
|
||||||
|
return unicode.IsSpace(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// splitting string by space but considering quoted section
|
||||||
|
items := strings.FieldsFunc(parts[1], f)
|
||||||
|
|
||||||
|
result := bytes.NewBufferString("{{< highlight ")
|
||||||
|
result.WriteString(items[0]) // language
|
||||||
|
options := items[1:]
|
||||||
|
for i, opt := range options {
|
||||||
|
opt = strings.Replace(opt, "\"", "", -1)
|
||||||
|
if opt == "linenos" {
|
||||||
|
opt = "linenos=table"
|
||||||
|
}
|
||||||
|
if i == 0 {
|
||||||
|
opt = " \"" + opt
|
||||||
|
}
|
||||||
|
if i < len(options)-1 {
|
||||||
|
opt += ","
|
||||||
|
} else if i == len(options)-1 {
|
||||||
|
opt += "\""
|
||||||
|
}
|
||||||
|
result.WriteString(opt)
|
||||||
|
}
|
||||||
|
|
||||||
|
result.WriteString(" >}}")
|
||||||
|
return result.String()
|
||||||
|
}
|
||||||
|
|
||||||
func replaceImageTag(match string) string {
|
func replaceImageTag(match string) string {
|
||||||
r := regexp.MustCompile(`{%\s+img\s*(\p{L}*)\s+([\S]*/[\S]+)\s+(\d*)\s*(\d*)\s*(.*?)\s*%}`)
|
r := regexp.MustCompile(`{%\s+img\s*(\p{L}*)\s+([\S]*/[\S]+)\s+(\d*)\s*(\d*)\s*(.*?)\s*%}`)
|
||||||
result := bytes.NewBufferString("{{< figure ")
|
result := bytes.NewBufferString("{{< figure ")
|
||||||
|
|
|
@ -97,6 +97,9 @@ func TestConvertJekyllContent(t *testing.T) {
|
||||||
{map[interface{}]interface{}{},
|
{map[interface{}]interface{}{},
|
||||||
"{% highlight go %}\nvar s int\n{% endhighlight %}",
|
"{% highlight go %}\nvar s int\n{% endhighlight %}",
|
||||||
"{{< highlight go >}}\nvar s int\n{{< / highlight >}}"},
|
"{{< highlight go >}}\nvar s int\n{{< / highlight >}}"},
|
||||||
|
{map[interface{}]interface{}{},
|
||||||
|
"{% highlight go linenos hl_lines=\"1 2\" %}\nvar s string\nvar i int\n{% endhighlight %}",
|
||||||
|
"{{< highlight go \"linenos=table,hl_lines=1 2\" >}}\nvar s string\nvar i int\n{{< / highlight >}}"},
|
||||||
|
|
||||||
// Octopress image tag
|
// Octopress image tag
|
||||||
{map[interface{}]interface{}{},
|
{map[interface{}]interface{}{},
|
||||||
|
|
Loading…
Reference in a new issue