15
15
package pandoc
16
16
17
17
import (
18
+ "strings"
19
+
18
20
"github.com/cli/safeexec"
19
21
"github.com/gohugoio/hugo/htesting"
22
+ "github.com/mitchellh/mapstructure"
20
23
21
24
"github.com/gohugoio/hugo/identity"
25
+ "github.com/gohugoio/hugo/markup/bibliography"
22
26
"github.com/gohugoio/hugo/markup/internal"
27
+ "github.com/gohugoio/hugo/markup/pandoc/pandoc_config"
23
28
24
29
"github.com/gohugoio/hugo/markup/converter"
25
30
31
+ "fmt"
32
+ "os"
26
33
"path"
27
34
)
28
35
36
+ type paramer interface {
37
+ Param (interface {}) (interface {}, error )
38
+ }
39
+
40
+ type searchPaths struct {
41
+ Paths []string
42
+ }
43
+
44
+ func (s * searchPaths ) NormalizePath (in_path string ) (string , error ) {
45
+ if path .IsAbs (in_path ) {
46
+ return in_path , nil
47
+ }
48
+
49
+ for _ , p := range s .Paths {
50
+ fp := path .Join (p , in_path )
51
+ if _ , err := os .Stat (fp ); err == nil {
52
+ return fp , nil
53
+ }
54
+ }
55
+ return "" , fmt .Errorf ("Can't find %s" , in_path )
56
+ }
57
+
58
+ func (s * searchPaths ) AsResourcePath () string {
59
+ return strings .Join (s .Paths , ":" )
60
+ }
61
+
29
62
// Provider is the package entry point.
30
63
var Provider converter.ProviderProvider = provider {}
31
64
@@ -35,27 +68,27 @@ type provider struct {
35
68
func (p provider ) New (cfg converter.ProviderConfig ) (converter.Provider , error ) {
36
69
return converter .NewProvider ("pandoc" , func (ctx converter.DocumentContext ) (converter.Converter , error ) {
37
70
return & pandocConverter {
38
- ctx : ctx ,
39
- cfg : cfg ,
71
+ docCtx : ctx ,
72
+ cfg : cfg ,
40
73
}, nil
41
74
}), nil
42
75
}
43
76
44
77
type pandocConverter struct {
45
- ctx converter.DocumentContext
46
- cfg converter.ProviderConfig
78
+ docCtx converter.DocumentContext
79
+ cfg converter.ProviderConfig
47
80
}
48
81
49
82
func (c * pandocConverter ) Convert (ctx converter.RenderContext ) (converter.Result , error ) {
50
- return converter .Bytes (c .getPandocContent (ctx .Src , c . ctx )), nil
83
+ return converter .Bytes (c .getPandocContent (ctx .Src )), nil
51
84
}
52
85
53
86
func (c * pandocConverter ) Supports (feature identity.Identity ) bool {
54
87
return false
55
88
}
56
89
57
90
// getPandocContent calls pandoc as an external helper to convert pandoc markdown to HTML.
58
- func (c * pandocConverter ) getPandocContent (src []byte , ctx converter. DocumentContext ) []byte {
91
+ func (c * pandocConverter ) getPandocContent (src []byte ) []byte {
59
92
logger := c .cfg .Logger
60
93
pandoc_path := getPandocExecPath ()
61
94
if pandoc_path == "" {
@@ -64,21 +97,46 @@ func (c *pandocConverter) getPandocContent(src []byte, ctx converter.DocumentCon
64
97
return src
65
98
}
66
99
67
- arguments := c .cfg .MarkupConfig .Pandoc .AsPandocArguments ()
100
+ searchPathSet := searchPaths {
101
+ Paths : []string {path .Dir (c .docCtx .Filename ), "static" , "." },
102
+ }
103
+
104
+ var pandocConfig pandoc_config.Config = c .cfg .MarkupConfig .Pandoc
105
+ var bibConfig bibliography.Config = c .cfg .MarkupConfig .Bibliography
106
+
107
+ if pageParameters , ok := c .docCtx .Document .(paramer ); ok {
108
+ if bibParam , err := pageParameters .Param ("bibliography" ); err == nil {
109
+ mapstructure .WeakDecode (bibParam , & bibConfig )
110
+ }
111
+
112
+ if pandocParam , err := pageParameters .Param ("pandoc" ); err == nil {
113
+ mapstructure .WeakDecode (pandocParam , & pandocConfig )
114
+ }
115
+ }
68
116
69
- bibliography := c . cfg . MarkupConfig . Bibliography
117
+ arguments := pandocConfig . AsPandocArguments ( & searchPathSet )
70
118
71
- if bibliography .Source != "" {
72
- arguments = append (arguments , "--bibliography" , bibliography .Source )
119
+ if bibConfig .Source != "" {
120
+ sourcePath , err := searchPathSet .NormalizePath (bibConfig .Source )
121
+ if err != nil {
122
+ logger .Errorf ("Can't find bibliography: %s" , bibConfig .Source )
123
+ } else {
124
+ arguments = append (arguments , "--bibliography" , sourcePath )
125
+ }
73
126
}
74
127
75
- if bibliography .CitationStyle != "" {
76
- arguments = append (arguments , "--csl" , bibliography .CitationStyle )
128
+ if bibConfig .CitationStyle != "" {
129
+ citationPath , err := searchPathSet .NormalizePath (bibConfig .CitationStyle )
130
+ if err != nil {
131
+ logger .Errorf ("Can't find citation style: %s" , bibConfig .CitationStyle )
132
+ } else {
133
+ arguments = append (arguments , "--csl" , citationPath )
134
+ }
77
135
}
78
136
79
- arguments = append (arguments , "--resource-path" , path . Dir ( ctx . Filename ))
137
+ arguments = append (arguments , "--resource-path" , searchPathSet . AsResourcePath ( ))
80
138
81
- return internal .ExternallyRenderContent (c .cfg , ctx , src , pandoc_path , arguments )
139
+ return internal .ExternallyRenderContent (c .cfg , c . docCtx , src , pandoc_path , arguments )
82
140
}
83
141
84
142
func getPandocExecPath () string {
0 commit comments