From 3b18ef83f93ca79cabccb87bdc5730bac1970f67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Cie=C5=9Blak?= Date: Tue, 4 Oct 2016 16:31:50 +0200 Subject: [PATCH 01/19] Remove Razor reference from FSharp.Literate --- paket.lock | 4 +- .../FSharp.CodeFormat.fsproj | 6 +-- .../FSharp.Formatting.CommandTool.fsproj | 10 ++--- .../FSharp.Formatting.Common.fsproj | 4 +- src/FSharp.Literate/FSharp.Literate.fsproj | 40 ++----------------- src/FSharp.Literate/paket.references | 2 - .../FSharp.MetadataFormat.fsproj | 10 ++--- .../FSharp.Literate.Tests.fsproj | 6 +-- .../FSharp.Markdown.Tests.fsproj | 10 ++--- .../FSharp.MetadataFormat.Tests.fsproj | 10 ++--- .../files/FsLib/FsLib1.fsproj | 10 ++--- .../files/FsLib/FsLib2.fsproj | 10 ++--- .../files/TestLib/TestLib1.fsproj | 10 ++--- .../files/TestLib/TestLib2.fsproj | 10 ++--- 14 files changed, 52 insertions(+), 90 deletions(-) diff --git a/paket.lock b/paket.lock index 04f628c2f..ce4088f8e 100644 --- a/paket.lock +++ b/paket.lock @@ -1,7 +1,6 @@ REDIRECTS: ON NUGET remote: http://www.nuget.org/api/v2 - specs: CommandLineParser (1.9.71) FAKE (4.22.8) FSharp.Compiler.Service (2.0.0.6) @@ -21,5 +20,4 @@ NUGET Zlib.Portable (1.11) - framework: portable-net40+sl50+wp80+win80 GITHUB remote: matthid/Yaaf.FSharp.Scripting - specs: - src/source/Yaaf.FSharp.Scripting/YaafFSharpScripting.fs (48789eeb63382a6934966472e270e87880cb49ed) + src/source/Yaaf.FSharp.Scripting/YaafFSharpScripting.fs (48789eeb63382a6934966472e270e87880cb49ed) \ No newline at end of file diff --git a/src/FSharp.CodeFormat/FSharp.CodeFormat.fsproj b/src/FSharp.CodeFormat/FSharp.CodeFormat.fsproj index 8686e8d61..4681c815f 100644 --- a/src/FSharp.CodeFormat/FSharp.CodeFormat.fsproj +++ b/src/FSharp.CodeFormat/FSharp.CodeFormat.fsproj @@ -106,7 +106,7 @@ --> - + ..\..\packages\FSharp.Compiler.Service\lib\net40\FSharp.Compiler.Service.dll @@ -115,7 +115,7 @@ - + ..\..\packages\FSharp.Compiler.Service\lib\net45\FSharp.Compiler.Service.dll @@ -126,7 +126,7 @@ - + ..\..\packages\FSharpVSPowerTools.Core\lib\net45\FSharpVSPowerTools.Core.dll diff --git a/src/FSharp.Formatting.CommandTool/FSharp.Formatting.CommandTool.fsproj b/src/FSharp.Formatting.CommandTool/FSharp.Formatting.CommandTool.fsproj index 9267d2d07..316ffe1e1 100644 --- a/src/FSharp.Formatting.CommandTool/FSharp.Formatting.CommandTool.fsproj +++ b/src/FSharp.Formatting.CommandTool/FSharp.Formatting.CommandTool.fsproj @@ -142,7 +142,7 @@ - + ..\..\packages\CommandLineParser\lib\net40\CommandLine.dll @@ -151,7 +151,7 @@ - + ..\..\packages\CommandLineParser\lib\net45\CommandLine.dll @@ -162,7 +162,7 @@ - + ..\..\packages\Microsoft.AspNet.Razor\lib\net45\System.Web.Razor.dll @@ -173,7 +173,7 @@ - + ..\..\packages\RazorEngine\lib\net40\RazorEngine.dll @@ -182,7 +182,7 @@ - + ..\..\packages\RazorEngine\lib\net45\RazorEngine.dll diff --git a/src/FSharp.Formatting.Common/FSharp.Formatting.Common.fsproj b/src/FSharp.Formatting.Common/FSharp.Formatting.Common.fsproj index 33e7125c9..466b0ee31 100644 --- a/src/FSharp.Formatting.Common/FSharp.Formatting.Common.fsproj +++ b/src/FSharp.Formatting.Common/FSharp.Formatting.Common.fsproj @@ -83,7 +83,7 @@ - + ..\..\packages\FSharp.Compiler.Service\lib\net40\FSharp.Compiler.Service.dll @@ -92,7 +92,7 @@ - + ..\..\packages\FSharp.Compiler.Service\lib\net45\FSharp.Compiler.Service.dll diff --git a/src/FSharp.Literate/FSharp.Literate.fsproj b/src/FSharp.Literate/FSharp.Literate.fsproj index 6f7df1c52..60ed222fd 100644 --- a/src/FSharp.Literate/FSharp.Literate.fsproj +++ b/src/FSharp.Literate/FSharp.Literate.fsproj @@ -63,9 +63,6 @@ Common\StringParsing.fs - - Common\Razor.fs - @@ -107,7 +104,7 @@ True - - + ..\..\packages\FSharp.Compiler.Service\lib\net40\FSharp.Compiler.Service.dll @@ -125,7 +122,7 @@ - + ..\..\packages\FSharp.Compiler.Service\lib\net45\FSharp.Compiler.Service.dll @@ -135,35 +132,4 @@ - - - - - ..\..\packages\Microsoft.AspNet.Razor\lib\net45\System.Web.Razor.dll - True - True - - - - - - - - - ..\..\packages\RazorEngine\lib\net40\RazorEngine.dll - True - True - - - - - - - ..\..\packages\RazorEngine\lib\net45\RazorEngine.dll - True - True - - - - \ No newline at end of file diff --git a/src/FSharp.Literate/paket.references b/src/FSharp.Literate/paket.references index 1570c9dec..e0dfff83f 100644 --- a/src/FSharp.Literate/paket.references +++ b/src/FSharp.Literate/paket.references @@ -1,3 +1 @@ FSharp.Compiler.Service -Microsoft.AspNet.Razor -RazorEngine diff --git a/src/FSharp.MetadataFormat/FSharp.MetadataFormat.fsproj b/src/FSharp.MetadataFormat/FSharp.MetadataFormat.fsproj index 790605fd9..e097a7a59 100644 --- a/src/FSharp.MetadataFormat/FSharp.MetadataFormat.fsproj +++ b/src/FSharp.MetadataFormat/FSharp.MetadataFormat.fsproj @@ -119,7 +119,7 @@ - + ..\..\packages\FSharp.Compiler.Service\lib\net40\FSharp.Compiler.Service.dll @@ -128,7 +128,7 @@ - + ..\..\packages\FSharp.Compiler.Service\lib\net45\FSharp.Compiler.Service.dll @@ -139,7 +139,7 @@ - + ..\..\packages\Microsoft.AspNet.Razor\lib\net45\System.Web.Razor.dll @@ -150,7 +150,7 @@ - + ..\..\packages\RazorEngine\lib\net40\RazorEngine.dll @@ -159,7 +159,7 @@ - + ..\..\packages\RazorEngine\lib\net45\RazorEngine.dll diff --git a/tests/FSharp.Literate.Tests/FSharp.Literate.Tests.fsproj b/tests/FSharp.Literate.Tests/FSharp.Literate.Tests.fsproj index e96be3a09..70055fec4 100644 --- a/tests/FSharp.Literate.Tests/FSharp.Literate.Tests.fsproj +++ b/tests/FSharp.Literate.Tests/FSharp.Literate.Tests.fsproj @@ -135,7 +135,7 @@ - + ..\..\packages\Microsoft.AspNet.Razor\lib\net45\System.Web.Razor.dll @@ -153,7 +153,7 @@ - + ..\..\packages\RazorEngine\lib\net40\RazorEngine.dll @@ -162,7 +162,7 @@ - + ..\..\packages\RazorEngine\lib\net45\RazorEngine.dll diff --git a/tests/FSharp.Markdown.Tests/FSharp.Markdown.Tests.fsproj b/tests/FSharp.Markdown.Tests/FSharp.Markdown.Tests.fsproj index 01ed64dc6..06e3a3ad8 100644 --- a/tests/FSharp.Markdown.Tests/FSharp.Markdown.Tests.fsproj +++ b/tests/FSharp.Markdown.Tests/FSharp.Markdown.Tests.fsproj @@ -107,19 +107,19 @@ - + + + True + ..\..\packages\FSharp.Data\lib\net40\FSharp.Data.dll True True - - True - - + ..\..\packages\FSharp.Data\lib\portable-net40+sl5+wp8+win8\FSharp.Data.dll diff --git a/tests/FSharp.MetadataFormat.Tests/FSharp.MetadataFormat.Tests.fsproj b/tests/FSharp.MetadataFormat.Tests/FSharp.MetadataFormat.Tests.fsproj index 12069c1a4..bc9251eb4 100644 --- a/tests/FSharp.MetadataFormat.Tests/FSharp.MetadataFormat.Tests.fsproj +++ b/tests/FSharp.MetadataFormat.Tests/FSharp.MetadataFormat.Tests.fsproj @@ -105,7 +105,7 @@ - + ..\..\packages\FSharp.Compiler.Service\lib\net40\FSharp.Compiler.Service.dll @@ -114,7 +114,7 @@ - + ..\..\packages\FSharp.Compiler.Service\lib\net45\FSharp.Compiler.Service.dll @@ -125,7 +125,7 @@ - + ..\..\packages\Microsoft.AspNet.Razor\lib\net45\System.Web.Razor.dll @@ -143,7 +143,7 @@ - + ..\..\packages\RazorEngine\lib\net40\RazorEngine.dll @@ -152,7 +152,7 @@ - + ..\..\packages\RazorEngine\lib\net45\RazorEngine.dll diff --git a/tests/FSharp.MetadataFormat.Tests/files/FsLib/FsLib1.fsproj b/tests/FSharp.MetadataFormat.Tests/files/FsLib/FsLib1.fsproj index 2b261984d..14ed6d95b 100644 --- a/tests/FSharp.MetadataFormat.Tests/files/FsLib/FsLib1.fsproj +++ b/tests/FSharp.MetadataFormat.Tests/files/FsLib/FsLib1.fsproj @@ -72,7 +72,7 @@ --> - + ..\..\..\..\packages\FSharp.Compiler.Service\lib\net40\FSharp.Compiler.Service.dll @@ -81,7 +81,7 @@ - + ..\..\..\..\packages\FSharp.Compiler.Service\lib\net45\FSharp.Compiler.Service.dll @@ -92,7 +92,7 @@ - + ..\..\..\..\packages\Microsoft.AspNet.Razor\lib\net45\System.Web.Razor.dll @@ -103,7 +103,7 @@ - + ..\..\..\..\packages\RazorEngine\lib\net40\RazorEngine.dll @@ -112,7 +112,7 @@ - + ..\..\..\..\packages\RazorEngine\lib\net45\RazorEngine.dll diff --git a/tests/FSharp.MetadataFormat.Tests/files/FsLib/FsLib2.fsproj b/tests/FSharp.MetadataFormat.Tests/files/FsLib/FsLib2.fsproj index a4fd49d9f..7d4412e2e 100644 --- a/tests/FSharp.MetadataFormat.Tests/files/FsLib/FsLib2.fsproj +++ b/tests/FSharp.MetadataFormat.Tests/files/FsLib/FsLib2.fsproj @@ -74,7 +74,7 @@ - + ..\..\..\..\packages\FSharp.Compiler.Service\lib\net40\FSharp.Compiler.Service.dll @@ -83,7 +83,7 @@ - + ..\..\..\..\packages\FSharp.Compiler.Service\lib\net45\FSharp.Compiler.Service.dll @@ -94,7 +94,7 @@ - + ..\..\..\..\packages\Microsoft.AspNet.Razor\lib\net45\System.Web.Razor.dll @@ -105,7 +105,7 @@ - + ..\..\..\..\packages\RazorEngine\lib\net40\RazorEngine.dll @@ -114,7 +114,7 @@ - + ..\..\..\..\packages\RazorEngine\lib\net45\RazorEngine.dll diff --git a/tests/FSharp.MetadataFormat.Tests/files/TestLib/TestLib1.fsproj b/tests/FSharp.MetadataFormat.Tests/files/TestLib/TestLib1.fsproj index 06aa031e8..655dfc1bf 100644 --- a/tests/FSharp.MetadataFormat.Tests/files/TestLib/TestLib1.fsproj +++ b/tests/FSharp.MetadataFormat.Tests/files/TestLib/TestLib1.fsproj @@ -73,7 +73,7 @@ --> - + ..\..\..\..\packages\FSharp.Compiler.Service\lib\net40\FSharp.Compiler.Service.dll @@ -82,7 +82,7 @@ - + ..\..\..\..\packages\FSharp.Compiler.Service\lib\net45\FSharp.Compiler.Service.dll @@ -93,7 +93,7 @@ - + ..\..\..\..\packages\Microsoft.AspNet.Razor\lib\net45\System.Web.Razor.dll @@ -104,7 +104,7 @@ - + ..\..\..\..\packages\RazorEngine\lib\net40\RazorEngine.dll @@ -113,7 +113,7 @@ - + ..\..\..\..\packages\RazorEngine\lib\net45\RazorEngine.dll diff --git a/tests/FSharp.MetadataFormat.Tests/files/TestLib/TestLib2.fsproj b/tests/FSharp.MetadataFormat.Tests/files/TestLib/TestLib2.fsproj index 15610c01a..d119aa3f5 100644 --- a/tests/FSharp.MetadataFormat.Tests/files/TestLib/TestLib2.fsproj +++ b/tests/FSharp.MetadataFormat.Tests/files/TestLib/TestLib2.fsproj @@ -69,7 +69,7 @@ --> - + ..\..\..\..\packages\FSharp.Compiler.Service\lib\net40\FSharp.Compiler.Service.dll @@ -78,7 +78,7 @@ - + ..\..\..\..\packages\FSharp.Compiler.Service\lib\net45\FSharp.Compiler.Service.dll @@ -89,7 +89,7 @@ - + ..\..\..\..\packages\Microsoft.AspNet.Razor\lib\net45\System.Web.Razor.dll @@ -100,7 +100,7 @@ - + ..\..\..\..\packages\RazorEngine\lib\net40\RazorEngine.dll @@ -109,7 +109,7 @@ - + ..\..\..\..\packages\RazorEngine\lib\net45\RazorEngine.dll From a9e1f1137fe0b214d0b0e1448ef7ee0e13540809 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Cie=C5=9Blak?= Date: Tue, 4 Oct 2016 16:35:02 +0200 Subject: [PATCH 02/19] Use plugable generator function --- src/FSharp.Literate/Contexts.fs | 15 ++-- src/FSharp.Literate/Formatting.fs | 67 +++++------------ src/FSharp.Literate/Main.fs | 118 ++++++++++++++++++------------ 3 files changed, 101 insertions(+), 99 deletions(-) diff --git a/src/FSharp.Literate/Contexts.fs b/src/FSharp.Literate/Contexts.fs index 733abc674..aea975aaf 100644 --- a/src/FSharp.Literate/Contexts.fs +++ b/src/FSharp.Literate/Contexts.fs @@ -2,9 +2,9 @@ open FSharp.CodeFormat -/// Specifies a context that is passed to functions +/// Specifies a context that is passed to functions /// that need to use the F# compiler -type CompilerContext = +type CompilerContext = { // An instance of the F# code formatting agent FormatAgent : CodeFormatAgent // F# interactive evaluator @@ -18,8 +18,11 @@ type CompilerContext = [] type OutputKind = Html | Latex +/// Defines type of function responsible for generating output +type Generator = (string list option) -> string -> list -> string option -> string -> seq -> unit + /// Specifies a context that is passed to functions that generate the output -type ProcessingContext = +type ProcessingContext = { // Path to the template file TemplateFile : string option // Short prefix code added to all HTML 'id' elements @@ -34,5 +37,7 @@ type ProcessingContext = GenerateHeaderAnchors : bool // The output format OutputKind : OutputKind - // Where to look for Razor templates, when using Razor - LayoutRoots : seq } + // Where to look for templates + LayoutRoots : seq + // Function generating output + Generator : Generator } diff --git a/src/FSharp.Literate/Formatting.fs b/src/FSharp.Literate/Formatting.fs index 93bbad5a3..0fc552df9 100644 --- a/src/FSharp.Literate/Formatting.fs +++ b/src/FSharp.Literate/Formatting.fs @@ -15,31 +15,31 @@ open FSharp.Markdown module Formatting = /// Format document with the specified output kind - let format doc generateAnchors outputKind = + let format doc generateAnchors outputKind = match outputKind with | OutputKind.Latex -> Markdown.WriteLatex(doc) - | OutputKind.Html -> + | OutputKind.Html -> let sb = new System.Text.StringBuilder() use wr = new StringWriter(sb) Html.formatMarkdown wr generateAnchors System.Environment.NewLine true doc.DefinedLinks doc.Paragraphs sb.ToString() /// Try find first-level heading in the paragraph collection - let findHeadings paragraphs generateAnchors (outputKind:OutputKind) = - paragraphs |> Seq.tryPick (function - | Heading(1, text, r) -> + let findHeadings paragraphs generateAnchors (outputKind:OutputKind) = + paragraphs |> Seq.tryPick (function + | Heading(1, text, r) -> let doc = MarkdownDocument([Span(text, r)], dict []) Some(format doc generateAnchors outputKind) | _ -> None) - /// Given literate document, get a new MarkdownDocument that represents the + /// Given literate document, get a new MarkdownDocument that represents the /// entire source code of the specified document (with possible `fsx` formatting) let getSourceDocument (doc:LiterateDocument) = match doc.Source with | LiterateSource.Markdown text -> doc.With(paragraphs = [CodeBlock (text, "", "", None)]) | LiterateSource.Script snippets -> - let paragraphs = + let paragraphs = [ for Snippet(name, lines) in snippets do if snippets.Length > 1 then yield Heading(3, [Literal(name, None)], None) @@ -53,64 +53,33 @@ module Formatting = /// [omit] module Templating = - /// Replace {parameter} in the input string with - /// values defined in the specified list - let private replaceParameters (contentTag:string) (parameters:seq) input = - match input with - | None -> - // If there is no template, return just document + tooltips - let lookup = parameters |> dict - lookup.[contentTag] + "\n\n" + lookup.["tooltips"] - | Some input -> - // First replace keys with some uglier keys and then replace them with values - // (in case one of the keys appears in some other value) - let id = System.Guid.NewGuid().ToString("d") - let input = parameters |> Seq.fold (fun (html:string) (key, value) -> - html.Replace("{" + key + "}", "{" + key + id + "}")) input - let result = parameters |> Seq.fold (fun (html:string) (key, value) -> - html.Replace("{" + key + id + "}", value)) input - result - - /// Depending on the template file, use either Razor engine - /// or simple Html engine with {replacements} to format the document - let private generateFile references contentTag parameters templateOpt output layoutRoots = - match templateOpt with - | Some (file:string) when file.EndsWith("cshtml", true, CultureInfo.InvariantCulture) -> - let razor = RazorRender(layoutRoots |> Seq.toList, [], file, ?references = references) - let props = [ "Properties", dict parameters ] - let generated = razor.ProcessFile(props) - File.WriteAllText(output, generated) - | _ -> - let templateOpt = templateOpt |> Option.map File.ReadAllText - File.WriteAllText(output, replaceParameters contentTag parameters templateOpt) - // ------------------------------------------------------------------------------------ // Formate literate document // ------------------------------------------------------------------------------------ - let processFile references (doc:LiterateDocument) output ctx = + let processFile references (doc:LiterateDocument) output ctx = // If we want to include the source code of the script, then process // the entire source and generate replacement {source} => ...some html... let sourceReplacements = - if ctx.IncludeSource then - let doc = + if ctx.IncludeSource then + let doc = Formatting.getSourceDocument doc - |> Transformations.replaceLiterateParagraphs ctx + |> Transformations.replaceLiterateParagraphs ctx let content = Formatting.format doc.MarkdownDocument ctx.GenerateHeaderAnchors ctx.OutputKind [ "source", content ] else [] // Get page title (either heading or file name) - let pageTitle = + let pageTitle = let name = Path.GetFileNameWithoutExtension(output) defaultArg (Formatting.findHeadings doc.Paragraphs ctx.GenerateHeaderAnchors ctx.OutputKind) name // To avoid clashes in templating use {contents} for Latex and older {document} for HTML - let contentTag = - match ctx.OutputKind with - | OutputKind.Html -> "document" - | OutputKind.Latex -> "contents" + let contentTag = + match ctx.OutputKind with + | OutputKind.Html -> "document" + | OutputKind.Latex -> "contents" // Replace all special elements with ordinary Html/Latex Markdown let doc = Transformations.replaceLiterateParagraphs ctx doc @@ -118,10 +87,10 @@ module Templating = let tipsHtml = doc.FormattedTips // Construct new Markdown document and write it - let parameters = + let parameters = ctx.Replacements @ sourceReplacements @ [ "page-title", pageTitle "page-source", doc.SourceFile contentTag, formattedDocument "tooltips", tipsHtml ] - generateFile references contentTag parameters ctx.TemplateFile output ctx.LayoutRoots + ctx.Generator references contentTag parameters ctx.TemplateFile output ctx.LayoutRoots diff --git a/src/FSharp.Literate/Main.fs b/src/FSharp.Literate/Main.fs index 37a7314ac..136e7daa1 100644 --- a/src/FSharp.Literate/Main.fs +++ b/src/FSharp.Literate/Main.fs @@ -14,43 +14,71 @@ open FSharp.CodeFormat /// The `ProcessMarkdown` and `ProcessScriptFile` methods process a single Markdown document /// and F# script, respectively. The `ProcessDirectory` method handles an entire directory tree /// (looking for `*.fsx` and `*.md` files). -type Literate private () = +type Literate private () = + + /// Replace {parameter} in the input string with + /// values defined in the specified list + static let replaceParameters (contentTag:string) (parameters:seq) input = + match input with + | None -> + // If there is no template, return just document + tooltips + let lookup = parameters |> dict + lookup.[contentTag] + "\n\n" + lookup.["tooltips"] + | Some input -> + // First replace keys with some uglier keys and then replace them with values + // (in case one of the keys appears in some other value) + let id = System.Guid.NewGuid().ToString("d") + let input = parameters |> Seq.fold (fun (html:string) (key, value) -> + html.Replace("{" + key + "}", "{" + key + id + "}")) input + let result = parameters |> Seq.fold (fun (html:string) (key, value) -> + html.Replace("{" + key + id + "}", value)) input + result + + + /// Depending on the template file, use either Razor engine + /// or simple Html engine with {replacements} to format the document + static let generatePlainFile references contentTag parameters templateOpt output layoutRoots = + let templateOpt = templateOpt |> Option.map File.ReadAllText + File.WriteAllText(output, replaceParameters contentTag parameters templateOpt) + /// Build default options context for formatting literate document - static let formattingContext templateFile format prefix lineNumbers includeSource generateAnchors replacements layoutRoots = - { TemplateFile = templateFile + static let formattingContext templateFile format prefix lineNumbers includeSource generateAnchors replacements layoutRoots generator= + { TemplateFile = templateFile Replacements = defaultArg replacements [] GenerateLineNumbers = defaultArg lineNumbers true IncludeSource = defaultArg includeSource false Prefix = defaultArg prefix "fs" OutputKind = defaultArg format OutputKind.Html GenerateHeaderAnchors = defaultArg generateAnchors false - LayoutRoots = defaultArg layoutRoots [] } - + LayoutRoots = defaultArg layoutRoots [] + Generator = defaultArg generator generatePlainFile + } + /// Build default options context for parsing literate scripts/documents - static let parsingContext formatAgent evaluator compilerOptions definedSymbols = + static let parsingContext formatAgent evaluator compilerOptions definedSymbols = let agent = match formatAgent with | Some agent -> agent | _ -> CodeFormat.CreateAgent() { FormatAgent = agent CompilerOptions = compilerOptions - Evaluator = evaluator + Evaluator = evaluator DefinedSymbols = Option.map (String.concat ",") definedSymbols } - + /// Get default output file name, given various information static let defaultOutput output input kind = - match output, defaultArg kind OutputKind.Html with + match output, defaultArg kind OutputKind.Html with | Some out, _ -> out | _, OutputKind.Latex -> Path.ChangeExtension(input, "tex") | _, OutputKind.Html -> Path.ChangeExtension(input, "html") - + /// Apply the specified transformations to a document static let transform references doc = - let doc = + let doc = if references <> Some true then doc else Transformations.generateReferences doc - doc + doc static let customize customizeDocument ctx doc = match customizeDocument with @@ -60,9 +88,9 @@ type Literate private () = // ------------------------------------------------------------------------------------ // Parsing functions // ------------------------------------------------------------------------------------ - + /// Parse F# Script file - static member ParseScriptFile + static member ParseScriptFile ( path, ?formatAgent, ?compilerOptions, ?definedSymbols, ?references, ?fsiEvaluator ) = let ctx = parsingContext formatAgent fsiEvaluator compilerOptions definedSymbols ParseScript.parseScriptFile path (File.ReadAllText path) ctx @@ -71,7 +99,7 @@ type Literate private () = |> Transformations.evaluateCodeSnippets ctx /// Parse F# Script file - static member ParseScriptString + static member ParseScriptString ( content, ?path, ?formatAgent, ?compilerOptions, ?definedSymbols, ?references, ?fsiEvaluator ) = let ctx = parsingContext formatAgent fsiEvaluator compilerOptions definedSymbols ParseScript.parseScriptFile (defaultArg path "C:\\Document.fsx") content ctx @@ -83,7 +111,7 @@ type Literate private () = static member ParseMarkdownFile ( path, ?formatAgent, ?compilerOptions, ?definedSymbols, ?references, ?fsiEvaluator ) = let ctx = parsingContext formatAgent fsiEvaluator compilerOptions definedSymbols - ParseMarkdown.parseMarkdown path (File.ReadAllText path) + ParseMarkdown.parseMarkdown path (File.ReadAllText path) |> transform references |> Transformations.formatCodeSnippets path ctx |> Transformations.evaluateCodeSnippets ctx @@ -102,7 +130,7 @@ type Literate private () = // ------------------------------------------------------------------------------------ static member WriteHtml(doc:LiterateDocument, ?prefix, ?lineNumbers, ?generateAnchors) = - let ctx = formattingContext None (Some OutputKind.Html) prefix lineNumbers None generateAnchors None None + let ctx = formattingContext None (Some OutputKind.Html) prefix lineNumbers None generateAnchors None None None let doc = Transformations.replaceLiterateParagraphs ctx doc let doc = MarkdownDocument(doc.Paragraphs @ [InlineBlock(doc.FormattedTips, None)], doc.DefinedLinks) let sb = new System.Text.StringBuilder() @@ -111,18 +139,18 @@ type Literate private () = sb.ToString() static member WriteHtml(doc:LiterateDocument, writer:TextWriter, ?prefix, ?lineNumbers, ?generateAnchors) = - let ctx = formattingContext None (Some OutputKind.Html) prefix lineNumbers None generateAnchors None None + let ctx = formattingContext None (Some OutputKind.Html) prefix lineNumbers None generateAnchors None None None let doc = Transformations.replaceLiterateParagraphs ctx doc let doc = MarkdownDocument(doc.Paragraphs @ [InlineBlock(doc.FormattedTips, None)], doc.DefinedLinks) Html.formatMarkdown writer ctx.GenerateHeaderAnchors Environment.NewLine true doc.DefinedLinks doc.Paragraphs static member WriteLatex(doc:LiterateDocument, ?prefix, ?lineNumbers, ?generateAnchors) = - let ctx = formattingContext None (Some OutputKind.Latex) prefix lineNumbers None generateAnchors None None + let ctx = formattingContext None (Some OutputKind.Latex) prefix lineNumbers None generateAnchors None None None let doc = Transformations.replaceLiterateParagraphs ctx doc Markdown.WriteLatex(MarkdownDocument(doc.Paragraphs, doc.DefinedLinks)) static member WriteLatex(doc:LiterateDocument, writer:TextWriter, ?prefix, ?lineNumbers, ?generateAnchors) = - let ctx = formattingContext None (Some OutputKind.Latex) prefix lineNumbers None generateAnchors None None + let ctx = formattingContext None (Some OutputKind.Latex) prefix lineNumbers None generateAnchors None None None let doc = Transformations.replaceLiterateParagraphs ctx doc Markdown.WriteLatex(MarkdownDocument(doc.Paragraphs, doc.DefinedLinks), writer) @@ -131,7 +159,7 @@ type Literate private () = // ------------------------------------------------------------------------------------ static member FormatLiterateNodes(doc:LiterateDocument, ?format, ?prefix, ?lineNumbers, ?generateAnchors) = - let ctx = formattingContext None format prefix lineNumbers None generateAnchors None None + let ctx = formattingContext None format prefix lineNumbers None generateAnchors None None None Transformations.replaceLiterateParagraphs ctx doc // ------------------------------------------------------------------------------------ @@ -141,65 +169,65 @@ type Literate private () = /// Process the given literate document static member ProcessDocument ( doc, output, ?templateFile, ?format, ?prefix, ?lineNumbers, ?includeSource, ?generateAnchors, - ?replacements, ?layoutRoots, ?assemblyReferences) = - let ctx = formattingContext templateFile format prefix lineNumbers includeSource generateAnchors replacements layoutRoots + ?replacements, ?layoutRoots, ?assemblyReferences, ?generator) = + let ctx = formattingContext templateFile format prefix lineNumbers includeSource generateAnchors replacements layoutRoots generator Templating.processFile assemblyReferences doc output ctx /// Process Markdown document static member ProcessMarkdown - ( input, ?templateFile, ?output, ?format, ?formatAgent, ?prefix, ?compilerOptions, + ( input, ?templateFile, ?output, ?format, ?formatAgent, ?prefix, ?compilerOptions, ?lineNumbers, ?references, ?replacements, ?includeSource, ?layoutRoots, ?generateAnchors, - ?assemblyReferences, ?customizeDocument ) = - let doc = + ?assemblyReferences, ?customizeDocument, ?generator ) = + let doc = Literate.ParseMarkdownFile - ( input, ?formatAgent=formatAgent, ?compilerOptions=compilerOptions, + ( input, ?formatAgent=formatAgent, ?compilerOptions=compilerOptions, ?references = references ) - let ctx = formattingContext templateFile format prefix lineNumbers includeSource generateAnchors replacements layoutRoots + let ctx = formattingContext templateFile format prefix lineNumbers includeSource generateAnchors replacements layoutRoots generator let doc = customize customizeDocument ctx doc Templating.processFile assemblyReferences doc (defaultOutput output input format) ctx /// Process F# Script file static member ProcessScriptFile - ( input, ?templateFile, ?output, ?format, ?formatAgent, ?prefix, ?compilerOptions, + ( input, ?templateFile, ?output, ?format, ?formatAgent, ?prefix, ?compilerOptions, ?lineNumbers, ?references, ?fsiEvaluator, ?replacements, ?includeSource, ?layoutRoots, - ?generateAnchors, ?assemblyReferences, ?customizeDocument ) = - let doc = + ?generateAnchors, ?assemblyReferences, ?customizeDocument, ?generator ) = + let doc = Literate.ParseScriptFile - ( input, ?formatAgent=formatAgent, ?compilerOptions=compilerOptions, + ( input, ?formatAgent=formatAgent, ?compilerOptions=compilerOptions, ?references = references, ?fsiEvaluator = fsiEvaluator ) - let ctx = formattingContext templateFile format prefix lineNumbers includeSource generateAnchors replacements layoutRoots + let ctx = formattingContext templateFile format prefix lineNumbers includeSource generateAnchors replacements layoutRoots generator let doc = customize customizeDocument ctx doc Templating.processFile assemblyReferences doc (defaultOutput output input format) ctx /// Process directory containing a mix of Markdown documents and F# Script files static member ProcessDirectory - ( inputDirectory, ?templateFile, ?outputDirectory, ?format, ?formatAgent, ?prefix, ?compilerOptions, + ( inputDirectory, ?templateFile, ?outputDirectory, ?format, ?formatAgent, ?prefix, ?compilerOptions, ?lineNumbers, ?references, ?fsiEvaluator, ?replacements, ?includeSource, ?layoutRoots, ?generateAnchors, ?assemblyReferences, ?processRecursive, ?customizeDocument ) = let processRecursive = defaultArg processRecursive true // Call one or the other process function with all the arguments - let processScriptFile file output = + let processScriptFile file output = Literate.ProcessScriptFile - ( file, ?templateFile = templateFile, output = output, ?format = format, - ?formatAgent = formatAgent, ?prefix = prefix, ?compilerOptions = compilerOptions, - ?lineNumbers = lineNumbers, ?references = references, ?fsiEvaluator = fsiEvaluator, ?replacements = replacements, + ( file, ?templateFile = templateFile, output = output, ?format = format, + ?formatAgent = formatAgent, ?prefix = prefix, ?compilerOptions = compilerOptions, + ?lineNumbers = lineNumbers, ?references = references, ?fsiEvaluator = fsiEvaluator, ?replacements = replacements, ?includeSource = includeSource, ?layoutRoots = layoutRoots, ?generateAnchors = generateAnchors, ?assemblyReferences = assemblyReferences, ?customizeDocument = customizeDocument ) - let processMarkdown file output = + let processMarkdown file output = Literate.ProcessMarkdown - ( file, ?templateFile = templateFile, output = output, ?format = format, - ?formatAgent = formatAgent, ?prefix = prefix, ?compilerOptions = compilerOptions, - ?lineNumbers = lineNumbers, ?references = references, ?replacements = replacements, + ( file, ?templateFile = templateFile, output = output, ?format = format, + ?formatAgent = formatAgent, ?prefix = prefix, ?compilerOptions = compilerOptions, + ?lineNumbers = lineNumbers, ?references = references, ?replacements = replacements, ?includeSource = includeSource, ?layoutRoots = layoutRoots, ?generateAnchors = generateAnchors, ?assemblyReferences = assemblyReferences, ?customizeDocument = customizeDocument ) - + /// Recursively process all files in the directory tree - let rec processDirectory indir outdir = + let rec processDirectory indir outdir = // Create output directory if it does not exist if Directory.Exists(outdir) |> not then - try Directory.CreateDirectory(outdir) |> ignore + try Directory.CreateDirectory(outdir) |> ignore with _ -> failwithf "Cannot create directory '%s'" outdir let fsx = [ for f in Directory.GetFiles(indir, "*.fsx") -> processScriptFile, f ] From 18356751edcb386c71dd7f697ca7702b61f168c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Cie=C5=9Blak?= Date: Tue, 4 Oct 2016 21:30:43 +0200 Subject: [PATCH 03/19] Add generator to ProcessDirectory --- src/FSharp.Literate/Main.fs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/FSharp.Literate/Main.fs b/src/FSharp.Literate/Main.fs index 136e7daa1..cf4f39e8f 100644 --- a/src/FSharp.Literate/Main.fs +++ b/src/FSharp.Literate/Main.fs @@ -205,7 +205,7 @@ type Literate private () = static member ProcessDirectory ( inputDirectory, ?templateFile, ?outputDirectory, ?format, ?formatAgent, ?prefix, ?compilerOptions, ?lineNumbers, ?references, ?fsiEvaluator, ?replacements, ?includeSource, ?layoutRoots, ?generateAnchors, - ?assemblyReferences, ?processRecursive, ?customizeDocument ) = + ?assemblyReferences, ?processRecursive, ?customizeDocument, ?generator ) = let processRecursive = defaultArg processRecursive true // Call one or the other process function with all the arguments let processScriptFile file output = @@ -214,14 +214,14 @@ type Literate private () = ?formatAgent = formatAgent, ?prefix = prefix, ?compilerOptions = compilerOptions, ?lineNumbers = lineNumbers, ?references = references, ?fsiEvaluator = fsiEvaluator, ?replacements = replacements, ?includeSource = includeSource, ?layoutRoots = layoutRoots, ?generateAnchors = generateAnchors, - ?assemblyReferences = assemblyReferences, ?customizeDocument = customizeDocument ) + ?assemblyReferences = assemblyReferences, ?customizeDocument = customizeDocument, ?generator = generator ) let processMarkdown file output = Literate.ProcessMarkdown ( file, ?templateFile = templateFile, output = output, ?format = format, ?formatAgent = formatAgent, ?prefix = prefix, ?compilerOptions = compilerOptions, ?lineNumbers = lineNumbers, ?references = references, ?replacements = replacements, ?includeSource = includeSource, ?layoutRoots = layoutRoots, ?generateAnchors = generateAnchors, - ?assemblyReferences = assemblyReferences, ?customizeDocument = customizeDocument ) + ?assemblyReferences = assemblyReferences, ?customizeDocument = customizeDocument, ?generator = generator ) /// Recursively process all files in the directory tree let rec processDirectory indir outdir = From 02dbe756713376c900e212b21077a59a66c6f7b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Cie=C5=9Blak?= Date: Tue, 4 Oct 2016 22:23:59 +0200 Subject: [PATCH 04/19] Use record type --- src/FSharp.Literate/Contexts.fs | 16 +++++++++++++--- src/FSharp.Literate/Formatting.fs | 11 ++++++++++- src/FSharp.Literate/Main.fs | 6 +++--- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/FSharp.Literate/Contexts.fs b/src/FSharp.Literate/Contexts.fs index aea975aaf..7f816c483 100644 --- a/src/FSharp.Literate/Contexts.fs +++ b/src/FSharp.Literate/Contexts.fs @@ -18,8 +18,18 @@ type CompilerContext = [] type OutputKind = Html | Latex -/// Defines type of function responsible for generating output -type Generator = (string list option) -> string -> list -> string option -> string -> seq -> unit + +/// Defines input type for output generator +type GeneratorInput = + { + References : string list option + ContentTag : string + Parameters : (string * string) list + TemplateFile : string option + OutputFile : string + LayoutRoots : string seq + } + /// Specifies a context that is passed to functions that generate the output type ProcessingContext = @@ -40,4 +50,4 @@ type ProcessingContext = // Where to look for templates LayoutRoots : seq // Function generating output - Generator : Generator } + Generator : GeneratorInput -> unit } diff --git a/src/FSharp.Literate/Formatting.fs b/src/FSharp.Literate/Formatting.fs index 0fc552df9..2ae27f472 100644 --- a/src/FSharp.Literate/Formatting.fs +++ b/src/FSharp.Literate/Formatting.fs @@ -93,4 +93,13 @@ module Templating = "page-source", doc.SourceFile contentTag, formattedDocument "tooltips", tipsHtml ] - ctx.Generator references contentTag parameters ctx.TemplateFile output ctx.LayoutRoots + let gi = { + References = references + ContentTag = contentTag + Parameters = parameters + TemplateFile = ctx.TemplateFile + OutputFile = output + LayoutRoots = ctx.LayoutRoots + } + + ctx.Generator gi diff --git a/src/FSharp.Literate/Main.fs b/src/FSharp.Literate/Main.fs index cf4f39e8f..3dea542fd 100644 --- a/src/FSharp.Literate/Main.fs +++ b/src/FSharp.Literate/Main.fs @@ -37,9 +37,9 @@ type Literate private () = /// Depending on the template file, use either Razor engine /// or simple Html engine with {replacements} to format the document - static let generatePlainFile references contentTag parameters templateOpt output layoutRoots = - let templateOpt = templateOpt |> Option.map File.ReadAllText - File.WriteAllText(output, replaceParameters contentTag parameters templateOpt) + static let generatePlainFile (gi : GeneratorInput) = + let templateOpt = gi.TemplateFile |> Option.map File.ReadAllText + File.WriteAllText(gi.OutputFile, replaceParameters gi.ContentTag gi.Parameters templateOpt) /// Build default options context for formatting literate document From 2fd147c893bb9f21fa1201630025851d3c44985d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Cie=C5=9Blak?= Date: Tue, 4 Oct 2016 23:19:21 +0200 Subject: [PATCH 05/19] Remove Razor dependency from FSharp.MetadataFormat --- .../FSharp.MetadataFormat.fsproj | 36 +- src/FSharp.MetadataFormat/Main.fs | 483 +++++++++--------- src/FSharp.MetadataFormat/paket.references | 4 +- 3 files changed, 236 insertions(+), 287 deletions(-) diff --git a/src/FSharp.MetadataFormat/FSharp.MetadataFormat.fsproj b/src/FSharp.MetadataFormat/FSharp.MetadataFormat.fsproj index e097a7a59..61826e835 100644 --- a/src/FSharp.MetadataFormat/FSharp.MetadataFormat.fsproj +++ b/src/FSharp.MetadataFormat/FSharp.MetadataFormat.fsproj @@ -61,7 +61,7 @@ -