mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-30 01:43:32 -05:00
better handling of detecting markup format
This commit is contained in:
parent
f432b187a0
commit
d0ef3d43bd
2 changed files with 24 additions and 8 deletions
|
@ -490,17 +490,35 @@ func (p *Page) ExecuteTemplate(layout string) *bytes.Buffer {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (page *Page) guessMarkupType() string {
|
func (page *Page) guessMarkupType() string {
|
||||||
|
// First try the explicitly set markup from the frontmatter
|
||||||
if page.Markup != "" {
|
if page.Markup != "" {
|
||||||
return page.Markup
|
format := guessType(page.Markup)
|
||||||
|
if format != "unknown" {
|
||||||
|
return format
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasSuffix(page.FileName, ".md") {
|
// Then try to guess from the extension
|
||||||
return "md"
|
ext := strings.ToLower(path.Ext(page.FileName))
|
||||||
|
if strings.HasPrefix(ext, ".") {
|
||||||
|
return guessType(ext[1:])
|
||||||
}
|
}
|
||||||
|
|
||||||
return "unknown"
|
return "unknown"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func guessType(in string) string {
|
||||||
|
switch in {
|
||||||
|
case "md", "markdown", "mdown":
|
||||||
|
return "markdown"
|
||||||
|
case "rst":
|
||||||
|
return "rst"
|
||||||
|
case "html", "htm":
|
||||||
|
return "html"
|
||||||
|
}
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
|
||||||
func (page *Page) parse(reader io.Reader) error {
|
func (page *Page) parse(reader io.Reader) error {
|
||||||
p, err := parser.ReadFrom(reader)
|
p, err := parser.ReadFrom(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -530,14 +548,10 @@ func (page *Page) parse(reader io.Reader) error {
|
||||||
|
|
||||||
func (page *Page) Convert() error {
|
func (page *Page) Convert() error {
|
||||||
switch page.guessMarkupType() {
|
switch page.guessMarkupType() {
|
||||||
case "md", "markdown", "mdown":
|
case "markdown":
|
||||||
page.convertMarkdown(bytes.NewReader([]byte(page.Content)))
|
page.convertMarkdown(bytes.NewReader([]byte(page.Content)))
|
||||||
case "rst":
|
case "rst":
|
||||||
page.convertRestructuredText(bytes.NewReader([]byte(page.Content)))
|
page.convertRestructuredText(bytes.NewReader([]byte(page.Content)))
|
||||||
case "html":
|
|
||||||
fallthrough
|
|
||||||
default:
|
|
||||||
page.Content = template.HTML(page.Content)
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -229,6 +229,7 @@ func TestSkipRender(t *testing.T) {
|
||||||
{"sect/doc5.html", []byte("<!doctype html><html>{{ template \"head\" }}<body>body5</body></html>"), "sect"},
|
{"sect/doc5.html", []byte("<!doctype html><html>{{ template \"head\" }}<body>body5</body></html>"), "sect"},
|
||||||
{"sect/doc6.html", []byte("<!doctype html><html>{{ template \"head_abs\" }}<body>body5</body></html>"), "sect"},
|
{"sect/doc6.html", []byte("<!doctype html><html>{{ template \"head_abs\" }}<body>body5</body></html>"), "sect"},
|
||||||
{"doc7.html", []byte("<html><body>doc7 content</body></html>"), ""},
|
{"doc7.html", []byte("<html><body>doc7 content</body></html>"), ""},
|
||||||
|
{"sect/doc8.html", []byte("---\nmarkup: md\n---\n# title\nsome *content*"), "sect"},
|
||||||
}
|
}
|
||||||
|
|
||||||
s := &Site{
|
s := &Site{
|
||||||
|
@ -267,6 +268,7 @@ func TestSkipRender(t *testing.T) {
|
||||||
{"sect/doc5.html", "<!doctype html><html><head><script src=\"script.js\"></script></head><body>body5</body></html>"},
|
{"sect/doc5.html", "<!doctype html><html><head><script src=\"script.js\"></script></head><body>body5</body></html>"},
|
||||||
{"sect/doc6.html", "<!doctype html><html><head><script src=\"http://auth/bub/script.js\"></script></head><body>body5</body></html>"},
|
{"sect/doc6.html", "<!doctype html><html><head><script src=\"http://auth/bub/script.js\"></script></head><body>body5</body></html>"},
|
||||||
{"doc7.html", "<html><body>doc7 content</body></html>"},
|
{"doc7.html", "<html><body>doc7 content</body></html>"},
|
||||||
|
{"sect/doc8.html", "<h1>title</h1>\n\n<p>some <em>content</em></p>\n"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
|
Loading…
Reference in a new issue