2023-01-04 12:24:36 -05:00
|
|
|
// Copyright 2023 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"
|
2023-01-04 12:24:36 -05:00
|
|
|
"context"
|
2015-12-02 05:36:18 -05:00
|
|
|
"errors"
|
2018-10-20 11:38:49 -04:00
|
|
|
"fmt"
|
2023-02-18 17:43:26 -05:00
|
|
|
"io"
|
2015-12-02 05:36:18 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
2023-01-04 12:24:36 -05:00
|
|
|
|
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
|
|
|
|
2015-12-02 05:36:18 -05:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2018-11-19 11:33:54 -05:00
|
|
|
"unicode"
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
"github.com/bep/simplecobra"
|
2022-04-26 13:57:04 -04:00
|
|
|
"github.com/gohugoio/hugo/common/htime"
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
"github.com/gohugoio/hugo/common/hugio"
|
2019-11-21 15:59:38 -05:00
|
|
|
"github.com/gohugoio/hugo/common/maps"
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/helpers"
|
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
|
|
|
"github.com/gohugoio/hugo/parser"
|
2023-01-04 12:24:36 -05:00
|
|
|
"github.com/gohugoio/hugo/parser/metadecoders"
|
|
|
|
"github.com/gohugoio/hugo/parser/pageparser"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/spf13/cobra"
|
2015-12-02 05:36:18 -05:00
|
|
|
)
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func newImportCommand() *importCommand {
|
|
|
|
var c *importCommand
|
|
|
|
c = &importCommand{
|
|
|
|
commands: []simplecobra.Commander{
|
|
|
|
&simpleCommand{
|
|
|
|
name: "jekyll",
|
|
|
|
short: "hugo import from Jekyll",
|
|
|
|
long: `hugo import from Jekyll.
|
|
|
|
|
2015-12-02 05:36:18 -05:00
|
|
|
Import from Jekyll requires two paths, e.g. ` + "`hugo import jekyll jekyll_root_path target_path`.",
|
2023-01-04 12:24:36 -05:00
|
|
|
run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
|
|
|
|
if len(args) < 2 {
|
|
|
|
return newUserError(`import from jekyll requires two paths, e.g. ` + "`hugo import jekyll jekyll_root_path target_path`.")
|
|
|
|
}
|
|
|
|
return c.importFromJekyll(args)
|
|
|
|
},
|
2023-05-28 04:44:40 -04:00
|
|
|
withc: func(cmd *cobra.Command, r *rootCommand) {
|
2023-01-04 12:24:36 -05:00
|
|
|
cmd.Flags().BoolVar(&c.force, "force", false, "allow import into non-empty target directory")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-04-09 14:37:17 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
return c
|
2018-04-09 14:37:17 -04:00
|
|
|
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
type importCommand struct {
|
|
|
|
r *rootCommand
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
force bool
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
commands []simplecobra.Commander
|
|
|
|
}
|
2015-12-03 01:51:48 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *importCommand) Commands() []simplecobra.Commander {
|
|
|
|
return c.commands
|
|
|
|
}
|
2015-12-03 01:51:48 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *importCommand) Name() string {
|
|
|
|
return "import"
|
|
|
|
}
|
2016-03-01 19:06:02 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *importCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args []string) error {
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-01 19:06:02 -05:00
|
|
|
|
2023-05-17 12:45:23 -04:00
|
|
|
func (c *importCommand) Init(cd *simplecobra.Commandeer) error {
|
|
|
|
cmd := cd.CobraCommand
|
2023-01-04 12:24:36 -05:00
|
|
|
cmd.Short = "Import your site from others."
|
|
|
|
cmd.Long = `Import your site from other web site generators like Jekyll.
|
2016-03-01 19:06:02 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
Import requires a subcommand, e.g. ` + "`hugo import jekyll jekyll_root_path target_path`."
|
2015-12-03 01:51:48 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
return nil
|
|
|
|
}
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-05-17 12:45:23 -04:00
|
|
|
func (c *importCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
|
2023-01-04 12:24:36 -05:00
|
|
|
c.r = cd.Root.Command.(*rootCommand)
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (i *importCommand) createConfigFromJekyll(fs afero.Fs, inpath string, kind metadecoders.Format, jekyllConfig map[string]any) (err error) {
|
|
|
|
title := "My New Hugo Site"
|
|
|
|
baseURL := "http://example.org/"
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
for key, value := range jekyllConfig {
|
|
|
|
lowerKey := strings.ToLower(key)
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
switch lowerKey {
|
|
|
|
case "title":
|
|
|
|
if str, ok := value.(string); ok {
|
|
|
|
title = str
|
|
|
|
}
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
case "url":
|
|
|
|
if str, ok := value.(string); ok {
|
|
|
|
baseURL = str
|
|
|
|
}
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
in := map[string]any{
|
|
|
|
"baseURL": baseURL,
|
|
|
|
"title": title,
|
|
|
|
"languageCode": "en-us",
|
|
|
|
"disablePathToLower": true,
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
2016-03-01 19:06:02 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
var buf bytes.Buffer
|
|
|
|
err = parser.InterfaceToConfig(in, kind, &buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-01 19:06:02 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
return helpers.WriteToDisk(filepath.Join(inpath, "hugo."+string(kind)), &buf, fs)
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *importCommand) getJekyllDirInfo(fs afero.Fs, jekyllRoot string) (map[string]bool, bool) {
|
2016-03-01 19:06:02 -05:00
|
|
|
postDirs := make(map[string]bool)
|
|
|
|
hasAnyPost := false
|
2023-02-18 17:43:26 -05:00
|
|
|
if entries, err := os.ReadDir(jekyllRoot); err == nil {
|
2016-03-01 19:06:02 -05:00
|
|
|
for _, entry := range entries {
|
|
|
|
if entry.IsDir() {
|
|
|
|
subDir := filepath.Join(jekyllRoot, entry.Name())
|
2023-01-04 12:24:36 -05:00
|
|
|
if isPostDir, hasAnyPostInDir := c.retrieveJekyllPostDir(fs, subDir); isPostDir {
|
2016-03-01 19:06:02 -05:00
|
|
|
postDirs[entry.Name()] = hasAnyPostInDir
|
|
|
|
if hasAnyPostInDir {
|
|
|
|
hasAnyPost = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return postDirs, hasAnyPost
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *importCommand) createSiteFromJekyll(jekyllRoot, targetDir string, jekyllPostDirs map[string]bool) error {
|
2022-05-21 14:41:28 -04:00
|
|
|
fs := &afero.OsFs{}
|
2015-12-03 01:51:48 -05:00
|
|
|
if exists, _ := helpers.Exists(targetDir, fs); exists {
|
|
|
|
if isDir, _ := helpers.IsDir(targetDir, fs); !isDir {
|
2019-11-24 12:57:39 -05:00
|
|
|
return errors.New("target path \"" + targetDir + "\" exists but is not a directory")
|
2015-12-03 01:51:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
isEmpty, _ := helpers.IsEmpty(targetDir, fs)
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
if !isEmpty && !c.force {
|
2019-11-24 12:57:39 -05:00
|
|
|
return errors.New("target path \"" + targetDir + "\" exists and is not empty")
|
2015-12-03 01:51:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
jekyllConfig := c.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")
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
c.createConfigFromJekyll(fs, targetDir, "yaml", jekyllConfig)
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
c.copyJekyllFilesAndFolders(jekyllRoot, filepath.Join(targetDir, "static"), jekyllPostDirs)
|
2015-12-03 01:51:48 -05:00
|
|
|
|
2019-11-24 12:57:39 -05:00
|
|
|
return nil
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *importCommand) convertJekyllContent(m any, content string) (string, error) {
|
|
|
|
metadata, _ := maps.ToStringMapE(m)
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
lines := strings.Split(content, "\n")
|
|
|
|
var resultLines []string
|
|
|
|
for _, line := range lines {
|
|
|
|
resultLines = append(resultLines, strings.Trim(line, "\r\n"))
|
|
|
|
}
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
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)
|
|
|
|
}
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
replaceList := []struct {
|
|
|
|
re *regexp.Regexp
|
|
|
|
replace string
|
|
|
|
}{
|
|
|
|
{regexp.MustCompile("(?i)<!-- more -->"), "<!--more-->"},
|
|
|
|
{regexp.MustCompile(`\{%\s*raw\s*%\}\s*(.*?)\s*\{%\s*endraw\s*%\}`), "$1"},
|
|
|
|
{regexp.MustCompile(`{%\s*endhighlight\s*%}`), "{{< / highlight >}}"},
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
for _, replace := range replaceList {
|
|
|
|
content = replace.re.ReplaceAllString(content, replace.replace)
|
|
|
|
}
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05: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*%}`), c.replaceImageTag},
|
|
|
|
{regexp.MustCompile(`{%\s*highlight\s*(.*?)\s*%}`), c.replaceHighlightTag},
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
for _, replace := range replaceListFunc {
|
|
|
|
content = replace.re.ReplaceAllStringFunc(content, replace.replace)
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
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
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *importCommand) convertJekyllMetaData(m any, postName string, postDate time.Time, draft bool) (any, error) {
|
|
|
|
metadata, err := maps.ToStringMapE(m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
if draft {
|
|
|
|
metadata["draft"] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range metadata {
|
2015-12-02 05:36:18 -05:00
|
|
|
lowerKey := strings.ToLower(key)
|
|
|
|
|
|
|
|
switch lowerKey {
|
2023-01-04 12:24:36 -05:00
|
|
|
case "layout":
|
|
|
|
delete(metadata, key)
|
|
|
|
case "permalink":
|
2015-12-02 05:36:18 -05:00
|
|
|
if str, ok := value.(string); ok {
|
2023-01-04 12:24:36 -05:00
|
|
|
metadata["url"] = str
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
delete(metadata, key)
|
|
|
|
case "category":
|
2015-12-02 05:36:18 -05:00
|
|
|
if str, ok := value.(string); ok {
|
2023-01-04 12:24:36 -05:00
|
|
|
metadata["categories"] = []string{str}
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
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)
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
metadata["date"] = postDate.Format(time.RFC3339)
|
|
|
|
|
|
|
|
return metadata, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *importCommand) convertJekyllPost(path, relPath, targetDir string, draft bool) error {
|
|
|
|
jww.TRACE.Println("Converting", path)
|
|
|
|
|
|
|
|
filename := filepath.Base(path)
|
|
|
|
postDate, postName, err := c.parseJekyllFilename(filename)
|
|
|
|
if err != nil {
|
|
|
|
c.r.Printf("Failed to parse filename '%s': %s. Skipping.", filename, err)
|
|
|
|
return nil
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
jww.TRACE.Println(filename, postDate, postName)
|
|
|
|
|
|
|
|
targetFile := filepath.Join(targetDir, relPath)
|
|
|
|
targetParentDir := filepath.Dir(targetFile)
|
|
|
|
os.MkdirAll(targetParentDir, 0777)
|
|
|
|
|
|
|
|
contentBytes, err := os.ReadFile(path)
|
2015-12-02 05:36:18 -05:00
|
|
|
if err != nil {
|
2023-01-04 12:24:36 -05:00
|
|
|
c.r.logger.Errorln("Read file error:", path)
|
2015-12-02 05:36:18 -05:00
|
|
|
return err
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
pf, err := pageparser.ParseFrontMatterAndContent(bytes.NewReader(contentBytes))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse file %q: %s", filename, err)
|
|
|
|
}
|
|
|
|
newmetadata, err := c.convertJekyllMetaData(pf.FrontMatter, postName, postDate, draft)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to convert metadata for file %q: %s", filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := c.convertJekyllContent(newmetadata, string(pf.Content))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to convert content for file %q: %s", filename, err)
|
|
|
|
}
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
fs := hugofs.Os
|
|
|
|
if err := helpers.WriteToDisk(targetFile, strings.NewReader(content), fs); err != nil {
|
|
|
|
return fmt.Errorf("failed to save file %q: %s", filename, err)
|
|
|
|
}
|
|
|
|
return nil
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *importCommand) copyJekyllFilesAndFolders(jekyllRoot, dest string, jekyllPostDirs map[string]bool) (err error) {
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
fs := hugofs.Os
|
|
|
|
|
|
|
|
fi, err := fs.Stat(jekyllRoot)
|
2015-12-02 05:36:18 -05:00
|
|
|
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
|
|
|
|
}
|
2023-02-18 17:43:26 -05:00
|
|
|
entries, err := os.ReadDir(jekyllRoot)
|
2019-01-02 06:33:26 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-02 05:36:18 -05:00
|
|
|
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 {
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
err = hugio.CopyDir(fs, sfp, dfp, nil)
|
2016-03-01 19:06:02 -05:00
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Println(err)
|
|
|
|
}
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lowerEntryName := strings.ToLower(entry.Name())
|
2020-12-02 07:23:25 -05:00
|
|
|
exceptSuffix := []string{
|
|
|
|
".md", ".markdown", ".html", ".htm",
|
|
|
|
".xml", ".textile", "rakefile", "gemfile", ".lock",
|
|
|
|
}
|
2015-12-02 05:36:18 -05:00
|
|
|
isExcept := false
|
|
|
|
for _, suffix := range exceptSuffix {
|
|
|
|
if strings.HasSuffix(lowerEntryName, suffix) {
|
|
|
|
isExcept = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isExcept && entry.Name()[0] != '.' && entry.Name()[0] != '_' {
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
err = hugio.CopyFile(fs, sfp, dfp)
|
2015-12-02 05:36:18 -05:00
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *importCommand) importFromJekyll(args []string) error {
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
jekyllRoot, err := filepath.Abs(filepath.Clean(args[0]))
|
2015-12-02 05:36:18 -05:00
|
|
|
if err != nil {
|
2023-01-04 12:24:36 -05:00
|
|
|
return newUserError("path error:", args[0])
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
targetDir, err := filepath.Abs(filepath.Clean(args[1]))
|
2015-12-02 05:36:18 -05:00
|
|
|
if err != nil {
|
2023-01-04 12:24:36 -05:00
|
|
|
return newUserError("path error:", args[1])
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
c.r.Println("Import Jekyll from:", jekyllRoot, "to:", targetDir)
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
if strings.HasPrefix(filepath.Dir(targetDir), jekyllRoot) {
|
|
|
|
return newUserError("abort: target path should not be inside the Jekyll root")
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
fs := afero.NewOsFs()
|
|
|
|
jekyllPostDirs, hasAnyPost := c.getJekyllDirInfo(fs, jekyllRoot)
|
|
|
|
if !hasAnyPost {
|
|
|
|
return errors.New("abort: jekyll root contains neither posts nor drafts")
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
err = c.createSiteFromJekyll(jekyllRoot, targetDir, jekyllPostDirs)
|
2015-12-02 05:36:18 -05:00
|
|
|
if err != nil {
|
2023-01-04 12:24:36 -05:00
|
|
|
return newUserError(err)
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
c.r.Println("Importing...")
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
fileCount := 0
|
|
|
|
callback := func(path string, fi hugofs.FileMetaInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
if fi.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
relPath, err := filepath.Rel(jekyllRoot, path)
|
|
|
|
if err != nil {
|
|
|
|
return newUserError("get rel path error:", path)
|
|
|
|
}
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
relPath = filepath.ToSlash(relPath)
|
|
|
|
draft := false
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
switch {
|
|
|
|
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))
|
|
|
|
draft = true
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
fileCount++
|
|
|
|
return c.convertJekyllPost(path, relPath, targetDir, draft)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
c.r.Println("Congratulations!", fileCount, "post(s) imported!")
|
|
|
|
c.r.Println("Now, start Hugo by yourself:\n" +
|
|
|
|
"$ git clone https://github.com/spf13/herring-cove.git " + args[1] + "/themes/herring-cove")
|
|
|
|
c.r.Println("$ cd " + args[1] + "\n$ hugo server --theme=herring-cove")
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
return nil
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *importCommand) loadJekyllConfig(fs afero.Fs, jekyllRoot string) map[string]any {
|
|
|
|
path := filepath.Join(jekyllRoot, "_config.yml")
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
exists, err := helpers.Exists(path, fs)
|
2015-12-02 05:36:18 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
if err != nil || !exists {
|
|
|
|
c.r.Println("_config.yaml not found: Is the specified Jekyll root correct?")
|
|
|
|
return nil
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
f, err := fs.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
b, err := io.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
m, err := metadecoders.Default.UnmarshalToMap(b, metadecoders.YAML)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
2017-04-22 16:35:52 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *importCommand) parseJekyllFilename(filename string) (time.Time, string, error) {
|
|
|
|
re := regexp.MustCompile(`(\d+-\d+-\d+)-(.+)\..*`)
|
|
|
|
r := re.FindAllStringSubmatch(filename, -1)
|
|
|
|
if len(r) == 0 {
|
|
|
|
return htime.Now(), "", errors.New("filename not match")
|
2017-04-22 16:35:52 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
postDate, err := time.Parse("2006-1-2", r[0][1])
|
|
|
|
if err != nil {
|
|
|
|
return htime.Now(), "", err
|
2019-11-24 12:57:39 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
postName := r[0][2]
|
|
|
|
|
|
|
|
return postDate, postName, nil
|
2015-12-02 05:36:18 -05:00
|
|
|
}
|
2017-04-22 16:35:52 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *importCommand) replaceHighlightTag(match string) string {
|
2018-11-19 11:33:54 -05:00
|
|
|
r := regexp.MustCompile(`{%\s*highlight\s*(.*?)\s*%}`)
|
|
|
|
parts := r.FindStringSubmatch(match)
|
|
|
|
lastQuote := rune(0)
|
|
|
|
f := func(c rune) bool {
|
|
|
|
switch {
|
|
|
|
case c == lastQuote:
|
|
|
|
lastQuote = rune(0)
|
|
|
|
return false
|
|
|
|
case lastQuote != rune(0):
|
|
|
|
return false
|
|
|
|
case unicode.In(c, unicode.Quotation_Mark):
|
|
|
|
lastQuote = c
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return unicode.IsSpace(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// splitting string by space but considering quoted section
|
|
|
|
items := strings.FieldsFunc(parts[1], f)
|
|
|
|
|
|
|
|
result := bytes.NewBufferString("{{< highlight ")
|
|
|
|
result.WriteString(items[0]) // language
|
|
|
|
options := items[1:]
|
|
|
|
for i, opt := range options {
|
|
|
|
opt = strings.Replace(opt, "\"", "", -1)
|
|
|
|
if opt == "linenos" {
|
|
|
|
opt = "linenos=table"
|
|
|
|
}
|
|
|
|
if i == 0 {
|
|
|
|
opt = " \"" + opt
|
|
|
|
}
|
|
|
|
if i < len(options)-1 {
|
|
|
|
opt += ","
|
|
|
|
} else if i == len(options)-1 {
|
|
|
|
opt += "\""
|
|
|
|
}
|
|
|
|
result.WriteString(opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
result.WriteString(" >}}")
|
|
|
|
return result.String()
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *importCommand) replaceImageTag(match string) string {
|
2017-04-22 16:35:52 -04:00
|
|
|
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
|
2023-01-04 12:24:36 -05:00
|
|
|
c.replaceOptionalPart(result, "class", parts[1])
|
|
|
|
c.replaceOptionalPart(result, "src", parts[2])
|
|
|
|
c.replaceOptionalPart(result, "width", parts[3])
|
|
|
|
c.replaceOptionalPart(result, "height", parts[4])
|
2017-04-22 16:35:52 -04:00
|
|
|
// title + alt
|
|
|
|
part := parts[5]
|
|
|
|
if len(part) > 0 {
|
|
|
|
splits := strings.Split(part, "'")
|
|
|
|
lenSplits := len(splits)
|
|
|
|
if lenSplits == 1 {
|
2023-01-04 12:24:36 -05:00
|
|
|
c.replaceOptionalPart(result, "title", splits[0])
|
2017-04-22 16:35:52 -04:00
|
|
|
} else if lenSplits == 3 {
|
2023-01-04 12:24:36 -05:00
|
|
|
c.replaceOptionalPart(result, "title", splits[1])
|
2017-04-22 16:35:52 -04:00
|
|
|
} else if lenSplits == 5 {
|
2023-01-04 12:24:36 -05:00
|
|
|
c.replaceOptionalPart(result, "title", splits[1])
|
|
|
|
c.replaceOptionalPart(result, "alt", splits[3])
|
2017-04-22 16:35:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result.WriteString(">}}")
|
|
|
|
return result.String()
|
|
|
|
}
|
2020-12-02 07:23:25 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *importCommand) replaceOptionalPart(buffer *bytes.Buffer, partName string, part string) {
|
2017-04-22 16:35:52 -04:00
|
|
|
if len(part) > 0 {
|
|
|
|
buffer.WriteString(partName + "=\"" + part + "\" ")
|
|
|
|
}
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
|
|
|
func (c *importCommand) 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 := os.ReadDir(dir); err == nil {
|
|
|
|
for _, entry := range entries {
|
|
|
|
if entry.IsDir() {
|
|
|
|
subDir := filepath.Join(dir, entry.Name())
|
|
|
|
if isPostDir, hasAnyPost := c.retrieveJekyllPostDir(fs, subDir); isPostDir {
|
|
|
|
return isPostDir, hasAnyPost
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, true
|
|
|
|
}
|