Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#load "fsreveal.fsx"

open System.Text.RegularExpressions

// Git configuration (used for publishing documentation in gh-pages branch)
// The profile where the project is posted
let gitOwner = "myGitUser"
Expand Down Expand Up @@ -57,6 +59,45 @@ let copyPics() =
with
| exn -> traceImportant <| sprintf "Could not copy picture: %s" exn.Message

let downloadAssets file =
let download (url:string) (filename:string) =
let content = (new System.Net.WebClient()).DownloadString(url)
File.WriteAllText((outDir </> filename), content)

let matchDownloadAndModifyAsset input tag attr subdir =
let regex = sprintf @"^(.*<%s.*%s\s*=\s*"")(//[^""]+?)(\?[^""]+?)?("".*)$" tag attr
let m = Regex.Match(input, regex)
if (m.Success)
then
let filePath = subdir </> Path.GetFileName(m.Groups.[2].Value)
let url = "http:" + m.Groups.[2].Value + m.Groups.[3].Value

download url filePath

Some (m.Groups.[1].Value + filePath + m.Groups.[4].Value)
else
None

let (|ScriptTag|_|) input =
matchDownloadAndModifyAsset input "script" "src" "js"

let (|LinkTag|_|) input =
matchDownloadAndModifyAsset input "link" "href" "css"

let downloadAssetIfNeeded line =
match line with
| ScriptTag l -> l
| LinkTag l -> l
| _ -> line

let oldFile = file + ".old"
File.Move(file, oldFile)

use stream = new StreamWriter(file);
File.ReadLines oldFile
|> Seq.map downloadAssetIfNeeded
|> Seq.iter stream.WriteLine

let generateFor (file:FileInfo) =
try
copyPics()
Expand All @@ -71,6 +112,10 @@ let generateFor (file:FileInfo) =

tryGenerate 3

let outFile = outDir </> (System.IO.Path.ChangeExtension(file.Name, "html"))
trace <| sprintf "Created '%s', downloading CDN-loaded assets..." outFile
downloadAssets outFile

copyStylesheet()
with
| :? FileNotFoundException as exn ->
Expand Down