Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Razor engine update and improved mono support. #209

Merged
merged 4 commits into from
Oct 7, 2014
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ source http://nuget.org/api/v2
nuget FAKE
nuget FSharp.Compiler.Service
nuget CommandLineParser 1.9.71
nuget Microsoft.AspNet.Razor 2.0.30506.0
nuget RazorEngine 3.3.0
nuget Microsoft.AspNet.Razor == 2.0.30506.0
nuget RazorEngine.N 3.5.0
nuget NUnit
nuget NUnit.Runners
nuget NuGet.CommandLine
9 changes: 5 additions & 4 deletions paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ NUGET
remote: http://nuget.org/api/v2
specs:
CommandLineParser (1.9.71)
FAKE (3.5.2)
FSharp.Compiler.Service (0.0.59)
FAKE (3.5.8)
FSharp.Compiler.Service (0.0.62)
Microsoft.AspNet.Razor (2.0.30506.0)
NuGet.CommandLine (2.8.2)
NUnit (2.6.3)
NUnit.Runners (2.6.3)
RazorEngine (3.3.0)
Microsoft.AspNet.Razor (>= 2.0.30506.0)
RazorEngine.N (3.5.0)
Microsoft.AspNet.Razor (2.0.30506.0)
Microsoft.AspNet.Razor (>= 3.2.2.0)
6 changes: 3 additions & 3 deletions src/Common/AssemblyInfo.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ open System.Reflection
[<assembly: AssemblyTitleAttribute("FSharp.Formatting")>]
[<assembly: AssemblyProductAttribute("FSharp.Formatting")>]
[<assembly: AssemblyDescriptionAttribute("A package of libraries for building great F# documentation, samples and blogs")>]
[<assembly: AssemblyVersionAttribute("2.4.25")>]
[<assembly: AssemblyFileVersionAttribute("2.4.25")>]
[<assembly: AssemblyVersionAttribute("2.4.26")>]
[<assembly: AssemblyFileVersionAttribute("2.4.26")>]
[<assembly: AssemblyCopyrightAttribute("Apache 2.0 License")>]
do ()

module internal AssemblyVersionInformation =
let [<Literal>] Version = "2.4.25"
let [<Literal>] Version = "2.4.26"
88 changes: 43 additions & 45 deletions src/Common/Razor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,19 @@ open RazorEngine
open RazorEngine.Text
open RazorEngine.Templating
open RazorEngine.Configuration
open RazorEngine.Compilation.Resolver

type GetMemberBinderImpl (name) =
inherit GetMemberBinder(name, false)
let notImpl () = raise <| new NotImplementedException()
override x.FallbackGetMember(v, sug) = notImpl()

type RazorRender(layoutRoots, namespaces, templateName:string, ?model_type:System.Type) =
type MyTemplateService (config) =
inherit TemplateService(config)

member x.Resolver = config.Resolver

type RazorRender(layoutRoots, namespaces, templateName:string, ?modeltype:System.Type, ?references : string list) =
let templateName =
if templateName.EndsWith(".cshtml") then
templateName.Substring(0, templateName.Length - 7)
Expand All @@ -50,23 +56,25 @@ type RazorRender(layoutRoots, namespaces, templateName:string, ?model_type:Syste
{ new ITemplateResolver with
member x.Resolve name =
templateCache.GetOrAdd (name, fun name ->
match RazorRender.Resolve(layoutRoots, name + ".cshtml") with
| Some file -> File.ReadAllText(file)
| None ->
failwith "Could not find template file: %s\nSearching in: %A" name layoutRoots
null)
}
do RazorRender.Resolver <- templateResolver
File.ReadAllText(RazorRender.Resolve(layoutRoots, name + ".cshtml"))) }

// Configure templating engine
let config = new TemplateServiceConfiguration()
do config.EncodedStringFactory <- new RawStringFactory()
do config.Resolver <- templateResolver
do
match references with
| Some r ->
config.ReferenceResolver <-
{ new IAssemblyReferenceResolver with
member x.GetReferences (_, _) =
r |> List.toSeq }
| None -> ()

do namespaces |> Seq.iter (config.Namespaces.Add >> ignore)
do config.BaseTemplateType <- typedefof<DocPageTemplateBase<_>>
do config.Debug <- true
let templateservice = new TemplateService(config)
do Razor.SetTemplateService(templateservice)
do config.Debug <- true
let templateservice = new MyTemplateService(config)

