forked from gohugoio/hugo
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Virtual Pages via JSON stream
- Loading branch information
1 parent
ab5862c
commit 0f32b37
Showing
10 changed files
with
614 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright © 2014 Steve Francia <[email protected]>. | ||
// | ||
// Licensed under the Simple Public 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://opensource.org/licenses/Simple-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. | ||
|
||
package helpers | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/spf13/afero" | ||
jww "github.com/spf13/jwalterweatherman" | ||
) | ||
|
||
func JsonDecoder(url string, hc *http.Client, fs afero.Fs) (*json.Decoder, error) { | ||
if url == "" { | ||
return nil, nil | ||
} | ||
if strings.Contains(url, "://") { | ||
jww.INFO.Printf("Downloading content JSON: %s ...", url) | ||
res, err := hc.Get(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return json.NewDecoder(res.Body), nil | ||
} | ||
|
||
if e, err := Exists(url, fs); !e { | ||
return nil, err | ||
} | ||
|
||
f, err := fs.Open(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return json.NewDecoder(f), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright © 2014 Steve Francia <[email protected]>. | ||
// | ||
// Licensed under the Simple Public 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://opensource.org/licenses/Simple-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. | ||
|
||
package source | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
|
||
"bytes" | ||
"path/filepath" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/spf13/hugo/helpers" | ||
jww "github.com/spf13/jwalterweatherman" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
type ( | ||
jsonPage struct { | ||
FilePath string `json:"Path"` | ||
Content string `json:"Content"` | ||
} | ||
) | ||
|
||
func (p *jsonPage) Reader() io.Reader { | ||
return bytes.NewReader([]byte(p.Content)) | ||
} | ||
|
||
func (p *jsonPage) Path() string { | ||
return filepath.Clean(p.FilePath) | ||
} | ||
|
||
// jsonStreamToFiles acts as the main function to be called in url.go | ||
func loadJson(hc *http.Client, fs afero.Fs) []Pager { | ||
url := viper.GetString("SourceUrl") | ||
if url == "" { | ||
return nil | ||
} | ||
|
||
dec, err := helpers.JsonDecoder(url, hc, fs) | ||
if err != nil || dec == nil { | ||
jww.ERROR.Printf("Failed to get json resource \"%s\" with error message: %s", url, err) | ||
return nil | ||
} | ||
|
||
c := 0 | ||
sources := make([]Pager, 0, 1000) | ||
jww.INFO.Printf("Generating files from JSON %s", url) | ||
for { | ||
var s jsonPage | ||
if err := dec.Decode(&s); err == io.EOF { | ||
jww.INFO.Printf("Generated %d file/s from JSON stream", c) | ||
break | ||
} else if err != nil { | ||
jww.WARN.Printf("Parser Error in JSON stream: %s", err.Error()) | ||
} else { | ||
sources = append(sources, &s) | ||
c++ | ||
} | ||
} | ||
|
||
return sources | ||
} |
Oops, something went wrong.