From 1fd2bfd08e82f00fc2b2d426e92113119918d5a5 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Tue, 31 May 2022 15:28:11 -0400 Subject: [PATCH] - Add autoHtmx field / htmx partial support - Add page_foot tag for scripts - Add htmx to admin area - Move create/permalink import to its own module - Add htmx to tech-blog theme - Move dashboard to admin/dashboard --- src/MyWebLog.Data/Data.fs | 1 + src/MyWebLog.Domain/DataTypes.fs | 4 + src/MyWebLog.Domain/ViewModels.fs | 17 ++ src/MyWebLog/DotLiquidBespoke.fs | 60 ++++++- src/MyWebLog/Handlers/Admin.fs | 14 +- src/MyWebLog/Handlers/Helpers.fs | 10 +- src/MyWebLog/Handlers/Routes.fs | 2 +- src/MyWebLog/Handlers/User.fs | 3 +- src/MyWebLog/Maintenance.fs | 126 ++++++++++++++ src/MyWebLog/MyWebLog.fsproj | 3 + src/MyWebLog/Program.fs | 162 +----------------- src/MyWebLog/appsettings.json | 2 +- .../themes/admin/layout-partial.liquid | 62 +++++++ src/MyWebLog/themes/admin/layout.liquid | 7 +- src/MyWebLog/themes/admin/settings.liquid | 14 +- src/MyWebLog/themes/bit-badger/layout.liquid | 2 +- .../themes/daniel-j-summers/layout.liquid | 2 +- .../themes/tech-blog/layout-partial.liquid | 14 ++ src/MyWebLog/themes/tech-blog/layout.liquid | 23 ++- .../wwwroot/themes/tech-blog/style.css | 58 +++++++ 20 files changed, 396 insertions(+), 190 deletions(-) create mode 100644 src/MyWebLog/Maintenance.fs create mode 100644 src/MyWebLog/themes/admin/layout-partial.liquid create mode 100644 src/MyWebLog/themes/tech-blog/layout-partial.liquid diff --git a/src/MyWebLog.Data/Data.fs b/src/MyWebLog.Data/Data.fs index 6139d8a..49389da 100644 --- a/src/MyWebLog.Data/Data.fs +++ b/src/MyWebLog.Data/Data.fs @@ -752,6 +752,7 @@ module WebLog = "postsPerPage", webLog.postsPerPage "timeZone", webLog.timeZone "themePath", webLog.themePath + "autoHtmx", webLog.autoHtmx ] write; withRetryDefault; ignoreResult } diff --git a/src/MyWebLog.Domain/DataTypes.fs b/src/MyWebLog.Domain/DataTypes.fs index a8e8089..8f33973 100644 --- a/src/MyWebLog.Domain/DataTypes.fs +++ b/src/MyWebLog.Domain/DataTypes.fs @@ -275,6 +275,9 @@ type WebLog = /// The RSS options for this web log rss : RssOptions + + /// Whether to automatically load htmx + autoHtmx : bool } /// Functions to support web logs @@ -291,6 +294,7 @@ module WebLog = urlBase = "" timeZone = "" rss = RssOptions.empty + autoHtmx = false } /// Get the host (including scheme) and extra path from the URL base diff --git a/src/MyWebLog.Domain/ViewModels.fs b/src/MyWebLog.Domain/ViewModels.fs index 7f1327e..a495625 100644 --- a/src/MyWebLog.Domain/ViewModels.fs +++ b/src/MyWebLog.Domain/ViewModels.fs @@ -676,7 +676,11 @@ type SettingsModel = /// The theme to use to display the web log themePath : string + + /// Whether to automatically load htmx + autoHtmx : bool } + /// Create a settings model from a web log static member fromWebLog (webLog : WebLog) = { name = webLog.name @@ -685,6 +689,19 @@ type SettingsModel = postsPerPage = webLog.postsPerPage timeZone = webLog.timeZone themePath = webLog.themePath + autoHtmx = webLog.autoHtmx + } + + /// Update a web log with settings from the form + member this.update (webLog : WebLog) = + { webLog with + name = this.name + subtitle = if this.subtitle = "" then None else Some this.subtitle + defaultPage = this.defaultPage + postsPerPage = this.postsPerPage + timeZone = this.timeZone + themePath = this.themePath + autoHtmx = this.autoHtmx } diff --git a/src/MyWebLog/DotLiquidBespoke.fs b/src/MyWebLog/DotLiquidBespoke.fs index 122ca0f..95c1578 100644 --- a/src/MyWebLog/DotLiquidBespoke.fs +++ b/src/MyWebLog/DotLiquidBespoke.fs @@ -5,12 +5,17 @@ open System open System.IO open System.Web open DotLiquid +open Giraffe.ViewEngine open MyWebLog.ViewModels /// Get the current web log from the DotLiquid context let webLog (ctx : Context) = ctx.Environments[0].["web_log"] :?> WebLog +/// Does an asset exist for the current theme? +let assetExists fileName (webLog : WebLog) = + File.Exists (Path.Combine ("wwwroot", "themes", webLog.themePath, fileName)) + /// Obtain the link from known types let permalink (ctx : Context) (item : obj) (linkFunc : WebLog -> Permalink -> string) = match item with @@ -72,9 +77,11 @@ type EditPostLinkFilter () = type NavLinkFilter () = static member NavLink (ctx : Context, url : string, text : string) = let webLog = webLog ctx + let _, path = WebLog.hostAndPath webLog + let path = if path = "" then path else $"{path.Substring 1}/" seq { "
  • " @@ -98,10 +105,9 @@ type PageHeadTag () = result.WriteLine $"""""" // Theme assets - let has fileName = File.Exists (Path.Combine ("wwwroot", "themes", webLog.themePath, fileName)) - if has "style.css" then + if assetExists "style.css" webLog then result.WriteLine $"""{s}""" - if has "favicon.ico" then + if assetExists "favicon.ico" webLog then result.WriteLine $"""{s}""" // RSS feeds and canonical URLs @@ -132,6 +138,22 @@ type PageHeadTag () = let url = WebLog.absoluteUrl webLog (Permalink page.permalink) result.WriteLine $"""{s}""" + +/// Create various items in the page header based on the state of the page being generated +type PageFootTag () = + inherit Tag () + + override this.Render (context : Context, result : TextWriter) = + let webLog = webLog context + // spacer + let s = " " + + if webLog.autoHtmx then + result.WriteLine $"{s}{RenderView.AsString.htmlNode Htmx.Script.minified}" + + if assetExists "script.js" webLog then + result.WriteLine $"""{s}""" + /// A filter to generate a relative link type RelativeLinkFilter () = @@ -167,7 +189,7 @@ type UserLinksTag () = """