Implement cli argument for file path, path validation, file type validation
This commit is contained in:
parent
1667fb10ef
commit
26adec4a97
6 changed files with 111 additions and 2 deletions
22
internal/convert/mdToHtml.go
Normal file
22
internal/convert/mdToHtml.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package convert
|
||||
|
||||
import (
|
||||
"github.com/gomarkdown/markdown"
|
||||
"github.com/gomarkdown/markdown/html"
|
||||
"github.com/gomarkdown/markdown/parser"
|
||||
)
|
||||
|
||||
// This function takes a Markdown document and returns an HTML document.
|
||||
func MdToHTML(md []byte) ([]byte, error) {
|
||||
// create parser with extensions
|
||||
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
|
||||
p := parser.NewWithExtensions(extensions)
|
||||
doc := p.Parse(md)
|
||||
|
||||
// build HTML renderer
|
||||
htmlFlags := html.CommonFlags | html.HrefTargetBlank
|
||||
opts := html.RendererOptions{Flags: htmlFlags}
|
||||
renderer := html.NewRenderer(opts)
|
||||
|
||||
return markdown.Render(doc, renderer), nil
|
||||
}
|
33
internal/util/path.go
Normal file
33
internal/util/path.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
// Package util provides general utilities.
|
||||
package util
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// CheckExtension checks if the file located at path (string)
|
||||
// matches the provided extension type
|
||||
func CheckExtension(path, ext string) error {
|
||||
if filepath.Ext(path) == ext {
|
||||
return nil
|
||||
} else {
|
||||
return errors.New("Invalid extension.")
|
||||
}
|
||||
}
|
||||
|
||||
// PathIsValid checks if a path is valid.
|
||||
// If requireFile is set, directories are not considered valid.
|
||||
func PathIsValid(path string, requireFile bool) bool {
|
||||
s, err := os.Stat(path)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
} else if requireFile {
|
||||
fmt.Printf("Directory status: %s\n", strconv.FormatBool(s.IsDir()))
|
||||
return !s.IsDir()
|
||||
}
|
||||
return err == nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue