Skip to content

Commit

Permalink
add paramters --move-no-md, --skip-dot and --pages
Browse files Browse the repository at this point in the history
  • Loading branch information
kpym committed Jan 25, 2024
1 parent e9a7881 commit 5eca1b1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
8 changes: 2 additions & 6 deletions HOWTO.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down
35 changes: 31 additions & 4 deletions gm_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
Expand All @@ -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")
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions gm_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ var (
outdir string
inpatterns []string
readme bool
move bool
skipdot bool
pages bool

// template flags
css string
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 5eca1b1

Please sign in to comment.