Skip to content

Commit

Permalink
Merge pull request #38 from krymtkts:feature/refine
Browse files Browse the repository at this point in the history
Refine.
  • Loading branch information
krymtkts committed Aug 5, 2023
2 parents df3374e + b713c8e commit 9778f38
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 105 deletions.
22 changes: 9 additions & 13 deletions src/App.fs
Expand Up @@ -15,21 +15,17 @@ render
src = "contents"
dst = "docs"

postsRoot = "/posts"
postsTitle = "Posts"
pagesRoot = "/pages"
paesTitle = "Pages"
tagsRoot = "/tags"
tagsTitle = "Tags"
archivesRoot = "/archives"
archivesTitle = "Archives"
posts = { root = "/posts"; title = "Posts" }
pages = { root = "/pages"; title = "Pages" }
tags = { root = "/tags"; title = "Tags" }
archives =
{ root = "/archives"
title = "Archives" }

feed = "feed"
feedName = "feed"

additionalNavs =
[ Link
{ text = "About Me"
path = "/pages/about.html"
sitemap = No } ]
[ { text = "About Me"
path = "/pages/about.html" } ]

}
52 changes: 29 additions & 23 deletions src/Common.fs
@@ -1,13 +1,10 @@
module Common

open System.Text.RegularExpressions
open Fable.Core.JsInterop
open Fable.Core
open Fable.Core.JsInterop
open Feliz
open Node
open Marked
open HighlightJs
open Yaml

module IO =
let resolve (path: string) = File.absolutePath path
Expand All @@ -19,6 +16,8 @@ module IO =
let parent = Directory.dirname

module private Util =
open HighlightJs
open Marked

let mdToHtml s = Regex.Replace(s, @"\.md\b", ".html")

Expand All @@ -29,7 +28,7 @@ module private Util =
let heading =
fun (text: string) (level: int) ->
let escapedText = Regex.Replace(string text, @"[^\w]+", "-")
let l = level.ToString()
let l = string level

$"""<h{l}><a name="{escapedText}" class="anchor" href="#{escapedText}">{text}</a></h{l}>"""

Expand Down Expand Up @@ -103,21 +102,28 @@ module Component =
liA ref <| Text title

module Parser =
open Yaml

type FrontMatter =
abstract title: string
abstract tags: string array option
abstract date: string option

let private pattern =
Regex(@"^---\s*\n(?<frontMatter>[\s\S]*?)\n?---\s*\n?(?<content>[\s\S]*)")
let private matchFrontMatter s =
Regex.Match(s, @"^---\s*\n(?<frontMatter>[\s\S]*?)\n?---\s*\n?(?<content>[\s\S]*)")

let (|Empty|Matched|) (xs: Match) =
match xs.Success with
| false -> Empty
| _ -> Matched xs

let private extractFrontMatter (str: string) =
match pattern.IsMatch str with
| true ->
let matches = pattern.Match str
let f: FrontMatter = Yaml.parse matches.Groups.["frontMatter"].Value
Some(f), matches.Groups.["content"].Value
| _ -> None, str
matchFrontMatter str
|> function
| Empty -> None, str
| Matched matches ->
let f: FrontMatter = Yaml.parse matches.Groups.["frontMatter"].Value
Some(f), matches.Groups.["content"].Value

/// Parses a markdown string
let parseMarkdown str = Util.parseMarkdown str
Expand Down Expand Up @@ -168,9 +174,7 @@ module Misc =

match leaf.Split '-' |> List.ofArray with
| year :: month :: day :: _ ->
match [ year; month; day ]
|> List.map System.Int32.TryParse
with
match [ year; month; day ] |> List.map Int32.TryParse with
| [ (true, year); (true, month); (true, day) ] ->
let date = $"%04d{year}-%02d{month}-%02d{day}"
Post(date)
Expand Down Expand Up @@ -311,6 +315,10 @@ module Misc =
| x -> x)
)

module String =

let inline format pattern x =
(^a: (member ToString: string -> string) (x, pattern))

module DateTime =
open System
Expand All @@ -329,9 +337,8 @@ module DateTime =

// TODO: write binding.
let formatter: obj = Intl.DateTimeFormat "en-US" options
let zonePattern = new Regex(@"GMT([+-])(\d+)")

