Compare commits
2 commits
86602d1834
...
27a169b30c
| Author | SHA1 | Date | |
|---|---|---|---|
| 27a169b30c | |||
| 922649e546 |
3 changed files with 137 additions and 0 deletions
16
src/Bif/Bif.fsproj
Normal file
16
src/Bif/Bif.fsproj
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="Program.fs" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\FibLib\FibLib.fsproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
82
src/Bif/Program.fs
Normal file
82
src/Bif/Program.fs
Normal file
|
|
@ -0,0 +1,82 @@
|
||||||
|
open System.Net
|
||||||
|
open System.IO
|
||||||
|
open Fibble.FibLib
|
||||||
|
open Fibble.FibLib.Ast // Ger oss tillgång till Element, Text, RawHtml etc.
|
||||||
|
open Fibble.FibLib.Pandoc
|
||||||
|
open Fibble.FibLib.Utils
|
||||||
|
|
||||||
|
// ==========================================
|
||||||
|
// 1. Prelude (Dina egna taggar)
|
||||||
|
// ==========================================
|
||||||
|
|
||||||
|
|
||||||
|
let myPrelude : Map<string, TagRenderer> =
|
||||||
|
Map [
|
||||||
|
"quotient", fun _ args _ ->
|
||||||
|
match args with
|
||||||
|
| [_, one; _, two] ->
|
||||||
|
Text (sprintf "%d" (int one / int two))
|
||||||
|
| _ ->
|
||||||
|
Text "[Fel: quotient kräver två argument]"
|
||||||
|
"bold", fun _ args children ->
|
||||||
|
Element("b", args, children)
|
||||||
|
"image", positional (fun _ args _ -> Element("img", [("src", args[0])], []))
|
||||||
|
"value", positional (fun meta args _ ->
|
||||||
|
match Map.tryFind args.Head meta with
|
||||||
|
| Some(v) -> Text v
|
||||||
|
| None -> Text $"value {args.Head} not found in metadata")
|
||||||
|
"link", positional (fun _ args children ->
|
||||||
|
let url = if args.Length > 0 then args.[0].Trim '"' else "#"
|
||||||
|
Element("a", [("href", args.Head)], children))
|
||||||
|
|
||||||
|
// @br har varken argument eller barn, så vi returnerar bara rå HTML direkt
|
||||||
|
"br", nameToElement "br"
|
||||||
|
"table", fun _ _ children -> Text "hej"
|
||||||
|
"md", fun meta args children -> RawHtml (mdToHtml (stringifyNodes children))
|
||||||
|
]
|
||||||
|
|
||||||
|
// ==========================================
|
||||||
|
// 2. Mall och Evaluator
|
||||||
|
// ==========================================
|
||||||
|
//
|
||||||
|
module File =
|
||||||
|
let readFile path =
|
||||||
|
match Path.Exists(path) with
|
||||||
|
| true -> File.ReadAllText(path)
|
||||||
|
| _ -> failwith $"{path} does not exist"
|
||||||
|
|
||||||
|
let pageTemplate = File.readFile "_page-template"
|
||||||
|
|
||||||
|
let sourceCode = File.readFile "document.fib"
|
||||||
|
// ==========================================
|
||||||
|
// 3. Huvudpipeline
|
||||||
|
// ==========================================
|
||||||
|
let processDocument (source: string) =
|
||||||
|
let evaluator = Evaluators.FsiEvaluator()
|
||||||
|
|
||||||
|
// Steg 1: Parsa koden
|
||||||
|
let metadata, rawBlocks = Parser.parse source
|
||||||
|
|
||||||
|
|
||||||
|
// Steg 2: Transformera och exekvera trädet
|
||||||
|
let evaluatedBlocks =
|
||||||
|
rawBlocks |> List.map (function
|
||||||
|
| Paragraph children ->
|
||||||
|
Paragraph (children
|
||||||
|
|> List.map (Execution.transform metadata myPrelude evaluator))
|
||||||
|
| Section(l, a, children) ->
|
||||||
|
Section(l, a, children
|
||||||
|
|> List.map (Execution.transform metadata myPrelude evaluator))
|
||||||
|
)
|
||||||
|
|
||||||
|
// Steg 3: Be printern skriva ut trädet till HTML
|
||||||
|
let bodyHtml = HtmlPrinter.render (metadata, evaluatedBlocks)
|
||||||
|
|
||||||
|
// Steg 4: Fyll i din HTML-mall
|
||||||
|
let mutable finalHtml = pageTemplate.Replace("{{body}}", bodyHtml)
|
||||||
|
|
||||||
|
finalHtml
|
||||||
|
|
||||||
|
// Kör programmet
|
||||||
|
let output = processDocument sourceCode
|
||||||
|
printfn "%s" output
|
||||||
39
src/Bif/document.fib
Normal file
39
src/Bif/document.fib
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
---
|
||||||
|
author: bajs
|
||||||
|
date: yesterday
|
||||||
|
title: FSI Test
|
||||||
|
---
|
||||||
|
|
||||||
|
@section{Definitioner}
|
||||||
|
Vi definierar en funktion och en variabel.
|
||||||
|
@"""
|
||||||
|
let globalCounter = ref 0
|
||||||
|
|
||||||
|
let increment () =
|
||||||
|
globalCounter.Value <- globalCounter.Value + 1
|
||||||
|
sprintf "Räknaren är nu %d" globalCounter.Value
|
||||||
|
"""
|
||||||
|
@(1)
|
||||||
|
|
||||||
|
@link[www.google.com]{hejsan hoppsan fallerallera}
|
||||||
|
|
||||||
|
Detta är en paragraf som har lite text i sig och lite @md{inbäddad *markdown* som kanske} funkar. Det är bra så.
|
||||||
|
|
||||||
|
@md{
|
||||||
|
en tabell
|
||||||
|
|
||||||
|
hej hopp
|
||||||
|
--- ----
|
||||||
|
abc defg
|
||||||
|
bde dddd
|
||||||
|
ueo eoau
|
||||||
|
ueo aoeu
|
||||||
|
}
|
||||||
|
|
||||||
|
elleR?
|
||||||
|
|
||||||
|
@section{Användning}
|
||||||
|
Första anropet: @(increment())
|
||||||
|
Andra anropet: @(increment())
|
||||||
|
|
||||||
|
Test av utskrift: @value[date]
|
||||||
Loading…
Add table
Add a link
Reference in a new issue