2017-03-13 18:55:02 -04:00
|
|
|
// Copyright 2017 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-11-29 22:32:53 -05:00
|
|
|
// Package data provides template functions for working with external data
|
|
|
|
// sources.
|
2017-03-13 18:55:02 -04:00
|
|
|
package data
|
|
|
|
|
2017-05-01 23:41:08 -04:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/csv"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2020-10-21 05:17:48 -04:00
|
|
|
"github.com/gohugoio/hugo/common/constants"
|
|
|
|
"github.com/gohugoio/hugo/common/loggers"
|
|
|
|
|
2019-10-10 04:18:30 -04:00
|
|
|
"github.com/spf13/cast"
|
|
|
|
|
2018-11-08 04:24:13 -05:00
|
|
|
"github.com/gohugoio/hugo/cache/filecache"
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/deps"
|
2018-10-03 08:58:09 -04:00
|
|
|
_errors "github.com/pkg/errors"
|
2017-05-01 23:41:08 -04:00
|
|
|
)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
// New returns a new instance of the data-namespaced template functions.
|
|
|
|
func New(deps *deps.Deps) *Namespace {
|
2018-11-08 04:24:13 -05:00
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
return &Namespace{
|
2018-11-08 04:24:13 -05:00
|
|
|
deps: deps,
|
|
|
|
cacheGetCSV: deps.FileCaches.GetCSVCache(),
|
|
|
|
cacheGetJSON: deps.FileCaches.GetJSONCache(),
|
|
|
|
client: http.DefaultClient,
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Namespace provides template functions for the "data" namespace.
|
|
|
|
type Namespace struct {
|
|
|
|
deps *deps.Deps
|
2017-05-01 23:41:08 -04:00
|
|
|
|
2018-11-08 04:24:13 -05:00
|
|
|
cacheGetJSON *filecache.Cache
|
|
|
|
cacheGetCSV *filecache.Cache
|
|
|
|
|
2017-05-01 23:41:08 -04:00
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCSV expects a data separator and one or n-parts of a URL to a resource which
|
|
|
|
// can either be a local or a remote one.
|
|
|
|
// The data separator can be a comma, semi-colon, pipe, etc, but only one character.
|
|
|
|
// If you provide multiple parts for the URL they will be joined together to the final URL.
|
|
|
|
// GetCSV returns nil or a slice slice to use in a short code.
|
2019-10-10 04:18:30 -04:00
|
|
|
func (ns *Namespace) GetCSV(sep string, urlParts ...interface{}) (d [][]string, err error) {
|
|
|
|
url := joinURL(urlParts)
|
2018-11-08 04:24:13 -05:00
|
|
|
cache := ns.cacheGetCSV
|
2017-05-01 23:41:08 -04:00
|
|
|
|
2019-03-24 05:11:16 -04:00
|
|
|
unmarshal := func(b []byte) (bool, error) {
|
2018-11-08 04:24:13 -05:00
|
|
|
if !bytes.Contains(b, []byte(sep)) {
|
2019-03-24 05:11:16 -04:00
|
|
|
return false, _errors.Errorf("cannot find separator %s in CSV for %s", sep, url)
|
2017-05-01 23:41:08 -04:00
|
|
|
}
|
|
|
|
|
2018-11-08 04:24:13 -05:00
|
|
|
if d, err = parseCSV(b, sep); err != nil {
|
|
|
|
err = _errors.Wrapf(err, "failed to parse CSV file %s", url)
|
2017-05-01 23:41:08 -04:00
|
|
|
|
2019-03-24 05:11:16 -04:00
|
|
|
return true, err
|
2017-05-01 23:41:08 -04:00
|
|
|
}
|
|
|
|
|
2019-03-24 05:11:16 -04:00
|
|
|
return false, nil
|
2018-11-08 04:24:13 -05:00
|
|
|
}
|
2017-05-01 23:41:08 -04:00
|
|
|
|
2018-11-08 04:24:13 -05:00
|
|
|
var req *http.Request
|
|
|
|
req, err = http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, _errors.Wrapf(err, "failed to create request for getCSV for resource %s", url)
|
|
|
|
}
|
2018-10-21 06:20:21 -04:00
|
|
|
|
2018-11-08 04:24:13 -05:00
|
|
|
req.Header.Add("Accept", "text/csv")
|
|
|
|
req.Header.Add("Accept", "text/plain")
|
|
|
|
|
|
|
|
err = ns.getResource(cache, unmarshal, req)
|
|
|
|
if err != nil {
|
2020-10-21 05:17:48 -04:00
|
|
|
ns.deps.Log.(loggers.IgnorableLogger).Errorsf(constants.ErrRemoteGetCSV, "Failed to get CSV resource %q: %s", url, err)
|
2019-01-28 18:27:43 -05:00
|
|
|
return nil, nil
|
2017-05-01 23:41:08 -04:00
|
|
|
}
|
2018-09-10 15:02:18 -04:00
|
|
|
|
2017-05-01 23:41:08 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetJSON expects one or n-parts of a URL to a resource which can either be a local or a remote one.
|
|
|
|
// If you provide multiple parts they will be joined together to the final URL.
|
|
|
|
// GetJSON returns nil or parsed JSON to use in a short code.
|
2019-10-10 04:18:30 -04:00
|
|
|
func (ns *Namespace) GetJSON(urlParts ...interface{}) (interface{}, error) {
|
2018-11-08 04:24:13 -05:00
|
|
|
var v interface{}
|
2019-10-10 04:18:30 -04:00
|
|
|
url := joinURL(urlParts)
|
2018-11-08 04:24:13 -05:00
|
|
|
cache := ns.cacheGetJSON
|
2017-05-01 23:41:08 -04:00
|
|
|
|
2018-11-08 04:24:13 -05:00
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, _errors.Wrapf(err, "Failed to create request for getJSON resource %s", url)
|
|
|
|
}
|
2017-05-01 23:41:08 -04:00
|
|
|
|
2019-03-24 05:11:16 -04:00
|
|
|
unmarshal := func(b []byte) (bool, error) {
|
2018-11-08 04:24:13 -05:00
|
|
|
err := json.Unmarshal(b, &v)
|
2017-05-01 23:41:08 -04:00
|
|
|
if err != nil {
|
2019-03-24 05:11:16 -04:00
|
|
|
return true, err
|
2017-05-01 23:41:08 -04:00
|
|
|
}
|
2019-03-24 05:11:16 -04:00
|
|
|
return false, nil
|
2017-05-01 23:41:08 -04:00
|
|
|
}
|
2018-09-10 15:02:18 -04:00
|
|
|
|
2018-11-08 04:24:13 -05:00
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
|
|
|
|
|
err = ns.getResource(cache, unmarshal, req)
|
2018-09-10 15:02:18 -04:00
|
|
|
if err != nil {
|
2020-10-21 05:17:48 -04:00
|
|
|
ns.deps.Log.(loggers.IgnorableLogger).Errorsf(constants.ErrRemoteGetJSON, "Failed to get JSON resource %q: %s", url, err)
|
2019-01-28 18:27:43 -05:00
|
|
|
return nil, nil
|
2018-09-10 15:02:18 -04:00
|
|
|
}
|
2018-11-08 04:24:13 -05:00
|
|
|
|
|
|
|
return v, nil
|
2017-05-01 23:41:08 -04:00
|
|
|
}
|
|
|
|
|
2019-10-10 04:18:30 -04:00
|
|
|
func joinURL(urlParts []interface{}) string {
|
|
|
|
return strings.Join(cast.ToStringSlice(urlParts), "")
|
|
|
|
}
|
|
|
|
|
2017-05-01 23:41:08 -04:00
|
|
|
// parseCSV parses bytes of CSV data into a slice slice string or an error
|
|
|
|
func parseCSV(c []byte, sep string) ([][]string, error) {
|
|
|
|
if len(sep) != 1 {
|
2019-01-28 18:27:43 -05:00
|
|
|
return nil, errors.New("Incorrect length of CSV separator: " + sep)
|
2017-05-01 23:41:08 -04:00
|
|
|
}
|
|
|
|
b := bytes.NewReader(c)
|
|
|
|
r := csv.NewReader(b)
|
|
|
|
rSep := []rune(sep)
|
|
|
|
r.Comma = rSep[0]
|
|
|
|
r.FieldsPerRecord = 0
|
|
|
|
return r.ReadAll()
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|