let toRFC822DateTime (d: DateTime) =
let toRFC822DateTimeString (d: DateTime) =
let parts: obj [] = formatter?formatToParts (d)
let p: string [] = parts |> Array.map (fun x -> x?value)
let d = $"{p.[0]}{p.[1]}{p.[4]} {p.[2]} {p.[6]}"
Expand All @@ -341,7 +348,7 @@ module DateTime =
match p.[14] with
| "UTC" -> "+0000"
| z ->
let item = zonePattern.Matches(z)
let item = Regex.Matches(z, @"GMT([+-])(\d+)")
let group = item.Item 0
let op = (group.Groups.Item 1).Value
let offset = int (group.Groups.Item 2).Value
Expand All @@ -350,8 +357,7 @@ module DateTime =

$"{d} {t} {z}"

module String =
open System
let parseToRFC822DateTimeString (s: string) =
DateTime.Parse(s) |> toRFC822DateTimeString

let toRFC822DateTime (s: string) =
DateTime.Parse(s) |> DateTime.toRFC822DateTime
let toRFC3339Date (d: DateTime) = d |> String.format "yyyy-MM-dd"
130 changes: 71 additions & 59 deletions src/Generator.fs
Expand Up @@ -135,7 +135,7 @@ module Generation =
|> Map.toList
|> Seq.map (fun (tag, _) ->
{ loc = sourceToSitemap $"{pathRoot}{def.tagRoot}" $"{tag}.html"
lastmod = now.ToString("yyyy-MM-dd")
lastmod = now |> DateTime.toRFC3339Date
priority = def.priority })

tagsContent, tagPageContens, locs
Expand Down Expand Up @@ -163,7 +163,7 @@ module Generation =
| Yes n ->
Some
{ loc = $"{pathRoot}{navi.path}"
lastmod = now.ToString("yyyy-MM-dd")
lastmod = now |> DateTime.toRFC3339Date
priority = n }
| No -> None

Expand Down Expand Up @@ -281,7 +281,7 @@ module Generation =
| Some d -> d
| None -> meta.date
| None -> meta.date
|> String.toRFC822DateTime
|> DateTime.parseToRFC822DateTimeString

{ guid = link
link = link
Expand All @@ -299,7 +299,7 @@ module Generation =
description = conf.description
link = conf.link
xml = conf.feed
lastBuildDate = now |> DateTime.toRFC822DateTime
lastBuildDate = now |> DateTime.toRFC822DateTimeString
generator = generatorName }
items

Expand Down Expand Up @@ -364,7 +364,7 @@ module Rndering =
let date =
match layout with
| Post d -> chooseDate fm d
| Page -> chooseDate fm <| now.ToString("yyyy-MM-dd")
| Page -> chooseDate fm <| DateTime.toRFC3339Date now

