Skip to content

Commit e0bfb19

Browse files
Robert PickeringKrzysztof-Cieslak
authored andcommitted
Refactor template
This one is a bit more subjective, my aim was to get rid of some of the copy and pasted code in the template. I found it easier to develop my own site once these changes where applied.
1 parent 25a2715 commit e0bfb19

File tree

6 files changed

+56
-77
lines changed

6 files changed

+56
-77
lines changed

src/Fornax.Core/AssemblyInfo.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ open System.Reflection
55
[<assembly: AssemblyTitleAttribute("Fornax.Core")>]
66
[<assembly: AssemblyProductAttribute("Fornax")>]
77
[<assembly: AssemblyDescriptionAttribute("Fornax is a static site generator using type safe F# DSL to define page layouts")>]
8-
[<assembly: AssemblyVersionAttribute("0.11.1")>]
9-
[<assembly: AssemblyFileVersionAttribute("0.11.1")>]
8+
[<assembly: AssemblyVersionAttribute("0.11.2")>]
9+
[<assembly: AssemblyFileVersionAttribute("0.11.2")>]
1010
do ()
1111

1212
module internal AssemblyVersionInformation =
1313
let [<Literal>] AssemblyTitle = "Fornax.Core"
1414
let [<Literal>] AssemblyProduct = "Fornax"
1515
let [<Literal>] AssemblyDescription = "Fornax is a static site generator using type safe F# DSL to define page layouts"
16-
let [<Literal>] AssemblyVersion = "0.11.1"
17-
let [<Literal>] AssemblyFileVersion = "0.11.1"
16+
let [<Literal>] AssemblyVersion = "0.11.2"
17+
let [<Literal>] AssemblyFileVersion = "0.11.2"

src/Fornax.Template/generators/index.fsx

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,11 @@ let generate' (ctx : SiteContents) (_: string) =
1111
|> Option.map (fun si -> si.description)
1212
|> Option.defaultValue ""
1313

14-
let published (post: Postloader.Post) =
15-
post.published
16-
|> Option.defaultValue System.DateTime.Now
17-
|> fun n -> n.ToString("yyyy-MM-dd")
18-
19-
2014
let psts =
2115
posts
22-
|> Seq.sortByDescending published
16+
|> Seq.sortByDescending Layout.published
2317
|> Seq.toList
24-
|> List.map (fun post ->
25-
div [Class "card article"] [
26-
div [Class "card-content"] [
27-
div [Class "media-content has-text-centered"] [
28-
p [Class "title article-title"; ] [ a [Href post.link] [!! post.title]]
29-
p [Class "subtitle is-6 article-subtitle"] [
30-
a [Href "#"] [!! (defaultArg post.author "")]
31-
!! (sprintf "on %s" (published post))
32-
]
33-
]
34-
div [Class "content article-body"] [
35-
!! post.content
36-
]
37-
]
38-
])
18+
|> List.map Layout.postLayout
3919

