Skip to content

Commit

Permalink
fix build problems when out-dir is not empty
Browse files Browse the repository at this point in the history
  • Loading branch information
kpym committed Jan 26, 2024
1 parent 96c4e08 commit fe9cab1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion HOWTO.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 32 additions & 3 deletions gm_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand All @@ -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
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions gm_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down

0 comments on commit fe9cab1

Please sign in to comment.