let handleCompile source f =
try
Expand Down Expand Up @@ -96,57 +104,47 @@ type RazorRender(layoutRoots, namespaces, templateName:string, ?model_type:Syste
for k, v in defaultArg properties [] do
viewBag.AddValue(k, v)
viewBag

/// Global resolver (for use in 'DocPageTempalateBase')
static member val Resolver = null with get, set

/// Find file in one of the specified layout roots
static member Resolve(layoutRoots, name) =
static member TryResolve(layoutRoots, name) =
layoutRoots |> Seq.tryPick (fun layoutRoot ->
let partFile = Path.Combine(layoutRoot, name)
if File.Exists(partFile) then Some partFile else None)
static member ForceResolve(layoutRoots, name) =
match RazorRender.Resolve(layoutRoots, name) with
static member Resolve(layoutRoots, name) =
match RazorRender.TryResolve(layoutRoots, name) with
| Some f -> f
| None ->
failwith "Could not find template file: %s\nSearching in: %A" name layoutRoots
static member Run<'m>(name, model:'m, viewBag:DynamicViewBag) =
if Razor.Resolve<'m>(name, model) = null then
let templateContent = RazorRender.Resolver.Resolve(name)
Razor.Compile(templateContent, (if obj.ReferenceEquals(model, null) then typeof<'m> else model.GetType()), name)
Razor.Run<'m>(name, model, viewBag)

member internal x.HandleCompile source f = handleCompile source f
member internal x.TemplateName = templateName
member internal x.TemplateService = templateservice
member internal x.TemplateResolver = templateResolver
member internal x.WithProperties properties = withProperties properties x.ViewBag

/// Dynamic object with more properties (?)
member val ViewBag = new DynamicViewBag() with get, set
member x.ProcessFile(?properties) =
handleCompile templateName (fun _ ->
RazorRender.Run<obj>(templateName, null, x.WithProperties properties))
/// Process source file and return result as a string
member x.ProcessFileParse(?properties) =
handleCompile templateName (fun _ ->
Razor.Parse(templateResolver.Resolve templateName, null, x.WithProperties properties, templateName))

member x.ProcessFile(?properties) = x.ProcessFileModel(null, ?properties = properties)
member x.ProcessFileParse(?properties) = x.ProcessFileParseModel(null, ?properties = properties)

member x.ProcessFileModel(model:obj,?properties) =
handleCompile templateName (fun _ ->
RazorRender.Run<obj>(templateName, model, x.WithProperties properties))
if templateservice.Resolve(templateName, model) = null then
let templateContent = templateResolver.Resolve(templateName)
templateservice.Compile(templateContent, model.GetType(), templateName)
templateservice.Run(templateName, model, x.WithProperties properties))

/// Process source file and return result as a string
member x.ProcessFileParseModel(model:obj, ?properties) =
handleCompile templateName (fun _ ->
Razor.Parse(templateResolver.Resolve templateName, model, x.WithProperties properties, templateName))
templateservice.Parse(templateResolver.Resolve templateName, model, x.WithProperties properties, templateName))

