From 5eca1b1b706f9aa29b08ed3cb14e0e8243716a53 Mon Sep 17 00:00:00 2001 From: kpym Date: Thu, 25 Jan 2024 20:51:10 +0100 Subject: [PATCH] add paramters --move-no-md, --skip-dot and --pages --- HOWTO.md | 8 ++------ README.md | 8 ++++++-- gm_build.go | 35 +++++++++++++++++++++++++++++++---- gm_parameters.go | 16 ++++++++++++++++ 4 files changed, 55 insertions(+), 12 deletions(-) diff --git a/HOWTO.md b/HOWTO.md index 587cb64..0ed2dc9 100644 --- a/HOWTO.md +++ b/HOWTO.md @@ -61,15 +61,11 @@ the served folder is `some/folder/` and the requested url is `localhost:8080/fil Here is an example of possible `.gitlab-ci.yml`: ```yaml -variables: - DOCKER_DRIVER: overlay2 # for speed up pages: image: alpine script: - - wget -c https://github.com/kpym/gm/releases/download/v0.7.0/gm_0.7.0_Linux_64bit.tar.gz -O - | tar -xz gm - - ./gm '*.md' -o public - - mv public/README.html public/index.html - # add here more commands to move files to public + - wget -c https://github.com/kpym/gm/releases/download/v0.16.0/gm_0.16.0_Linux_64bit.tar.gz -O - | tar -xz gm + - ./gm --pages '**/*' artifacts: paths: - public diff --git a/README.md b/README.md index 172ea73..534216e 100644 --- a/README.md +++ b/README.md @@ -23,13 +23,14 @@ This tool is a thin wrapper around the [github.com/yuin/goldmark](https://github ``` > gm -h -gm (version: 0.15.0): a goldmark cli tool which is a thin wrapper around github.com/yuin/goldmark (versio: v1.5.6). +gm (version: 0.16.0): 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; - - if the .html file exists it is overwritten; + - if the corresponding .html file already exists, it is overwritten; - 'stdin' is converted to 'stdout'; - when a pattern is used, only the matched .md files are considered. + - the pattern can contain '*', '?', the '**' glob pattern, '[class]' and {alt1,...} alternatives. When serving (with '--serve' or '-s' option): - the .md files are converted and served as html with live.js (for live updates); @@ -43,6 +44,9 @@ gm (version: 0.15.0): a goldmark cli tool which is a thin wrapper around github. --html string The html template (file or string). -o, --out-dir string The build output folder (created if not already existing, not used when serving). --readme-index Compile README.md to index.html (not used when serving). + --move-no-md Move all non markdown non dot files to the output folder (not used when serving). + --skip-dot Skip dot files (not used when serving). + --pages Shortcut for --outdir='public' --readme-index --move-no-md --skip-dot (not used when serving). --links-md2html Replace .md with .html in links to local files (not used when serving). (default true) --gm-attribute goldmark option: allows to define attributes on some elements. (default true) --gm-auto-heading-id goldmark option: enables auto heading ids. (default true) diff --git a/gm_build.go b/gm_build.go index a58495a..d66b813 100644 --- a/gm_build.go +++ b/gm_build.go @@ -59,8 +59,24 @@ func buildMd(infile string) { } } +func pathHasDot(path string) bool { + wasSeparator := true + for i := 0; i < len(path); i++ { + if path[i] == '.' && wasSeparator { + return true + } + wasSeparator = os.IsPathSeparator(path[i]) + } + return false +} + // 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 + cwd, err := os.Getwd() + check(err, "Problem getting the current directory.") + dirFS := os.DirFS(cwd) + movefiles := move && filepath.Clean(outdir) != filepath.Clean(cwd) // check all patterns for _, pattern := range inpatterns { info("Looking for '%s'.\n", pattern) @@ -69,10 +85,6 @@ func buildFiles() { buildMd("") continue } - // get the current directory as a filesystem, needed for doublestar.Glob - cwd, err := os.Getwd() - check(err, "Problem getting the current directory.") - dirFS := os.DirFS(cwd) // look for all files with the given patterns // but build only .md ones allfiles, err := doublestar.Glob(dirFS, pattern, doublestar.WithFilesOnly(), doublestar.WithNoFollow()) @@ -82,10 +94,25 @@ func buildFiles() { continue } for _, infile := range allfiles { + infile = filepath.Clean(infile) + if skipdot && pathHasDot(infile) { + info(" Skipping %s...\n", infile) + continue + } if strings.HasSuffix(infile, ".md") { info(" Converting %s...", infile) buildMd(infile) info("done.\n") + } else if movefiles && !strings.HasPrefix(infile, outdir) { + // move the file if it is not markdown and not already in the output folder + info(" Moving %s...", infile) + outfile := filepath.Join(outdir, infile) + if os.MkdirAll(filepath.Dir(outfile), os.ModePerm) != nil { + check(err, "Problem to reach/create folder:", filepath.Dir(outfile)) + } + err := os.Rename(infile, outfile) + check(err, "Problem moving", infile) + info("done.\n") } } } diff --git a/gm_parameters.go b/gm_parameters.go index 8f5ef94..1ac7723 100644 --- a/gm_parameters.go +++ b/gm_parameters.go @@ -56,6 +56,9 @@ var ( outdir string inpatterns []string readme bool + move bool + skipdot bool + pages bool // template flags css string @@ -113,6 +116,9 @@ func SetParameters() { pflag.StringVarP(&outdir, "out-dir", "o", "", "The build output folder (created if not already existing, not used when serving).") pflag.BoolVar(&readme, "readme-index", false, "Compile README.md to index.html (not used when serving).") + pflag.BoolVar(&move, "move-no-md", false, "Move all non markdown non dot files to the output folder (not used when serving).") + pflag.BoolVar(&skipdot, "skip-dot", false, "Skip dot files (not used when serving).") + pflag.BoolVar(&pages, "pages", false, "Shortcut for --outdir='public' --readme-index --move-no-md --skip-dot (not used when serving).") pflag.BoolVar(&localmdlinks, "links-md2html", true, "Replace .md with .html in links to local files (not used when serving).") pflag.BoolVar(&attribute, "gm-attribute", true, "goldmark option: allows to define attributes on some elements.") @@ -169,6 +175,16 @@ func SetParameters() { htmlshell = defaultHTMLTemplate } + //set flags from shortcuts + if pages { + if outdir == "" { + outdir = "public" + } + readme = true + move = true + skipdot = true + } + if serve { setServeParameters() } else {