Fibble/src/FibLib/Pandoc.fs

45 lines
1.5 KiB
Forth
Raw Normal View History

2026-03-28 14:20:34 +01:00
namespace Fibble.FibLib
2026-03-31 13:36:22 +02:00
open System
open System.Xml.Schema
2026-03-28 14:20:34 +01:00
module Pandoc =
open System.Diagnostics
let toHtml (from: string) (markdownText: string) =
let startInfo = ProcessStartInfo(
FileName = "pandoc",
Arguments = $"-f {from} -t html5 --lua-filter strip-p.lua",
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true)
try
use proc = Process.Start startInfo
// Skriv markdown till Pandoc
use stdin = proc.StandardInput
2026-03-31 13:36:22 +02:00
stdin.WriteLine markdownText
2026-03-28 14:20:34 +01:00
stdin.Close() // Måste stängas Pandoc vet att texten är slut
// Läs ut resultatet
let htmlOutput = proc.StandardOutput.ReadToEnd()
let errorOutput = proc.StandardError.ReadToEnd()
2026-03-31 13:36:22 +02:00
2026-03-28 14:20:34 +01:00
proc.WaitForExit()
2026-03-31 13:36:22 +02:00
Console.WriteLine(htmlOutput)
2026-03-28 14:20:34 +01:00
if proc.ExitCode = 0 then
htmlOutput.Trim()
else
sprintf "\n<pre>%s</pre> and <pre>%s</pre>" errorOutput markdownText
with ex ->
// Fångar upp om Pandoc inte är installerat eller inte finns i PATH
sprintf "\n<pre>%s</pre> and <pre>%s</pre>" ex.Message markdownText
let mdToHtml markdownText =
2026-03-31 13:36:22 +02:00
let res= toHtml "markdown" markdownText
res