and RazorRender<'model>(layoutRoots, namespaces, templateName) =
inherit RazorRender(layoutRoots, namespaces, templateName, typeof<'model>)
and RazorRender<'model>(layoutRoots, namespaces, templateName, ?references) =
inherit RazorRender(layoutRoots, namespaces, templateName, modeltype = typeof<'model>, ?references = references)

member x.ProcessFile(model:'model, ?properties) =
x.HandleCompile x.TemplateName (fun _ ->
RazorRender.Run<'model>(x.TemplateName, model, x.WithProperties properties))
/// Process source file and return result as a string
member x.ProcessFileParse(model:'model, ?properties) =
x.HandleCompile x.TemplateName (fun _ ->
Razor.Parse<'model>(RazorRender.Resolver.Resolve x.TemplateName, model, x.WithProperties properties, x.TemplateName))
member x.ProcessFile(model:'model, ?properties) = x.ProcessFileModel(model, ?properties = properties)
member x.ProcessFileParse(model:'model, ?properties) = x.ProcessFileParseModel(model, ?properties = properties)

and StringDictionary(dict:IDictionary<string, string>) =
member x.Dictionary = dict
Expand Down Expand Up @@ -195,8 +193,8 @@ and [<AbstractClass>] DocPageTemplateBase<'T>() =
and set (value:StringDictionary) = x.trySetViewBagValue<IDictionary<string, string>> "Properties" value.Dictionary

member x.Root = x.Properties.["root"]
member x.RenderPart(name, model:obj) =
if Razor.Resolve(name, model) = null then
let templateContent = RazorRender.Resolver.Resolve(name)
Razor.Compile(templateContent, model.GetType(), name)
Razor.Run(name, model)
member x.RenderPart(name, model:obj) =
if x.TemplateService.Resolve(name, model) = null then
let templateContent = (x.TemplateService :?> MyTemplateService).Resolver.Resolve(name)
x.TemplateService.Compile(templateContent, model.GetType(), name)
x.TemplateService.Run(name, model, null)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -141,7 +141,16 @@
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework' And $(TargetFrameworkVersion) == 'v4.0'">
<ItemGroup>
<Reference Include="RazorEngine">
<HintPath>..\..\packages\RazorEngine\lib\net40\RazorEngine.dll</HintPath>
<HintPath>..\..\packages\RazorEngine.N\lib\net40\RazorEngine.dll</HintPath>
<Private>True</Private>
<Paket>True</Paket>
</Reference>
</ItemGroup>
</When>
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework' And $(TargetFrameworkVersion) == 'v4.5'">
<ItemGroup>
<Reference Include="RazorEngine">
<HintPath>..\..\packages\RazorEngine.N\lib\net45\RazorEngine.dll</HintPath>
<Private>True</Private>
<Paket>True</Paket>
</Reference>
Expand All @@ -150,7 +159,7 @@
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework'">
<ItemGroup>
<Reference Include="RazorEngine">
<HintPath>..\..\packages\RazorEngine\lib\net40\RazorEngine.dll</HintPath>
<HintPath>..\..\packages\RazorEngine.N\lib\net45\RazorEngine.dll</HintPath>
<Private>True</Private>
<Paket>True</Paket>
</Reference>
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Formatting.CommandTool/paket.references
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
CommandLineParser
RazorEngine
RazorEngine.N
15 changes: 12 additions & 3 deletions src/FSharp.Literate/FSharp.Literate.fsproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -157,7 +157,16 @@
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework' And $(TargetFrameworkVersion) == 'v4.0'">
<ItemGroup>
<Reference Include="RazorEngine">
<HintPath>..\..\packages\RazorEngine\lib\net40\RazorEngine.dll</HintPath>
<HintPath>..\..\packages\RazorEngine.N\lib\net40\RazorEngine.dll</HintPath>
<Private>True</Private>
<Paket>True</Paket>
</Reference>
</ItemGroup>
</When>
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework' And $(TargetFrameworkVersion) == 'v4.5'">
<ItemGroup>
<Reference Include="RazorEngine">
<HintPath>..\..\packages\RazorEngine.N\lib\net45\RazorEngine.dll</HintPath>
<Private>True</Private>
<Paket>True</Paket>
</Reference>
Expand All @@ -166,7 +175,7 @@
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework'">
<ItemGroup>
<Reference Include="RazorEngine">
<HintPath>..\..\packages\RazorEngine\lib\net40\RazorEngine.dll</HintPath>
<HintPath>..\..\packages\RazorEngine.N\lib\net45\RazorEngine.dll</HintPath>
<Private>True</Private>
<Paket>True</Paket>
</Reference>
Expand Down
8 changes: 4 additions & 4 deletions src/FSharp.Literate/Formatting.fs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ module Templating =

/// Depending on the template file, use either Razor engine
/// or simple Html engine with {replacements} to format the document
let private generateFile contentTag parameters templateOpt output layoutRoots =
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, [], Path.GetFileNameWithoutExtension file)
let razor = RazorRender(layoutRoots, [], Path.GetFileNameWithoutExtension file, ?references = references)
let props = [ "Properties", dict parameters ]
let generated = razor.ProcessFile(props)
File.WriteAllText(output, generated)
Expand All @@ -85,7 +85,7 @@ module Templating =
// Formate literate document
// ------------------------------------------------------------------------------------

let processFile (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...
Expand Down Expand Up @@ -121,4 +121,4 @@ module Templating =
"page-source", doc.SourceFile
contentTag, formattedDocument
"tooltips", tipsHtml ]
generateFile contentTag parameters ctx.TemplateFile output ctx.LayoutRoots
generateFile references contentTag parameters ctx.TemplateFile output ctx.LayoutRoots
22 changes: 13 additions & 9 deletions src/FSharp.Literate/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -136,46 +136,46 @@ type Literate private () =
/// Process Markdown document
static member ProcessMarkdown
( input, ?templateFile, ?output, ?format, ?formatAgent, ?prefix, ?compilerOptions,
?lineNumbers, ?references, ?replacements, ?includeSource, ?layoutRoots, ?generateAnchors ) =
?lineNumbers, ?references, ?replacements, ?includeSource, ?layoutRoots, ?generateAnchors, ?assemblyReferences ) =
let doc =
Literate.ParseMarkdownFile
( input, ?formatAgent=formatAgent, ?compilerOptions=compilerOptions,
?references = references )
let ctx = formattingContext templateFile format prefix lineNumbers includeSource generateAnchors replacements layoutRoots
Templating.processFile doc (defaultOutput output input format) ctx
Templating.processFile assemblyReferences doc (defaultOutput output input format) ctx


/// Process F# Script file
static member ProcessScriptFile
( input, ?templateFile, ?output, ?format, ?formatAgent, ?prefix, ?compilerOptions,
?lineNumbers, ?references, ?fsiEvaluator, ?replacements, ?includeSource, ?layoutRoots, ?generateAnchors ) =
?lineNumbers, ?references, ?fsiEvaluator, ?replacements, ?includeSource, ?layoutRoots, ?generateAnchors, ?assemblyReferences ) =
let doc =
Literate.ParseScriptFile
( input, ?formatAgent=formatAgent, ?compilerOptions=compilerOptions,
?references = references, ?fsiEvaluator = fsiEvaluator )
let ctx = formattingContext templateFile format prefix lineNumbers includeSource generateAnchors replacements layoutRoots
Templating.processFile doc (defaultOutput output input format) ctx
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,
?lineNumbers, ?references, ?fsiEvaluator, ?replacements, ?includeSource, ?layoutRoots, ?generateAnchors ) =

?lineNumbers, ?references, ?fsiEvaluator, ?replacements, ?includeSource, ?layoutRoots, ?generateAnchors, ?assemblyReferences, ?processRecursive ) =
let processRecursive = defaultArg processRecursive true
// Call one or the other process function with all the arguments
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,
?includeSource = includeSource, ?layoutRoots = layoutRoots, ?generateAnchors = generateAnchors )
?includeSource = includeSource, ?layoutRoots = layoutRoots, ?generateAnchors = generateAnchors, ?assemblyReferences = assemblyReferences )
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 )
?includeSource = includeSource, ?layoutRoots = layoutRoots, ?generateAnchors = generateAnchors, ?assemblyReferences = assemblyReferences )

