<commit_before>#I @"packages/FsReveal/fsreveal/"
#I @"packages/FAKE/tools/"
#I @"packages/RazorEngine/lib/net40/"
#I @"packages/Suave/lib/"

#r "FakeLib.dll"
#r "suave.dll"

#load "fsreveal.fsx"

open FsReveal
open Fake
open System.IO
open System.Diagnostics
open Suave
open Suave.Web
open Suave.Http
open Suave.Http.Files

let outDir = "output"

Target "Clean" (fun _ ->
    CleanDirs [outDir]
)

let copyPics() =
    !! "slides/images/*.*"
    |> CopyFiles (outDir @@ "images")

let generateFor (file:FileInfo) =    
    copyPics()
    let rec tryGenerate trials =
        try
            let outputFileName = file.Name.Replace(file.Extension,".html")
            match file.Extension with   
            | ".md" ->  FsReveal.GenerateOutputFromMarkdownFile outDir outputFileName file.FullName
            | ".fsx" -> FsReveal.GenerateOutputFromScriptFile outDir outputFileName file.FullName
            | _ -> ()
        with 
        | exn when trials > 0 -> tryGenerate (trials - 1)
        | exn -> 
            traceImportant <| sprintf "Could not generate slides for %s:" file.FullName
            traceImportant exn.Message

    tryGenerate 3

let startWebServer () =
    let serverConfig = 
        { default_config with
           home_folder = Some (System.IO.Path.Combine(__SOURCE_DIRECTORY__, outDir))
        }
    let app =
        Writers.set_header "Cache-Control" "no-cache, no-store, must-revalidate"
        >>= Writers.set_header "Pragma" "no-cache"
        >>= Writers.set_header "Expires" "0"
        >>= browse
    web_server_async serverConfig app |> snd |> Async.Start
    Process.Start "http://localhost:8083/input.html" |> ignore

Target "GenerateSlides" (fun _ ->
    !! "slides/*.md"
      ++ "slides/*.fsx"
    |> Seq.map fileInfo
    |> Seq.iter generateFor
)

Target "KeepRunning" (fun _ ->
    use watcher = new FileSystemWatcher(DirectoryInfo("slides").FullName,"*.*")
    watcher.EnableRaisingEvents <- true
    watcher.Changed.Add(fun e -> fileInfo e.FullPath |> generateFor)
    watcher.Created.Add(fun e -> fileInfo e.FullPath |> generateFor)
    watcher.Renamed.Add(fun e -> fileInfo e.FullPath |> generateFor)

    startWebServer ()

    traceImportant "Waiting for slide edits. Press any key to stop."

    System.Console.ReadKey() |> ignore

    watcher.EnableRaisingEvents <- false
    watcher.Dispose()<commit_msg>Do not fire generateFor function if the file is hidden<commit_after>#I @"packages/FsReveal/fsreveal/"
#I @"packages/FAKE/tools/"
#I @"packages/RazorEngine/lib/net40/"
#I @"packages/Suave/lib/"

#r "FakeLib.dll"
#r "suave.dll"

#load "fsreveal.fsx"

open FsReveal
open Fake
open System.IO
open System.Diagnostics
open Suave
open Suave.Web
open Suave.Http
open Suave.Http.Files

let outDir = "output"

Target "Clean" (fun _ ->
    CleanDirs [outDir]
)

let copyPics() =
    !! "slides/images/*.*"
    |> CopyFiles (outDir @@ "images")

let generateFor (file:FileInfo) =    
    copyPics()
    let rec tryGenerate trials =
        try
            let outputFileName = file.Name.Replace(file.Extension,".html")
            match file.Extension with   
            | ".md" ->  FsReveal.GenerateOutputFromMarkdownFile outDir outputFileName file.FullName
            | ".fsx" -> FsReveal.GenerateOutputFromScriptFile outDir outputFileName file.FullName
            | _ -> ()
        with 
        | exn when trials > 0 -> tryGenerate (trials - 1)
        | exn -> 
            traceImportant <| sprintf "Could not generate slides for %s:" file.FullName
            traceImportant exn.Message

    tryGenerate 3

let handleWatcherEvents (e:FileSystemEventArgs) =
    let fi = fileInfo e.FullPath 
    traceImportant fi.Name
    match fi.Attributes.HasFlag FileAttributes.Hidden with
            | true -> ()
            | _ -> generateFor fi

let startWebServer () =
    let serverConfig = 
        { default_config with
           home_folder = Some (System.IO.Path.Combine(__SOURCE_DIRECTORY__, outDir))
        }
    let app =
        Writers.set_header "Cache-Control" "no-cache, no-store, must-revalidate"
        >>= Writers.set_header "Pragma" "no-cache"
        >>= Writers.set_header "Expires" "0"
        >>= browse
    web_server_async serverConfig app |> snd |> Async.Start
    Process.Start "http://localhost:8083/input.html" |> ignore

Target "GenerateSlides" (fun _ ->
    !! "slides/*.md"
      ++ "slides/*.fsx"
    |> Seq.map fileInfo
    |> Seq.iter generateFor
)

Target "KeepRunning" (fun _ ->
    use watcher = new FileSystemWatcher(DirectoryInfo("slides").FullName,"*.*")
    watcher.EnableRaisingEvents <- true
    watcher.IncludeSubdirectories <- true
    watcher.Changed.Add(handleWatcherEvents)
    watcher.Created.Add(handleWatcherEvents)
    watcher.Renamed.Add(handleWatcherEvents)

    startWebServer ()

    traceImportant "Waiting for slide edits. Press any key to stop."

    System.Console.ReadKey() |> ignore

    watcher.EnableRaisingEvents <- false
    watcher.Dispose()
{'fraction_non_alphanumeric': 0.11978221415607986, 'fraction_numerical': 0.004033071183706392, 'mean_word_length': 2.7920489296636086, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3242713', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1578, 'n_words': 358}