From fe9cab19356c42ff605947443faf989bd81baa69 Mon Sep 17 00:00:00 2001 From: kpym Date: Fri, 26 Jan 2024 18:37:07 +0100 Subject: [PATCH] fix build problems when out-dir is not empty --- HOWTO.md | 2 +- README.md | 2 +- gm_build.go | 35 ++++++++++++++++++++++++++++++++--- gm_parameters.go | 1 + 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/HOWTO.md b/HOWTO.md index 3efc39b..e69e696 100644 --- a/HOWTO.md +++ b/HOWTO.md @@ -64,7 +64,7 @@ Here is an example of possible `.gitlab-ci.yml`: pages: image: alpine script: - - wget -c https://github.com/kpym/gm/releases/download/v0.17.2/gm_0.17.2_Linux_64bit.tar.gz -O - | tar -C /usr/local/bin -xz gm + - wget -c https://github.com/kpym/gm/releases/download/v0.17.3/gm_0.17.3_Linux_64bit.tar.gz -O - | tar -C /usr/local/bin -xz gm - gm --pages '**/*' artifacts: paths: diff --git a/README.md b/README.md index 498fae7..a6d6b35 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ This tool is a thin wrapper around the [github.com/yuin/goldmark](https://github ``` > gm -h -gm (version: 0.17.2): a goldmark cli tool which is a thin wrapper around github.com/yuin/goldmark (versio: v1.5.6). +gm (version: 0.17.3): a goldmark cli tool which is a thin wrapper around github.com/yuin/goldmark (versio: v1.5.6). If not serving (no '--serve' or '-s' option is used): - the .md files are converted and saved as .html with the same base name; diff --git a/gm_build.go b/gm_build.go index d66b813..416fc44 100644 --- a/gm_build.go +++ b/gm_build.go @@ -59,6 +59,19 @@ func buildMd(infile string) { } } +func pathFirstPart(path string) string { + i := 0 + for ; i < len(path); i++ { + if os.IsPathSeparator(path[i]) { + break + } + } + if i == len(path) { + return path + string(os.PathSeparator) + } + return path[:i+1] +} + func pathHasDot(path string) bool { wasSeparator := true for i := 0; i < len(path); i++ { @@ -72,12 +85,25 @@ func pathHasDot(path string) bool { // buildFiles convert all .md files verifying one of the patterns to .html func buildFiles() { - // get the current directory as a filesystem, needed for doublestar.Glob + // get the current directory cwd, err := os.Getwd() check(err, "Problem getting the current directory.") + // get the current directory as a filesystem, needed for doublestar.Glob dirFS := os.DirFS(cwd) - movefiles := move && filepath.Clean(outdir) != filepath.Clean(cwd) + // normalize the output directory and set movefiles and outstart + outdir, err = filepath.Abs(outdir) + check(err, "Problem getting the absolute path of the output directory.") + movefiles := move && outdir != cwd + outdir, err = filepath.Rel(cwd, outdir) + check(err, "Problem getting the relative path of the output directory.") + // get the first part of the relative out path + outstart := pathFirstPart(outdir) // check all patterns + action := "Building" + if movefiles { + action = "Building and moving" + } + info(action+" files from '%s' to '%s'.\n", cwd, outdir) for _, pattern := range inpatterns { info("Looking for '%s'.\n", pattern) // if the input is piped @@ -99,11 +125,14 @@ func buildFiles() { info(" Skipping %s...\n", infile) continue } + if strings.HasPrefix(infile, outstart) { + continue + } if strings.HasSuffix(infile, ".md") { info(" Converting %s...", infile) buildMd(infile) info("done.\n") - } else if movefiles && !strings.HasPrefix(infile, outdir) { + } else if movefiles { // move the file if it is not markdown and not already in the output folder info(" Moving %s...", infile) outfile := filepath.Join(outdir, infile) diff --git a/gm_parameters.go b/gm_parameters.go index 69523af..6ad5efd 100644 --- a/gm_parameters.go +++ b/gm_parameters.go @@ -237,6 +237,7 @@ func setBuildParameters() { // check the "out dir" if outdir != "" { + outdir = filepath.Clean(outdir) if os.MkdirAll(outdir, os.ModePerm) != nil { check(fmt.Errorf("the specified output folder '%s' is not reachable", outdir)) }