Write root page using a template

This commit is contained in:
Fred Pauchet 2022-11-07 19:10:49 +01:00
parent 3106e81146
commit 329dcc39cd
6 changed files with 34 additions and 85 deletions

View File

@ -1,3 +1,3 @@
# jack
A static blog generator written in Go
A static blog generator written in Go

View File

@ -67,6 +67,8 @@ func main() {
site.Append(&article)
}
site.WriteRootPage()
site.Build()
fmt.Println("HTML files were written to: ", *outputDir)

View File

@ -1,12 +0,0 @@
package main
import "testing"
func TestSayHi(t *testing.T) {
expected := "Hi, I'm Jack"
greeting := sayHi()
if greeting != expected {
t.Errorf("Greeting was incorrect, got: '%s', want: '%s'", greeting, expected)
}
}

View File

@ -3,9 +3,11 @@ package models
import (
"bytes"
"fmt"
"html/template"
"io/ioutil"
"log"
"os"
"path"
"strings"
"github.com/yuin/goldmark"
@ -59,6 +61,26 @@ func (s *Site) Append(a *Article) {
fmt.Println(categories)
}
func (s *Site) WriteRootPage() {
t, err := template.ParseFiles("templates/index.gohtml")
if err != nil {
panic(err)
}
file, err := os.Create(path.Join(s.OutputDir, "index.html"))
if err != nil {
panic(err)
}
err = t.Execute(file, s)
if err != nil {
panic(err)
}
}
func (s *Site) Build() {
for _, article := range s.Articles {
article.Convert()

View File

@ -1,72 +0,0 @@
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)
}
})
}
}

9
templates/index.gohtml Normal file
View File

@ -0,0 +1,9 @@
<h1>Site generated by Jack</h1>
<p>{{ .Name }}</p>
<h2>Articles</h2>
{{ range .Articles }}
<p>{{ .Title }}</p>
{{ end }}