mirror of
https://github.com/gohugoio/hugo.git
synced 2024-05-11 05:54:58 +00:00
Make replaceShortcodeTokens rewrite the input slice
Currently a `[]byte` copy is returned. In most cases this is the safe thing to do, but we should just modify/grow the slice as needed. This is faster and consumes less memory: ``` benchmark old ns/op new ns/op delta BenchmarkReplaceShortcodeTokens-4 7350 4419 -39.88% benchmark old allocs new allocs delta BenchmarkReplaceShortcodeTokens-4 5 1 -80.00% benchmark old bytes new bytes delta BenchmarkReplaceShortcodeTokens-4 4816 1152 -76.08% ``` This commit is aso a small spring cleaning of duplicated code in the different `PageConvert` methods. Fixes #1516
This commit is contained in:
@@ -56,25 +56,7 @@ type markdownHandler struct {
|
||||
|
||||
func (h markdownHandler) Extensions() []string { return []string{"mdown", "markdown", "md"} }
|
||||
func (h markdownHandler) PageConvert(p *Page, t tpl.Template) HandledResult {
|
||||
p.ProcessShortcodes(t)
|
||||
|
||||
tmpContent, tmpTableOfContents := helpers.ExtractTOC(p.renderContent(helpers.RemoveSummaryDivider(p.rawContent)))
|
||||
|
||||
if len(p.contentShortCodes) > 0 {
|
||||
replaced, err := replaceShortcodeTokensInsources(shortcodePlaceholderPrefix, p.contentShortCodes,
|
||||
tmpContent, tmpTableOfContents)
|
||||
if err != nil {
|
||||
jww.FATAL.Printf("Fail to replace shortcode tokens in %s:\n%s", p.BaseFileName(), err.Error())
|
||||
return HandledResult{err: err}
|
||||
}
|
||||
tmpContent = replaced[0]
|
||||
tmpTableOfContents = replaced[1]
|
||||
}
|
||||
|
||||
p.Content = helpers.BytesToHTML(tmpContent)
|
||||
p.TableOfContents = helpers.BytesToHTML(tmpTableOfContents)
|
||||
|
||||
return HandledResult{err: nil}
|
||||
return commonConvert(p, t)
|
||||
}
|
||||
|
||||
type htmlHandler struct {
|
||||
@@ -84,21 +66,18 @@ type htmlHandler struct {
|
||||
func (h htmlHandler) Extensions() []string { return []string{"html", "htm"} }
|
||||
func (h htmlHandler) PageConvert(p *Page, t tpl.Template) HandledResult {
|
||||
p.ProcessShortcodes(t)
|
||||
var content []byte
|
||||
var err error
|
||||
|
||||
if len(p.contentShortCodes) > 0 {
|
||||
content, err = replaceShortcodeTokens(p.rawContent, shortcodePlaceholderPrefix, p.contentShortCodes)
|
||||
p.rawContent, err = replaceShortcodeTokens(p.rawContent, shortcodePlaceholderPrefix, p.contentShortCodes)
|
||||
|
||||
if err != nil {
|
||||
jww.FATAL.Printf("Fail to replace shortcode tokens in %s:\n%s", p.BaseFileName(), err.Error())
|
||||
jww.FATAL.Printf("Failed to replace short code tokens in %s:\n%s", p.BaseFileName(), err.Error())
|
||||
return HandledResult{err: err}
|
||||
}
|
||||
} else {
|
||||
content = p.rawContent
|
||||
}
|
||||
|
||||
p.Content = helpers.BytesToHTML(content)
|
||||
p.Content = helpers.BytesToHTML(p.rawContent)
|
||||
return HandledResult{err: nil}
|
||||
}
|
||||
|
||||
@@ -108,27 +87,7 @@ type asciidocHandler struct {
|
||||
|
||||
func (h asciidocHandler) Extensions() []string { return []string{"asciidoc", "adoc", "ad"} }
|
||||
func (h asciidocHandler) PageConvert(p *Page, t tpl.Template) HandledResult {
|
||||
p.ProcessShortcodes(t)
|
||||
|
||||
// TODO(spf13) Add/Refactor AsciiDoc Logic here
|
||||
tmpContent, tmpTableOfContents := helpers.ExtractTOC(p.renderContent(helpers.RemoveSummaryDivider(p.rawContent)))
|
||||
|
||||
if len(p.contentShortCodes) > 0 {
|
||||
replaced, err := replaceShortcodeTokensInsources(shortcodePlaceholderPrefix, p.contentShortCodes,
|
||||
tmpContent, tmpTableOfContents)
|
||||
if err != nil {
|
||||
jww.FATAL.Printf("Fail to replace shortcode tokens in %s:\n%s", p.BaseFileName(), err.Error())
|
||||
return HandledResult{err: err}
|
||||
}
|
||||
tmpContent = replaced[0]
|
||||
tmpTableOfContents = replaced[1]
|
||||
}
|
||||
|
||||
p.Content = helpers.BytesToHTML(tmpContent)
|
||||
p.TableOfContents = helpers.BytesToHTML(tmpTableOfContents)
|
||||
|
||||
//err := p.Convert()
|
||||
return HandledResult{page: p, err: nil}
|
||||
return commonConvert(p, t)
|
||||
}
|
||||
|
||||
type rstHandler struct {
|
||||
@@ -137,25 +96,7 @@ type rstHandler struct {
|
||||
|
||||
func (h rstHandler) Extensions() []string { return []string{"rest", "rst"} }
|
||||
func (h rstHandler) PageConvert(p *Page, t tpl.Template) HandledResult {
|
||||
p.ProcessShortcodes(t)
|
||||
|
||||
tmpContent, tmpTableOfContents := helpers.ExtractTOC(p.renderContent(helpers.RemoveSummaryDivider(p.rawContent)))
|
||||
|
||||
if len(p.contentShortCodes) > 0 {
|
||||
replaced, err := replaceShortcodeTokensInsources(shortcodePlaceholderPrefix, p.contentShortCodes,
|
||||
tmpContent, tmpTableOfContents)
|
||||
if err != nil {
|
||||
jww.FATAL.Printf("Fail to replace shortcode tokens in %s:\n%s", p.BaseFileName(), err.Error())
|
||||
return HandledResult{err: err}
|
||||
}
|
||||
tmpContent = replaced[0]
|
||||
tmpTableOfContents = replaced[1]
|
||||
}
|
||||
|
||||
p.Content = helpers.BytesToHTML(tmpContent)
|
||||
p.TableOfContents = helpers.BytesToHTML(tmpTableOfContents)
|
||||
|
||||
return HandledResult{err: nil}
|
||||
return commonConvert(p, t)
|
||||
}
|
||||
|
||||
type mmarkHandler struct {
|
||||
@@ -164,21 +105,27 @@ type mmarkHandler struct {
|
||||
|
||||
func (h mmarkHandler) Extensions() []string { return []string{"mmark"} }
|
||||
func (h mmarkHandler) PageConvert(p *Page, t tpl.Template) HandledResult {
|
||||
return commonConvert(p, t)
|
||||
}
|
||||
|
||||
func commonConvert(p *Page, t tpl.Template) HandledResult {
|
||||
p.ProcessShortcodes(t)
|
||||
|
||||
tmpContent, tmpTableOfContents := helpers.ExtractTOC(p.renderContent(helpers.RemoveSummaryDivider(p.rawContent)))
|
||||
var err error
|
||||
|
||||
renderedContent := p.renderContent(helpers.RemoveSummaryDivider(p.rawContent))
|
||||
|
||||
if len(p.contentShortCodes) > 0 {
|
||||
tmpContentWithTokensReplaced, err := replaceShortcodeTokens(tmpContent, shortcodePlaceholderPrefix, p.contentShortCodes)
|
||||
renderedContent, err = replaceShortcodeTokens(renderedContent, shortcodePlaceholderPrefix, p.contentShortCodes)
|
||||
|
||||
if err != nil {
|
||||
jww.FATAL.Printf("Fail to replace short code tokens in %s:\n%s", p.BaseFileName(), err.Error())
|
||||
jww.FATAL.Printf("Failed to replace shortcode tokens in %s:\n%s", p.BaseFileName(), err.Error())
|
||||
return HandledResult{err: err}
|
||||
} else {
|
||||
tmpContent = tmpContentWithTokensReplaced
|
||||
}
|
||||
}
|
||||
|
||||
tmpContent, tmpTableOfContents := helpers.ExtractTOC(renderedContent)
|
||||
|
||||
p.Content = helpers.BytesToHTML(tmpContent)
|
||||
p.TableOfContents = helpers.BytesToHTML(tmpTableOfContents)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user