2016-03-21 19:28:42 -04:00
|
|
|
// Copyright 2016 The Hugo Authors. All rights reserved.
|
2015-12-02 05:36:18 -05:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/helpers"
|
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
|
|
|
"github.com/gohugoio/hugo/hugolib"
|
|
|
|
"github.com/gohugoio/hugo/parser"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/spf13/cast"
|
|
|
|
"github.com/spf13/cobra"
|
2015-12-02 05:36:18 -05:00
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
|
|
|
)
|
|
|
|
|
2015-12-02 05:42:53 -05:00
|
|
|
func init() {
|
|
|
|
importCmd.AddCommand(importJekyllCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
var importCmd = &cobra.Command{
|
|
|
|
Use: "import",
|
|
|
|
Short: "Import your site from others.",
|
|
|
|
Long: `Import your site from other web site generators like Jekyll.
|
|
|
|
|
|
|
|
Import requires a subcommand, e.g. ` + "`hugo import jekyll jekyll_root_path target_path`.",
|
|
|
|
RunE: nil,
|
|
|
|
}
|
|
|
|
|
2015-12-02 05:36:18 -05:00
|
|
|
var importJekyllCmd = &cobra.Command{
|
|
|
|
Use: "jekyll",
|
|
|
|
Short: "hugo import from Jekyll",
|
|
|
|
Long: `hugo import from Jekyll.
|
|
|
|
|
|
|
|
Import from Jekyll requires two paths, e.g. ` + "`hugo import jekyll jekyll_root_path target_path`.",
|
2015-12-02 05:42:53 -05:00
|
|
|
RunE: importFromJekyll,
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2015-12-03 01:51:48 -05:00
|
|
|
func init() {
|
2017-04-26 15:58:10 -04:00
|
|
|
importJekyllCmd.Flags().Bool("force", false, "allow import into non-empty target directory")
|
2015-12-03 01:51:48 -05:00
|
|
|
}
|
|
|
|
|
2015-12-02 05:42:53 -05:00
|
|
|
func importFromJekyll(cmd *cobra.Command, args []string) error {
|
2015-12-02 05:36:18 -05:00
|
|
|
|
|
|
|
if len(args) < 2 {
|
2015-12-02 05:42:53 -05:00
|
|
|
return newUserError(`Import from Jekyll requires two paths, e.g. ` + "`hugo import jekyll jekyll_root_path target_path`.")
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
jekyllRoot, err := filepath.Abs(filepath.Clean(args[0]))
|
|
|
|
if err != nil {
|
2015-12-02 05:42:53 -05:00
|
|
|
return newUserError("Path error:", args[0])
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
targetDir, err := filepath.Abs(filepath.Clean(args[1]))
|
|
|
|
if err != nil {
|
2015-12-02 05:42:53 -05:00
|
|
|
return newUserError("Path error:", args[1])
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
jww.INFO.Println("Import Jekyll from:", jekyllRoot, "to:", targetDir)
|
2015-12-03 01:51:48 -05:00
|
|
|
|
2016-09-11 04:48:31 -04:00
|
|
|
if strings.HasPrefix(filepath.Dir(targetDir), jekyllRoot) {
|
2015-12-03 01:51:48 -05:00
|
|
|
return newUserError("Target path should not be inside the Jekyll root, aborting.")
|
|
|
|
}
|
|
|
|
|
|
|
|
forceImport, _ := cmd.Flags().GetBool("force")
|
2016-03-01 19:06:02 -05:00
|
|
|
|
|
|
|
fs := afero.NewOsFs()
|
|
|
|
jekyllPostDirs, hasAnyPost := getJekyllDirInfo(fs, jekyllRoot)
|
|
|
|
if !hasAnyPost {
|
|
|
|
return errors.New("Your Jekyll root contains neither posts nor drafts, aborting.")
|
|
|
|
}
|
|
|
|
|
|
|
|
site, err := createSiteFromJekyll(jekyllRoot, targetDir, jekyllPostDirs, forceImport)
|
|
|
|
|
2017-01-03 11:28:51 -05:00
|
|
|
if err != nil {
|
2016-03-01 19:06:02 -05:00
|
|
|
return newUserError(err)
|
2015-12-03 01:51:48 -05:00
|
|
|
}
|
|
|
|
|
2016-11-22 12:47:20 -05:00
|
|
|
jww.FEEDBACK.Println("Importing...")
|
2015-12-02 05:36:18 -05:00
|
|
|
|
|
|
|
fileCount := 0
|
|
|
|
callback := func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
relPath, err := filepath.Rel(jekyllRoot, path)
|
|
|
|
if err != nil {
|
2015-12-02 05:42:53 -05:00
|
|
|
return newUserError("Get rel path error:", path)
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
relPath = filepath.ToSlash(relPath)
|
2016-03-24 18:34:12 -04:00
|
|
|
draft := false
|
2015-12-02 05:36:18 -05:00
|
|
|
|
|
|
|
switch {
|
2016-03-01 19:06:02 -05:00
|
|
|
case strings.Contains(relPath, "_posts/"):
|
|
|
|
relPath = filepath.Join("content/post", strings.Replace(relPath, "_posts/", "", -1))
|
|
|
|
case strings.Contains(relPath, "_drafts/"):
|
|
|
|
relPath = filepath.Join("content/draft", strings.Replace(relPath, "_drafts/", "", -1))
|
2015-12-02 05:36:18 -05:00
|
|
|
draft = true
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fileCount++
|
2017-01-03 11:28:51 -05:00
|
|
|
return convertJekyllPost(site, path, relPath, targetDir, draft)
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2016-03-01 19:06:02 -05:00
|
|
|
for jekyllPostDir, hasAnyPostInDir := range jekyllPostDirs {
|
|
|
|
if hasAnyPostInDir {
|
|
|
|
if err = helpers.SymbolicWalk(hugofs.Os, filepath.Join(jekyllRoot, jekyllPostDir), callback); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
2016-03-01 19:06:02 -05:00
|
|
|
|
|
|
|
jww.FEEDBACK.Println("Congratulations!", fileCount, "post(s) imported!")
|
|
|
|
jww.FEEDBACK.Println("Now, start Hugo by yourself:\n" +
|
|
|
|
"$ git clone https://github.com/spf13/herring-cove.git " + args[1] + "/themes/herring-cove")
|
|
|
|
jww.FEEDBACK.Println("$ cd " + args[1] + "\n$ hugo server --theme=herring-cove")
|
|
|
|
|
2016-11-22 12:47:20 -05:00
|
|
|
jww.FEEDBACK.Println("Congratulations!", fileCount, "post(s) imported!")
|
|
|
|
jww.FEEDBACK.Println("Now, start Hugo by yourself:\n" +
|
2016-03-21 18:01:25 -04:00
|
|
|
"$ git clone https://github.com/spf13/herring-cove.git " + args[1] + "/themes/herring-cove")
|
2016-11-22 12:47:20 -05:00
|
|
|
jww.FEEDBACK.Println("$ cd " + args[1] + "\n$ hugo server --theme=herring-cove")
|
2015-12-02 05:42:53 -05:00
|
|
|
|
|
|
|
return nil
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2016-03-01 19:06:02 -05:00
|
|
|
func getJekyllDirInfo(fs afero.Fs, jekyllRoot string) (map[string]bool, bool) {
|
|
|
|
postDirs := make(map[string]bool)
|
|
|
|
hasAnyPost := false
|
|
|
|
if entries, err := ioutil.ReadDir(jekyllRoot); err == nil {
|
|
|
|
for _, entry := range entries {
|
|
|
|
if entry.IsDir() {
|
|
|
|
subDir := filepath.Join(jekyllRoot, entry.Name())
|
|
|
|
if isPostDir, hasAnyPostInDir := retrieveJekyllPostDir(fs, subDir); isPostDir {
|
|
|
|
postDirs[entry.Name()] = hasAnyPostInDir
|
|
|
|
if hasAnyPostInDir {
|
|
|
|
hasAnyPost = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return postDirs, hasAnyPost
|
|
|
|
}
|
|
|
|
|
|
|
|
func retrieveJekyllPostDir(fs afero.Fs, dir string) (bool, bool) {
|
|
|
|
if strings.HasSuffix(dir, "_posts") || strings.HasSuffix(dir, "_drafts") {
|
|
|
|
isEmpty, _ := helpers.IsEmpty(dir, fs)
|
|
|
|
return true, !isEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
if entries, err := ioutil.ReadDir(dir); err == nil {
|
|
|
|
for _, entry := range entries {
|
|
|
|
if entry.IsDir() {
|
|
|
|
subDir := filepath.Join(dir, entry.Name())
|
|
|
|
if isPostDir, hasAnyPost := retrieveJekyllPostDir(fs, subDir); isPostDir {
|
|
|
|
return isPostDir, hasAnyPost
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func createSiteFromJekyll(jekyllRoot, targetDir string, jekyllPostDirs map[string]bool, force bool) (*hugolib.Site, error) {
|
2017-01-10 04:55:03 -05:00
|
|
|
s, err := hugolib.NewSiteDefaultLang()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fs := s.Fs.Source
|
2015-12-03 01:51:48 -05:00
|
|
|
if exists, _ := helpers.Exists(targetDir, fs); exists {
|
|
|
|
if isDir, _ := helpers.IsDir(targetDir, fs); !isDir {
|
2017-01-03 11:28:51 -05:00
|
|
|
return nil, errors.New("Target path \"" + targetDir + "\" already exists but not a directory")
|
2015-12-03 01:51:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
isEmpty, _ := helpers.IsEmpty(targetDir, fs)
|
|
|
|
|
|
|
|
if !isEmpty && !force {
|
2017-01-03 11:28:51 -05:00
|
|
|
return nil, errors.New("Target path \"" + targetDir + "\" already exists and is not empty")
|
2015-12-03 01:51:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
jekyllConfig := loadJekyllConfig(fs, jekyllRoot)
|
2015-12-03 01:51:48 -05:00
|
|
|
|
2015-12-02 05:36:18 -05:00
|
|
|
mkdir(targetDir, "layouts")
|
|
|
|
mkdir(targetDir, "content")
|
|
|
|
mkdir(targetDir, "archetypes")
|
|
|
|
mkdir(targetDir, "static")
|
|
|
|
mkdir(targetDir, "data")
|
|
|
|
mkdir(targetDir, "themes")
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
createConfigFromJekyll(fs, targetDir, "yaml", jekyllConfig)
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2016-03-01 19:06:02 -05:00
|
|
|
copyJekyllFilesAndFolders(jekyllRoot, filepath.Join(targetDir, "static"), jekyllPostDirs)
|
2015-12-03 01:51:48 -05:00
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
return s, nil
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
func loadJekyllConfig(fs afero.Fs, jekyllRoot string) map[string]interface{} {
|
2015-12-02 05:36:18 -05:00
|
|
|
path := filepath.Join(jekyllRoot, "_config.yml")
|
|
|
|
|
|
|
|
exists, err := helpers.Exists(path, fs)
|
|
|
|
|
|
|
|
if err != nil || !exists {
|
2015-12-03 01:51:48 -05:00
|
|
|
jww.WARN.Println("_config.yaml not found: Is the specified Jekyll root correct?")
|
2015-12-02 05:36:18 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := fs.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(f)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := parser.HandleYAMLMetaData(b)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
func createConfigFromJekyll(fs afero.Fs, inpath string, kind string, jekyllConfig map[string]interface{}) (err error) {
|
2015-12-02 05:36:18 -05:00
|
|
|
title := "My New Hugo Site"
|
2016-10-24 14:56:00 -04:00
|
|
|
baseURL := "http://example.org/"
|
2015-12-02 05:36:18 -05:00
|
|
|
|
|
|
|
for key, value := range jekyllConfig {
|
|
|
|
lowerKey := strings.ToLower(key)
|
|
|
|
|
|
|
|
switch lowerKey {
|
|
|
|
case "title":
|
|
|
|
if str, ok := value.(string); ok {
|
|
|
|
title = str
|
|
|
|
}
|
|
|
|
|
|
|
|
case "url":
|
|
|
|
if str, ok := value.(string); ok {
|
2016-10-24 14:56:00 -04:00
|
|
|
baseURL = str
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
in := map[string]interface{}{
|
2016-10-24 14:56:00 -04:00
|
|
|
"baseURL": baseURL,
|
2015-12-02 05:36:18 -05:00
|
|
|
"title": title,
|
|
|
|
"languageCode": "en-us",
|
|
|
|
"disablePathToLower": true,
|
|
|
|
}
|
|
|
|
kind = parser.FormatSanitize(kind)
|
|
|
|
|
2016-12-26 16:23:20 -05:00
|
|
|
var buf bytes.Buffer
|
|
|
|
err = parser.InterfaceToConfig(in, parser.FormatToLeadRune(kind), &buf)
|
2015-12-02 05:36:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-26 16:23:20 -05:00
|
|
|
return helpers.WriteToDisk(filepath.Join(inpath, "config."+kind), &buf, fs)
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2016-03-22 18:44:02 -04:00
|
|
|
func copyFile(source string, dest string) error {
|
2015-12-02 05:36:18 -05:00
|
|
|
sf, err := os.Open(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer sf.Close()
|
|
|
|
df, err := os.Create(dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer df.Close()
|
|
|
|
_, err = io.Copy(df, sf)
|
|
|
|
if err == nil {
|
|
|
|
si, err := os.Stat(source)
|
|
|
|
if err != nil {
|
|
|
|
err = os.Chmod(dest, si.Mode())
|
2016-03-22 18:44:02 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-03-22 18:44:02 -04:00
|
|
|
return nil
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2016-03-22 18:44:02 -04:00
|
|
|
func copyDir(source string, dest string) error {
|
2015-12-02 05:36:18 -05:00
|
|
|
fi, err := os.Stat(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return errors.New(source + " is not a directory")
|
|
|
|
}
|
|
|
|
err = os.MkdirAll(dest, fi.Mode())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
entries, err := ioutil.ReadDir(source)
|
|
|
|
for _, entry := range entries {
|
|
|
|
sfp := filepath.Join(source, entry.Name())
|
|
|
|
dfp := filepath.Join(dest, entry.Name())
|
|
|
|
if entry.IsDir() {
|
|
|
|
err = copyDir(sfp, dfp)
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Println(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = copyFile(sfp, dfp)
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-01 19:06:02 -05:00
|
|
|
func copyJekyllFilesAndFolders(jekyllRoot, dest string, jekyllPostDirs map[string]bool) (err error) {
|
2015-12-02 05:36:18 -05:00
|
|
|
fi, err := os.Stat(jekyllRoot)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return errors.New(jekyllRoot + " is not a directory")
|
|
|
|
}
|
|
|
|
err = os.MkdirAll(dest, fi.Mode())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
entries, err := ioutil.ReadDir(jekyllRoot)
|
|
|
|
for _, entry := range entries {
|
|
|
|
sfp := filepath.Join(jekyllRoot, entry.Name())
|
|
|
|
dfp := filepath.Join(dest, entry.Name())
|
|
|
|
if entry.IsDir() {
|
|
|
|
if entry.Name()[0] != '_' && entry.Name()[0] != '.' {
|
2016-03-01 19:06:02 -05:00
|
|
|
if _, ok := jekyllPostDirs[entry.Name()]; !ok {
|
|
|
|
err = copyDir(sfp, dfp)
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Println(err)
|
|
|
|
}
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lowerEntryName := strings.ToLower(entry.Name())
|
|
|
|
exceptSuffix := []string{".md", ".markdown", ".html", ".htm",
|
|
|
|
".xml", ".textile", "rakefile", "gemfile", ".lock"}
|
|
|
|
isExcept := false
|
|
|
|
for _, suffix := range exceptSuffix {
|
|
|
|
if strings.HasSuffix(lowerEntryName, suffix) {
|
|
|
|
isExcept = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isExcept && entry.Name()[0] != '.' && entry.Name()[0] != '_' {
|
|
|
|
err = copyFile(sfp, dfp)
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseJekyllFilename(filename string) (time.Time, string, error) {
|
|
|
|
re := regexp.MustCompile(`(\d+-\d+-\d+)-(.+)\..*`)
|
|
|
|
r := re.FindAllStringSubmatch(filename, -1)
|
|
|
|
if len(r) == 0 {
|
|
|
|
return time.Now(), "", errors.New("filename not match")
|
|
|
|
}
|
|
|
|
|
2017-03-13 07:50:57 -04:00
|
|
|
postDate, err := time.Parse("2006-1-2", r[0][1])
|
2015-12-02 05:36:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return time.Now(), "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
postName := r[0][2]
|
|
|
|
|
|
|
|
return postDate, postName, nil
|
|
|
|
}
|
|
|
|
|
2017-01-03 11:28:51 -05:00
|
|
|
func convertJekyllPost(s *hugolib.Site, path, relPath, targetDir string, draft bool) error {
|
2015-12-02 05:36:18 -05:00
|
|
|
jww.TRACE.Println("Converting", path)
|
|
|
|
|
|
|
|
filename := filepath.Base(path)
|
|
|
|
postDate, postName, err := parseJekyllFilename(filename)
|
|
|
|
if err != nil {
|
2015-12-14 09:30:58 -05:00
|
|
|
jww.WARN.Printf("Failed to parse filename '%s': %s. Skipping.", filename, err)
|
|
|
|
return nil
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
jww.TRACE.Println(filename, postDate, postName)
|
|
|
|
|
|
|
|
targetFile := filepath.Join(targetDir, relPath)
|
|
|
|
targetParentDir := filepath.Dir(targetFile)
|
|
|
|
os.MkdirAll(targetParentDir, 0777)
|
|
|
|
|
|
|
|
contentBytes, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Println("Read file error:", path)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
psr, err := parser.ReadFrom(bytes.NewReader(contentBytes))
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Println("Parse file error:", path)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata, err := psr.Metadata()
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Println("Processing file error:", path)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newmetadata, err := convertJekyllMetaData(metadata, postName, postDate, draft)
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Println("Convert metadata error:", path)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
jww.TRACE.Println(newmetadata)
|
|
|
|
content := convertJekyllContent(newmetadata, string(psr.Content()))
|
|
|
|
|
2017-01-03 11:28:51 -05:00
|
|
|
page, err := s.NewPage(filename)
|
2015-12-02 05:36:18 -05:00
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Println("New page error", filename)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
page.SetSourceContent([]byte(content))
|
|
|
|
page.SetSourceMetaData(newmetadata, parser.FormatToLeadRune("yaml"))
|
|
|
|
page.SaveSourceAs(targetFile)
|
|
|
|
|
|
|
|
jww.TRACE.Println("Target file:", targetFile)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertJekyllMetaData(m interface{}, postName string, postDate time.Time, draft bool) (interface{}, error) {
|
|
|
|
url := postDate.Format("/2006/01/02/") + postName + "/"
|
|
|
|
|
|
|
|
metadata, err := cast.ToStringMapE(m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if draft {
|
|
|
|
metadata["draft"] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range metadata {
|
|
|
|
lowerKey := strings.ToLower(key)
|
|
|
|
|
|
|
|
switch lowerKey {
|
|
|
|
case "layout":
|
|
|
|
delete(metadata, key)
|
|
|
|
case "permalink":
|
|
|
|
if str, ok := value.(string); ok {
|
|
|
|
url = str
|
|
|
|
}
|
|
|
|
delete(metadata, key)
|
|
|
|
case "category":
|
|
|
|
if str, ok := value.(string); ok {
|
|
|
|
metadata["categories"] = []string{str}
|
|
|
|
}
|
|
|
|
delete(metadata, key)
|
|
|
|
case "excerpt_separator":
|
|
|
|
if key != lowerKey {
|
|
|
|
delete(metadata, key)
|
|
|
|
metadata[lowerKey] = value
|
|
|
|
}
|
|
|
|
case "date":
|
|
|
|
if str, ok := value.(string); ok {
|
|
|
|
re := regexp.MustCompile(`(\d+):(\d+):(\d+)`)
|
|
|
|
r := re.FindAllStringSubmatch(str, -1)
|
|
|
|
if len(r) > 0 {
|
|
|
|
hour, _ := strconv.Atoi(r[0][1])
|
|
|
|
minute, _ := strconv.Atoi(r[0][2])
|
|
|
|
second, _ := strconv.Atoi(r[0][3])
|
|
|
|
postDate = time.Date(postDate.Year(), postDate.Month(), postDate.Day(), hour, minute, second, 0, time.UTC)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(metadata, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata["url"] = url
|
|
|
|
metadata["date"] = postDate.Format(time.RFC3339)
|
|
|
|
|
|
|
|
return metadata, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertJekyllContent(m interface{}, content string) string {
|
|
|
|
metadata, _ := cast.ToStringMapE(m)
|
|
|
|
|
|
|
|
lines := strings.Split(content, "\n")
|
|
|
|
var resultLines []string
|
|
|
|
for _, line := range lines {
|
|
|
|
resultLines = append(resultLines, strings.Trim(line, "\r\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
content = strings.Join(resultLines, "\n")
|
|
|
|
|
|
|
|
excerptSep := "<!--more-->"
|
|
|
|
if value, ok := metadata["excerpt_separator"]; ok {
|
|
|
|
if str, strOk := value.(string); strOk {
|
|
|
|
content = strings.Replace(content, strings.TrimSpace(str), excerptSep, -1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
replaceList := []struct {
|
|
|
|
re *regexp.Regexp
|
|
|
|
replace string
|
|
|
|
}{
|
2017-03-18 11:16:25 -04:00
|
|
|
{regexp.MustCompile("(?i)<!-- more -->"), "<!--more-->"},
|
2015-12-02 05:36:18 -05:00
|
|
|
{regexp.MustCompile(`\{%\s*raw\s*%\}\s*(.*?)\s*\{%\s*endraw\s*%\}`), "$1"},
|
|
|
|
{regexp.MustCompile(`{%\s*highlight\s*(.*?)\s*%}`), "{{< highlight $1 >}}"},
|
|
|
|
{regexp.MustCompile(`{%\s*endhighlight\s*%}`), "{{< / highlight >}}"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, replace := range replaceList {
|
|
|
|
content = replace.re.ReplaceAllString(content, replace.replace)
|
|
|
|
}
|
|
|
|
|
2017-04-22 16:35:52 -04:00
|
|
|
replaceListFunc := []struct {
|
|
|
|
re *regexp.Regexp
|
|
|
|
replace func(string) string
|
|
|
|
}{
|
|
|
|
// Octopress image tag: http://octopress.org/docs/plugins/image-tag/
|
|
|
|
{regexp.MustCompile(`{%\s+img\s*(.*?)\s*%}`), replaceImageTag},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, replace := range replaceListFunc {
|
|
|
|
content = replace.re.ReplaceAllStringFunc(content, replace.replace)
|
|
|
|
}
|
|
|
|
|
2015-12-02 05:36:18 -05:00
|
|
|
return content
|
|
|
|
}
|
2017-04-22 16:35:52 -04:00
|
|
|
|
|
|
|
func replaceImageTag(match string) string {
|
|
|
|
r := regexp.MustCompile(`{%\s+img\s*(\p{L}*)\s+([\S]*/[\S]+)\s+(\d*)\s*(\d*)\s*(.*?)\s*%}`)
|
|
|
|
result := bytes.NewBufferString("{{< figure ")
|
|
|
|
parts := r.FindStringSubmatch(match)
|
|
|
|
// Index 0 is the entire string, ignore
|
|
|
|
replaceOptionalPart(result, "class", parts[1])
|
|
|
|
replaceOptionalPart(result, "src", parts[2])
|
|
|
|
replaceOptionalPart(result, "width", parts[3])
|
|
|
|
replaceOptionalPart(result, "height", parts[4])
|
|
|
|
// title + alt
|
|
|
|
part := parts[5]
|
|
|
|
if len(part) > 0 {
|
|
|
|
splits := strings.Split(part, "'")
|
|
|
|
lenSplits := len(splits)
|
|
|
|
if lenSplits == 1 {
|
|
|
|
replaceOptionalPart(result, "title", splits[0])
|
|
|
|
} else if lenSplits == 3 {
|
|
|
|
replaceOptionalPart(result, "title", splits[1])
|
|
|
|
} else if lenSplits == 5 {
|
|
|
|
replaceOptionalPart(result, "title", splits[1])
|
|
|
|
replaceOptionalPart(result, "alt", splits[3])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.WriteString(">}}")
|
|
|
|
return result.String()
|
|
|
|
|
|
|
|
}
|
|
|
|
func replaceOptionalPart(buffer *bytes.Buffer, partName string, part string) {
|
|
|
|
if len(part) > 0 {
|
|
|
|
buffer.WriteString(partName + "=\"" + part + "\" ")
|
|
|
|
}
|
|
|
|
}
|