Write root page using a template
This commit is contained in:
parent
3106e81146
commit
329dcc39cd
|
@ -1,3 +1,3 @@
|
||||||
# jack
|
# jack
|
||||||
|
|
||||||
A static blog generator written in Go
|
A static blog generator written in Go
|
||||||
|
|
2
main.go
2
main.go
|
@ -67,6 +67,8 @@ func main() {
|
||||||
site.Append(&article)
|
site.Append(&article)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
site.WriteRootPage()
|
||||||
|
|
||||||
site.Build()
|
site.Build()
|
||||||
|
|
||||||
fmt.Println("HTML files were written to: ", *outputDir)
|
fmt.Println("HTML files were written to: ", *outputDir)
|
||||||
|
|
12
main_test.go
12
main_test.go
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -3,9 +3,11 @@ package models
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/yuin/goldmark"
|
"github.com/yuin/goldmark"
|
||||||
|
@ -59,6 +61,26 @@ func (s *Site) Append(a *Article) {
|
||||||
fmt.Println(categories)
|
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() {
|
func (s *Site) Build() {
|
||||||
for _, article := range s.Articles {
|
for _, article := range s.Articles {
|
||||||
article.Convert()
|
article.Convert()
|
||||||
|
|
|
@ -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)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
<h1>Site generated by Jack</h1>
|
||||||
|
|
||||||
|
<p>{{ .Name }}</p>
|
||||||
|
|
||||||
|
<h2>Articles</h2>
|
||||||
|
|
||||||
|
{{ range .Articles }}
|
||||||
|
<p>{{ .Title }}</p>
|
||||||
|
{{ end }}
|
Loading…
Reference in New Issue