Add BaseFs to RenderingContext
authorNiklas Fasching <niklas.fasching@gmail.com>
Thu, 3 Oct 2019 21:27:51 +0000 (23:27 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 7 Oct 2019 15:30:15 +0000 (17:30 +0200)
The org mode renderer supports including other files [1]. We don't want to
allow reading of arbitrary files (go-org defaults to ioutil.ReadFile [2]) but want
to make use of the FileSystem abstractions hugo provides. For starters we will
allow reading from the content directory only

[1]: e.g. `#+INCLUDE: ./foo.py src python` includes `foo.py` as a python source
block.

helpers/content.go
hugolib/page__per_output.go

index f6576c04f75335985fe5daa74fa2c20da278a70f..591403cb040647170aca9dbe8d8e86be158522cb 100644 (file)
@@ -27,6 +27,7 @@ import (
        "unicode/utf8"
 
        "github.com/gohugoio/hugo/common/maps"
+       "github.com/gohugoio/hugo/hugolib/filesystems"
        "github.com/niklasfasching/go-org/org"
 
        bp "github.com/gohugoio/hugo/bufferpool"
@@ -34,6 +35,7 @@ import (
        "github.com/miekg/mmark"
        "github.com/mitchellh/mapstructure"
        "github.com/russross/blackfriday"
+       "github.com/spf13/afero"
        jww "github.com/spf13/jwalterweatherman"
 
        "strings"
@@ -466,6 +468,7 @@ func ExtractTOC(content []byte) (newcontent []byte, toc []byte) {
 // for a given content rendering.
 // By creating you must set the Config, otherwise it will panic.
 type RenderingContext struct {
+       BaseFs       *filesystems.BaseFs
        Content      []byte
        PageFmt      string
        DocumentID   string
@@ -752,6 +755,9 @@ func getPandocContent(ctx *RenderingContext) []byte {
 func orgRender(ctx *RenderingContext, c ContentSpec) []byte {
        config := org.New()
        config.Log = jww.WARN
+       config.ReadFile = func(filename string) ([]byte, error) {
+               return afero.ReadFile(ctx.BaseFs.Content.Fs, filename)
+       }
        writer := org.NewHTMLWriter()
        writer.HighlightCodeBlock = func(source, lang string) string {
                highlightedSource, err := c.Highlight(source, lang, "")
index 3638f9669bb02c39c83a6c001c8e0f924cff1312..6a126270367693e027106b9e8389d9359c37c49e 100644 (file)
@@ -143,6 +143,7 @@ func newPageContentOutput(p *pageState) func(f output.Format) (*pageContentOutpu
                                        html := cp.p.s.ContentSpec.RenderBytes(&helpers.RenderingContext{
                                                Content: []byte(cp.p.m.summary), RenderTOC: false, PageFmt: cp.p.m.markup,
                                                Cfg:        p.Language(),
+                                               BaseFs:     p.s.BaseFs,
                                                DocumentID: p.File().UniqueID(), DocumentName: p.File().Path(),
                                                Config: cp.p.getRenderingConfig()})
                                        html = cp.p.s.ContentSpec.TrimShortHTML(html)
@@ -314,6 +315,7 @@ func (cp *pageContentOutput) renderContent(p page.Page, content []byte) []byte {
        return cp.p.s.ContentSpec.RenderBytes(&helpers.RenderingContext{
                Content: content, RenderTOC: true, PageFmt: cp.p.m.markup,
                Cfg:        p.Language(),
+               BaseFs:     cp.p.s.BaseFs,
                DocumentID: p.File().UniqueID(), DocumentName: p.File().Path(),
                Config: cp.p.getRenderingConfig()})
 }