/// Recursively process all files in the directory tree
let rec processDirectory indir outdir =
// Create output directory if it does not exist
Expand All @@ -196,6 +196,10 @@ type Literate private () =
if changeTime > generateTime then
printfn "Generating '%s.%s'" name ext
func file output
if processRecursive then
for d in Directory.EnumerateDirectories(indir) do
let name = Path.GetFileName(d)
processDirectory (Path.Combine(indir, name)) (Path.Combine(outdir, name))

let outputDirectory = defaultArg outputDirectory inputDirectory
processDirectory inputDirectory outputDirectory
2 changes: 1 addition & 1 deletion src/FSharp.Literate/paket.references
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
FSharp.Compiler.Service
Microsoft.AspNet.Razor
RazorEngine
RazorEngine.N
15 changes: 12 additions & 3 deletions src/FSharp.MetadataFormat/FSharp.MetadataFormat.fsproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -154,7 +154,16 @@
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework' And $(TargetFrameworkVersion) == 'v4.0'">
<ItemGroup>
<Reference Include="RazorEngine">
<HintPath>..\..\packages\RazorEngine\lib\net40\RazorEngine.dll</HintPath>
<HintPath>..\..\packages\RazorEngine.N\lib\net40\RazorEngine.dll</HintPath>
<Private>True</Private>
<Paket>True</Paket>
</Reference>
</ItemGroup>
</When>
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework' And $(TargetFrameworkVersion) == 'v4.5'">
<ItemGroup>
<Reference Include="RazorEngine">
<HintPath>..\..\packages\RazorEngine.N\lib\net45\RazorEngine.dll</HintPath>
<Private>True</Private>
<Paket>True</Paket>
</Reference>
Expand All @@ -163,7 +172,7 @@
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework'">
<ItemGroup>
<Reference Include="RazorEngine">
<HintPath>..\..\packages\RazorEngine\lib\net40\RazorEngine.dll</HintPath>
<HintPath>..\..\packages\RazorEngine.N\lib\net45\RazorEngine.dll</HintPath>
<Private>True</Private>
<Paket>True</Paket>
</Reference>
Expand Down
Loading