4020
Layout.layout ctx "Home" [
4121
section [Class "hero is-info is-medium is-bold"] [

src/Fornax.Template/generators/layout.fsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,25 @@ let render (ctx : SiteContents) cnt =
8181
let disableLiveRefresh = ctx.TryGetValue<Postloader.PostConfig> () |> Option.map (fun n -> n.disableLiveRefresh) |> Option.defaultValue false
8282
cnt
8383
|> HtmlElement.ToString
84-
|> fun n -> if disableLiveRefresh then n else injectWebsocketCode n
84+
|> fun n -> if disableLiveRefresh then n else injectWebsocketCode n
85+
86+
let published (post: Postloader.Post) =
87+
post.published
88+
|> Option.defaultValue System.DateTime.Now
89+
|> fun n -> n.ToString("yyyy-MM-dd")
90+
91+
let postLayout (post: Postloader.Post) =
92+
div [Class "card article"] [
93+
div [Class "card-content"] [
94+
div [Class "media-content has-text-centered"] [
95+
p [Class "title article-title"; ] [ a [Href post.link] [!! post.title]]
96+
p [Class "subtitle is-6 article-subtitle"] [
97+
a [Href "#"] [!! (defaultArg post.author "")]
98+
!! (sprintf "on %s" (published post))
99+
]
100+
]
101+
div [Class "content article-body"] [
102+
!! post.content
103+
]
104+
]
105+
]

src/Fornax.Template/generators/post.fsx

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ let generate' (ctx : SiteContents) (page: string) =
1616
|> Option.map (fun si -> si.description)
1717
|> Option.defaultValue ""
1818

19-
let published (post: Postloader.Post) =
20-
post.published
21-
|> Option.defaultValue System.DateTime.Now
22-
|> fun n -> n.ToString("yyyy-MM-dd")
23-
2419
Layout.layout ctx post.title [
2520
section [Class "hero is-info is-medium is-bold"] [
2621
div [Class "hero-body"] [
@@ -32,20 +27,7 @@ let generate' (ctx : SiteContents) (page: string) =
3227
div [Class "container"] [
3328
section [Class "articles"] [
3429
div [Class "column is-8 is-offset-2"] [
35-
div [Class "card article"] [
36-
div [Class "card-content"] [
37-
div [Class "media-content has-text-centered"] [
38-
p [Class "title article-title"; ] [ a [Href post.link] [!! post.title]]
39-
p [Class "subtitle is-6 article-subtitle"] [
40-
a [Href "#"] [!! (defaultArg post.author "")]
41-
!! (sprintf "on %s" (published post))
42-
]
43-
]
44-
div [Class "content article-body"] [
45-
!! post.content
46-
]
47-
]
48-
]
30+
Layout.postLayout post
4931
]
5032
]
5133
]

src/Fornax.Template/loaders/postloader.fsx

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Post = {
1616
content: string
1717
}
1818

19+
let contentDir = "posts"
1920

2021
let markdownPipeline =
2122
MarkdownPipelineBuilder()
@@ -32,10 +33,19 @@ let getConfig (fileContent : string) =
3233
let fileContent = fileContent.Split '\n'
3334
let fileContent = fileContent |> Array.skip 1 //First line must be ---
3435
let indexOfSeperator = fileContent |> Array.findIndex isSeparator
36+
let splitKey (line: string) =
37+
let seperatorIndex = line.IndexOf(':')
38+
if seperatorIndex > 0 then
39+
let key = line.[.. seperatorIndex - 1].Trim().ToLower()
40+
let value = line.[seperatorIndex + 1 ..].Trim()
41+
Some(key, value)
42+
else
43+
None
3544
fileContent
3645
|> Array.splitAt indexOfSeperator
3746
|> fst
38-
|> String.concat "\n"
47+
|> Seq.choose splitKey
48+
|> Map.ofSeq
3949

4050
///`fileContent` - content of page to parse. Usually whole content of `.md` file
4151
///returns HTML version of content of the page
@@ -54,36 +64,22 @@ let trimString (str : string) =
5464
let loadFile n =
5565
let text = System.IO.File.ReadAllText n
5666

57-
let config = (getConfig text).Split( '\n') |> List.ofArray
58-
67+
let config = getConfig text
5968
let content = getContent text
6069

61-
let file = System.IO.Path.Combine("posts", (n |> System.IO.Path.GetFileNameWithoutExtension) + ".md").Replace("\\", "/")
62-
let link = "/" + System.IO.Path.Combine("posts", (n |> System.IO.Path.GetFileNameWithoutExtension) + ".html").Replace("\\", "/")
63-
64-
let title = config |> List.find (fun n -> n.ToLower().StartsWith "title" ) |> fun n -> n.Split(':').[1] |> trimString
65-
66-
let author =
67-
try
68-
config |> List.tryFind (fun n -> n.ToLower().StartsWith "author" ) |> Option.map (fun n -> n.Split(':').[1] |> trimString)
69-
with
70-
| _ -> None
70+
let file = System.IO.Path.Combine(contentDir, (n |> System.IO.Path.GetFileNameWithoutExtension) + ".md").Replace("\\", "/")
71+
let link = "/" + System.IO.Path.Combine(contentDir, (n |> System.IO.Path.GetFileNameWithoutExtension) + ".html").Replace("\\", "/")
7172

72-
let published =
73-
try
74-
config |> List.tryFind (fun n -> n.ToLower().StartsWith "published" ) |> Option.map (fun n -> n.Split(':').[1] |> trimString |> System.DateTime.Parse)
75-
with
76-
| _ -> None
73+
let title = config |> Map.find "title" |> trimString
74+
let author = config |> Map.tryFind "author" |> Option.map trimString
75+
let published = config |> Map.tryFind "published" |> Option.map (trimString >> System.DateTime.Parse)
7776

7877
let tags =
79-
try
80-
let x =
81-
config
82-
|> List.tryFind (fun n -> n.ToLower().StartsWith "tags" )
83-
|> Option.map (fun n -> n.Split(':').[1] |> trimString |> fun n -> n.Split ',' |> Array.toList )
84-
defaultArg x []
85-
with
86-
| _ -> []
78+
let tagsOpt =
79+
config
80+
|> Map.tryFind "tags"
81+
|> Option.map (trimString >> fun n -> n.Split ',' |> Array.toList)
82+
defaultArg tagsOpt []
8783

8884
{ file = file
8985
link = link
@@ -94,7 +90,7 @@ let loadFile n =
9490
content = content }
9591

9692
let loader (projectRoot: string) (siteContent: SiteContents) =
97-
let postsPath = System.IO.Path.Combine(projectRoot, "posts")
93+
let postsPath = System.IO.Path.Combine(projectRoot, contentDir)
9894
System.IO.Directory.GetFiles postsPath
9995
|> Array.filter (fun n -> n.EndsWith ".md")
10096
|> Array.map loadFile

src/Fornax/AssemblyInfo.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ open System.Reflection
55
[<assembly: AssemblyTitleAttribute("Fornax")>]
66
[<assembly: AssemblyProductAttribute("Fornax")>]
77
[<assembly: AssemblyDescriptionAttribute("Fornax is a static site generator using type safe F# DSL to define page layouts")>]
8-
[<assembly: AssemblyVersionAttribute("0.11.1")>]
9-
[<assembly: AssemblyFileVersionAttribute("0.11.1")>]
8+
[<assembly: AssemblyVersionAttribute("0.11.2")>]
9+
[<assembly: AssemblyFileVersionAttribute("0.11.2")>]
1010
do ()
1111

1212
module internal AssemblyVersionInformation =
1313
let [<Literal>] AssemblyTitle = "Fornax"
1414
let [<Literal>] AssemblyProduct = "Fornax"
1515
let [<Literal>] AssemblyDescription = "Fornax is a static site generator using type safe F# DSL to define page layouts"
16-
let [<Literal>] AssemblyVersion = "0.11.1"
17-
let [<Literal>] AssemblyFileVersion = "0.11.1"
16+
let [<Literal>] AssemblyVersion = "0.11.2"
17+
let [<Literal>] AssemblyFileVersion = "0.11.2"

0 commit comments

Comments
 (0)