Implemented converting a markdown file to HTML
This commit is contained in:
parent
b36db1119c
commit
43cf614462
4 changed files with 79 additions and 34 deletions
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/ficcdaf/zona/internal/convert"
|
||||||
"github.com/ficcdaf/zona/internal/util"
|
"github.com/ficcdaf/zona/internal/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -45,13 +46,5 @@ func main() {
|
||||||
fmt.Println("File validation failed!")
|
fmt.Println("File validation failed!")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
convert.ConvertFile(*mdPath, "test/test.html")
|
||||||
file, err := os.Open(*mdPath)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error reading file:", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
fmt.Println("File opened :)")
|
|
||||||
}
|
}
|
||||||
|
|
76
internal/convert/convert.go
Normal file
76
internal/convert/convert.go
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
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,22 +0,0 @@
|
||||||
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
|
|
||||||
}
|
|
|
@ -3,10 +3,8 @@ package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// CheckExtension checks if the file located at path (string)
|
// CheckExtension checks if the file located at path (string)
|
||||||
|
@ -26,7 +24,7 @@ func PathIsValid(path string, requireFile bool) bool {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return false
|
return false
|
||||||
} else if requireFile {
|
} else if requireFile {
|
||||||
fmt.Printf("Directory status: %s\n", strconv.FormatBool(s.IsDir()))
|
// fmt.Printf("Directory status: %s\n", strconv.FormatBool(s.IsDir()))
|
||||||
return !s.IsDir()
|
return !s.IsDir()
|
||||||
}
|
}
|
||||||
return err == nil
|
return err == nil
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue