Skip to content

Commit

Permalink
Implement Page bundling and image handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Dec 20, 2017
1 parent 02f2735 commit 60e65ce
Show file tree
Hide file tree
Showing 72 changed files with 4,545 additions and 2,907 deletions.
30 changes: 24 additions & 6 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

131 changes: 78 additions & 53 deletions commands/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
package commands

import (
"errors"
"fmt"
"path/filepath"
"time"

src "github.com/gohugoio/hugo/source"

"github.com/gohugoio/hugo/hugolib"

"path/filepath"

"github.com/gohugoio/hugo/parser"
"github.com/spf13/cast"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -78,6 +81,10 @@ func init() {
}

func convertContents(mark rune) error {
if outputDir == "" && !unsafe {
return newUserError("Unsafe operation not allowed, use --unsafe or set a different output path")
}

cfg, err := InitializeConfig()
if err != nil {
return err
Expand All @@ -88,71 +95,89 @@ func convertContents(mark rune) error {
return err
}

site := h.Sites[0]

if err = site.Initialise(); err != nil {
if err := h.Build(hugolib.BuildCfg{SkipRender: true}); err != nil {
return err
}

if site.Source == nil {
panic("site.Source not set")
}
if len(site.Source.Files()) < 1 {
return errors.New("No source files found")
}
site := h.Sites[0]

contentDir := site.PathSpec.AbsPathify(site.Cfg.GetString("contentDir"))
site.Log.FEEDBACK.Println("processing", len(site.Source.Files()), "content files")
for _, file := range site.Source.Files() {
site.Log.INFO.Println("Attempting to convert", file.LogicalName())
page, err := site.NewPage(file.LogicalName())
if err != nil {
site.Log.FEEDBACK.Println("processing", len(site.AllPages), "content files")
for _, p := range site.AllPages {
if err := convertAndSavePage(p, site, mark); err != nil {
return err
}
}
return nil
}

psr, err := parser.ReadFrom(file.Contents)
if err != nil {
site.Log.ERROR.Println("Error processing file:", file.Path())
return err
}
metadata, err := psr.Metadata()
if err != nil {
site.Log.ERROR.Println("Error processing file:", file.Path())
func convertAndSavePage(p *hugolib.Page, site *hugolib.Site, mark rune) error {
// The resources are not in .Site.AllPages.
for _, r := range p.Resources.ByType("page") {
if err := convertAndSavePage(r.(*hugolib.Page), site, mark); err != nil {
return err
}
}

// better handling of dates in formats that don't have support for them
if mark == parser.FormatToLeadRune("json") || mark == parser.FormatToLeadRune("yaml") || mark == parser.FormatToLeadRune("toml") {
newMetadata := cast.ToStringMap(metadata)
for k, v := range newMetadata {
switch vv := v.(type) {
case time.Time:
newMetadata[k] = vv.Format(time.RFC3339)
}
}
metadata = newMetadata
}
if p.Filename() == "" {
// No content file.
return nil
}

page.SetDir(filepath.Join(contentDir, file.Dir()))
page.SetSourceContent(psr.Content())
if err = page.SetSourceMetaData(metadata, mark); err != nil {
site.Log.ERROR.Printf("Failed to set source metadata for file %q: %s. For more info see For more info see https://github.com/gohugoio/hugo/issues/2458", page.FullFilePath(), err)
continue
}
site.Log.INFO.Println("Attempting to convert", p.LogicalName())
newPage, err := site.NewPage(p.LogicalName())
if err != nil {
return err
}

if outputDir != "" {
if err = page.SaveSourceAs(filepath.Join(outputDir, page.FullFilePath())); err != nil {
return fmt.Errorf("Failed to save file %q: %s", page.FullFilePath(), err)
}
} else {
if unsafe {
if err = page.SaveSource(); err != nil {
return fmt.Errorf("Failed to save file %q: %s", page.FullFilePath(), err)
}
} else {
site.Log.FEEDBACK.Println("Unsafe operation not allowed, use --unsafe or set a different output path")
f, _ := p.File.(src.ReadableFile)
file, err := f.Open()
if err != nil {
site.Log.ERROR.Println("Error reading file:", p.Path())
file.Close()
return nil
}

psr, err := parser.ReadFrom(file)
if err != nil {
site.Log.ERROR.Println("Error processing file:", p.Path())
file.Close()
return err
}

file.Close()

metadata, err := psr.Metadata()
if err != nil {
site.Log.ERROR.Println("Error processing file:", p.Path())
return err
}

// better handling of dates in formats that don't have support for them
if mark == parser.FormatToLeadRune("json") || mark == parser.FormatToLeadRune("yaml") || mark == parser.FormatToLeadRune("toml") {
newMetadata := cast.ToStringMap(metadata)
for k, v := range newMetadata {
switch vv := v.(type) {
case time.Time:
newMetadata[k] = vv.Format(time.RFC3339)
}
}
metadata = newMetadata
}

newPage.SetSourceContent(psr.Content())
if err = newPage.SetSourceMetaData(metadata, mark); err != nil {
site.Log.ERROR.Printf("Failed to set source metadata for file %q: %s. For more info see For more info see https://github.com/gohugoio/hugo/issues/2458", newPage.FullFilePath(), err)
return nil
}

newFilename := p.Filename()
if outputDir != "" {
newFilename = filepath.Join(outputDir, p.Dir(), newPage.LogicalName())
}

if err = newPage.SaveSourceAs(newFilename); err != nil {
return fmt.Errorf("Failed to save file %q: %s", newFilename, err)
}

return nil
}
Loading

0 comments on commit 60e65ce

Please sign in to comment.