removed DEV code from main branch
This commit is contained in:
parent
8e299bef35
commit
c0b98d7a99
14 changed files with 0 additions and 289 deletions
4
build.sh
4
build.sh
|
@ -1,4 +0,0 @@
|
||||||
#!/bin/bash
|
|
||||||
|
|
||||||
go build -o bin/zona ./cmd/zona
|
|
||||||
ln -sf bin/zona ./zona
|
|
|
@ -1,53 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/ficcdaf/zona/internal/util"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
// }
|
|
||||||
// convert.ConvertFile(*mdPath, "test/test.html")
|
|
||||||
err := util.Traverse(*mdPath, "foobar")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Error: %s\n", err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
5
go.mod
5
go.mod
|
@ -1,5 +0,0 @@
|
||||||
module github.com/ficcdaf/zona
|
|
||||||
|
|
||||||
go 1.23.2
|
|
||||||
|
|
||||||
require github.com/gomarkdown/markdown v0.0.0-20240930133441-72d49d9543d8 // indirect
|
|
|
@ -1,76 +0,0 @@
|
||||||
package convert
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"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
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteFile writes a given byte array to the given path.
|
|
||||||
func WriteFile(b []byte, p string) error {
|
|
||||||
f, err := os.Create(p)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = f.Write(b)
|
|
||||||
defer f.Close()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadFile reads a byte array from a given path.
|
|
||||||
func ReadFile(p string) ([]byte, error) {
|
|
||||||
f, err := os.Open(p)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var result []byte
|
|
||||||
buf := make([]byte, 1024)
|
|
||||||
for {
|
|
||||||
n, err := f.Read(buf)
|
|
||||||
// check for a non EOF error
|
|
||||||
if err != nil && err != io.EOF {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// n==0 when there are no chunks left to read
|
|
||||||
if n == 0 {
|
|
||||||
defer f.Close()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
result = append(result, buf[:n]...)
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ConvertFile(in string, out string) error {
|
|
||||||
md, err := ReadFile(in)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
html, err := MdToHTML(md)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = WriteFile(html, out)
|
|
||||||
return err
|
|
||||||
}
|
|
|
@ -1,50 +0,0 @@
|
||||||
package tree
|
|
||||||
|
|
||||||
// Node is a struct containing nodes of a file tree.
|
|
||||||
type Node struct {
|
|
||||||
// can be nil
|
|
||||||
Parent *Node
|
|
||||||
Name string
|
|
||||||
// Empty value mean directory
|
|
||||||
Ext string
|
|
||||||
// cannot be nil; may have len 0
|
|
||||||
Children []*Node
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewNode constructs and returns a Node instance.
|
|
||||||
// parent and children are optional and can be nil,
|
|
||||||
// in which case Parent will be stored as nil,
|
|
||||||
// but Children will be initialized as []*Node of len 0.
|
|
||||||
// If ext == "", the Node is a directory.
|
|
||||||
func NewNode(name string, ext string, parent *Node, children []*Node) *Node {
|
|
||||||
var c []*Node
|
|
||||||
if children == nil {
|
|
||||||
c = make([]*Node, 0)
|
|
||||||
} else {
|
|
||||||
c = children
|
|
||||||
}
|
|
||||||
n := &Node{
|
|
||||||
Name: name,
|
|
||||||
Ext: ext,
|
|
||||||
Parent: parent,
|
|
||||||
Children: c,
|
|
||||||
}
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *Node) IsRoot() bool {
|
|
||||||
return n.Parent == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *Node) IsTail() bool {
|
|
||||||
return len(n.Children) == 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *Node) IsDir() bool {
|
|
||||||
return n.Ext == ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Implement recursive depth-first traversal to process a tree
|
|
||||||
|
|
||||||
func Traverse(root *Node) {
|
|
||||||
}
|
|
|
@ -1,94 +0,0 @@
|
||||||
// Package util provides general utilities.
|
|
||||||
package util
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io/fs"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
func getRoot(path string) string {
|
|
||||||
for {
|
|
||||||
parent := filepath.Dir(path)
|
|
||||||
if parent == "." {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
path = parent
|
|
||||||
}
|
|
||||||
fmt.Println("getRoot: ", path)
|
|
||||||
return path
|
|
||||||
}
|
|
||||||
|
|
||||||
func replaceRoot(inPath, outRoot string) string {
|
|
||||||
relPath := strings.TrimPrefix(inPath, getRoot(inPath))
|
|
||||||
outPath := filepath.Join(outRoot, relPath)
|
|
||||||
return outPath
|
|
||||||
}
|
|
||||||
|
|
||||||
func createFileWithParents(path string) error {
|
|
||||||
dir := filepath.Dir(path)
|
|
||||||
// Check if the parent directory already exists
|
|
||||||
// before trying to create it
|
|
||||||
if _, dirErr := os.Stat(dir); os.IsNotExist(dirErr) {
|
|
||||||
// create directories
|
|
||||||
err := os.MkdirAll(dir, os.ModePerm)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// TODO: write the file here
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func processFile(inPath string, entry fs.DirEntry, err error, outRoot string) error {
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !entry.IsDir() {
|
|
||||||
ext := filepath.Ext(inPath)
|
|
||||||
fmt.Println("Root: ", replaceRoot(inPath, outRoot))
|
|
||||||
switch ext {
|
|
||||||
case ".md":
|
|
||||||
fmt.Println("Processing markdown...")
|
|
||||||
default:
|
|
||||||
// All other file types, we copy!
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fmt.Printf("Visited: %s\n", inPath)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func Traverse(root string, outRoot string) error {
|
|
||||||
// err := filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error {
|
|
||||||
walkFunc := func(path string, entry fs.DirEntry, err error) error {
|
|
||||||
return processFile(path, entry, err, outRoot)
|
|
||||||
}
|
|
||||||
err := filepath.WalkDir(root, walkFunc)
|
|
||||||
return err
|
|
||||||
}
|
|
|
@ -1,3 +0,0 @@
|
||||||
# My amazing markdown file!
|
|
||||||
|
|
||||||
I can _even_ do **this**!
|
|
|
@ -1,3 +0,0 @@
|
||||||
<h1 id="my-amazing-markdown-file">My amazing markdown file!</h1>
|
|
||||||
|
|
||||||
<p>I can <em>even</em> do <strong>this</strong>!</p>
|
|
1
zona
1
zona
|
@ -1 +0,0 @@
|
||||||
bin/zona
|
|
Loading…
Add table
Add a link
Reference in a new issue