Working (basic) version of the html generator
This commit is contained in:
parent
770195324c
commit
e9e6b8c547
1
go.mod
1
go.mod
|
@ -5,6 +5,7 @@ go 1.18
|
||||||
require github.com/yuin/goldmark v1.4.14
|
require github.com/yuin/goldmark v1.4.14
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/otiai10/copy v1.7.0 // indirect
|
||||||
github.com/yuin/goldmark-meta v1.1.0 // indirect
|
github.com/yuin/goldmark-meta v1.1.0 // indirect
|
||||||
gopkg.in/yaml.v2 v2.3.0 // indirect
|
gopkg.in/yaml.v2 v2.3.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
6
go.sum
6
go.sum
|
@ -1,3 +1,9 @@
|
||||||
|
github.com/otiai10/copy v1.7.0 h1:hVoPiN+t+7d2nzzwMiDHPSOogsWAStewq3TwU05+clE=
|
||||||
|
github.com/otiai10/copy v1.7.0/go.mod h1:rmRl6QPdJj6EiUqXQ/4Nn2lLXoNQjFCQbbNrxgc/t3U=
|
||||||
|
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
|
||||||
|
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
|
||||||
|
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
|
||||||
|
github.com/otiai10/mint v1.3.3/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
|
||||||
github.com/yuin/goldmark v1.4.14 h1:jwww1XQfhJN7Zm+/a1ZA/3WUiEBEroYFNTiV3dKwM8U=
|
github.com/yuin/goldmark v1.4.14 h1:jwww1XQfhJN7Zm+/a1ZA/3WUiEBEroYFNTiV3dKwM8U=
|
||||||
github.com/yuin/goldmark v1.4.14/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
github.com/yuin/goldmark v1.4.14/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
github.com/yuin/goldmark-meta v1.1.0 h1:pWw+JLHGZe8Rk0EGsMVssiNb/AaPMHfSRszZeUeiOUc=
|
github.com/yuin/goldmark-meta v1.1.0 h1:pWw+JLHGZe8Rk0EGsMVssiNb/AaPMHfSRszZeUeiOUc=
|
||||||
|
|
16
main.go
16
main.go
|
@ -7,6 +7,8 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
cp "github.com/otiai10/copy"
|
||||||
)
|
)
|
||||||
|
|
||||||
func walk(rootDir string) ([]string, error) {
|
func walk(rootDir string) ([]string, error) {
|
||||||
|
@ -42,11 +44,17 @@ func main() {
|
||||||
outputDir = flag.String("out", defaultOutputDir, "Specify the output directory")
|
outputDir = flag.String("out", defaultOutputDir, "Specify the output directory")
|
||||||
flag.Usage = func() {
|
flag.Usage = func() {
|
||||||
fmt.Printf("Jack expects: \n")
|
fmt.Printf("Jack expects: \n")
|
||||||
fmt.Printf("./jack -dir <rootDir>\n")
|
fmt.Printf("./jack -dir <rootDir> -out <outputDir>\n")
|
||||||
}
|
}
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
files, _ := walk(*rootDir)
|
copyErr := cp.Copy(*rootDir, *outputDir)
|
||||||
|
|
||||||
|
if copyErr != nil {
|
||||||
|
fmt.Println(copyErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
files, _ := walk(*outputDir)
|
||||||
|
|
||||||
var articles []models.Article
|
var articles []models.Article
|
||||||
|
|
||||||
|
@ -59,10 +67,8 @@ func main() {
|
||||||
fmt.Println(article)
|
fmt.Println(article)
|
||||||
|
|
||||||
articles = append(articles, article)
|
articles = append(articles, article)
|
||||||
}
|
|
||||||
|
|
||||||
for _, article := range articles {
|
article.Write()
|
||||||
article.WriteTo(outputDir)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("HTML files were written to: ", *outputDir)
|
fmt.Println("HTML files were written to: ", *outputDir)
|
||||||
|
|
|
@ -2,11 +2,10 @@ package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"strings"
|
||||||
|
|
||||||
"github.com/yuin/goldmark"
|
"github.com/yuin/goldmark"
|
||||||
meta "github.com/yuin/goldmark-meta"
|
meta "github.com/yuin/goldmark-meta"
|
||||||
|
@ -24,7 +23,7 @@ type Article struct {
|
||||||
RelativeFilePath string
|
RelativeFilePath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a Article) FromFile() Article {
|
func (a *Article) FromFile() {
|
||||||
// Build an Article based on a file path
|
// Build an Article based on a file path
|
||||||
|
|
||||||
content, err := os.ReadFile(a.File)
|
content, err := os.ReadFile(a.File)
|
||||||
|
@ -32,10 +31,10 @@ func (a Article) FromFile() Article {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return a.FromContent(content)
|
a.FromContent(content)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a Article) FromContent(content []byte) Article {
|
func (a *Article) FromContent(content []byte) {
|
||||||
// Build an Article instance from a byte[] content
|
// Build an Article instance from a byte[] content
|
||||||
|
|
||||||
md := goldmark.New(
|
md := goldmark.New(
|
||||||
|
@ -54,23 +53,12 @@ func (a Article) FromContent(content []byte) Article {
|
||||||
if err := md.Convert(content, &buf, parser.WithContext(context)); err != nil {
|
if err := md.Convert(content, &buf, parser.WithContext(context)); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
a.HtmlContent = buf.String()
|
||||||
metadata := meta.Get(context)
|
metadata := meta.Get(context)
|
||||||
a.Title, _ = metadata["Title"].(string)
|
a.Title, _ = metadata["Title"].(string)
|
||||||
a.HtmlContent = buf.String()
|
|
||||||
|
|
||||||
fmt.Println(a.Title)
|
|
||||||
fmt.Println(a.HtmlContent)
|
|
||||||
|
|
||||||
return a
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a Article) WriteTo(outputPath *string) {
|
func (a Article) Write() {
|
||||||
outputFilePath := filepath.Join(*outputPath, a.RelativeFilePath, "index.html")
|
outputFilePath := strings.Replace(a.File, ".md", ".html", -1)
|
||||||
|
ioutil.WriteFile(outputFilePath, []byte(a.HtmlContent), 0644)
|
||||||
// creates the full directories tree
|
|
||||||
if err := os.MkdirAll("a/b/c/d", os.ModePerm); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
ioutil.WriteFile(outputFilePath, []byte(a.HtmlContent), 0644) // the 0644 is octal representation of the filemode
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue