Roald87 / roald87.github.io

Mostly cooking and coding in TwinCAT.
https://cookncode.com
MIT License
4 stars 2 forks source link

Convert gif to Webp once JPT issue is fixed #11

Open Roald87 opened 2 years ago

Roald87 commented 2 years ago

wait for fix: https://github.com/rbuchberger/jekyll_picture_tag/issues/239

open System.Text.RegularExpressions

let posts = System.IO.Directory.GetFiles("/home/roald/Dropbox/repos/GithubPages/_posts", "*.markdown")
let readLines filePath = System.IO.File.ReadLines(filePath) |> Seq.toArray

let (|Regex|_|) pattern input =
    let m = Regex.Match(input, pattern)
    if m.Success then Some(List.tail [ for g in m.Groups -> g.Value ])
    else None

let jekyll_image_tag = 
    Array.map (fun line -> 
        match line with 
        | l when (l:string).Contains(".gif)") -> line // change this to not ignore .gif
        | Regex @"!\[([\w\s\.\-]+)\]\(\/assets\/([\w\-\.\/]+)\)" [ alt; path ] -> $"{{%% picture {path} --alt {alt} %%}}"
        | Regex @"!\[\]\(\/assets\/([\w\-\.\/]+)\)" [ path ] -> $"{{%% picture {path} %%}}"
        | _ -> line
    )

let writeToFile filename text = System.IO.File.WriteAllLines(filename, text)

posts
|> Array.map readLines
|> Array.map jekyll_image_tag 
|> Array.iteri (fun i -> writeToFile posts[i])