Implement ReadFile and ReadContent methods

This commit is contained in:
Fred Pauchet 2022-11-01 21:10:05 +01:00
parent e92c41835d
commit 014a072b25
3 changed files with 97 additions and 14 deletions

View File

@ -48,14 +48,19 @@ func main() {
files, _ := walk(*rootDir)
var articles []models.Article
for _, file := range files {
fmt.Println(file)
article := models.Article{
File: file,
}
article.ReadFile()
article.FromFile()
fmt.Println(article)
articles = append(articles, article)
}
fmt.Println(*outputDir)
fmt.Println("HTML files were written to: ", *outputDir)
fmt.Println("Number of parsed articles: ", len(articles))
}

View File

@ -5,7 +5,6 @@ import (
"fmt"
"log"
"os"
"time"
"github.com/yuin/goldmark"
meta "github.com/yuin/goldmark-meta"
@ -15,25 +14,28 @@ import (
)
type Article struct {
Title string
HtmlContent string
publishedDate string
Url string
File string
Title string
HtmlContent string
publishedDate string
Url string
File string
RelativeFilePath string
}
func (a Article) PublishedDate() {
var t time.Time
fmt.Println(t)
}
func (a Article) ReadFile() Article {
func (a Article) FromFile() Article {
// Build an Article based on a file path
content, err := os.ReadFile(a.File)
if err != nil {
log.Fatal(err)
}
return a.FromContent(content)
}
func (a Article) FromContent(content []byte) Article {
// Build an Article instance from a byte[] content
md := goldmark.New(
goldmark.WithExtensions(extension.GFM, meta.Meta),
goldmark.WithParserOptions(
@ -59,3 +61,7 @@ func (a Article) ReadFile() Article {
return a
}
func (a Article) WriteTo(outputPath *string) {
}

72
models/models_test.go Normal file
View File

@ -0,0 +1,72 @@
package models
import (
"reflect"
"testing"
)
func TestArticle_FromFile(t *testing.T) {
type fields struct {
Title string
HtmlContent string
publishedDate string
Url string
File string
}
tests := []struct {
name string
fields fields
want Article
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := Article{
Title: tt.fields.Title,
HtmlContent: tt.fields.HtmlContent,
publishedDate: tt.fields.publishedDate,
Url: tt.fields.Url,
File: tt.fields.File,
}
if got := a.FromFile(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Article.FromFile() = %v, want %v", got, tt.want)
}
})
}
}
func TestArticle_FromContent(t *testing.T) {
type fields struct {
Title string
HtmlContent string
publishedDate string
Url string
File string
}
type args struct {
content []byte
}
tests := []struct {
name string
fields fields
args args
want Article
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := Article{
Title: tt.fields.Title,
HtmlContent: tt.fields.HtmlContent,
publishedDate: tt.fields.publishedDate,
Url: tt.fields.Url,
File: tt.fields.File,
}
if got := a.FromContent(tt.args.content); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Article.FromContent() = %v, want %v", got, tt.want)
}
})
}
}