Re-render shortcode on template or data file change

Fixes  #1971
This commit is contained in:
Bjørn Erik Pedersen 2016-03-17 15:51:52 +01:00 committed by Steve Francia
parent e5e1bcc271
commit de8fc8761a
4 changed files with 108 additions and 77 deletions

View file

@ -134,6 +134,6 @@ func commonConvert(p *Page, t tpl.Template) HandledResult {
p.Content = helpers.BytesToHTML(tmpContent)
p.TableOfContents = helpers.BytesToHTML(tmpTableOfContents)
p.rendered = true
return HandledResult{err: nil}
}

View file

@ -69,7 +69,8 @@ type Page struct {
linkTitle string
frontmatter []byte
rawContent []byte
contentShortCodes map[string]string
contentShortCodes map[string]string // TODO(bep) this shouldn't be needed.
shortcodes map[string]shortcode
plain string // TODO should be []byte
plainWords []string
plainInit sync.Once
@ -82,6 +83,7 @@ type Page struct {
Source
Position `json:"-"`
Node
rendered bool
}
type Source struct {
@ -907,9 +909,15 @@ func (p *Page) ProcessShortcodes(t tpl.Template) {
// these short codes aren't used until after Page render,
// but processed here to avoid coupling
// TODO(bep) Move this and remove p.contentShortCodes
if !p.rendered {
tmpContent, tmpContentShortCodes, _ := extractAndRenderShortcodes(string(p.rawContent), p, t)
p.rawContent = []byte(tmpContent)
p.contentShortCodes = tmpContentShortCodes
} else {
// shortcode template may have changed, rerender
p.contentShortCodes = renderShortcodes(p.shortcodes, p, t)
}
}

View file

@ -263,14 +263,30 @@ func renderShortcode(sc shortcode, parent *ShortcodeWithPage, p *Page, t tpl.Tem
func extractAndRenderShortcodes(stringToParse string, p *Page, t tpl.Template) (string, map[string]string, error) {
if p.rendered {
panic("Illegal state: Page already marked as rendered, please reuse the shortcodes")
}
content, shortcodes, err := extractShortcodes(stringToParse, p, t)
renderedShortcodes := make(map[string]string)
if err != nil {
// try to render what we have whilst logging the error
jww.ERROR.Println(err.Error())
}
// Save for reuse
// TODO(bep) refactor this
p.shortcodes = shortcodes
renderedShortcodes := renderShortcodes(shortcodes, p, t)
return content, renderedShortcodes, err
}
func renderShortcodes(shortcodes map[string]shortcode, p *Page, t tpl.Template) map[string]string {
renderedShortcodes := make(map[string]string)
for key, sc := range shortcodes {
if sc.err != nil {
// need to have something to replace with
@ -280,8 +296,7 @@ func extractAndRenderShortcodes(stringToParse string, p *Page, t tpl.Template) (
}
}
return content, renderedShortcodes, err
return renderedShortcodes
}
var shortCodeIllegalState = errors.New("Illegal shortcode state")

View file

@ -475,7 +475,6 @@ func (s *Site) ReBuild(events []fsnotify.Event) error {
s.resetPageBuildState()
// If a content file changes, we need to reload only it and re-render the entire site.
if len(sourceChanged) > 0 {
// First step is to read the changed files and (re)place them in site.Pages
// This includes processing any meta-data for that content
@ -510,6 +509,14 @@ func (s *Site) ReBuild(events []fsnotify.Event) error {
go incrementalReadCollator(s, readResults, pageChan, fileConvChan, coordinator, errs)
go converterCollator(s, convertResults, errs)
if len(tmplChanged) > 0 || len(dataChanged) > 0 {
// Do not need to read the files again, but they need conversion
// for shortocde re-rendering.
for _, p := range s.Pages {
pageChan <- p
}
}
for _, ev := range sourceChanged {
if ev.Op&fsnotify.Remove == fsnotify.Remove {
@ -560,6 +567,7 @@ func (s *Site) ReBuild(events []fsnotify.Event) error {
s.timerStep("read & convert pages from source")
if len(sourceChanged) > 0 {
s.setupPrevNext()
if err = s.BuildSiteMeta(); err != nil {
return err