-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
305 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/UpCloudLtd/mdtest/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
transforms []string | ||
outputPath string | ||
|
||
normaliseCmd = &cobra.Command{ | ||
Aliases: []string{"normalize"}, | ||
Use: "normalise", | ||
Short: "Normalise the fenced code block info texts", | ||
Long: "Normalise the fenced code block info texts. By default, removes all info texts defined after the language identifier from the starting code-block fence.", | ||
Args: cobra.MinimumNArgs(1), | ||
} | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(normaliseCmd) | ||
normaliseCmd.Flags().StringArrayVarP(&transforms, "transform", "t", nil, "transform info text key in `old=new` format, e.g., `-t filename=title` would transform `filename` info text to `title` info text") | ||
normaliseCmd.Flags().StringVarP(&outputPath, "output", "o", "", "`directory` where to save the normalised files") | ||
_ = normaliseCmd.MarkFlagRequired("output") | ||
normaliseCmd.RunE = func(cmd *cobra.Command, args []string) error { | ||
cmd.SilenceUsage = true | ||
|
||
params := utils.NormalizeParameters{ | ||
OutputPath: outputPath, | ||
Transforms: transforms, | ||
} | ||
|
||
return utils.Normalize(args, params) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Success: normalise info texts | ||
|
||
The normalise command with `-t filename=title` transform argument should remove and `no_value` and `key=value` args and replace `filename` key with `title`, | ||
|
||
```sh no_value key=value filename=true.sh | ||
exit 0 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"github.com/UpCloudLtd/progress/messages" | ||
) | ||
|
||
type PathWarning struct { | ||
path string | ||
err error | ||
} | ||
|
||
func (warn PathWarning) Message() messages.Update { | ||
return messages.Update{ | ||
Message: fmt.Sprintf("Finding %s", warn.path), | ||
Details: fmt.Sprintf("Error: %s", warn.err.Error()), | ||
Status: messages.MessageStatusWarning, | ||
} | ||
} | ||
|
||
func ParseFilePaths(rawPaths []string, depth int) ([]string, []PathWarning) { | ||
paths := []string{} | ||
warnings := []PathWarning{} | ||
for _, rawPath := range rawPaths { | ||
info, err := os.Stat(rawPath) | ||
if err != nil { | ||
warnings = append(warnings, PathWarning{rawPath, err}) | ||
if info == nil { | ||
continue | ||
} | ||
} | ||
|
||
if info.Mode().IsDir() && depth != 0 { | ||
files, err := os.ReadDir(rawPath) | ||
if err != nil { | ||
warnings = append(warnings, PathWarning{rawPath, err}) | ||
} | ||
|
||
dirRawPaths := []string{} | ||
for _, file := range files { | ||
dirRawPaths = append(dirRawPaths, path.Join(rawPath, file.Name())) | ||
} | ||
|
||
dirPaths, dirWarnings := ParseFilePaths(dirRawPaths, depth-1) | ||
if dirWarnings != nil { | ||
warnings = append(warnings, dirWarnings...) | ||
} | ||
|
||
paths = append(paths, dirPaths...) | ||
} | ||
|
||
if strings.HasSuffix(rawPath, ".md") { | ||
paths = append(paths, rawPath) | ||
} | ||
} | ||
return paths, warnings | ||
} | ||
|
||
func ParseOptions(optionsStr string) (string, map[string]string) { | ||
optionsList := strings.Split(optionsStr, " ") | ||
options := make(map[string]string) | ||
|
||
lang := optionsList[0] | ||
for _, option := range optionsList[1:] { | ||
items := strings.SplitN(option, "=", 2) | ||
|
||
key := items[0] | ||
value := "" | ||
if len(items) > 1 { | ||
value = items[1] | ||
} | ||
|
||
options[key] = value | ||
} | ||
|
||
return lang, options | ||
} |
Oops, something went wrong.