diff --git a/cmd/zona/main.go b/cmd/zona/main.go index 8f61880..16d8a94 100644 --- a/cmd/zona/main.go +++ b/cmd/zona/main.go @@ -1,9 +1,57 @@ package main import ( + "errors" + "flag" "fmt" + "os" + + "github.com/ficcdaf/zona/internal/util" ) -func main() { - fmt.Println("Hello, world!") +// validateFile checks whether a given path +// is a valid file && matches an expected extension +func validateFile(path, ext string) bool { + return (util.CheckExtension(path, ext) == nil) && (util.PathIsValid(path, true)) +} + +func main() { + mdPath := flag.String("file", "", "Path to the markdown file.") + flag.Parse() + if *mdPath == "" { + // no flag provided, check for positional argument instead + n := flag.NArg() + var e error + switch n { + case 1: + // we read the positional arg + arg := flag.Arg(0) + // mdPath wants a pointer so we get arg's address + mdPath = &arg + case 0: + // in case of no flag and no arg, we fail + e = errors.New("Required argument missing!") + default: + // more args than expected is also fail + e = errors.New("Unexpected arguments!") + } + if e != nil { + fmt.Printf("Error: %s\n", e.Error()) + os.Exit(1) + } + + } + if !validateFile(*mdPath, ".md") { + fmt.Println("File validation failed!") + os.Exit(1) + } + + file, err := os.Open(*mdPath) + if err != nil { + fmt.Println("Error reading file:", err) + os.Exit(1) + } + defer file.Close() + + fmt.Println("File opened :)") } diff --git a/go.mod b/go.mod index 8d6c6f2..182992a 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/ficcdaf/zona go 1.23.2 + +require github.com/gomarkdown/markdown v0.0.0-20240930133441-72d49d9543d8 // indirect diff --git a/internal/convert/mdToHtml.go b/internal/convert/mdToHtml.go new file mode 100644 index 0000000..8ddfa30 --- /dev/null +++ b/internal/convert/mdToHtml.go @@ -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 +} diff --git a/internal/util/path.go b/internal/util/path.go new file mode 100644 index 0000000..5b1da96 --- /dev/null +++ b/internal/util/path.go @@ -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 +} diff --git a/test/in.md b/test/in.md new file mode 100644 index 0000000..a933e82 --- /dev/null +++ b/test/in.md @@ -0,0 +1,3 @@ +# My amazing markdown file! + +I can _even_ do **this**! diff --git a/zona b/zona new file mode 120000 index 0000000..0ee2e17 --- /dev/null +++ b/zona @@ -0,0 +1 @@ +bin/zona \ No newline at end of file