return
{ frontMatter = fm
Expand Down Expand Up @@ -525,6 +525,10 @@ type Mode =
| Development
| Production

type Content = { root: string; title: string }

type AdditionalNav = { text: string; path: string }

type RnderOptions =
{ stage: Mode
siteName: string
Expand All @@ -538,51 +542,60 @@ type RnderOptions =
src: string
dst: string

postsRoot: string
postsTitle: string
pagesRoot: string
paesTitle: string
tagsRoot: string
tagsTitle: string
archivesRoot: string
archivesTitle: string
posts: Content
pages: Content
tags: Content
archives: Content

additionalNavs: Nav list
additionalNavs: AdditionalNav list

feed: string
feedName: string

}

let private buildNavList opts =
let index = "/index.html"
let feed = $"/{opts.feedName}.xml"

index,
feed,
List.concat [ [ Title
{ text = opts.siteName
path = index
sitemap = Yes "1.0" }
Link
{ text = opts.archives.title
path = $"{opts.archives.root}.html"
sitemap = Yes "0.9" }
Link
{ text = opts.tags.title
path = $"{opts.tags.root}.html"
sitemap = Yes "0.9" } ]
List.map
(fun n ->
Link
{ text = n.text
path = n.path
sitemap = No })
opts.additionalNavs
[ Link
{ text = "RSS"
path = feed
sitemap = No } ] ]

let private buildDevScript opts =
match opts.stage with
| Development -> Some("/js/dev.js"), [ ("src/Dev.fs.js", $"{opts.dst}{opts.pathRoot}/js/dev.js") ]
| Production -> None, []

let render (opts: RnderOptions) =
promise {
let index = "/index.html"
let feed = $"/{opts.feed}.xml"

let navs =
[ Title
{ text = opts.siteName
path = index
sitemap = Yes "1.0" }
Link
{ text = opts.archivesTitle
path = $"{opts.archivesRoot}.html"
sitemap = Yes "0.9" }
Link
{ text = opts.tagsTitle
path = $"{opts.tagsRoot}.html"
sitemap = Yes "0.9" } ]
@ opts.additionalNavs
@ [ Link
{ text = "RSS"
path = feed
sitemap = No } ]

let navbar, navSitemap = generateNavbar opts.pathRoot navs

let devInjection, devScript =
match opts.stage with
| Development -> Some("/js/dev.js"), [ ("src/Dev.fs.js", $"{opts.dst}{opts.pathRoot}/js/dev.js") ]
| Production -> None, []
let index, feed, navs = buildNavList opts

let (navbar: Fable.React.ReactElement), navSitemap =
generateNavbar opts.pathRoot navs

let devInjection, devScript = buildDevScript opts

let site: FixedSiteContent =
{ lang = opts.lang
Expand All @@ -591,39 +604,39 @@ let render (opts: RnderOptions) =
title = opts.siteName
description = opts.description
url = opts.siteUrl
pathRoot = opts.pathRoot
pathRoot = opts.pathRoot // TODO: remove pathRoot from here and add to new created path type that includes src, dst and pathRoot.
copyright = opts.copyright
favicon = opts.favicon
devInjection = devInjection }

let renderPostAndPages = renderMarkdowns site opts.tagsRoot
let! metaPosts = renderPostAndPages $"{opts.src}{opts.postsRoot}" $"{opts.dst}{opts.pathRoot}{opts.postsRoot}"
let! metaPages = renderPostAndPages $"{opts.src}{opts.pagesRoot}" $"{opts.dst}{opts.pathRoot}{opts.pagesRoot}"
let renderPostAndPages = renderMarkdowns site opts.tags.root
let! metaPosts = renderPostAndPages $"{opts.src}{opts.posts.root}" $"{opts.dst}{opts.pathRoot}{opts.posts.root}"
let! metaPages = renderPostAndPages $"{opts.src}{opts.pages.root}" $"{opts.dst}{opts.pathRoot}{opts.pages.root}"

do! renderIndex site opts.tagsRoot metaPosts $"{opts.dst}{opts.pathRoot}{index}"
do! renderIndex site opts.tags.root metaPosts $"{opts.dst}{opts.pathRoot}{index}"

let archiveDefs =
[ Posts
{ title = opts.postsTitle
{ title = opts.posts.title
metas = metaPosts
root = opts.postsRoot
root = opts.posts.root
priority = "0.8" }
Pages
{ title = opts.paesTitle
{ title = opts.pages.title
metas = metaPages
root = opts.pagesRoot
root = opts.pages.root
priority = "0.8" } ]

let! archiveLocs = renderArchives site archiveDefs $"{opts.dst}{site.pathRoot}{opts.archivesRoot}.html"
let! archiveLocs = renderArchives site archiveDefs $"{opts.dst}{site.pathRoot}{opts.archives.root}.html"

let tagDef =
{ title = opts.tagsTitle
{ title = opts.tags.title
metas = Seq.concat [ metaPosts; metaPages ]
tagRoot = opts.tagsRoot
postRoot = opts.postsRoot
tagRoot = opts.tags.root
postRoot = opts.posts.root
priority = "0.9" }

let! tagLocs = renderTags site tagDef $"{opts.dst}{site.pathRoot}{opts.tagsRoot}.html"
let! tagLocs = renderTags site tagDef $"{opts.dst}{site.pathRoot}{opts.tags.root}.html"
do! render404 site $"{opts.dst}{opts.pathRoot}/404.html"

do!
Expand All @@ -634,14 +647,13 @@ let render (opts: RnderOptions) =
tagLocs
archiveLocs ])


do!
renderFeed
{ title = opts.siteName
description = opts.description
link = $"{opts.siteUrl}{opts.pathRoot}"
feed = feed
postRoot = opts.postsRoot
postRoot = opts.posts.root
posts = metaPosts }
$"{opts.dst}{opts.pathRoot}{feed}"

Expand Down

0 comments on commit 9778f38

Please sign in to comment.