Skip to content

Instantly share code, notes, and snippets.

@lontivero
Forked from OnurGumus/blog.fsx
Created November 1, 2022 18:09
Show Gist options
  • Save lontivero/4c101797d9bf06a8fa842a08eb7891fd to your computer and use it in GitHub Desktop.
Save lontivero/4c101797d9bf06a8fa842a08eb7891fd to your computer and use it in GitHub Desktop.
FSharp blog
open System.IO
#r "paket:
nuget Fake.IO.FileSystem
nuget Fake.Core.Trace
nuget FSharp.Data
nuget Fable.React
nuget FSharp.Literate
nuget Fake.Core.Target //"
#load ".fake/blog.fsx/intellisense.fsx"
open FSharp.Literate
let evaluationOptions =
[|
|]
let compilerOptions =
("-r:System.Runtime.dll" :: "-r:System.Net.WebClient.dll":: "-r:System.Runtime.Extensions.dll" ::Array.toList evaluationOptions)
|> String.concat " "
let parse source =
let doc =
Literate.ParseScriptString(source, compilerOptions=compilerOptions
, fsiEvaluator = FsiEvaluator([|"--reference:System.Runtime.Extensions.dll"; |]))
// for err in doc.Errors do
// Printf.printfn "%A" err
// let doc2 = Literate.FormatLiterateNodes(doc, OutputKind.Html, "", true, true)
// for err in doc2.Errors do
// Printf.printfn "%A" err
// printf "%A" (doc2).FormattedTips
// Literate.WriteHtml(doc) |> printf "%A"
// doc2
doc
let format prefix (doc: LiterateDocument) = Literate.WriteHtml(doc, prefix = prefix) // Formatting.format doc.MarkdownDocument true OutputKind.Html
open Fable.React
open Fable.React.Props
open FSharp.Markdown
type Post =
{ title: string
content: string }
let snipet = """
(** # *F# literate* in action *)
let convert i = i.ToString()
printfn "%s" (convert 5)
"""
let values =
"""
(** # code execution *)
let square x = x * x
let v = square 3
(** the value is: *)
(*** include-value: v ***)"""
|> parse
|> format "1"
let output =
"""
(** # printing *)
let square x = x * x
(*** define-output: result ***)
printfn "result: %d" (square 3)
(** the value is: *)
(*** include-output: result ***)"""
|> parse
|> format "2"
let fs =
snipet
|> parse
|> format "3"
let tips1 =
let doc = parse output
doc.FormattedTips
let tips2 =
let doc = parse values
doc.FormattedTips
let template post =
html [ Lang "en" ]
[ head [] [ title [] [ str ("My blog / " + post.title) ] ]
body []
[ RawText post.content
RawText tips1
RawText tips2
RawText values
RawText output
script [ Src "tips.js" ] []
link [ Href "style.css" ; Rel "stylesheet" ; Type "text/css"] ] ]
let render html =
fragment []
[ RawText "<!doctype html>"
RawText "\n"
html ]
|> Fable.ReactServer.renderToString
let myblog =
{ title = "super post"
content = fs } // Markdown.TransformHtml "# **interesting** things" }
|> template
|> render
System.IO.File.WriteAllText("myblog.html", myblog)
let poem =
"The lesser world was daubed\n\
By a colorist of modest skill\n\
A master limned you in the finest inks\n\
And with a fresh-cut quill.\n"
printf "%A" poem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment