This repository has been archived by the owner on May 6, 2020. It is now read-only.
forked from IrcDotNet/IrcDotNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateDocs.fsx
209 lines (176 loc) · 8.95 KB
/
generateDocs.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// ----------------------------------------------------------------------------
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
// ----------------------------------------------------------------------------
(*
This file handles the generation of the docs (it is called by the build automatically).
*)
#I @".nuget/Build/FAKE/tools/" // FAKE
#I @".nuget/Build/FSharp.Compiler.Service/lib/net40/"
#I @".nuget/Build/FSharp.Formatting/lib/net40/"
#r @"FakeLib.dll" //FAKE
#load @"buildConfig.fsx"
open BuildConfig
// Documentation
#r "FSharp.Compiler.Service.dll"
#r "System.Web.dll"
#r "System.Web.Razor.dll"
#r "RazorEngine.dll"
#r "FSharp.Markdown.dll"
#r "FSharp.Literate.dll"
#r "FSharp.CodeFormat.dll"
#r "FSharp.MetadataFormat.dll"
open System.Collections.Generic
open System.IO
open System.Web
open Fake
open Fake.Git
open Fake.FSharpFormatting
open AssemblyInfoFile
open FSharp.Markdown
open FSharp.Literate
open FSharp.MetadataFormat
open RazorEngine.Compilation
// Documentation
/// Processes an entire directory containing multiple script files
/// (*.fsx) and Markdown documents (*.md) and it specifies additional
/// replacements for the template file
//let website_root = "file://" + Path.GetFullPath (outDocDir @@ "html")
let formattingContext templateFile format generateAnchors replacements layoutRoots =
{ TemplateFile = templateFile
Replacements = defaultArg replacements []
GenerateLineNumbers = true
IncludeSource = false
Prefix = "fs"
OutputKind = defaultArg format OutputKind.Html
GenerateHeaderAnchors = defaultArg generateAnchors false
LayoutRoots = defaultArg layoutRoots [] }
let rec replaceCodeBlocks ctx = function
| Matching.LiterateParagraph(special) ->
match special with
| LanguageTaggedCode(lang, code) ->
let inlined =
match ctx.OutputKind with
| OutputKind.Html ->
let code = HttpUtility.HtmlEncode code
let codeHtmlKey = sprintf "language-%s" lang
sprintf "<pre class=\"line-numbers %s\"><code class=\"%s\">%s</code></pre>" codeHtmlKey codeHtmlKey code
| OutputKind.Latex ->
sprintf "\\begin{lstlisting}\n%s\n\\end{lstlisting}" code
Some(InlineBlock(inlined))
| _ -> Some (EmbedParagraphs special)
| Matching.ParagraphNested(pn, nested) ->
let nested = List.map (List.choose (replaceCodeBlocks ctx)) nested
Some(Matching.ParagraphNested(pn, nested))
| par -> Some par
let buildAllDocumentation outDocDir website_root =
let references =
if isMono then
// Workaround compiler errors in Razor-ViewEngine
let d = RazorEngine.Compilation.ReferenceResolver.UseCurrentAssembliesReferenceResolver()
let loadedList = d.GetReferences() |> Seq.map (fun c -> c.GetFile()) |> Seq.cache
//// We replace the list and add required items manually as mcs doesn't like duplicates...
let getItem name =
loadedList |> Seq.find (fun l -> l.Contains name)
[ (getItem "FSharp.Core").Replace("4.3.0.0", "4.3.1.0") // (if isMono then "/usr/lib64/mono/gac/FSharp.Core/4.3.1.0__b03f5f7f11d50a3a/FSharp.Core.dll" else "FSharp.Core")
Path.GetFullPath "./.nuget/Build/FSharp.Compiler.Service/lib/net40/FSharp.Compiler.Service.dll"
Path.GetFullPath "./.nuget/Build/FSharp.Formatting/lib/net40/System.Web.Razor.dll"
Path.GetFullPath "./.nuget/Build/FSharp.Formatting/lib/net40/RazorEngine.dll"
Path.GetFullPath "./.nuget/Build/FSharp.Formatting/lib/net40/FSharp.Literate.dll"
Path.GetFullPath "./.nuget/Build/FSharp.Formatting/lib/net40/FSharp.CodeFormat.dll"
Path.GetFullPath "./.nuget/Build/FSharp.Formatting/lib/net40/FSharp.MetadataFormat.dll" ]
|> Some
else None
let projInfo =
[ "root", website_root
"page-description", projectDescription
"page-author", page_author
"github-link", github_url
"project-name", projectName
"project-summary", projectSummary
"project-commit", commitHash
"project-author", authors |> Seq.head
"project-github", github_url
"project-issues", sprintf "%s/issues" github_url
"project-new-issue", sprintf "%s/issues/new" github_url
"project-nuget", nuget_url]
// Copy static files and CSS + JS from F# Formatting
let copyDocContentFiles () =
ensureDirectory (outDocDir @@ "html" @@ "content")
CopyRecursive "./doc/content" (outDocDir @@ "html" @@ "content") true |> Log "Copying file: "
//CopyRecursive (formatting @@ "styles") (output @@ "content") true
// |> Log "Copying styles and scripts: "
let processDocumentationFiles(outputKind) =
let indexTemplate, template, outDirName, indexName =
match outputKind with
| OutputKind.Html -> docTemplatesDir @@ "docpage-index.cshtml", docTemplatesDir @@ "docpage.cshtml", "html", "index.html"
| OutputKind.Latex -> docTemplatesDir @@ "template-color.tex", docTemplatesDir @@ "template-color.tex", "latex", "Readme.tex"
let outDir = outDocDir @@ outDirName
let handleDoc template (doc:LiterateDocument) outfile =
// prismjs support
let ctx = formattingContext (Some template) (Some outputKind) (Some true) (Some projInfo) (Some layoutRoots)
let newParagraphs = List.choose (replaceCodeBlocks ctx) doc.Paragraphs
Templating.processFile references (doc.With(paragraphs = newParagraphs)) outfile ctx
let processMarkdown template infile outfile =
let doc = Literate.ParseMarkdownFile( infile )
handleDoc template doc outfile
let processScriptFile template infile outfile =
let doc = Literate.ParseScriptFile( infile )
handleDoc template doc outfile
let rec processDirectory template indir outdir =
// Create output directory if it does not exist
if Directory.Exists(outdir) |> not then
try Directory.CreateDirectory(outdir) |> ignore
with _ -> failwithf "Cannot create directory '%s'" outdir
let fsx = [ for f in Directory.GetFiles(indir, "*.fsx") -> processScriptFile template, f ]
let mds = [ for f in Directory.GetFiles(indir, "*.md") -> processMarkdown template, f ]
for func, file in fsx @ mds do
let dir = Path.GetDirectoryName(file)
let name = Path.GetFileNameWithoutExtension(file)
let ext = (match outputKind with OutputKind.Latex -> "tex" | _ -> "html")
let output = Path.Combine(outdir, sprintf "%s.%s" name ext)
// Update only when needed
let changeTime = File.GetLastWriteTime(file)
let generateTime = File.GetLastWriteTime(output)
if changeTime > generateTime then
printfn "Generating '%s/%s.%s'" dir name ext
func file output
for d in Directory.EnumerateDirectories(indir) do
let name = Path.GetFileName(d)
processDirectory template (Path.Combine(indir, name)) (Path.Combine(outdir, name))
processDirectory template "./doc" outDir
processMarkdown indexTemplate "./README.md" (outDir @@ indexName)
// Build API reference from XML comments
let referenceBinaries =
[
"IrcDotNet.dll"
]
let buildReference () =
let referenceDir = outDocDir @@ "html"
let outDir = referenceDir @@ "references"
ensureDirectory referenceDir
ensureDirectory outDir
let binaries =
referenceBinaries
|> List.map (fun lib-> Path.GetFullPath( buildDir @@ "net45" @@ lib ))
MetadataFormat.Generate
(binaries, Path.GetFullPath outDir, layoutRoots,
parameters = projInfo,
libDirs = [ Path.GetFullPath ("./build/net45") ],
otherFlags = [ "-r:System";"-r:System.Core";"-r:System.Xml";"-r:System.Xml.Linq"],
sourceRepo = github_url + "/blob/master/",
sourceFolder = "./",
publicOnly = true,
markDownComments = false, // <see cref=""/> support
?assemblyReferences = references )
CleanDirs [ outDocDir ]
copyDocContentFiles()
processDocumentationFiles OutputKind.Html
processDocumentationFiles OutputKind.Latex
buildReference()
MyTarget "GithubDoc" (fun _ -> buildAllDocumentation (outDocDir @@ sprintf "%s.github.io" github_user) (sprintf "https://%s.github.io/%s" github_user github_project))
MyTarget "LocalDoc" (fun _ ->
buildAllDocumentation (outDocDir @@ "local") ("file://" + Path.GetFullPath (outDocDir @@ "local" @@ "html"))
trace (sprintf "Local documentation has been finished, you can view it by opening %s in your browser!" (Path.GetFullPath (outDocDir @@ "local" @@ "html" @@ "index.html")))
)
RunTargetOrDefault "LocalDoc"