summaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
authorChris Trimble <trimbo@gmail.com>2019-11-24 09:57:39 -0800
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-11-25 09:46:44 +0100
commit8a89b8582f0f681dc28961adb05ab0bf66da9543 (patch)
tree4dca6f3bb2212bdeb466505237820451e0d0a0ed /commands
parente1175ae83a365e0b17ec5904194e68ff3833e15a (diff)
commands: Fix jekyll metadata import on individual posts
Prior refactor had eliminated writing FrontMatter for Jekyll imports. This fixes that bug as well as adds a regression test. Also removed unused site var, replaced some raw strings, and added regression test for \r\n removal. Fixes #5576
Diffstat (limited to 'commands')
-rw-r--r--commands/import_jekyll.go35
-rw-r--r--commands/import_jekyll_test.go16
2 files changed, 34 insertions, 17 deletions
diff --git a/commands/import_jekyll.go b/commands/import_jekyll.go
index b1cc53378..d2e7b27da 100644
--- a/commands/import_jekyll.go
+++ b/commands/import_jekyll.go
@@ -105,7 +105,7 @@ func (i *importCmd) importFromJekyll(cmd *cobra.Command, args []string) error {
return errors.New("abort: jekyll root contains neither posts nor drafts")
}
- site, err := i.createSiteFromJekyll(jekyllRoot, targetDir, jekyllPostDirs, forceImport)
+ err = i.createSiteFromJekyll(jekyllRoot, targetDir, jekyllPostDirs, forceImport)
if err != nil {
return newUserError(err)
@@ -142,7 +142,7 @@ func (i *importCmd) importFromJekyll(cmd *cobra.Command, args []string) error {
}
fileCount++
- return convertJekyllPost(site, path, relPath, targetDir, draft)
+ return convertJekyllPost(path, relPath, targetDir, draft)
}
for jekyllPostDir, hasAnyPostInDir := range jekyllPostDirs {
@@ -200,22 +200,22 @@ func (i *importCmd) retrieveJekyllPostDir(fs afero.Fs, dir string) (bool, bool)
return false, true
}
-func (i *importCmd) createSiteFromJekyll(jekyllRoot, targetDir string, jekyllPostDirs map[string]bool, force bool) (*hugolib.Site, error) {
+func (i *importCmd) createSiteFromJekyll(jekyllRoot, targetDir string, jekyllPostDirs map[string]bool, force bool) error {
s, err := hugolib.NewSiteDefaultLang()
if err != nil {
- return nil, err
+ return err
}
fs := s.Fs.Source
if exists, _ := helpers.Exists(targetDir, fs); exists {
if isDir, _ := helpers.IsDir(targetDir, fs); !isDir {
- return nil, errors.New("target path \"" + targetDir + "\" exists but is not a directory")
+ return errors.New("target path \"" + targetDir + "\" exists but is not a directory")
}
isEmpty, _ := helpers.IsEmpty(targetDir, fs)
if !isEmpty && !force {
- return nil, errors.New("target path \"" + targetDir + "\" exists and is not empty")
+ return errors.New("target path \"" + targetDir + "\" exists and is not empty")
}
}
@@ -232,7 +232,7 @@ func (i *importCmd) createSiteFromJekyll(jekyllRoot, targetDir string, jekyllPos
i.copyJekyllFilesAndFolders(jekyllRoot, filepath.Join(targetDir, "static"), jekyllPostDirs)
- return s, nil
+ return nil
}
func (i *importCmd) loadJekyllConfig(fs afero.Fs, jekyllRoot string) map[string]interface{} {
@@ -375,7 +375,7 @@ func parseJekyllFilename(filename string) (time.Time, string, error) {
return postDate, postName, nil
}
-func convertJekyllPost(s *hugolib.Site, path, relPath, targetDir string, draft bool) error {
+func convertJekyllPost(path, relPath, targetDir string, draft bool) error {
jww.TRACE.Println("Converting", path)
filename := filepath.Base(path)
@@ -409,7 +409,11 @@ func convertJekyllPost(s *hugolib.Site, path, relPath, targetDir string, draft b
return err
}
- content := convertJekyllContent(newmetadata, string(pf.content))
+ content, err := convertJekyllContent(newmetadata, string(pf.content))
+ if err != nil {
+ jww.ERROR.Println("Converting Jekyll error:", path)
+ return err
+ }
fs := hugofs.Os
if err := helpers.WriteToDisk(targetFile, strings.NewReader(content), fs); err != nil {
@@ -471,7 +475,7 @@ func convertJekyllMetaData(m interface{}, postName string, postDate time.Time, d
return metadata, nil
}
-func convertJekyllContent(m interface{}, content string) string {
+func convertJekyllContent(m interface{}, content string) (string, error) {
metadata, _ := maps.ToStringMapE(m)
lines := strings.Split(content, "\n")
@@ -515,7 +519,16 @@ func convertJekyllContent(m interface{}, content string) string {
content = replace.re.ReplaceAllStringFunc(content, replace.replace)
}
- return content
+ var buf bytes.Buffer
+ if len(metadata) != 0 {
+ err := parser.InterfaceToFrontMatter(m, metadecoders.YAML, &buf)
+ if err != nil {
+ return "", err
+ }
+ }
+ buf.WriteString(content)
+
+ return buf.String(), nil
}
func replaceHighlightTag(match string) string {
diff --git a/commands/import_jekyll_test.go b/commands/import_jekyll_test.go
index 4ae26b95c..c87c224b2 100644
--- a/commands/import_jekyll_test.go
+++ b/commands/import_jekyll_test.go
@@ -91,11 +91,12 @@ func TestConvertJekyllContent(t *testing.T) {
expect string
}{
{map[interface{}]interface{}{},
- `Test content\n<!-- more -->\npart2 content`, `Test content\n<!--more-->\npart2 content`},
+ "Test content\r\n<!-- more -->\npart2 content", "Test content\n<!--more-->\npart2 content"},
{map[interface{}]interface{}{},
- `Test content\n<!-- More -->\npart2 content`, `Test content\n<!--more-->\npart2 content`},
+ "Test content\n<!-- More -->\npart2 content", "Test content\n<!--more-->\npart2 content"},
{map[interface{}]interface{}{"excerpt_separator": "<!--sep-->"},
- `Test content\n<!--sep-->\npart2 content`, `Test content\n<!--more-->\npart2 content`},
+ "Test content\n<!--sep-->\npart2 content",
+ "---\nexcerpt_separator: <!--sep-->\n---\nTest content\n<!--more-->\npart2 content"},
{map[interface{}]interface{}{}, "{% raw %}text{% endraw %}", "text"},
{map[interface{}]interface{}{}, "{%raw%} text2 {%endraw %}", "text2"},
{map[interface{}]interface{}{},
@@ -124,10 +125,13 @@ func TestConvertJekyllContent(t *testing.T) {
{map[interface{}]interface{}{},
"{% img right /placekitten/300/500 'Place Kitten #4' 'An image of a very cute kitten' %}",
"{{< figure class=\"right\" src=\"/placekitten/300/500\" title=\"Place Kitten #4\" alt=\"An image of a very cute kitten\" >}}"},
+ {map[interface{}]interface{}{"category": "book", "layout": "post", "Date": "2015-10-01 12:13:11"},
+ "somecontent",
+ "---\nDate: \"2015-10-01 12:13:11\"\ncategory: book\nlayout: post\n---\nsomecontent"},
}
-
for _, data := range testDataList {
- result := convertJekyllContent(data.metadata, data.content)
- c.Assert(data.expect, qt.Equals, result)
+ result, err := convertJekyllContent(data.metadata, data.content)
+ c.Assert(result, qt.Equals, data.expect)
+ c.Assert(err, qt.IsNil)
}
}