├── .gitattributes ├── .gitignore ├── .paket ├── paket.exe └── paket.targets ├── .travis.yml ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── appveyor.yml ├── build.cmd ├── build.fsx ├── build.sh ├── buildConfig.fsx ├── doc ├── AboutRazor.md ├── Caching.md ├── DevelopReadme.md ├── Encoding.md ├── IntellisenseAndResharper.md ├── Isolation.md ├── LICENSE.md ├── LayoutAndPartial.md ├── ReferenceResolver.md ├── ReleaseNotes.md ├── Roslyn.md ├── TemplateBasics.md ├── TemplateManager.md ├── Upgrading.md ├── content │ ├── img │ │ ├── back_to_top.png │ │ ├── github-blue.png │ │ └── github.png │ ├── prism.css │ ├── prism.js │ ├── style.css │ ├── style.css.bak │ └── tips.js └── templates │ ├── docpage-index.cshtml │ ├── docpage.cshtml │ ├── reference │ ├── module.cshtml │ ├── namespaces.cshtml │ ├── part-members.cshtml │ ├── part-nested.cshtml │ └── type.cshtml │ ├── template-color.tex │ ├── template-math.tex │ ├── template-project.html │ └── template.cshtml ├── downloadNuget.fsx ├── generateDocs.fsx ├── nuget ├── RazorEngine-razor4.nuspec └── RazorEngine.nuspec ├── packages └── Yaaf.AdvancedBuilding │ └── content │ ├── build.cmd │ ├── build.sh │ ├── build.targets │ ├── buildConfigDef.fsx │ ├── buildInclude.fsx │ ├── downloadNugetInclude.fsx │ └── generateDocsInclude.fsx ├── paket.dependencies ├── paket.lock └── src ├── .gitignore ├── .nuget ├── NuGet.Config └── NuGet.targets ├── RazorEngine.Public.snk ├── RazorEngine.sln ├── RazorEngine.snk ├── SharedAssemblyInfo-Razor4.cs ├── SharedAssemblyInfo.cs ├── nuget.config ├── source ├── RazorEngine.Core │ ├── AttributeValue.cs │ ├── ClassDiagram.cd │ ├── CodeGenerators │ │ └── SetModelTypeCodeGenerator.cs │ ├── Common │ │ ├── CrossAppDomainDictionary.cs │ │ ├── CrossAppDomainObject.cs │ │ ├── GlobalSuppressions.cs │ │ └── HashCodeCombiner.cs │ ├── Compilation │ │ ├── CSharp │ │ │ ├── CSharpCodeParser.cs │ │ │ ├── CSharpDirectCompilerService.cs │ │ │ ├── CSharpRazorCodeGenerator.cs │ │ │ ├── CSharpRazorCodeLanguage.cs │ │ │ └── CSharpRoslynCompilerService.cs │ │ ├── CompilationData.cs │ │ ├── CompilerServiceBase.cs │ │ ├── CompilerServiceBuilder.cs │ │ ├── CompilerServicesUtility.cs │ │ ├── CrossAppDomainCleanUp.cs │ │ ├── DefaultCompilerServiceFactory.cs │ │ ├── DirectCompilerServiceBase.cs │ │ ├── DynamicObject │ │ │ └── Impromtu │ │ │ │ ├── ActLikeCaster.cs │ │ │ │ ├── ActLikeProxy.cs │ │ │ │ ├── ActLikeProxyAttribute.cs │ │ │ │ ├── ActLikeProxySerializationHelper.cs │ │ │ │ ├── AliasAttribute.cs │ │ │ │ ├── BareBonesList.cs │ │ │ │ ├── BinderHash.cs │ │ │ │ ├── BuildProxy.cs │ │ │ │ ├── Curry.cs │ │ │ │ ├── EmitExtensions.cs │ │ │ │ ├── IActLike.cs │ │ │ │ ├── IActLikeProxy.cs │ │ │ │ ├── IDynamicKnowLike.cs │ │ │ │ ├── ImpromptuForwarder.cs │ │ │ │ ├── ImpromptuForwarderAddRemove.cs │ │ │ │ ├── ImpromptuLateLibraryType.cs │ │ │ │ ├── Impromtu.cs │ │ │ │ ├── ImpromtuObject.cs │ │ │ │ ├── Invocation.cs │ │ │ │ ├── InvokeArg.cs │ │ │ │ ├── InvokeContext.cs │ │ │ │ ├── InvokeExt.cs │ │ │ │ ├── InvokeHelper-Regular.cs │ │ │ │ ├── InvokeHelper.cs │ │ │ │ ├── InvokeMemberName.cs │ │ │ │ ├── NonRecursiveInterfaceAttribute.cs │ │ │ │ ├── ThisFunction.cs │ │ │ │ ├── TypeHash.cs │ │ │ │ ├── UseNamedArgumentAttribute.cs │ │ │ │ └── Util.cs │ │ ├── ExecutionContextLessThread.cs │ │ ├── HasDynamicModelAttribute.cs │ │ ├── ICompilerService.cs │ │ ├── ICompilerServiceFactory.cs │ │ ├── Inspectors │ │ │ └── ICodeInspector.cs │ │ ├── RazorDynamicObject.cs │ │ ├── RazorEngineHost.cs │ │ ├── ReferenceResolver │ │ │ ├── CompilerReference.cs │ │ │ ├── IReferenceResolver.cs │ │ │ └── UseCurrentAssembliesReferenceResolver.cs │ │ ├── RoslynCompilerServiceBase.cs │ │ ├── RoslynCompilerServiceFactory.cs │ │ ├── TypeContext.cs │ │ └── VisualBasic │ │ │ ├── VBCodeParser.cs │ │ │ ├── VBDirectCompilerService.cs │ │ │ ├── VBRazorCodeGenerator.cs │ │ │ └── VBRazorCodeLanguage.cs │ ├── Configuration │ │ ├── Fluent │ │ │ ├── FluentConfigurationBuilder.cs │ │ │ ├── FluentTemplateServiceConfiguration.cs │ │ │ └── IConfigurationBuilder.cs │ │ ├── ITemplateServiceConfiguration.cs │ │ ├── RazorEngineConfigurationSection.cs │ │ ├── ReadOnlyTemplateServiceConfiguration.cs │ │ ├── TemplateServiceConfiguration.cs │ │ └── Xml │ │ │ ├── NamespaceConfigurationElement.cs │ │ │ ├── NamespaceConfigurationElementCollection.cs │ │ │ ├── TemplateServiceConfigurationElement.cs │ │ │ ├── TemplateServiceConfigurationElementCollection.cs │ │ │ └── XmlTemplateServiceConfiguration.cs │ ├── Encoding.cs │ ├── Engine.cs │ ├── Language.cs │ ├── Legacy │ │ ├── Razor.cs │ │ └── Templating │ │ │ ├── CachedTemplateItem.cs │ │ │ ├── DelegateTemplateResolver.cs │ │ │ ├── ITemplateResolver.cs │ │ │ ├── ITemplateService.cs │ │ │ ├── IsolatedTemplateService.cs │ │ │ ├── Parallel │ │ │ ├── DefaultParallelQueryPlan.cs │ │ │ └── IParallelQueryPlan.cs │ │ │ └── TemplateService.cs │ ├── PositionTagged.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── RazorEngine.Core.csproj │ ├── RazorEngine.snk │ ├── TaskRunner.cs │ ├── Templating │ │ ├── BaseTemplateKey.cs │ │ ├── CompiledTemplate.cs │ │ ├── DefaultActivator.cs │ │ ├── DefaultAppDomainFactory.cs │ │ ├── DefaultCachingProvider.cs │ │ ├── DelegateActivator.cs │ │ ├── DelegateAppDomainFactory.cs │ │ ├── DelegateTemplateManager.cs │ │ ├── DynamicViewBag.cs │ │ ├── DynamicWrapperService.cs │ │ ├── EmbeddedResourceTemplateManager.cs │ │ ├── ExecuteContext.cs │ │ ├── FullPathTemplateKey.cs │ │ ├── FullPathWithModifiedTimeTemplateKey.cs │ │ ├── HtmlTemplateBaseOfT.cs │ │ ├── IActivator.cs │ │ ├── IAppDomainFactory.cs │ │ ├── ICachingProvider.cs │ │ ├── ICompiledTemplate.cs │ │ ├── IInternalTemplateService.cs │ │ ├── IRazorEngineCore.cs │ │ ├── IRazorEngineService.cs │ │ ├── ITemplate.cs │ │ ├── ITemplateKey.cs │ │ ├── ITemplateManager.cs │ │ ├── ITemplateOfT.cs │ │ ├── ITemplateRunnerOfTModel.cs │ │ ├── ITemplateSource.cs │ │ ├── InstanceContext.cs │ │ ├── InternalTemplateService.cs │ │ ├── InvalidatingByModifiedTimeCachingProvider.cs │ │ ├── InvalidatingCachingProvider.cs │ │ ├── IsolatedRazorEngineService.cs │ │ ├── NameOnlyTemplateKey.cs │ │ ├── RazorEngineCore.cs │ │ ├── RazorEngineCoreWithCache.cs │ │ ├── RazorEngineService.cs │ │ ├── RazorEngineServiceExtensions.cs │ │ ├── ReferencesListForDynamicAssemblyResolution.cs │ │ ├── RequireNamespacesAttribute.cs │ │ ├── ResolvePathCheckModifiedTimeTemplateManager.cs │ │ ├── ResolvePathTemplateManager.cs │ │ ├── ResolveType.cs │ │ ├── TemplateBase.cs │ │ ├── TemplateBaseOfT.cs │ │ ├── TemplateCompilationException.cs │ │ ├── TemplateLoadingException.cs │ │ ├── TemplateParsingException.cs │ │ ├── TemplateRunnerExtensions.cs │ │ ├── TemplateRunnerOfTModel.cs │ │ ├── TemplateSource.cs │ │ ├── TemplateWriter.cs │ │ ├── TypeLoader.cs │ │ ├── WatchingResolvePathTemplateManager.cs │ │ └── WrapperTemplateManager.cs │ ├── Text │ │ ├── HtmlEncodedString.cs │ │ ├── HtmlEncodedStringFactory.cs │ │ ├── IEncodedString.cs │ │ ├── IEncodedStringFactory.cs │ │ ├── RawString.cs │ │ └── RawStringFactory.cs │ └── paket.references ├── RazorEngine.Hosts.Console │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── RazorEngine.Hosts.Console.csproj ├── RazorEngine.Mvc │ ├── MvcTemplateBase.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── RazorEngine.Mvc.csproj └── codeAnalysis.ruleset └── test ├── Test.RazorEngine.Core.Roslyn ├── Properties │ └── AssemblyInfo.cs ├── RazorEngine.snk ├── RoslynCompilerTestFixture.cs ├── Test.RazorEngine.Core.Roslyn.csproj └── paket.references ├── Test.RazorEngine.Core ├── ActivatorTestFixture.cs ├── CodeInspectorTestFixture.cs ├── CompilerServicesUtilityTestFixture.cs ├── ConfigurationTestFixture.cs ├── IsolatedRazorEngineServiceTestFixture.cs ├── IsolatedTemplateServiceTestFixture.cs ├── Issues │ ├── Release_3_0_TestFixture.cs │ └── Release_3_6_TestFixture.cs ├── Properties │ └── AssemblyInfo.cs ├── RazorEngine.snk ├── RazorEngineCleanupTests.cs ├── RazorEngineHostTestFixture.cs ├── RazorEngineServiceTestFixture.cs ├── TemplateBaseTestFixture.cs ├── TemplateServiceTestFixture.cs ├── Templating │ ├── EmbeddedResourceTemplateManagerTestFixture.cs │ ├── TemplateRunnerTestFixture.cs │ └── Templates │ │ ├── Layout.cshtml │ │ ├── Model.cs │ │ ├── NoModel.cshtml │ │ ├── Partial.cshtml │ │ ├── WithLayout.cshtml │ │ ├── WithModel.cshtml │ │ └── WithPartial.cshtml ├── Test.RazorEngine.Core.csproj ├── TestTypes │ ├── Activation │ │ ├── AutofacTemplateActivator.cs │ │ ├── CustomTemplateBase.cs │ │ ├── ITextFormatter.cs │ │ └── ReverseTextFormatter.cs │ ├── Animal.cs │ ├── AnimalViewModel.cs │ ├── BaseTypes │ │ ├── AddLanguageInfo.cs │ │ ├── NestedBaseClass.cs │ │ └── NonGenericTemplateBase.cs │ ├── Employee.cs │ ├── InlineTemplateModel.cs │ ├── Inspectors │ │ └── ThrowExceptionCodeInspector.cs │ ├── Person.cs │ ├── ThreadPoolItem.cs │ └── ValueObject.cs ├── VariousTestsFixture.cs ├── paket.references └── test │ └── TestHelper.dll ├── TestHelper ├── Properties │ └── AssemblyInfo.cs ├── TestClass.cs └── TestHelper.csproj └── TestRunnerHelper ├── App.config ├── Program.cs ├── Properties └── AssemblyInfo.cs └── TestRunnerHelper.csproj /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files 2 | * text=auto 3 | *.sh -crlf text=auto eol=lf 4 | 5 | # Custom for Visual Studio 6 | *.cs diff=csharp text=auto eol=lf 7 | *.fs diff=csharp text=auto eol=lf 8 | *.fsi diff=csharp text=auto eol=lf 9 | *.fsx diff=csharp text=auto eol=lf 10 | *.sln text eol=crlf merge=union 11 | *.csproj merge=union 12 | *.vbproj merge=union 13 | *.fsproj merge=union 14 | *.dbproj merge=union 15 | 16 | # Standard to msysgit 17 | *.doc diff=astextplain 18 | *.DOC diff=astextplain 19 | *.docx diff=astextplain 20 | *.DOCX diff=astextplain 21 | *.dot diff=astextplain 22 | *.DOT diff=astextplain 23 | *.pdf diff=astextplain 24 | *.PDF diff=astextplain 25 | *.rtf diff=astextplain 26 | *.RTF diff=astextplain 27 | -------------------------------------------------------------------------------- /.paket/paket.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Antaris/RazorEngine/aa5dd2661fe040ce39020949ebd33d98a530ebe4/.paket/paket.exe -------------------------------------------------------------------------------- /.paket/paket.targets: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | true 6 | 7 | true 8 | $(MSBuildThisFileDirectory) 9 | $(MSBuildThisFileDirectory)..\ 10 | 11 | 12 | 13 | $(PaketToolsPath)paket.exe 14 | $(PaketToolsPath)paket.bootstrapper.exe 15 | "$(PaketExePath)" 16 | mono --runtime=v4.0.30319 $(PaketExePath) 17 | "$(PaketBootStrapperExePath)" 18 | mono --runtime=v4.0.30319 $(PaketBootStrapperExePath) 19 | 20 | $(PaketCommand) restore 21 | $(PaketBootStrapperCommand) 22 | 23 | RestorePackages; $(BuildDependsOn); 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: csharp 2 | 3 | sudo: false 4 | 5 | mono: 6 | - latest 7 | - 4.4.2 8 | 9 | matrix: 10 | allow_failures: 11 | - mono: latest 12 | 13 | script: 14 | - ./build.sh All 15 | 16 | branches: 17 | except: 18 | - gh-pages 19 | 20 | notifications: 21 | webhooks: 22 | urls: 23 | - https://webhooks.gitter.im/e/4a71432bc093517cf9e2 24 | on_success: change # options: [always|never|change] default: always 25 | on_failure: always # options: [always|never|change] default: always 26 | on_start: false # default: false -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Thanks that you want to help! Please read the sections below. 2 | 3 | ## Contributing (General) 4 | 5 | If you want to send a bug report just open a new issue! 6 | 7 | Otherwise just fork the project an send a pull request to discuss and merge the changes. 8 | If you want to discuss the changes beforehand you can open a new issue. 9 | Even better when there is already an open issue marked as [up-for-grabs](https://github.com/Antaris/RazorEngine/labels/up-for-grabs). 10 | Please send the pull request against the `master` branch and mention an already existing issue (if present). 11 | 12 | This project is searching for new maintainers, so if you want to help write on gitter or start sending PRs :) 13 | 14 | ## Contributing Documentation 15 | 16 | As the documentation is generated from the repository you can help improving the documentation by editing the files in the `/doc` folder. 17 | You can even edit a page directly on github by clicking the edit button ([for example this page](https://github.com/Antaris/RazorEngine/blob/master/CONTRIBUTING.md)). 18 | See also https://help.github.com/articles/editing-files-in-your-repository/ 19 | (don't forget to send a pull request back after forking and changing something). 20 | 21 | ## Branching model 22 | 23 | The branching model in http://nvie.com/posts/a-successful-git-branching-model/ is used (which you do not need to read to send a pull request). 24 | However the naming differs: `develop` is called `master` and `master` is called `releases` in RazorEngine. 25 | 26 | ## Licensing 27 | 28 | This project is subject to the terms and conditions defined in file ['LICENSE.md'](https://github.com/Antaris/RazorEngine/blob/master/LICENSE.md), which is part of this source code package. 29 | 30 | You can find licenses of the programs this project depends on in either the "lib/$Project" folder or on their nuget page. 31 | 32 | 33 | ## Releasing 34 | 35 | 1. Update `ReleaseNotes.md` 36 | 2. Update `buildConfig.fsx` (versions) 37 | Roslyn Package versions only need to increased when we changed public API or when we made changes there. 38 | 39 | 3. Run `yaaf_merge_master=true PUSH_ROSLYN=true nugetkey= ./build.sh Release` 40 | - `yaaf_merge_master` -> can be used on build servers to force-switch on develop branch for the version bump commit. 41 | - `PUSH_ROSLYN` -> push the roslyn packages (if you increased their versions above) 42 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | init: 2 | - git config --global core.autocrlf input 3 | build_script: 4 | - cmd: build.cmd 5 | test: off 6 | version: 0.0.1.{build} 7 | artifacts: 8 | - path: bin 9 | name: bin 10 | 11 | notifications: 12 | - provider: Webhook 13 | url: https://webhooks.gitter.im/e/40fcaa9afb4b47ecafe7 14 | on_build_success: true 15 | on_build_failure: true 16 | -------------------------------------------------------------------------------- /build.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | if exist bootstrap.cmd ( 3 | call bootstrap.cmd 4 | ) 5 | 6 | set buildFile=build.fsx 7 | "packages/Yaaf.AdvancedBuilding/content/build.cmd" %* 8 | -------------------------------------------------------------------------------- /build.fsx: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // This file is subject to the terms and conditions defined in 3 | // file 'LICENSE.txt', which is part of this source code package. 4 | // ---------------------------------------------------------------------------- 5 | #load "packages/Yaaf.AdvancedBuilding/content/buildConfigDef.fsx" 6 | #load @"buildConfig.fsx" 7 | #load "packages/Yaaf.AdvancedBuilding/content/buildInclude.fsx" 8 | 9 | open System.IO 10 | open Fake 11 | let config = BuildInclude.config 12 | // Define your FAKE targets here 13 | 14 | let MyTarget = BuildInclude.MyTarget 15 | 16 | BuildInclude.documentationFAKEArgs <- "-nc" 17 | 18 | // This step ensures our current build is still compatible with FSharp.Formatting. 19 | MyTarget "CopyToFSharpFormatting" (fun _ -> 20 | // make the FSF load script happy 21 | [ "build/net45/RazorEngine.dll"; "packages/net45/Microsoft.AspNet.Razor/lib/net45/System.Web.Razor.dll" ] 22 | |> Seq.iter (fun source -> 23 | let dest = sprintf "packages/FSharp.Formatting/lib/net40/%s" (Path.GetFileName source) 24 | //try 25 | if File.Exists dest then 26 | trace (sprintf "Deleting %s" dest) 27 | File.Delete dest 28 | trace (sprintf "Copying %s to %s" source dest) 29 | File.Copy (source, dest) 30 | //with e -> 31 | // trace (sprintf "Couldn't copy %s to %s, because: %O" source dest e) 32 | )) 33 | 34 | "AfterBuild" 35 | ==> "CopyToFSharpFormatting" 36 | ==> "AllDocs" 37 | 38 | // start build 39 | RunTargetOrDefault "All" 40 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | if [ -f "bootstrap.sh" ]; 3 | then 4 | ./bootstrap.sh 5 | fi 6 | 7 | build="packages/Yaaf.AdvancedBuilding/content/build.sh" 8 | chmod +x "$build" 9 | "$build" $@ 10 | -------------------------------------------------------------------------------- /doc/AboutRazor.md: -------------------------------------------------------------------------------- 1 | # About Razor and its syntax 2 | 3 | ## About Razor 4 | The Razor parser was introduced as part of the [ASP.NET](http://www.asp.net) MVC and WebPages release by Microsoft. The Razor parser itself is designed to process a stream of characters to generate a C# or VB class which can be compiled. 5 | 6 | For an overview of how the Razor parser works under the hood, please visit [Andrew Nurse's blog](http://vibrantcode.com) for some in-depth articles. 7 | 8 | ## Razor Syntax 9 | The Razor syntax is designed to be a clean but robust syntax for merging both code and markup into a single language. Primarily Razor was designed for Html-like languages, but future editions may take advantage of the existing `MarkupParser` abstraction to deliver alternative markup languages (possibly BBCode, Latex, Markdown, etc.). An example Razor template could look like: 10 | 11 | ```markup 12 |
Hello @Model.Name, you are @Model.GetAge() years old.
13 | ``` 14 | 15 | This template is transformed into the body of a method, the `Execute` method, which could look something like this: 16 | 17 | ```csharp 18 | public void Execute() 19 | { 20 | WriteLiteral("
Hello "); 21 | Write(Model.Name); 22 | WriteLiteral(", you are "); 23 | Write(Model.GetAge()); 24 | WriteLiteral(" years old.
"); 25 | } 26 | ``` 27 | 28 | This mixture of code and markup allows for quite a declarative syntax where markup becomes a first-class feature alongside the code. Here is slightly more complex template: 29 | 30 | ```markup 31 | 36 | ``` 37 | 38 | Razor understands the code language (in this case, C#) because it operates dual parsers (a code parser and a markup parser). Much like the markup parser is geared up to understand Html, the code parser (in this example), is designed to understand C#. 39 | 40 | For an overview of the Razor syntax, please view [ScottGu's article: Introducing “Razor” – a new view engine for ASP.NET](http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx) 41 | 42 | ## Razor vs. MVC vs. WebPages vs. RazorEngine 43 | There is often a confusion about where Razor sits in this set of technologies. Essentially Razor is the parsing framework that does the work to take your text template and convert it into a compilable class. In terms of MVC and WebPages, they both utilise this parsing engine to convert text templates (view/page files) into executable classes (views/pages). Often we are asked questions such as "Where is @Html, @Url", etc. These are not features provided by Razor itself, but implementation details of the MVC and WebPages frameworks. 44 | 45 | RazorEngine is another consumer framework of the Razor parser. We wrap up the instantiation of the Razor parser and provide a common framework for using runtime template processing. 46 | -------------------------------------------------------------------------------- /doc/Caching.md: -------------------------------------------------------------------------------- 1 | 2 | # RazorEngine Caching API 3 | 4 | Moved to [TemplateManager.html](http://antaris.github.io/RazorEngine/TemplateManager.html) -------------------------------------------------------------------------------- /doc/Encoding.md: -------------------------------------------------------------------------------- 1 | # Encoding Values 2 | 3 | ## Encoding: 4 | By default RazorEngine is configured to encode as Html. This sometimes this presents problems were certain characters are encoded as Html but what you want is to output them as-is. To output something in raw format use the `@Raw()` built-in method as shown in the following example: 5 | 6 | ```csharp 7 | string template = "@Raw(Model.Data)"; 8 | var model = new { Data = "My raw double quotes appears here \"hello!\"" }; 9 | 10 | string result = Engine.Razor.RunCompile(template, "templateKey", null, model); 11 | ``` 12 | 13 | Which should result in: 14 | 15 | > `My raw double quotes appears here "hello!"` -------------------------------------------------------------------------------- /doc/LICENSE.md: -------------------------------------------------------------------------------- 1 | See https://github.com/Antaris/RazorEngine/blob/master/LICENSE.md -------------------------------------------------------------------------------- /doc/Roslyn.md: -------------------------------------------------------------------------------- 1 | 2 | # Roslyn compiler support 3 | 4 | Starting with 3.5.0 RazorEngine supports the Roslyn compilers (via the Microsoft.CodeAnalysis nuget package). 5 | The first thing you want to do is install the additional package: 6 | 7 | Install-Package RazorEngine.Roslyn 8 | 9 | > If you use the 4.x series (RazorEngine) you need the 4.x series of RazorEngine.Roslyn. 10 | 11 | To activate roslyn all you need to do is set the `CompilerServiceFactory` property in the configuration: 12 | 13 | ```csharp 14 | config.CompilerServiceFactory = new RazorEngine.Roslyn.RoslynCompilerServiceFactory(); 15 | ``` 16 | 17 | ## Known Limitation/Bugs 18 | 19 | - Debugging symbols do not work currently (If you know how Roslyn works please send a pull request!). 20 | - Debug symbols cannot be created on mono/unix: 21 | 22 | error: (0, 0) Unexpected error writing debug information -- 'The requested feature is not implemented.' 23 | 24 | - No support for the net40 build as the roslyn nuget package doesn't support net40! 25 | - Only C# support (VB.net support is kind of gone as Razor4 doesn't support VB.net). 26 | - If you find more please open a issue! 27 | - Running Roslyn on mono could lead to sigsegv crashes (mono bug): https://travis-ci.org/Antaris/RazorEngine/builds/45375847! 28 | -------------------------------------------------------------------------------- /doc/content/img/back_to_top.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Antaris/RazorEngine/aa5dd2661fe040ce39020949ebd33d98a530ebe4/doc/content/img/back_to_top.png -------------------------------------------------------------------------------- /doc/content/img/github-blue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Antaris/RazorEngine/aa5dd2661fe040ce39020949ebd33d98a530ebe4/doc/content/img/github-blue.png -------------------------------------------------------------------------------- /doc/content/img/github.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Antaris/RazorEngine/aa5dd2661fe040ce39020949ebd33d98a530ebe4/doc/content/img/github.png -------------------------------------------------------------------------------- /doc/content/tips.js: -------------------------------------------------------------------------------- 1 | var currentTip = null; 2 | var currentTipElement = null; 3 | 4 | function hideTip(evt, name, unique) { 5 | var el = document.getElementById(name); 6 | el.style.display = "none"; 7 | currentTip = null; 8 | } 9 | 10 | function findPos(obj) { 11 | // no idea why, but it behaves differently in webbrowser component 12 | if (window.location.search == "?inapp") 13 | return [obj.offsetLeft + 10, obj.offsetTop + 30]; 14 | 15 | var curleft = 0; 16 | var curtop = obj.offsetHeight; 17 | while (obj) { 18 | curleft += obj.offsetLeft; 19 | curtop += obj.offsetTop; 20 | obj = obj.offsetParent; 21 | }; 22 | return [curleft, curtop]; 23 | } 24 | 25 | function hideUsingEsc(e) { 26 | if (!e) { e = event; } 27 | hideTip(e, currentTipElement, currentTip); 28 | } 29 | 30 | function showTip(evt, name, unique, owner) { 31 | document.onkeydown = hideUsingEsc; 32 | if (currentTip == unique) return; 33 | currentTip = unique; 34 | currentTipElement = name; 35 | 36 | var pos = findPos(owner ? owner : (evt.srcElement ? evt.srcElement : evt.target)); 37 | var posx = pos[0]; 38 | var posy = pos[1]; 39 | 40 | var el = document.getElementById(name); 41 | var parent = (document.documentElement == null) ? document.body : document.documentElement; 42 | el.style.position = "absolute"; 43 | el.style.left = posx + "px"; 44 | el.style.top = posy + "px"; 45 | el.style.display = "block"; 46 | } -------------------------------------------------------------------------------- /doc/templates/docpage-index.cshtml: -------------------------------------------------------------------------------- 1 | @{ 2 | Layout = "template"; 3 | Title = Properties["page-title"]; 4 | Description = Properties["project-summary"]; 5 | } 6 | 7 | @Properties["document"] 8 | @Properties["tooltips"] -------------------------------------------------------------------------------- /doc/templates/docpage.cshtml: -------------------------------------------------------------------------------- 1 | @{ 2 | Layout = "template"; 3 | Title = Properties["page-title"]; 4 | Description = Properties["project-summary"]; 5 | } 6 | @Properties["document"] 7 | @Properties["tooltips"] -------------------------------------------------------------------------------- /doc/templates/reference/namespaces.cshtml: -------------------------------------------------------------------------------- 1 | @{ 2 | Layout = "template"; 3 | Title = "Namespaces - " + Properties["project-name"]; 4 | } 5 | 6 |

@Model.Name

7 | 8 |

Overview

9 | @foreach (var ns in Model.Namespaces) 10 | { 11 | if (ns.Types.Length + ns.Modules.Length > 0) 12 | { 13 | @ns.Name
14 | } 15 | } 16 | 17 | @foreach (var ns in Model.Namespaces) 18 | { 19 | if (ns.Types.Length + ns.Modules.Length > 0) 20 | { 21 |

Back to top @ns.Name Namespace

22 |
23 | @RenderPart("part-nested", new { 24 | Types = ns.Types, 25 | Modules = ns.Modules 26 | }) 27 |
28 | } 29 | } 30 | -------------------------------------------------------------------------------- /doc/templates/reference/part-members.cshtml: -------------------------------------------------------------------------------- 1 | @if (Enumerable.Count(Model.Members) > 0) { 2 |

@Model.Header

3 | 4 | 5 | 6 | 7 | 8 | @foreach (var it in Model.Members) 9 | { 10 | 11 | 26 | 36 | 37 | } 38 | 39 |
@Model.TableHeaderDescription
12 | @{ var id = Html.UniqueID().ToString(); } 13 | 14 | @Html.Encode(it.Details.FormatUsage(40)) 15 | 16 |
17 | Signature: @Html.Encode(it.Details.Signature)
18 | @if (!it.Details.Modifiers.IsEmpty) { 19 | Modifiers: @it.Details.FormatModifiers
20 | } 21 | @if (!it.Details.TypeArguments.IsEmpty) { 22 | Type parameters: @it.Details.FormatTypeArguments 23 | } 24 |
25 |
27 | @if (!String.IsNullOrEmpty(it.Details.FormatSourceLocation)) 28 | { 29 | 30 | 31 | 32 | 33 | } 34 | @it.Comment.FullText 35 |
40 | } -------------------------------------------------------------------------------- /doc/templates/reference/part-nested.cshtml: -------------------------------------------------------------------------------- 1 | @if (Enumerable.Count(Model.Types) > 0) { 2 | 3 | 4 | 5 | 6 | 7 | @foreach (var it in Model.Types) 8 | { 9 | 10 | 13 | 14 | 15 | } 16 | 17 |
TypeDescription
11 | @it.Name 12 | @it.Comment.Blurb
18 | } 19 | @if (Enumerable.Count(Model.Modules) > 0) { 20 | 21 | 22 | 23 | 24 | 25 | @foreach (var it in Model.Modules) 26 | { 27 | 28 | 31 | 32 | 33 | } 34 | 35 |
ModuleDescription
29 | @it.Name 30 | @it.Comment.Blurb
36 | } -------------------------------------------------------------------------------- /doc/templates/template-color.tex: -------------------------------------------------------------------------------- 1 | \documentclass{article} 2 | 3 | % Defining colors by names 4 | \usepackage{xcolor} 5 | % Verbatim enviroment 6 | \usepackage{fancyvrb} 7 | % Verbatim enviroment for unformatted source code 8 | \usepackage{listings} 9 | % Better font for backslash 10 | \usepackage[T1]{fontenc} 11 | \usepackage{hyperref} 12 | % Providing more features than usual tabular 13 | \usepackage{longtable} 14 | 15 | % Identifiers in color code #000000 16 | \newcommand{\id}[1]{\textcolor{black}{#1}} 17 | 18 | % Comments in green 19 | \definecolor{officegreen}{rgb}{0, 0.5, 0} 20 | \newcommand{\com}[1]{\textcolor{officegreen}{#1}} 21 | 22 | % Inactive elements in color code #808080 23 | \newcommand{\inact}[1]{\textcolor{gray}{#1}} 24 | 25 | % Keywords in color code #000080 26 | \definecolor{navy}{rgb}{0, 0, 0.5} 27 | \newcommand{\kwd}[1]{\textcolor{navy}{#1}} 28 | 29 | % Numbers in color code #008000 30 | \newcommand{\num}[1]{\textcolor{officegreen}{#1}} 31 | 32 | % Operators in color code #800080 33 | \newcommand{\ops}[1]{\textcolor{purple}{#1}} 34 | 35 | % Preprocessors in color code #800080 36 | \newcommand{\prep}[1]{\textcolor{purple}{#1}} 37 | 38 | % Strings in color code #808000 39 | \newcommand{\str}[1]{\textcolor{olive}{#1}} 40 | 41 | % Lines in color code #80b0b0 42 | % Define relative color to work correctly with \newcommand 43 | \definecolor{linecolor}{rgb}{0.5, 0.6875, 0.6875} 44 | \newcommand{\lines}[1]{\textcolor{linecolor}{#1}} 45 | 46 | % fsi output in color code #606060 47 | \definecolor{outputcolor}{rgb}{0.375, 0.375, 0.375} 48 | \newcommand{\fsi}[1]{\textcolor{outputcolor}{#1}} 49 | 50 | % Omitted parts in color code #808080 51 | \newcommand{\omi}[1]{\textcolor{gray}{#1}} 52 | 53 | % Overriding color and style of line numbers 54 | \renewcommand{\theFancyVerbLine}{ 55 | \lines{\small \arabic{FancyVerbLine}:}} 56 | 57 | \lstset{% 58 | backgroundcolor=\color{gray!15}, 59 | basicstyle=\ttfamily, 60 | breaklines=true, 61 | columns=fullflexible 62 | } 63 | 64 | \title{{page-title}} 65 | \date{} 66 | 67 | \begin{document} 68 | 69 | \maketitle 70 | 71 | {contents} 72 | 73 | {tooltips} 74 | 75 | \end{document} 76 | -------------------------------------------------------------------------------- /doc/templates/template-math.tex: -------------------------------------------------------------------------------- 1 | \documentclass{article} 2 | 3 | % Defining colors by names 4 | \usepackage{xcolor} 5 | % Verbatim enviroment 6 | \usepackage{fancyvrb} 7 | % Verbatim enviroment for unformatted source code 8 | \usepackage{listings} 9 | % Better font for backslash 10 | \usepackage[T1]{fontenc} 11 | \usepackage{hyperref} 12 | % Providing more features than usual tabular 13 | \usepackage{longtable} 14 | % Augment with more font faces 15 | \usepackage{bera} 16 | % More math symbols 17 | \usepackage{amssymb} 18 | 19 | % Simple key value store 20 | % (source at http://tex.stackexchange.com/questions/48907/global-key-value-dictionary) 21 | \def\addvalue#1#2{\expandafter\gdef\csname my@data@\detokenize{#1}\endcsname{#2}} 22 | \def\usevalue#1{% 23 | \ifcsname my@data@\detokenize{#1}\endcsname 24 | \csname my@data@\detokenize{#1}\expandafter\endcsname 25 | \else 26 | % if not found, return key as value 27 | {#1}\expandafter 28 | \fi 29 | } 30 | 31 | \addvalue{>=}{$\geq$} 32 | \addvalue{<=}{$\leq$} 33 | \addvalue{<>}{$\neq$} 34 | \addvalue{not}{$\neg$} 35 | \addvalue{&&}{$\land$} 36 | \addvalue{||}{$\lor$} 37 | \addvalue{->}{$\rightarrow$} 38 | \addvalue{<-}{$\leftarrow$} 39 | \addvalue{=>}{$\Rightarrow$} 40 | \addvalue{*}{$\times$} 41 | \addvalue{|>}{$\blacktriangleright$} 42 | \addvalue{<|}{$\blacktriangleleft$} 43 | 44 | % Identifiers 45 | \newcommand{\id}[1]{\texttt{#1}} 46 | 47 | % Comments 48 | \newcommand{\com}[1]{\textit{#1}} 49 | 50 | % Inactive elements 51 | \newcommand{\inact}[1]{\textmd{#1}} 52 | 53 | % Keywords 54 | \newcommand{\kwd}[1]{\textbf{#1}} 55 | 56 | % Numbers 57 | \newcommand{\num}[1]{\textrm{#1}} 58 | 59 | % Operators 60 | \newcommand{\ops}[1]{\usevalue{#1}} 61 | 62 | % Preprocessors 63 | \newcommand{\prep}[1]{\textsf{#1}} 64 | 65 | % Strings 66 | \newcommand{\str}[1]{\textrm{#1}} 67 | 68 | % Line numbers 69 | \newcommand{\lines}[1]{\textrm{#1}} 70 | 71 | % fsi output 72 | \newcommand{\fsi}[1]{\textsf{#1}} 73 | 74 | % Omitted parts 75 | \newcommand{\omi}[1]{\textsc{#1}} 76 | 77 | % Overriding color and style of line numbers 78 | \renewcommand{\theFancyVerbLine}{ 79 | \lines{\small \arabic{FancyVerbLine}:}} 80 | 81 | \lstset{% 82 | backgroundcolor=\color{gray!15}, 83 | basicstyle=\ttfamily, 84 | breaklines=true, 85 | columns=fullflexible 86 | } 87 | 88 | \title{{page-title}} 89 | \date{} 90 | 91 | \begin{document} 92 | 93 | \maketitle 94 | 95 | {contents} 96 | 97 | {tooltips} 98 | 99 | \end{document} 100 | -------------------------------------------------------------------------------- /doc/templates/template-project.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | {page-title} 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 25 | 26 | 27 |
28 |
29 | 33 |

{project-name}

34 |
35 |
36 |
37 |
38 | {document} 39 | {tooltips} 40 |
41 |
42 | 43 | 58 |
59 |
60 |
61 | Fork me on GitHub 62 | 63 | -------------------------------------------------------------------------------- /downloadNuget.fsx: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // This file is subject to the terms and conditions defined in 3 | // file 'LICENSE.txt', which is part of this source code package. 4 | // ---------------------------------------------------------------------------- 5 | // Get any working NuGet.exe 6 | 7 | #load "packages/Yaaf.AdvancedBuilding/content/downloadNugetInclude.fsx" -------------------------------------------------------------------------------- /generateDocs.fsx: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // This file is subject to the terms and conditions defined in 3 | // file 'LICENSE.txt', which is part of this source code package. 4 | // ---------------------------------------------------------------------------- 5 | open System.IO 6 | // Force to load our build! 7 | // As long as FSharp.Formatting is using the regular net45 build 8 | // This should work as expected. 9 | #I @"build/net45" 10 | #r @"build/net45/System.Web.Razor.dll" 11 | #r @"build/net45/RazorEngine.dll" 12 | 13 | #load "packages/Yaaf.AdvancedBuilding/content/buildConfigDef.fsx" 14 | #load @"buildConfig.fsx" 15 | #load "packages/Yaaf.AdvancedBuilding/content/generateDocsInclude.fsx" 16 | open Fake 17 | // Force load of System.Web.Razor.dll 18 | let someType = typeof 19 | let printAssemblies msg = 20 | printfn "%s. Loaded Assemblies:" msg 21 | System.AppDomain.CurrentDomain.GetAssemblies() 22 | |> Seq.choose (fun a -> try Some (a.GetName().FullName, a.Location) with _ -> None) 23 | //|> Seq.filter (fun l -> l.Contains ("Razor")) 24 | |> Seq.iter (fun (n, l) -> printfn "\t- %s: %s" n l) 25 | 26 | printAssemblies "starting documentation generation" 27 | try RunTargetOrDefault "LocalDoc" 28 | finally 29 | printAssemblies "Documentation generation finished" 30 | 31 | printfn "THE END" -------------------------------------------------------------------------------- /nuget/RazorEngine-razor4.nuspec: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | @project@ 5 | @project@ 6 | @build.number@ 7 | @authors@ 8 | @authors@ 9 | false 10 | https://github.com/Antaris/RazorEngine/blob/master/LICENSE.md 11 | https://github.com/Antaris/RazorEngine 12 | @summary@ 13 | @description@ 14 | @releaseNotes@ 15 | Copyright 2016 16 | @tags@ 17 | @dependencies@ 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /nuget/RazorEngine.nuspec: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | @project@ 5 | @project@ 6 | @build.number@ 7 | @authors@ 8 | @authors@ 9 | false 10 | https://github.com/Antaris/RazorEngine/blob/master/LICENSE.md 11 | https://github.com/Antaris/RazorEngine 12 | @summary@ 13 | @description@ 14 | @releaseNotes@ 15 | Copyright 2016 16 | @tags@ 17 | @dependencies@ 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /packages/Yaaf.AdvancedBuilding/content/build.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | REM cls 3 | set nuget_packages=packages 4 | set paket_packages=packages 5 | 6 | set nuget_path=NuGet.CommandLine/tools/NuGet.exe 7 | set fake_path=FAKE/tools/FAKE.exe 8 | 9 | REM resore paket build dependencies 10 | if exist ".paket/paket.exe" ( 11 | REM Batch is just a useless shit 12 | if "%PAKET_UPDATE%" == "y" ( 13 | echo Running paket update - as requested by PAKET_UPDATE=y 14 | .paket\paket.exe update 15 | if %ERRORLEVEL% NEQ 0 exit /b %ERRORLEVEL% 16 | ) 17 | 18 | if "%PAKET_UPDATE%" == "true" ( 19 | echo Running paket update - as requested by PAKET_UPDATE=y 20 | .paket\paket.exe update 21 | if %ERRORLEVEL% NEQ 0 exit /b %ERRORLEVEL% 22 | ) 23 | 24 | 25 | 26 | echo restore paket packages 27 | .paket\paket.exe restore 28 | if %ERRORLEVEL% NEQ 0 exit /b %ERRORLEVEL% 29 | 30 | set fake=%paket_packages%/%fake_path% 31 | set nuget=%paket_packages%/%nuget_path% 32 | ) 33 | REM Download NuGet (if not already available because of paket) 34 | if not exist %nuget% ( 35 | if exist downloadNuget.fsx ( 36 | if not exist %nuget_packages%/%nuget_path% ( 37 | echo Bootstrap Nuget 38 | "C:\Program Files (x86)\Microsoft SDKs\F#\3.1\Framework\v4.0\fsi.exe" downloadNuget.fsx 39 | if %ERRORLEVEL% NEQ 0 exit /b %ERRORLEVEL% 40 | ) 41 | set nuget=%nuget_packages%/%nuget_path% 42 | ) 43 | ) 44 | REM Restore Nuget build dependencies 45 | if exist packages.config ( 46 | if exist %nuget% ( 47 | echo Resolve build dependencies 48 | "%nuget%" "install" "packages.config" "-OutputDirectory" %nuget_packages% "-ExcludeVersion" 49 | if %ERRORLEVEL% NEQ 0 exit /b %ERRORLEVEL% 50 | ) else ( 51 | echo NuGet build dependencies file found but no NuGet.exe could be found, either add downloadNuget.fsx or add Nuget.Commandline as paket dependency!. 52 | ) 53 | ) 54 | 55 | REM FAKE could be available as nuget dependency 56 | if not exist %fake% ( 57 | set fake=%nuget_packages%/%fake_path% 58 | if not exist %fake% ( 59 | echo Could not find FAKE in nuget or paket dependencies! 60 | exit /b 1 61 | ) 62 | ) 63 | 64 | echo start FAKE for the rest of the build procedure... 65 | "%fake%" %* --fsiargs -d:WIN64 -d:FAKE build.fsx 66 | 67 | -------------------------------------------------------------------------------- /packages/Yaaf.AdvancedBuilding/content/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | if test "$OS" = "Windows_NT" 3 | then 4 | # use .Net 5 | MONO="" 6 | DEFINE="WIN64" 7 | FSI="C:\Program Files (x86)\Microsoft SDKs\F#\3.1\Framework\v4.0\fsi.exe" 8 | else 9 | # use mono 10 | command -v mono >/dev/null 2>&1 || { echo >&2 "Please install mono dependency."; exit 1; } 11 | myMono="mono --debug --runtime=v4.0" 12 | FSI="fsharpi" 13 | 14 | MONO="$myMono" 15 | DEFINE="MONO" 16 | fi 17 | 18 | function do_build { 19 | nuget_packages="packages" 20 | paket_packages="packages" 21 | 22 | nuget_path="NuGet.CommandLine/tools/NuGet.exe" 23 | fake_path="FAKE/tools/FAKE.exe" 24 | 25 | # Restore paket build dependencies 26 | if [ -f ".paket/paket.exe" ]; 27 | then 28 | 29 | if [ "$PAKET_UPDATE" == "y" ] || [ "$PAKET_UPDATE" == "true" ]; then 30 | echo "run paket update (as requested by PAKET_UPDATE=y)" 31 | $MONO .paket/paket.exe update 32 | exit_code=$? 33 | if [ $exit_code -ne 0 ]; then 34 | exit $exit_code 35 | fi 36 | fi 37 | 38 | echo "restore paket packages" 39 | $MONO .paket/paket.exe restore 40 | exit_code=$? 41 | if [ $exit_code -ne 0 ]; then 42 | exit $exit_code 43 | fi 44 | 45 | fake=$paket_packages/$fake_path 46 | nuget=$paket_packages/$nuget_path 47 | fi 48 | # Download NuGet (if not already available because of paket) 49 | if [ ! -f "$nuget" ]; 50 | then 51 | if [ -f downloadNuget.fsx ]; 52 | then 53 | if [ ! -f "$nuget_packages/$nuget_path" ]; then 54 | echo "Bootstrap Nuget" 55 | command -v "$FSI" >/dev/null 2>&1 || { echo >&2 "Please install fsharpi or download a NuGet.exe to $nuget_packages/$nuget_path"; exit 1; } 56 | "$FSI" downloadNuget.fsx 57 | fi 58 | nuget="$nuget_packages/$nuget_path" 59 | fi 60 | fi 61 | 62 | # Restore Nuget build dependencies 63 | if [ -f "packages.config" ]; 64 | then 65 | if [ -f "$nuget" ]; 66 | then 67 | echo "restore NuGet build dependencies." 68 | $MONO $nuget "install" "packages.config" "-OutputDirectory" "$nuget_packages" "-ExcludeVersion" 69 | else 70 | echo "NuGet build dependencies file found but no NuGet.exe could be found, either add downloadNuget.fsx or add Nuget.Commandline as paket dependency!." 71 | fi 72 | fi 73 | 74 | # FAKE could be available as nuget dependency 75 | if [ ! -f "$fake" ]; 76 | then 77 | fake="$nuget_packages/$fake_path" 78 | if [ ! -f "$fake" ]; 79 | then 80 | echo "Could not find FAKE in nuget or paket dependencies!" 81 | exit 1 82 | fi 83 | fi 84 | 85 | echo "start FAKE for the rest of the build procedure..." 86 | $MONO $fake $@ --fsiargs -d:$DEFINE -d:FAKE build.fsx 87 | return $? 88 | } 89 | 90 | if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then 91 | # we are sourced 92 | : 93 | else 94 | do_build $@ 95 | fi 96 | -------------------------------------------------------------------------------- /packages/Yaaf.AdvancedBuilding/content/downloadNugetInclude.fsx: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // This file is subject to the terms and conditions defined in 3 | // file 'LICENSE.txt', which is part of this source code package. 4 | // ---------------------------------------------------------------------------- 5 | // Get any working NuGet.exe 6 | // It's sad that this does'nt work on mono as they don't have a working "System.IO.Compression.FileSystem.dll" 7 | 8 | open System.Net 9 | open System.IO 10 | 11 | //let nugetCommandLinePackage = "https://www.nuget.org/api/v2/package/NuGet.Commandline" 12 | //let nugetPkg = "lib/NuGet.CommandLine/NuGet.CommandLine.nupkg" 13 | let nugetLink = "https://www.nuget.org/nuget.exe" 14 | let nugetExe = "packages/NuGet.CommandLine/tools/NuGet.exe" 15 | 16 | let nugetToolsPath = Path.GetDirectoryName(nugetExe) 17 | //let nugetPath = Path.GetDirectoryName(nugetToolsPath) 18 | 19 | let downloadFile (link:string) (filePath:string) = 20 | let wc = new WebClient() 21 | wc.DownloadFile(link, filePath) 22 | 23 | Directory.CreateDirectory(nugetToolsPath) |> ignore 24 | 25 | downloadFile nugetLink nugetExe 26 | //ZipFile.ExtractToDirectory(nugetPkg, nugetPath) 27 | -------------------------------------------------------------------------------- /paket.dependencies: -------------------------------------------------------------------------------- 1 | version 5.0.0 2 | source https://nuget.org/api/v2 3 | source https://ci.appveyor.com/nuget/fsharp-formatting 4 | 5 | nuget Nuget.CommandLine 6 | nuget FAKE 7 | nuget FSharp.Formatting prerelease 8 | nuget System.ValueTuple 9 | nuget Microsoft.AspNet.Razor 10 | 11 | group Net45 12 | condition: NET45 13 | source https://nuget.org/api/v2 14 | 15 | nuget Microsoft.AspNet.Razor ~> 3.0 16 | nuget Microsoft.CodeAnalysis ~> 1.3 17 | nuget System.Collections.Immutable ~> 1.2 18 | nuget System.Reflection.Metadata ~> 1.4 19 | 20 | group Net40 21 | condition: NET40 22 | source https://nuget.org/api/v2 23 | framework: net40 24 | 25 | nuget Microsoft.AspNet.Razor ~> 2.0 26 | 27 | group Razor4 28 | condition: RAZOR4 29 | source https://nuget.org/api/v2 30 | 31 | nuget Microsoft.AspNetCore.Razor ~> 1.0 32 | nuget Microsoft.CodeAnalysis ~> 1.3 33 | nuget System.Collections.Immutable ~> 1.2 34 | nuget System.Reflection.Metadata ~> 1.4 35 | 36 | group Test 37 | source https://nuget.org/api/v2 38 | 39 | nuget NUnit ~> 3.6 40 | nuget NUnit.Console ~> 3.6 41 | 42 | // Locked, because support for .NET 40 and Silverlight 5 are dropped in versions 4.5 + 43 | nuget Moq 4.2.1510.2205 44 | 45 | nuget Autofac ~> 3.5 46 | -------------------------------------------------------------------------------- /src/.gitignore: -------------------------------------------------------------------------------- 1 | Thumbs.db 2 | *.obj 3 | *.exe 4 | *.pdb 5 | *.user 6 | *.aps 7 | *.pch 8 | *.vspscc 9 | *_i.c 10 | *_p.c 11 | *.ncb 12 | *.suo 13 | *.sln.docstates 14 | *.tlb 15 | *.tlh 16 | *.bak 17 | *.cache 18 | *.ilk 19 | *.log 20 | [Bb]in/ 21 | [Dd]ebug*/ 22 | *.lib 23 | *.sbr 24 | obj/ 25 | [Rr]elease*/ 26 | _ReSharper*/ 27 | [Tt]est[Rr]esult* 28 | 29 | # Monodevelop 30 | test-results/ 31 | *.userprefs 32 | 33 | /templates -------------------------------------------------------------------------------- /src/.nuget/NuGet.Config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/RazorEngine.Public.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Antaris/RazorEngine/aa5dd2661fe040ce39020949ebd33d98a530ebe4/src/RazorEngine.Public.snk -------------------------------------------------------------------------------- /src/RazorEngine.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Antaris/RazorEngine/aa5dd2661fe040ce39020949ebd33d98a530ebe4/src/RazorEngine.snk -------------------------------------------------------------------------------- /src/SharedAssemblyInfo-Razor4.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System.Reflection; 3 | 4 | [assembly: AssemblyCompanyAttribute("RazorEngine")] 5 | [assembly: AssemblyProductAttribute("RazorEngine")] 6 | [assembly: AssemblyCopyrightAttribute("RazorEngine Copyright © RazorEngine Project 2011-2017")] 7 | [assembly: AssemblyVersionAttribute("4.5.1")] 8 | [assembly: AssemblyFileVersionAttribute("4.5.1")] 9 | [assembly: AssemblyInformationalVersionAttribute("4.5.1-alpha001")] 10 | namespace System { 11 | internal static class AssemblyVersionInformation { 12 | internal const System.String AssemblyCompany = "RazorEngine"; 13 | internal const System.String AssemblyProduct = "RazorEngine"; 14 | internal const System.String AssemblyCopyright = "RazorEngine Copyright © RazorEngine Project 2011-2017"; 15 | internal const System.String AssemblyVersion = "4.5.1"; 16 | internal const System.String AssemblyFileVersion = "4.5.1"; 17 | internal const System.String AssemblyInformationalVersion = "4.5.1-alpha001"; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/SharedAssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System.Reflection; 3 | 4 | [assembly: AssemblyCompanyAttribute("RazorEngine")] 5 | [assembly: AssemblyProductAttribute("RazorEngine")] 6 | [assembly: AssemblyCopyrightAttribute("RazorEngine Copyright © RazorEngine Project 2011-2017")] 7 | [assembly: AssemblyVersionAttribute("3.10.1")] 8 | [assembly: AssemblyFileVersionAttribute("3.10.1")] 9 | [assembly: AssemblyInformationalVersionAttribute("3.10.1-alpha001")] 10 | namespace System { 11 | internal static class AssemblyVersionInformation { 12 | internal const System.String AssemblyCompany = "RazorEngine"; 13 | internal const System.String AssemblyProduct = "RazorEngine"; 14 | internal const System.String AssemblyCopyright = "RazorEngine Copyright © RazorEngine Project 2011-2017"; 15 | internal const System.String AssemblyVersion = "3.10.1"; 16 | internal const System.String AssemblyFileVersion = "3.10.1"; 17 | internal const System.String AssemblyInformationalVersion = "3.10.1-alpha001"; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/nuget.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/AttributeValue.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine 2 | { 3 | using System; 4 | /// 5 | /// Razor Html Attribute value 6 | /// 7 | public class AttributeValue 8 | { 9 | /// 10 | /// Creates a new Razor Html Attribute value. 11 | /// 12 | /// 13 | /// 14 | /// 15 | public AttributeValue(PositionTagged prefix, PositionTagged value, bool literal) 16 | { 17 | Prefix = prefix; 18 | Value = value; 19 | Literal = literal; 20 | } 21 | 22 | /// 23 | /// The prefix of the attribute. 24 | /// 25 | public PositionTagged Prefix { get; private set; } 26 | 27 | /// 28 | /// The Value of the attribute. 29 | /// 30 | public PositionTagged Value { get; private set; } 31 | 32 | /// 33 | /// Indicates whether the attribute is a lital. 34 | /// 35 | public bool Literal { get; private set; } 36 | 37 | /// 38 | /// Convert from a tuple. 39 | /// 40 | /// 41 | /// 42 | public static AttributeValue FromTuple(Tuple, Tuple, bool> value) 43 | { 44 | return new AttributeValue(value.Item1, value.Item2, value.Item3); 45 | } 46 | 47 | /// 48 | /// Convert from a tuple. 49 | /// 50 | /// 51 | /// 52 | public static AttributeValue FromTuple(Tuple, Tuple, bool> value) 53 | { 54 | return new AttributeValue(value.Item1, new PositionTagged(value.Item2.Item1, value.Item2.Item2), value.Item3); 55 | } 56 | 57 | /// 58 | /// Convert from a tuple 59 | /// 60 | /// 61 | /// 62 | public static implicit operator AttributeValue(Tuple, Tuple, bool> value) 63 | { 64 | return FromTuple(value); 65 | } 66 | 67 | /// 68 | /// Convert from a tuple. 69 | /// 70 | /// 71 | /// 72 | public static implicit operator AttributeValue(Tuple, Tuple, bool> value) 73 | { 74 | return FromTuple(value); 75 | } 76 | } 77 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Common/CrossAppDomainObject.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Runtime.Remoting; 5 | using System.Security; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace RazorEngine 10 | { 11 | /// 12 | /// Enables access to objects across application domain boundaries. 13 | /// This type differs from by ensuring that the 14 | /// service lifetime is managed deterministically by the consumer. 15 | /// 16 | public abstract class CrossAppDomainObject : MarshalByRefObject, IDisposable 17 | { 18 | private bool _disposed; 19 | 20 | /// 21 | /// Cleans up the instance. 22 | /// 23 | ~CrossAppDomainObject() 24 | { 25 | Dispose(false); 26 | } 27 | 28 | /// 29 | /// Disconnects the remoting channel(s) of this object and all nested objects. 30 | /// 31 | [SecuritySafeCritical] 32 | private void Disconnect() 33 | { 34 | RemotingServices.Disconnect(this); 35 | } 36 | 37 | /// 38 | /// initializes the lifetime service for the current instance. 39 | /// 40 | /// null 41 | [SecurityCritical] 42 | public sealed override object InitializeLifetimeService() 43 | { 44 | // 45 | // Returning null designates an infinite non-expiring lease. 46 | // We must therefore ensure that RemotingServices.Disconnect() is called when 47 | // it's no longer needed otherwise there will be a memory leak. 48 | // 49 | return null; 50 | } 51 | 52 | /// 53 | /// Disposes the current instance. 54 | /// 55 | public void Dispose() 56 | { 57 | GC.SuppressFinalize(this); 58 | Dispose(true); 59 | } 60 | 61 | /// 62 | /// Disposes the current instance via the disposable pattern. 63 | /// 64 | /// true when Dispose() was called manually. 65 | protected virtual void Dispose(bool disposing) 66 | { 67 | if (_disposed) 68 | return; 69 | 70 | Disconnect(); 71 | _disposed = true; 72 | } 73 | 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Common/HashCodeCombiner.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | 3 | namespace RazorEngine.Common 4 | { 5 | internal class HashCodeCombiner 6 | { 7 | private long _combinedHash64 = 0x1505L; 8 | 9 | public int CombinedHash 10 | { 11 | get { return _combinedHash64.GetHashCode(); } 12 | } 13 | 14 | public HashCodeCombiner Add(IEnumerable e) 15 | { 16 | if (e == null) 17 | { 18 | Add(0); 19 | } 20 | else 21 | { 22 | int count = 0; 23 | foreach (object o in e) 24 | { 25 | Add(o); 26 | count++; 27 | } 28 | Add(count); 29 | } 30 | return this; 31 | } 32 | 33 | public HashCodeCombiner Add(int i) 34 | { 35 | _combinedHash64 = ((_combinedHash64 << 5) + _combinedHash64) ^ i; 36 | return this; 37 | } 38 | 39 | public HashCodeCombiner Add(object o) 40 | { 41 | int hashCode = (o != null) ? o.GetHashCode() : 0; 42 | Add(hashCode); 43 | return this; 44 | } 45 | 46 | public static HashCodeCombiner Start() 47 | { 48 | return new HashCodeCombiner(); 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/CSharp/CSharpRazorCodeGenerator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace RazorEngine.Compilation.CSharp 5 | { 6 | using System.CodeDom; 7 | using System.Security; 8 | #if RAZOR4 9 | using Microsoft.AspNetCore.Razor; 10 | using Microsoft.AspNetCore.Razor.CodeGenerators; 11 | using Microsoft.AspNetCore.Razor.Parser.SyntaxTree; 12 | using OriginalCSharpRazorCodeGenerator = Microsoft.AspNetCore.Razor.Chunks.Generators.RazorChunkGenerator; 13 | #else 14 | using System.Web.Razor; 15 | using System.Web.Razor.Parser.SyntaxTree; 16 | using OriginalCSharpRazorCodeGenerator = System.Web.Razor.Generator.CSharpRazorCodeGenerator; 17 | #endif 18 | using Templating; 19 | 20 | /// 21 | /// Defines a code generator that supports C# syntax. 22 | /// 23 | #if NET45 // Razor 2 has [assembly: SecurityTransparent] 24 | [SecurityCritical] 25 | #endif 26 | public class CSharpRazorCodeGenerator : OriginalCSharpRazorCodeGenerator 27 | { 28 | #region Constructor 29 | /// 30 | /// Initializes a new instance of the class. 31 | /// 32 | /// Name of the class. 33 | /// Name of the root namespace. 34 | /// Name of the source file. 35 | /// The host. 36 | /// Flag to specify that this generator is running in struct mode. 37 | public CSharpRazorCodeGenerator(string className, string rootNamespaceName, string sourceFileName, RazorEngineHost host, bool strictMode) 38 | : base(className, rootNamespaceName, sourceFileName, host) 39 | { 40 | StrictMode = strictMode; 41 | } 42 | 43 | #endregion 44 | 45 | #region Properties 46 | /// 47 | /// Gets whether the code generator is running in strict mode. 48 | /// 49 | public bool StrictMode { get; private set; } 50 | #endregion 51 | 52 | #region Methods 53 | /// 54 | /// Visits an error generated through parsing. 55 | /// 56 | /// The error that was generated. 57 | #if NET45 // Razor 2 has [assembly: SecurityTransparent] 58 | [SecurityCritical] 59 | #endif 60 | public override void VisitError(RazorError err) 61 | { 62 | if (StrictMode) 63 | throw new TemplateParsingException(err.Message, err.Location.CharacterIndex, err.Location.LineIndex); 64 | } 65 | #endregion 66 | } 67 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/CSharp/CSharpRazorCodeLanguage.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Compilation.CSharp 2 | { 3 | using System.Security; 4 | #if RAZOR4 5 | using Microsoft.AspNetCore.Razor; 6 | using Microsoft.AspNetCore.Razor.Chunks.Generators; 7 | using OriginalCSharpRazorCodeLanguage = Microsoft.AspNetCore.Razor.CSharpRazorCodeLanguage; 8 | #else 9 | using System.Web.Razor; 10 | using System.Web.Razor.Generator; 11 | using OriginalCSharpRazorCodeLanguage = System.Web.Razor.CSharpRazorCodeLanguage; 12 | #endif 13 | 14 | /// 15 | /// Provides a razor code language that supports the C# language. 16 | /// 17 | #if NET45 // Razor 2 has [assembly: SecurityTransparent] 18 | [SecurityCritical] 19 | #endif 20 | public class CSharpRazorCodeLanguage : OriginalCSharpRazorCodeLanguage 21 | { 22 | #region Constructor 23 | /// 24 | /// Initialises a new instance 25 | /// 26 | /// Flag to determine whether strict mode is enabled. 27 | public CSharpRazorCodeLanguage(bool strictMode) 28 | { 29 | StrictMode = strictMode; 30 | } 31 | #endregion 32 | 33 | #region Properties 34 | /// 35 | /// Gets whether strict mode is enabled. 36 | /// 37 | public bool StrictMode { get; private set; } 38 | #endregion 39 | 40 | #region Methods 41 | /// 42 | /// Creates the code generator. 43 | /// 44 | /// Name of the class. 45 | /// Name of the root namespace. 46 | /// Name of the source file. 47 | /// The host. 48 | #if NET45 // Razor 2 has [assembly: SecurityTransparent] 49 | [SecurityCritical] 50 | #endif 51 | #if RAZOR4 52 | public override RazorChunkGenerator CreateChunkGenerator(string className, string rootNamespaceName, string sourceFileName, RazorEngineHost host) 53 | #else 54 | public override RazorCodeGenerator CreateCodeGenerator(string className, string rootNamespaceName, string sourceFileName, RazorEngineHost host) 55 | #endif 56 | { 57 | return new CSharpRazorCodeGenerator(className, rootNamespaceName, sourceFileName, host, StrictMode); 58 | } 59 | #endregion 60 | } 61 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/CompilerServiceBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Compilation 2 | { 3 | using System.Diagnostics.Contracts; 4 | 5 | using Configuration; 6 | 7 | /// 8 | /// Manages creation of instances. 9 | /// 10 | public static class CompilerServiceBuilder 11 | { 12 | #region Fields 13 | private static ICompilerServiceFactory _factory = 14 | #if RAZOR4 15 | new Roslyn.RoslynCompilerServiceFactory(); 16 | #else 17 | new DefaultCompilerServiceFactory(); 18 | #endif 19 | private static readonly object sync = new object(); 20 | #endregion 21 | 22 | #region Methods 23 | /// 24 | /// Sets the used to create compiler service instances. 25 | /// 26 | /// The compiler service factory to use. 27 | public static void SetCompilerServiceFactory(ICompilerServiceFactory factory) 28 | { 29 | Contract.Requires(factory != null); 30 | 31 | lock (sync) 32 | { 33 | _factory = factory; 34 | } 35 | } 36 | 37 | /// 38 | /// Gets the for the specfied language. 39 | /// 40 | /// The code language. 41 | /// The compiler service instance. 42 | public static ICompilerService GetCompilerService(Language language) 43 | { 44 | lock (sync) 45 | { 46 | return _factory.CreateCompilerService(language); 47 | } 48 | } 49 | 50 | /// 51 | /// Gets the for the default . 52 | /// 53 | /// The compiler service instance. 54 | [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] 55 | public static ICompilerService GetDefaultCompilerService() 56 | { 57 | var config = RazorEngineConfigurationSection.GetConfiguration(); 58 | if (config == null) 59 | return GetCompilerService(Language.CSharp); 60 | 61 | return GetCompilerService(config.DefaultLanguage); 62 | } 63 | #endregion 64 | } 65 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/DefaultCompilerServiceFactory.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Compilation 2 | { 3 | using System; 4 | using System.Diagnostics.CodeAnalysis; 5 | 6 | using CSharp; 7 | using VisualBasic; 8 | using System.Security; 9 | 10 | /// 11 | /// Provides a default implementation of a . 12 | /// 13 | [Serializable] 14 | public class DefaultCompilerServiceFactory : ICompilerServiceFactory 15 | { 16 | #region Methods 17 | /// 18 | /// Creates a that supports the specified language. 19 | /// 20 | /// The . 21 | /// An instance of . 22 | [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")] 23 | [SecuritySafeCritical] 24 | public ICompilerService CreateCompilerService(Language language) 25 | { 26 | switch (language) 27 | { 28 | case Language.CSharp: 29 | return new CSharpDirectCompilerService(); 30 | 31 | case Language.VisualBasic: 32 | #if RAZOR4 33 | throw new NotSupportedException("Razor4 doesn't support VB.net apparently."); 34 | #else 35 | return new VBDirectCompilerService(); 36 | #endif 37 | 38 | default: 39 | throw new ArgumentException("Unsupported language: " + language); 40 | } 41 | } 42 | #endregion 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/DynamicObject/Impromtu/ActLikeCaster.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Runtime.Serialization; 5 | using System.Text; 6 | using RazorEngine.Compilation.ImpromptuInterface.Dynamic; 7 | 8 | namespace RazorEngine.Compilation.ImpromptuInterface 9 | { 10 | /// 11 | /// Extends the class to allow implicit 12 | /// and explicit conversions to any interface type. 13 | /// 14 | public class ActLikeCaster : ImpromptuForwarder 15 | { 16 | private List _interfaceTypes; 17 | 18 | /// 19 | /// handles any conversion call. 20 | /// 21 | /// 22 | /// 23 | /// true if successful. 24 | public override bool TryConvert(System.Dynamic.ConvertBinder binder, out object result) 25 | { 26 | result = null; 27 | 28 | if (binder.Type.IsInterface) 29 | { 30 | _interfaceTypes.Insert(0, binder.Type); 31 | result = Impromptu.DynamicActLike(Target, _interfaceTypes.ToArray()); 32 | return true; 33 | } 34 | 35 | if (binder.Type.IsInstanceOfType(Target)) 36 | { 37 | result = Target; 38 | } 39 | 40 | return false; 41 | } 42 | 43 | /// 44 | /// Initializes a new instance of the type. 45 | /// 46 | /// the target object for call forwarding. 47 | /// the supported interface types. 48 | public ActLikeCaster(object target, IEnumerable types) 49 | : base(target) 50 | { 51 | _interfaceTypes = types.ToList(); 52 | } 53 | 54 | #if !SILVERLIGHT 55 | /// 56 | /// Initializes a new instance of the type. 57 | /// 58 | /// the serialization info. 59 | /// the streaming context. 60 | public ActLikeCaster(SerializationInfo info, StreamingContext context) 61 | : base(info, context) 62 | { 63 | } 64 | 65 | #endif 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/DynamicObject/Impromtu/ActLikeProxyAttribute.cs: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright 2011 Ekon Benefits 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | 16 | 17 | using System; 18 | using System.Collections.Generic; 19 | using System.Linq; 20 | using System.Text; 21 | //using RazorEngine.Compilation.ImpromptuInterface.Internal.Support; 22 | 23 | namespace RazorEngine.Compilation.ImpromptuInterface.Build 24 | { 25 | /// 26 | /// Meta info describing proxy usage. Can be used to preload proxy. 27 | /// 28 | [AttributeUsage(AttributeTargets.Class)] 29 | [Serializable] 30 | public sealed class ActLikeProxyAttribute : Attribute 31 | { 32 | /// 33 | /// Initializes a new instance of the class. 34 | /// 35 | /// The interfaces. 36 | /// The context. 37 | public ActLikeProxyAttribute(Type[] interfaces, Type context) 38 | { 39 | Interfaces = interfaces; 40 | Context = context; 41 | } 42 | 43 | /// 44 | /// Gets or sets the interfaces. 45 | /// 46 | /// The interfaces. 47 | public Type[] Interfaces { get; set; } 48 | /// 49 | /// Gets or sets the context. 50 | /// 51 | /// The context. 52 | public Type Context { get; set; } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/DynamicObject/Impromtu/ActLikeProxySerializationHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Runtime.Serialization; 5 | using System.Security; 6 | using System.Text; 7 | 8 | namespace RazorEngine.Compilation.ImpromptuInterface.Build 9 | { 10 | #if !SILVERLIGHT 11 | 12 | 13 | 14 | /// 15 | /// Support Deserializing the proxy since on separate runs of an executable 16 | /// 17 | [Serializable] 18 | public class ActLikeProxySerializationHelper : IObjectReference 19 | { 20 | /// 21 | /// Original Object 22 | /// 23 | public object Original; 24 | /// 25 | /// Intefaces 26 | /// 27 | public Type[] Interfaces; 28 | 29 | /// 30 | /// Type Context 31 | /// 32 | public Type Context; 33 | 34 | /// 35 | /// Returns the real object that should be deserialized, rather than the object that the serialized stream specifies. 36 | /// 37 | /// The from which the current object is deserialized. 38 | /// 39 | /// Returns the actual object that is put into the graph. 40 | /// 41 | /// The caller does not have the required permission. The call will not work on a medium trusted server. 42 | [SecurityCritical] 43 | public object GetRealObject(StreamingContext context) 44 | { 45 | var tInterfaces = Interfaces; 46 | if (tInterfaces == null) 47 | { 48 | throw new InvalidOperationException("Expected SerializationData to contain a non-null Interfaces field. Please consider upgrading mono!"); 49 | } 50 | 51 | var tType = BuildProxy.BuildType(Context, tInterfaces.First(), tInterfaces.Skip(1).ToArray()); 52 | return Impromptu.InitializeProxy(tType, Original, tInterfaces); 53 | } 54 | 55 | } 56 | #endif 57 | } 58 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/DynamicObject/Impromtu/AliasAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace RazorEngine.Compilation.ImpromptuInterface 4 | { 5 | /// 6 | /// Alias to swap method/property/event call name invoked on original 7 | /// 8 | [AttributeUsage(System.AttributeTargets.Method 9 | | System.AttributeTargets.Property 10 | | System.AttributeTargets.Event)] 11 | public class AliasAttribute : Attribute 12 | { 13 | private string _name; 14 | 15 | /// 16 | /// Initializes a new instance of the class. 17 | /// 18 | /// The name. 19 | public AliasAttribute(string name) 20 | { 21 | _name = name; 22 | } 23 | 24 | /// 25 | /// Gets or sets the name. 26 | /// 27 | /// 28 | /// The name. 29 | /// 30 | public string Name 31 | { 32 | get { return _name; } 33 | 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/DynamicObject/Impromtu/IActLike.cs: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright 2011 Ekon Benefits 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | 16 | using System; 17 | 18 | namespace RazorEngine.Compilation.ImpromptuInterface.Dynamic 19 | { 20 | /// 21 | /// This interface can be used on your custom dynamic objects if you want impromptu interfaces without casting to object or using the static method syntax of ActLike. 22 | /// Also if you want to change the behavior for slightly for specific types as this will take precident when using the dynamic keyword or your specific type is known staticly. 23 | /// 24 | public interface IActLike 25 | { 26 | /// 27 | /// This interface can be used on your custom dynamic objects if you want impromptu interfaces without casting to object or using the static method syntax of ActLike. 28 | /// Also if you want to change the behavior for slightly for specific types as this will take precident when using the dynamic keyword or your specific type is known staticly. 29 | /// 30 | /// 31 | /// 32 | /// 33 | TInterface ActLike(params Type[] otherInterfaces) where TInterface : class; 34 | } 35 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/DynamicObject/Impromtu/IActLikeProxy.cs: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright 2011 Ekon Benefits 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | 16 | namespace RazorEngine.Compilation.ImpromptuInterface 17 | { 18 | /// 19 | /// This interface can be used to access the original content of your emitted type; 20 | /// 21 | public interface IActLikeProxy 22 | { 23 | /// 24 | /// Returns the proxied object 25 | /// 26 | dynamic Original { get; } 27 | 28 | } 29 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/DynamicObject/Impromtu/IDynamicKnowLike.cs: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright 2011 Ekon Benefits 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | 16 | using System; 17 | using System.Collections.Generic; 18 | 19 | namespace RazorEngine.Compilation.ImpromptuInterface.Dynamic 20 | { 21 | /// 22 | /// This interface can be used on your custom dynamic objects if you want to know the interface you are impromptu-ly implementing. 23 | /// 24 | public interface IDynamicKnowLike 25 | { 26 | /// 27 | /// Property used to pass interface information to proxied object 28 | /// 29 | IEnumerable KnownInterfaces { set; } 30 | 31 | /// 32 | /// Sets the known property spec. 33 | /// 34 | /// The known property spec. 35 | IDictionary KnownPropertySpec { set; } 36 | } 37 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/DynamicObject/Impromtu/ImpromptuForwarderAddRemove.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace RazorEngine.Compilation.ImpromptuInterface.Dynamic 7 | { 8 | class ImpromptuForwarderAddRemove 9 | { 10 | /// 11 | /// Implements the operator +. 12 | /// 13 | /// The left. 14 | /// The right. 15 | /// The result of the operator. 16 | public static ImpromptuForwarderAddRemove operator +(ImpromptuForwarderAddRemove left, object right) 17 | { 18 | left.Delegate = right; 19 | left.IsAdding = true; 20 | 21 | return left; 22 | } 23 | 24 | /// 25 | /// Implements the operator -. 26 | /// 27 | /// The left. 28 | /// The right. 29 | /// The result of the operator. 30 | public static ImpromptuForwarderAddRemove operator -(ImpromptuForwarderAddRemove left, object right) 31 | { 32 | left.Delegate = right; 33 | left.IsAdding = false; 34 | 35 | return left; 36 | } 37 | 38 | /// 39 | /// Gets or sets the delegate. 40 | /// 41 | /// The delegate. 42 | public object Delegate { get; protected set; } 43 | 44 | /// 45 | /// Gets or sets a value indicating whether this instance is adding. 46 | /// 47 | /// true if this instance is adding; otherwise, false. 48 | public bool IsAdding { get; protected set; } 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/DynamicObject/Impromtu/NonRecursiveInterfaceAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace RazorEngine.Compilation.ImpromptuInterface 7 | { 8 | /// 9 | /// Attribute on Inteface to stop proxy from recursively 10 | /// proxying other interfaces 11 | /// 12 | [AttributeUsage(System.AttributeTargets.Method | 13 | System.AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Property)] 14 | public class NonRecursiveInterfaceAttribute : Attribute 15 | { 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/DynamicObject/Impromtu/UseNamedArgumentAttribute.cs: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright 2011 Ekon Benefits 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | 16 | using System; 17 | using System.Collections.Generic; 18 | using System.Linq; 19 | using System.Text; 20 | 21 | namespace RazorEngine.Compilation.ImpromptuInterface 22 | { 23 | /// 24 | /// Attribute for Methods and Parameters on Custom Interfaces designed to be used with a dynamic implementation 25 | /// 26 | [AttributeUsage(System.AttributeTargets.Method 27 | | System.AttributeTargets.Parameter 28 | | System.AttributeTargets.Interface 29 | | System.AttributeTargets.Class)] 30 | public class UseNamedArgumentAttribute : Attribute 31 | { 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/HasDynamicModelAttribute.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Compilation 2 | { 3 | using System; 4 | 5 | /// 6 | /// Defines an attribute that marks the presence of a dynamic model in a template. 7 | /// 8 | [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)] 9 | public sealed class HasDynamicModelAttribute : Attribute 10 | { 11 | } 12 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/ICompilerService.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Compilation 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Reflection; 6 | 7 | using Inspectors; 8 | using RazorEngine.Compilation.ReferenceResolver; 9 | using System.Security; 10 | 11 | /// 12 | /// Defines the required contract for implementing a compiler service. 13 | /// 14 | public interface ICompilerService : IDisposable 15 | { 16 | #region Properties 17 | #if !RAZOR4 18 | /// 19 | /// Gets or sets the set of code inspectors. 20 | /// 21 | [Obsolete("This API is obsolete and will be removed in the next version (Razor4 doesn't use CodeDom for code-generation)!")] 22 | IEnumerable CodeInspectors { get; set; } 23 | #endif 24 | 25 | /// 26 | /// Gets or sets the reference resolver. 27 | /// 28 | IReferenceResolver ReferenceResolver { get; set; } 29 | 30 | /// 31 | /// Gets or sets whether the compiler service is operating in debug mode. 32 | /// 33 | bool Debug { get; set; } 34 | 35 | /// 36 | /// Gets or sets whether the compiler should load assemblies with Assembly.Load(byte[]) 37 | /// to prevent files from being locked. 38 | /// 39 | bool DisableTempFileLocking { get; set; } 40 | 41 | #endregion 42 | 43 | #region Methods 44 | /// 45 | /// Compiles the type defined in the specified type context. 46 | /// 47 | /// The type context which defines the type to compile. 48 | /// The compiled type. 49 | [SecurityCritical] 50 | Tuple CompileType(TypeContext context); 51 | 52 | /// 53 | /// Returns a set of assemblies that must be referenced by the compiled template. 54 | /// 55 | /// The set of assemblies. 56 | IEnumerable IncludeAssemblies(); 57 | #endregion 58 | } 59 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/ICompilerServiceFactory.cs: -------------------------------------------------------------------------------- 1 | using System.Security; 2 | namespace RazorEngine.Compilation 3 | { 4 | /// 5 | /// Defines the required contract for implementing a compiler service factory. 6 | /// 7 | public interface ICompilerServiceFactory 8 | { 9 | #region Methods 10 | /// 11 | /// Creates a that supports the specified language. 12 | /// 13 | /// The . 14 | /// An instance of . 15 | ICompilerService CreateCompilerService(Language language); 16 | #endregion 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/Inspectors/ICodeInspector.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Compilation.Inspectors 2 | { 3 | using System; 4 | using System.CodeDom; 5 | #if !RAZOR4 6 | /// 7 | /// Defines the required contract for implementing a code inspector. 8 | /// 9 | [Obsolete("This API is obsolete and will be removed in the next version (Razor4 doesn't use CodeDom for code-generation)!")] 10 | public interface ICodeInspector 11 | { 12 | #region Methods 13 | /// 14 | /// Inspects the specified code unit. 15 | /// 16 | /// The code unit. 17 | /// The code namespace declaration. 18 | /// The code type declaration. 19 | /// The code method declaration for the Execute method. 20 | void Inspect(CodeCompileUnit unit, CodeNamespace ns, CodeTypeDeclaration type, CodeMemberMethod executeMethod); 21 | #endregion 22 | } 23 | #endif 24 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/RazorEngineHost.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Compilation 2 | { 3 | using System; 4 | using System.Security; 5 | #if RAZOR4 6 | using Microsoft.AspNetCore.Razor; 7 | using Microsoft.AspNetCore.Razor.Parser; 8 | using OriginalRazorEngineHost = Microsoft.AspNetCore.Razor.RazorEngineHost; 9 | #else 10 | using System.Web.Razor; 11 | using System.Web.Razor.Parser; 12 | using OriginalRazorEngineHost = System.Web.Razor.RazorEngineHost; 13 | #endif 14 | 15 | /// 16 | /// Defines the custom razor engine host. 17 | /// 18 | #if NET45 // Razor 2 has [assembly: SecurityTransparent] 19 | [SecurityCritical] 20 | #endif 21 | public class RazorEngineHost : OriginalRazorEngineHost 22 | { 23 | #region Constructor 24 | /// 25 | /// Initialises a new instance of . 26 | /// 27 | /// The code language. 28 | /// The markup parser factory delegate. 29 | public RazorEngineHost(RazorCodeLanguage language, Func markupParserFactory) 30 | : base(language, markupParserFactory) { } 31 | #endregion 32 | 33 | #region Properties 34 | /// 35 | /// Gets or sets the default template type. 36 | /// 37 | public Type DefaultBaseTemplateType { get; set; } 38 | 39 | /// 40 | /// Gets or sets the default model type. 41 | /// 42 | public Type DefaultModelType { get; set; } 43 | 44 | #endregion 45 | 46 | #region Methods 47 | /// 48 | /// Decorates the code parser. 49 | /// 50 | /// The code parser. 51 | /// The decorated parser. 52 | #if NET45 // Razor 2 has [assembly: SecurityTransparent] 53 | [SecurityCritical] 54 | #endif 55 | public override ParserBase DecorateCodeParser(ParserBase incomingCodeParser) 56 | { 57 | if (incomingCodeParser is CSharpCodeParser) 58 | return new CSharp.CSharpCodeParser(); 59 | #if !RAZOR4 60 | if (incomingCodeParser is VBCodeParser) 61 | return new VisualBasic.VBCodeParser(); 62 | #endif 63 | 64 | return base.DecorateCodeParser(incomingCodeParser); 65 | } 66 | #endregion 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/ReferenceResolver/IReferenceResolver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Reflection; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace RazorEngine.Compilation.ReferenceResolver 10 | { 11 | /// 12 | /// Tries to resolve the references for a given compilation option. 13 | /// 14 | public interface IReferenceResolver 15 | { 16 | /// 17 | /// Resolve the reference for a compilation process. 18 | /// 19 | /// gives context about the compilation process. 20 | /// The references that should be included (requested by the compiler itself) 21 | /// the references which will be used in the compilation process. 22 | IEnumerable GetReferences(TypeContext context, IEnumerable includeAssemblies = null); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/ReferenceResolver/UseCurrentAssembliesReferenceResolver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace RazorEngine.Compilation.ReferenceResolver 9 | { 10 | /// 11 | /// Resolves the assemblies by using all currently loaded assemblies. See 12 | /// 13 | public class UseCurrentAssembliesReferenceResolver : IReferenceResolver 14 | { 15 | /// 16 | /// See 17 | /// 18 | /// 19 | /// 20 | /// 21 | public IEnumerable GetReferences(TypeContext context = null, IEnumerable includeAssemblies = null) 22 | { 23 | return CompilerServicesUtility 24 | .GetLoadedAssemblies() 25 | .Where(a => !a.IsDynamic && File.Exists(a.Location) && !a.Location.Contains(CompilerServiceBase.DynamicTemplateNamespace)) 26 | .GroupBy(a => a.GetName().Name).Select(grp => grp.First(y => y.GetName().Version == grp.Max(x => x.GetName().Version))) // only select distinct assemblies based on FullName to avoid loading duplicate assemblies 27 | .Select(a => CompilerReference.From(a)) 28 | .Concat(includeAssemblies ?? Enumerable.Empty()); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/RoslynCompilerServiceFactory.cs: -------------------------------------------------------------------------------- 1 | using RazorEngine.Compilation; 2 | using RazorEngine.Roslyn.CSharp; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Diagnostics.CodeAnalysis; 6 | using System.Linq; 7 | using System.Security; 8 | using System.Text; 9 | using System.Threading.Tasks; 10 | 11 | namespace RazorEngine.Roslyn 12 | { 13 | /// 14 | /// Provides a implementation of for the Roslyn implementation. 15 | /// 16 | [Serializable] 17 | public class RoslynCompilerServiceFactory : ICompilerServiceFactory 18 | { 19 | #region Methods 20 | /// 21 | /// Creates a that supports the specified language. 22 | /// 23 | /// The . 24 | /// An instance of . 25 | [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")] 26 | [SecuritySafeCritical] 27 | public ICompilerService CreateCompilerService(Language language) 28 | { 29 | switch (language) 30 | { 31 | case Language.CSharp: 32 | return new CSharpRoslynCompilerService(); 33 | 34 | case Language.VisualBasic: 35 | //#if RAZOR4 36 | throw new NotSupportedException("Razor4 doesn't support VB.net apparently."); 37 | //#else 38 | // return new VBRoslynCompilerService(); 39 | //#endif 40 | 41 | default: 42 | throw new ArgumentException("Unsupported language: " + language); 43 | } 44 | } 45 | #endregion 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/TypeContext.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Compilation 2 | { 3 | using RazorEngine.Templating; 4 | using ReferenceResolver; 5 | using System; 6 | using System.Collections.Generic; 7 | 8 | /// 9 | /// Defines a type context that describes a template to compile. 10 | /// 11 | public class TypeContext 12 | { 13 | private readonly Action> _addReferences; 14 | 15 | #region Constructor 16 | /// 17 | /// Initialises a new instance of . 18 | /// 19 | /// 20 | internal TypeContext(Action> addReferences) 21 | { 22 | if (addReferences == null) throw new ArgumentNullException(nameof(addReferences)); 23 | _addReferences = addReferences; 24 | ClassName = CompilerServicesUtility.GenerateClassName(); 25 | Namespaces = new HashSet(); 26 | } 27 | 28 | /// 29 | /// Creates a new TypeContext instance with the given classname and the given namespaces. 30 | /// 31 | /// 32 | /// 33 | internal TypeContext(string className, ISet namespaces) 34 | { 35 | _addReferences = refs => { }; 36 | ClassName = className; 37 | Namespaces = namespaces; 38 | } 39 | #endregion 40 | 41 | #region Properties 42 | /// 43 | /// Gets the class name. 44 | /// 45 | public string ClassName { get; private set; } 46 | 47 | /// 48 | /// Gets or sets the model type. 49 | /// 50 | public Type ModelType { get; set; } 51 | 52 | /// 53 | /// Gets the set of namespace imports. 54 | /// 55 | public ISet Namespaces { get; private set; } 56 | 57 | /// 58 | /// Gets or sets the template content. 59 | /// 60 | public ITemplateSource TemplateContent { get; set; } 61 | 62 | /// 63 | /// Gets or sets the base template type. 64 | /// 65 | public Type TemplateType { get; set; } 66 | 67 | /// 68 | /// Adds compiler references to the current dynamic assembly resolve list. 69 | /// 70 | /// the references to add to the dynamic resolve list. 71 | public void AddReferences(IEnumerable references) 72 | { 73 | _addReferences(references); 74 | } 75 | #endregion 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/VisualBasic/VBDirectCompilerService.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Compilation.VisualBasic 2 | { 3 | #if !RAZOR4 // no support for VB.net in Razor4? 4 | using System; 5 | using System.Diagnostics.CodeAnalysis; 6 | using System.Diagnostics.Contracts; 7 | #if RAZOR4 8 | using Microsoft.AspNet.Razor.Parser; 9 | #else 10 | using System.Web.Razor.Parser; 11 | #endif 12 | 13 | using Microsoft.VisualBasic; 14 | using System.Security; 15 | 16 | /// 17 | /// Defines a direct compiler service for the VB syntax. 18 | /// 19 | public class VBDirectCompilerService : DirectCompilerServiceBase 20 | { 21 | #region Constructor 22 | /// 23 | /// Initialises a new instance of . 24 | /// 25 | /// Specifies whether the strict mode parsing is enabled. 26 | /// The markup parser to use. 27 | [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Disposed in base class: DirectCompilerServiceBase")] 28 | [SecurityCritical] 29 | public VBDirectCompilerService(bool strictMode = true, Func markupParserFactory = null) 30 | : base( 31 | new VBRazorCodeLanguage(strictMode), 32 | new VBCodeProvider(), 33 | markupParserFactory) { } 34 | #endregion 35 | 36 | /// 37 | /// Extension of a source file without dot ("cs" for C# files or "vb" for VB.NET files). 38 | /// 39 | public override string SourceFileExtension 40 | { 41 | get 42 | { 43 | return "vb"; 44 | } 45 | } 46 | 47 | /// 48 | /// Builds a type name for the specified template type. 49 | /// 50 | /// The template type. 51 | /// The model type. 52 | /// The string type name (including namespace). 53 | [Pure] 54 | public override string BuildTypeName(Type templateType, Type modelType) 55 | { 56 | if (templateType == null) 57 | throw new ArgumentNullException("templateType"); 58 | 59 | var modelTypeName = CompilerServicesUtility.ResolveVBTypeName(modelType); 60 | return CompilerServicesUtility.VBCreateGenericType(templateType, modelTypeName, false); 61 | } 62 | 63 | } 64 | #endif 65 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/VisualBasic/VBRazorCodeGenerator.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Compilation.VisualBasic 2 | { 3 | #if !RAZOR4 // no support for VB.net in Razor4? 4 | using System.Security; 5 | using System.Web.Razor; 6 | using System.Web.Razor.Parser.SyntaxTree; 7 | using Templating; 8 | 9 | /// 10 | /// Defines a code generator that supports VB syntax. 11 | /// 12 | #if NET45 // Razor 2 has [assembly: SecurityTransparent] 13 | [SecurityCritical] 14 | #endif 15 | public class VBRazorCodeGenerator : System.Web.Razor.Generator.VBRazorCodeGenerator 16 | { 17 | #region Constructor 18 | /// 19 | /// Initializes a new instance of the class. 20 | /// 21 | /// Name of the class. 22 | /// Name of the root namespace. 23 | /// Name of the source file. 24 | /// The host. 25 | /// Flag to specify that this generator is running in struct mode. 26 | public VBRazorCodeGenerator(string className, string rootNamespaceName, string sourceFileName, RazorEngineHost host, bool strictMode) 27 | : base(className, rootNamespaceName, sourceFileName, host) 28 | { 29 | StrictMode = strictMode; 30 | } 31 | #endregion 32 | 33 | #region Properties 34 | /// 35 | /// Gets whether the code generator is running in strict mode. 36 | /// 37 | public bool StrictMode { get; private set; } 38 | #endregion 39 | 40 | #region Methods 41 | /// 42 | /// Visits an error generated through parsing. 43 | /// 44 | /// The error that was generated. 45 | #if NET45 // Razor 2 has [assembly: SecurityTransparent] 46 | [SecurityCritical] 47 | #endif 48 | public override void VisitError(RazorError err) 49 | { 50 | if (StrictMode) 51 | throw new TemplateParsingException(err.Message, err.Location.CharacterIndex, err.Location.LineIndex); 52 | } 53 | #endregion 54 | } 55 | #endif 56 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Compilation/VisualBasic/VBRazorCodeLanguage.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Compilation.VisualBasic 2 | { 3 | #if !RAZOR4 // no support for VB.net in Razor4? 4 | using System.Security; 5 | 6 | using System.Web.Razor; 7 | using System.Web.Razor.Generator; 8 | using OriginalVBRazorCodeLanguage = System.Web.Razor.VBRazorCodeLanguage; 9 | 10 | /// 11 | /// Provides a razor code language that supports the VB language. 12 | /// 13 | #if NET45 // Razor 2 has [assembly: SecurityTransparent] 14 | [SecurityCritical] 15 | #endif 16 | public class VBRazorCodeLanguage : OriginalVBRazorCodeLanguage 17 | { 18 | #region Constructor 19 | /// 20 | /// Initialises a new instance 21 | /// 22 | /// Flag to determine whether strict mode is enabled. 23 | public VBRazorCodeLanguage(bool strictMode) 24 | { 25 | StrictMode = strictMode; 26 | } 27 | #endregion 28 | 29 | #region Properties 30 | /// 31 | /// Gets whether strict mode is enabled. 32 | /// 33 | public bool StrictMode { get; private set; } 34 | #endregion 35 | 36 | #region Methods 37 | /// 38 | /// Creates the code generator. 39 | /// 40 | /// Name of the class. 41 | /// Name of the root namespace. 42 | /// Name of the source file. 43 | /// The host. 44 | /// An instance of . 45 | #if NET45 // Razor 2 has [assembly: SecurityTransparent] 46 | [SecurityCritical] 47 | #endif 48 | public override RazorCodeGenerator CreateCodeGenerator(string className, string rootNamespaceName, string sourceFileName, RazorEngineHost host) 49 | { 50 | return new VBRazorCodeGenerator(className, rootNamespaceName, sourceFileName, host, StrictMode); 51 | } 52 | #endregion 53 | } 54 | #endif 55 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Configuration/Xml/NamespaceConfigurationElement.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Configuration.Xml 2 | { 3 | using System.Configuration; 4 | 5 | /// 6 | /// Defines a configuration of a namespace. 7 | /// 8 | public class NamespaceConfigurationElement : ConfigurationElement 9 | { 10 | #region Fields 11 | private const string NamespaceAttribute = "namespace"; 12 | #endregion 13 | 14 | #region Properties 15 | /// 16 | /// Gets the namespace. 17 | /// 18 | [ConfigurationProperty(NamespaceAttribute, IsRequired = true)] 19 | public string Namespace 20 | { 21 | get { return (string)this[NamespaceAttribute]; } 22 | } 23 | #endregion 24 | } 25 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Configuration/Xml/NamespaceConfigurationElementCollection.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Configuration.Xml 2 | { 3 | using System.Configuration; 4 | 5 | /// 6 | /// Defines a collection of instances. 7 | /// 8 | [ConfigurationCollection(typeof(TemplateServiceConfigurationElement))] 9 | public class NamespaceConfigurationElementCollection : ConfigurationElementCollection 10 | { 11 | #region Methods 12 | /// 13 | /// Creates a new for use with the collection. 14 | /// 15 | /// The instance. 16 | protected override ConfigurationElement CreateNewElement() 17 | { 18 | return new NamespaceConfigurationElement(); 19 | } 20 | 21 | /// 22 | /// Gets a unique key for the specified element. 23 | /// 24 | /// The configuration element. 25 | /// The key for the element. 26 | protected override object GetElementKey(ConfigurationElement element) 27 | { 28 | return ((NamespaceConfigurationElement)element).Namespace; 29 | } 30 | #endregion 31 | } 32 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Configuration/Xml/TemplateServiceConfigurationElementCollection.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Configuration.Xml 2 | { 3 | using System.Configuration; 4 | 5 | /// 6 | /// Defines a collection of instances. 7 | /// 8 | [ConfigurationCollection(typeof(TemplateServiceConfigurationElement), AddItemName = "service")] 9 | public class TemplateServiceConfigurationElementCollection : ConfigurationElementCollection 10 | { 11 | #region Methods 12 | /// 13 | /// Creates a new for use with the collection. 14 | /// 15 | /// The instance. 16 | protected override ConfigurationElement CreateNewElement() 17 | { 18 | return new TemplateServiceConfigurationElement(); 19 | } 20 | 21 | /// 22 | /// Gets a unique key for the specified element. 23 | /// 24 | /// The configuration element. 25 | /// The key for the element. 26 | protected override object GetElementKey(ConfigurationElement element) 27 | { 28 | return ((TemplateServiceConfigurationElement)element).Name; 29 | } 30 | #endregion 31 | } 32 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Encoding.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine 2 | { 3 | /// 4 | /// Defines the possible values for encoding. 5 | /// 6 | public enum Encoding 7 | { 8 | /// 9 | /// Use html encoding. 10 | /// 11 | Html, 12 | 13 | /// 14 | /// Use raw text (no encoding) 15 | /// 16 | Raw 17 | } 18 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Engine.cs: -------------------------------------------------------------------------------- 1 | using RazorEngine.Templating; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Diagnostics.Contracts; 5 | using System.IO; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace RazorEngine 11 | { 12 | /// 13 | /// Provides quick access to the functionality of the class. 14 | /// 15 | public static class Engine 16 | { 17 | private static object _syncLock = new object(); 18 | private static IRazorEngineService _service; 19 | 20 | /// 21 | /// Quick access to RazorEngine. See . 22 | /// 23 | public static IRazorEngineService Razor 24 | { 25 | get 26 | { 27 | if (_service == null) 28 | { 29 | lock (_syncLock) 30 | { 31 | if (_service == null) 32 | { 33 | _service = RazorEngineService.Create(); 34 | } 35 | } 36 | } 37 | return _service; 38 | } 39 | set 40 | { 41 | _service = value; 42 | } 43 | } 44 | 45 | private static IRazorEngineService _isolatedService; 46 | 47 | /// 48 | /// Quick access to an isolated RazorEngine. See . 49 | /// 50 | public static IRazorEngineService IsolatedRazor 51 | { 52 | get 53 | { 54 | if (_isolatedService == null) 55 | { 56 | lock (_syncLock) 57 | { 58 | if (_isolatedService == null) 59 | { 60 | _isolatedService = IsolatedRazorEngineService.Create(); 61 | } 62 | } 63 | } 64 | return _isolatedService; 65 | } 66 | set 67 | { 68 | _isolatedService = value; 69 | } 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Language.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine 2 | { 3 | /// 4 | /// Defines the possible supported code languages. 5 | /// 6 | public enum Language 7 | { 8 | /// 9 | /// C# Language 10 | /// 11 | CSharp, 12 | 13 | /// 14 | /// Visual Basic Language 15 | /// 16 | VisualBasic 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Legacy/Templating/CachedTemplateItem.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Templating 2 | { 3 | using System; 4 | 5 | /// 6 | /// Defines a cached template item. 7 | /// 8 | [Obsolete("This class is no longer used.")] 9 | internal class CachedTemplateItem 10 | { 11 | #region Constructor 12 | /// 13 | /// Initialises a new instance of . 14 | /// 15 | /// The cached hash code. 16 | /// The template type. 17 | public CachedTemplateItem(int cachedHashCode, Type templateType) 18 | { 19 | CachedHashCode = cachedHashCode; 20 | TemplateType = templateType; 21 | } 22 | #endregion 23 | 24 | #region Properties 25 | /// 26 | /// Gets the cached hash code of the template. 27 | /// 28 | public int CachedHashCode { get; private set; } 29 | 30 | /// 31 | /// Gets the template type. 32 | /// 33 | public Type TemplateType { get; private set; } 34 | #endregion 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Legacy/Templating/DelegateTemplateResolver.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Templating 2 | { 3 | using System; 4 | using System.Diagnostics.Contracts; 5 | 6 | /// 7 | /// Provides an that supports delegated template resolution. 8 | /// 9 | [Obsolete("please use DelegateTemplateManager instead!")] 10 | public class DelegateTemplateResolver : ITemplateResolver 11 | { 12 | #region Fields 13 | private readonly Func _resolver; 14 | #endregion 15 | 16 | #region Constructor 17 | /// 18 | /// Initialises a new instance of . 19 | /// 20 | /// The resolver delegate. 21 | public DelegateTemplateResolver(Func resolver) 22 | { 23 | Contract.Requires(resolver != null); 24 | 25 | _resolver = resolver; 26 | } 27 | #endregion 28 | 29 | #region Methods 30 | /// 31 | /// Resolves a template. 32 | /// 33 | /// 34 | /// 35 | public string Resolve(string name) 36 | { 37 | return _resolver(name); 38 | } 39 | #endregion 40 | } 41 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Legacy/Templating/ITemplateResolver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace RazorEngine.Templating 3 | { 4 | /// 5 | /// Defines the required contract for implementing a template resolver. 6 | /// 7 | [Obsolete("Please use ITemplateManager instead")] 8 | public interface ITemplateResolver 9 | { 10 | #region Methods 11 | /// 12 | /// Resolves the template content with the specified name. 13 | /// 14 | /// The name of the template to resolve. 15 | /// The template content. 16 | string Resolve(string name); 17 | #endregion 18 | } 19 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Legacy/Templating/Parallel/DefaultParallelQueryPlan.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Templating.Parallel 2 | { 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | /// 7 | /// Represents a default parallel query plan. 8 | /// 9 | /// 10 | /// The uses the default 11 | /// result. The degree of parallelism by default is Math.Min(ProcessorCount, 64). 12 | /// 13 | /// The item type. 14 | public class DefaultParallelQueryPlan : IParallelQueryPlan 15 | { 16 | #region Methods 17 | /// 18 | /// Creates a parallel query for the specified source. 19 | /// 20 | /// The source enumerable. 21 | /// The parallel query. 22 | public ParallelQuery CreateQuery(IEnumerable source) 23 | { 24 | return source.AsParallel().AsOrdered(); 25 | } 26 | #endregion 27 | } 28 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Legacy/Templating/Parallel/IParallelQueryPlan.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Templating.Parallel 2 | { 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | /// 7 | /// Defines the required contract for implementing a parallel query plan. 8 | /// 9 | /// The item type. 10 | public interface IParallelQueryPlan 11 | { 12 | #region Methods 13 | /// 14 | /// Creates a parallel query for the specified source. 15 | /// 16 | /// The source enumerable. 17 | /// The parallel query. 18 | ParallelQuery CreateQuery(IEnumerable source); 19 | #endregion 20 | } 21 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | using System.Security; 5 | 6 | // General Information about an assembly is controlled through the following 7 | // set of attributes. Change these attribute values to modify the information 8 | // associated with an assembly. 9 | [assembly: AssemblyTitle("RazorEngine.Core")] 10 | [assembly: AssemblyDescription("RazorEngine - Core Framework")] 11 | 12 | // Setting ComVisible to false makes the types in this assembly not visible 13 | // to COM components. If you need to access a type in this assembly from 14 | // COM, set the ComVisible attribute to true on that type. 15 | [assembly: ComVisible(false)] 16 | 17 | // The following GUID is for the ID of the typelib if this project is exposed to COM 18 | [assembly: Guid("c11064c2-11b8-420b-81f0-4c6477cb5931")] 19 | 20 | // Allow the test assembly access to internals. 21 | [assembly: InternalsVisibleTo("Test.RazorEngine.Core, PublicKey=0024000004800000940000000602000000240000525341310004000001000100ad3b3604eb9ba317840ece0a65ec22fa67ee54cb4abb5148f184a90d9e9cdbc77c098fe3447ce9e13ef73d3e046016e7053f4c5c0ccd9f521514200dd09aa12cedc63bf39c30eb0516ac6b42bb645dfd41902290a87ceaf0309a9f08bfdd9cceb27b6186bfbe68ca91dca2508820c0723b0e4d94f3ef8049b8aa3f524d4715ca")] 22 | [assembly: InternalsVisibleTo("Test.RazorEngine.Core.Roslyn, PublicKey=0024000004800000940000000602000000240000525341310004000001000100ad3b3604eb9ba317840ece0a65ec22fa67ee54cb4abb5148f184a90d9e9cdbc77c098fe3447ce9e13ef73d3e046016e7053f4c5c0ccd9f521514200dd09aa12cedc63bf39c30eb0516ac6b42bb645dfd41902290a87ceaf0309a9f08bfdd9cceb27b6186bfbe68ca91dca2508820c0723b0e4d94f3ef8049b8aa3f524d4715ca")] 23 | 24 | [assembly: AllowPartiallyTrustedCallers] 25 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/RazorEngine.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Antaris/RazorEngine/aa5dd2661fe040ce39020949ebd33d98a530ebe4/src/source/RazorEngine.Core/RazorEngine.snk -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/TaskRunner.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Security; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace RazorEngine 9 | { 10 | /// 11 | /// Helper for missing net40 methods, REMOVE me when we are net45 only. 12 | /// 13 | internal class TaskRunner 14 | { 15 | /// 16 | /// Runs the given delegate in a new task (like Task.Run but works on net40). 17 | /// 18 | /// 19 | /// 20 | /// 21 | public static Task Run(Func t) 22 | { 23 | #if NET40 24 | var task = new Task(t); 25 | task.Start(); 26 | return task; 27 | #else 28 | return Task.Run(t); 29 | #endif 30 | } 31 | 32 | /// 33 | /// Runs the given delegate in a new task (like Task.Run but works on net40). 34 | /// 35 | /// 36 | /// 37 | public static Task Run(Action t) 38 | { 39 | #if NET40 40 | var task = new Task(t); 41 | task.Start(); 42 | return task; 43 | #else 44 | return Task.Run(t); 45 | #endif 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/BaseTemplateKey.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace RazorEngine.Templating 7 | { 8 | /// 9 | /// A base implementation for . 10 | /// You only need to provide the 11 | /// implementation which depends on the implementation. 12 | /// 13 | [Serializable] 14 | public abstract class BaseTemplateKey : ITemplateKey 15 | { 16 | /// 17 | /// See . 18 | /// 19 | readonly string _name; 20 | /// 21 | /// See . 22 | /// 23 | readonly ResolveType _resolveType; 24 | /// 25 | /// See . 26 | /// 27 | readonly ITemplateKey _context; 28 | 29 | /// 30 | /// Create a new instance. 31 | /// 32 | /// See 33 | /// See 34 | /// See 35 | public BaseTemplateKey(string name, ResolveType resolveType, ITemplateKey context) 36 | { 37 | _name = name; 38 | _resolveType = resolveType; 39 | _context = context; 40 | } 41 | /// 42 | /// See . 43 | /// 44 | public string Name 45 | { 46 | get { return _name; } 47 | } 48 | 49 | /// 50 | /// See . 51 | /// 52 | public ResolveType TemplateType 53 | { 54 | get { return _resolveType; } 55 | } 56 | 57 | /// 58 | /// See . 59 | /// 60 | public ITemplateKey Context 61 | { 62 | get { return _context; } 63 | } 64 | 65 | /// 66 | /// See . 67 | /// 68 | public abstract string GetUniqueKeyString(); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/CompiledTemplate.cs: -------------------------------------------------------------------------------- 1 | using RazorEngine.Compilation; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Reflection; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace RazorEngine.Templating 10 | { 11 | /// 12 | /// A simple readonly implementation of . 13 | /// 14 | internal class CompiledTemplate : ICompiledTemplate 15 | { 16 | private readonly CompilationData _tempFiles; 17 | private readonly ITemplateSource _source; 18 | private readonly ITemplateKey _key; 19 | private readonly Type _templateType; 20 | private readonly Type _modelType; 21 | 22 | public CompiledTemplate(CompilationData tempFiles, ITemplateKey key, ITemplateSource source, Type templateType, Type modelType) 23 | { 24 | _tempFiles = tempFiles; 25 | _key = key; 26 | _source = source; 27 | _templateType = templateType; 28 | _modelType = modelType; 29 | } 30 | 31 | public CompilationData CompilationData 32 | { 33 | get { return _tempFiles; } 34 | } 35 | 36 | public ITemplateKey Key 37 | { 38 | get { return _key; } 39 | } 40 | 41 | public ITemplateSource Template 42 | { 43 | get { return _source; } 44 | } 45 | 46 | public Type TemplateType 47 | { 48 | get { return _templateType; } 49 | } 50 | 51 | public Assembly TemplateAssembly 52 | { 53 | get { return _templateType.Assembly; } 54 | } 55 | 56 | public Type ModelType 57 | { 58 | get { return _modelType; } 59 | } 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/DefaultActivator.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Templating 2 | { 3 | using System; 4 | using System.Diagnostics.Contracts; 5 | 6 | /// 7 | /// Provides a default implementation of an . 8 | /// 9 | internal class DefaultActivator : IActivator 10 | { 11 | #region Methods 12 | /// 13 | /// Creates an instance of the specifed template. 14 | /// 15 | /// The instance context. 16 | /// An instance of . 17 | [Pure] 18 | public ITemplate CreateInstance(InstanceContext context) 19 | { 20 | if (context == null) 21 | throw new ArgumentNullException("context"); 22 | 23 | return context.Loader.CreateInstance(context.TemplateType); 24 | } 25 | #endregion 26 | } 27 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/DefaultAppDomainFactory.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Templating 2 | { 3 | using System; 4 | 5 | /// 6 | /// Provides a default implementation of an factory. 7 | /// 8 | public class DefaultAppDomainFactory : IAppDomainFactory 9 | { 10 | #region Methods 11 | /// 12 | /// Creates the . 13 | /// 14 | /// The instance. 15 | public AppDomain CreateAppDomain() 16 | { 17 | var current = AppDomain.CurrentDomain; 18 | var domain = AppDomain.CreateDomain("RazorHost", current.Evidence, current.SetupInformation); 19 | 20 | return domain; 21 | } 22 | #endregion 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/DelegateActivator.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Templating 2 | { 3 | using System; 4 | using System.Diagnostics.Contracts; 5 | 6 | /// 7 | /// Defines an activator that supports delegated activation. 8 | /// 9 | internal class DelegateActivator : IActivator 10 | { 11 | #region Fields 12 | private readonly Func _activator; 13 | #endregion 14 | 15 | #region Constructor 16 | /// 17 | /// Initialises a new instance of . 18 | /// 19 | /// The delegated used to create an instance of the template. 20 | public DelegateActivator(Func activator) 21 | { 22 | Contract.Requires(activator != null); 23 | 24 | _activator = activator; 25 | } 26 | #endregion 27 | 28 | #region Properties 29 | /// 30 | /// Gets the activator. 31 | /// 32 | internal Func Activator { get { return _activator; } } 33 | #endregion 34 | 35 | #region Methods 36 | /// 37 | /// Creates an instance of the specifed template. 38 | /// 39 | /// The instance context. 40 | /// An instance of . 41 | [Pure] 42 | public ITemplate CreateInstance(InstanceContext context) 43 | { 44 | return _activator(context); 45 | } 46 | #endregion 47 | } 48 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/DelegateAppDomainFactory.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Templating 2 | { 3 | using System; 4 | using System.Diagnostics.Contracts; 5 | 6 | /// 7 | /// Provides an factory that supports delegated creation. 8 | /// 9 | internal class DelegateAppDomainFactory : IAppDomainFactory 10 | { 11 | #region Fields 12 | private readonly Func _factory; 13 | #endregion 14 | 15 | #region Constructor 16 | /// 17 | /// Initialises a new instance of . 18 | /// 19 | /// The factory delegate. 20 | public DelegateAppDomainFactory(Func factory) 21 | { 22 | Contract.Requires(factory != null); 23 | 24 | _factory = factory; 25 | } 26 | #endregion 27 | 28 | #region Methods 29 | /// 30 | /// Creates the . 31 | /// 32 | /// The instance. 33 | public AppDomain CreateAppDomain() 34 | { 35 | return _factory(); 36 | } 37 | #endregion 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/EmbeddedResourceTemplateManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | 4 | namespace RazorEngine.Templating 5 | { 6 | /// 7 | /// A TemplateManager loading templates from embedded resources. 8 | /// 9 | public class EmbeddedResourceTemplateManager : ITemplateManager 10 | { 11 | /// 12 | /// Initializes a new TemplateManager. 13 | /// 14 | /// The type from the assembly that contains embedded resources that will act as a root type for Assembly.GetManifestResourceStream() calls. 15 | public EmbeddedResourceTemplateManager(Type rootType) 16 | { 17 | if (rootType == null) 18 | throw new ArgumentNullException(nameof(rootType)); 19 | 20 | this.RootType = rootType; 21 | } 22 | 23 | /// 24 | /// The type from the assembly that contains embedded resources 25 | /// 26 | public Type RootType { get; } 27 | 28 | /// 29 | /// Resolve the given key 30 | /// 31 | /// 32 | /// 33 | public ITemplateSource Resolve(ITemplateKey key) 34 | { 35 | using (var stream = this.RootType.Assembly.GetManifestResourceStream(this.RootType, key.Name + ".cshtml")) 36 | { 37 | if(stream == null) 38 | throw new TemplateLoadingException(string.Format("Couldn't load resource '{0}.{1}.cshtml' from assembly {2}", this.RootType.Namespace, key.Name, this.RootType.Assembly.FullName)); 39 | 40 | using (var reader = new StreamReader(stream)) 41 | { 42 | return new LoadedTemplateSource(reader.ReadToEnd()); 43 | } 44 | } 45 | } 46 | 47 | /// 48 | /// Get the given key. 49 | /// 50 | /// 51 | /// 52 | /// 53 | /// 54 | public ITemplateKey GetKey(string name, ResolveType resolveType, ITemplateKey context) 55 | { 56 | return new NameOnlyTemplateKey(name, resolveType, context); 57 | } 58 | 59 | /// 60 | /// Throws NotSupportedException. 61 | /// 62 | /// 63 | /// 64 | public void AddDynamic(ITemplateKey key, ITemplateSource source) 65 | { 66 | throw new NotSupportedException("Adding templates dynamically is not supported. This Manager only supports embedded resources."); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/FullPathWithModifiedTimeTemplateKey.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace RazorEngine.Templating 8 | { 9 | /// 10 | /// This implementation adds ModifiedTime property to 11 | /// 12 | [Serializable] 13 | public class FullPathWithModifiedTimeTemplateKey : FullPathTemplateKey 14 | { 15 | /// 16 | /// Initializes a new instance 17 | /// 18 | /// 19 | /// 20 | /// 21 | /// 22 | /// 23 | public FullPathWithModifiedTimeTemplateKey(string name, string fullPath, DateTime modifiedTime, ResolveType resolveType, ITemplateKey context) 24 | : base(name, fullPath, resolveType, context) 25 | { 26 | ModifiedTime = modifiedTime; 27 | } 28 | 29 | /// 30 | /// This value is used to check if cache is valid 31 | /// 32 | public DateTime ModifiedTime { get; set; } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/HtmlTemplateBaseOfT.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Templating 2 | { 3 | using System.Diagnostics; 4 | using System.IO; 5 | 6 | /// 7 | /// Provides a base implementation of an html template with a model. 8 | /// 9 | /// 10 | /// This type does not currently serve a purpose, and the WriteAttribute* API has been migrated to the TemplateBase type. This type is not deprecated, as it 11 | /// may form the basis for a future template that supports MVC like @Html syntax. 12 | /// 13 | /// The model type. 14 | public class HtmlTemplateBase : TemplateBase { } 15 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/IActivator.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Templating 2 | { 3 | /// 4 | /// Defines the required contract for implementing an activator. 5 | /// 6 | public interface IActivator 7 | { 8 | #region Methods 9 | /// 10 | /// Creates an instance of the specifed template. 11 | /// 12 | /// The instance context. 13 | /// An instance of . 14 | ITemplate CreateInstance(InstanceContext context); 15 | #endregion 16 | } 17 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/IAppDomainFactory.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Templating 2 | { 3 | using System; 4 | 5 | /// 6 | /// Defines the required contract for implementing an factory. 7 | /// 8 | public interface IAppDomainFactory 9 | { 10 | #region Methods 11 | /// 12 | /// Creates the . 13 | /// 14 | /// The instance. 15 | AppDomain CreateAppDomain(); 16 | #endregion 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/ICachingProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace RazorEngine.Templating 3 | { 4 | /// 5 | /// This interface represents the caching layer. 6 | /// 7 | public interface ICachingProvider : IDisposable 8 | { 9 | /// 10 | /// Request that a given template should be cached. 11 | /// 12 | /// The template to be cached. 13 | /// The key of the template. 14 | void CacheTemplate(ICompiledTemplate template, ITemplateKey key); 15 | 16 | /// 17 | /// Try to resolve a template within the cache. 18 | /// 19 | /// the key of the template. 20 | /// the model-type of the template. 21 | /// the resolved template 22 | /// true if a template was found. 23 | /// 24 | /// Implementations MUST decide if they allow multiple model-types for the 25 | /// same template key and SHOULD throw a exception when a template is requested with the wrong type! 26 | /// 27 | bool TryRetrieveTemplate(ITemplateKey key, Type modelType, out ICompiledTemplate template); 28 | 29 | /// 30 | /// Every caching provider must manage a instance. 31 | /// This instance makes sure that all assemblies can be resolved properly. 32 | /// 33 | TypeLoader TypeLoader { get; } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/ICompiledTemplate.cs: -------------------------------------------------------------------------------- 1 | using RazorEngine.Compilation; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.IO; 5 | using System.Linq; 6 | using System.Reflection; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace RazorEngine.Templating 11 | { 12 | /// 13 | /// Represents a compiled template. 14 | /// 15 | public interface ICompiledTemplate 16 | { 17 | /// 18 | /// The key for the template (used for resolving the source code). 19 | /// 20 | ITemplateKey Key { get; } 21 | 22 | /// 23 | /// The source of the template (ie the source code). 24 | /// 25 | ITemplateSource Template { get; } 26 | 27 | /// 28 | /// All temporary information about the compilation. 29 | /// 30 | CompilationData CompilationData { get; } 31 | 32 | /// 33 | /// The actual Type object of the generated template. 34 | /// 35 | Type TemplateType { get; } 36 | 37 | /// 38 | /// The generated assembly of the template. 39 | /// 40 | Assembly TemplateAssembly { get; } 41 | 42 | /// 43 | /// The type of the model (null = dynamic). 44 | /// 45 | Type ModelType { get; } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/IInternalTemplateService.cs: -------------------------------------------------------------------------------- 1 | using RazorEngine.Configuration; 2 | using RazorEngine.Text; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace RazorEngine.Templating 10 | { 11 | /// 12 | /// A internal contract for the class. 13 | /// 14 | public interface IInternalTemplateService 15 | { 16 | /// 17 | /// Gets the template service configuration. 18 | /// 19 | ITemplateServiceConfiguration Configuration { get; } 20 | 21 | /// 22 | /// Gets the encoded string factory. 23 | /// 24 | IEncodedStringFactory EncodedStringFactory { get; } 25 | 26 | /// 27 | /// Resolves the template, this is for internal use only 28 | /// 29 | /// 30 | /// 31 | /// 32 | /// 33 | /// 34 | /// 35 | ITemplate Resolve(string name, object model, Type modelType, DynamicViewBag viewbag, ResolveType resolveType); 36 | 37 | /// 38 | /// Adds a namespace that will be imported into the template. 39 | /// 40 | /// The namespace to be imported. 41 | void AddNamespace(string ns); 42 | 43 | /// 44 | /// Creates a new used to tracking templates. 45 | /// 46 | /// The instance of 47 | ExecuteContext CreateExecuteContext(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/IRazorEngineCore.cs: -------------------------------------------------------------------------------- 1 | using RazorEngine.Configuration; 2 | using System; 3 | using System.IO; 4 | namespace RazorEngine.Templating 5 | { 6 | #if DISABLED 7 | /// 8 | /// Defines an internal contract for compiling and running templates without caching. 9 | /// 10 | internal interface IRazorEngineCore 11 | { 12 | /// 13 | /// Gets the template service configuration. 14 | /// 15 | ITemplateServiceConfiguration Configuration { get; } 16 | 17 | /// 18 | /// Gets a given key from the implementation. 19 | /// See . 20 | /// 21 | /// 22 | /// 23 | /// 24 | /// 25 | ITemplateKey GetKey(string name, ResolveType resolveType = ResolveType.Global, ITemplateKey context = null); 26 | 27 | /// 28 | /// Compiles the specified template. 29 | /// 30 | /// The string template key. 31 | /// The model type. 32 | ICompiledTemplate Compile(ITemplateKey key, Type modelType); 33 | 34 | /// 35 | /// Runs the given compiled template. 36 | /// 37 | /// The compiled template. 38 | /// 39 | /// The model instance or NULL if no model exists. 40 | /// 41 | void RunTemplate(ICompiledTemplate template, TextWriter writer, object model, DynamicViewBag viewBag); 42 | } 43 | #endif 44 | } 45 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/ITemplate.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Threading.Tasks; 4 | namespace RazorEngine.Templating 5 | { 6 | /// 7 | /// Defines the required contract for implementing a template. 8 | /// 9 | public interface ITemplate 10 | { 11 | #region Properties 12 | /// 13 | /// Sets the internal template service. 14 | /// 15 | IInternalTemplateService InternalTemplateService { set; } 16 | 17 | /// 18 | /// OBSOLETE: Sets the template service. 19 | /// 20 | [Obsolete("Only provided for backwards compatibility, use RazorEngine instead.")] 21 | ITemplateService TemplateService { set; } 22 | #if RAZOR4 23 | #else 24 | /// 25 | /// Sets the cached template service. 26 | /// 27 | [Obsolete("Use the Razor property instead, this is obsolete as it makes it difficult to use the RazorEngine namespace within templates.")] 28 | IRazorEngineService RazorEngine { set; } 29 | #endif 30 | /// 31 | /// Sets the cached template service. 32 | /// 33 | IRazorEngineService Razor { set; } 34 | #endregion 35 | 36 | #region Methods 37 | /// 38 | /// Set the model of the template (if applicable). 39 | /// 40 | /// 41 | /// 42 | void SetData(object model, DynamicViewBag viewbag); 43 | 44 | /// 45 | /// Executes the compiled template. 46 | /// 47 | #if RAZOR4 48 | Task Execute(); 49 | #else 50 | void Execute(); 51 | #endif 52 | 53 | /// 54 | /// Runs the template and returns the result. 55 | /// 56 | /// The current execution context. 57 | /// 58 | /// The merged result of the template. 59 | #if RAZOR4 60 | Task Run(ExecuteContext context, TextWriter writer); 61 | #else 62 | void Run(ExecuteContext context, TextWriter writer); 63 | #endif 64 | 65 | /// 66 | /// Writes the specified object to the result. 67 | /// 68 | /// The value to write. 69 | void Write(object value); 70 | 71 | /// 72 | /// Writes the specified string to the result. 73 | /// 74 | /// The literal to write. 75 | void WriteLiteral(string literal); 76 | #endregion 77 | } 78 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/ITemplateKey.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace RazorEngine.Templating 8 | { 9 | /// 10 | /// With a template key a template can be resolved and loaded. 11 | /// Implementations of this interface are provided along with the ITemplateManager implementation. 12 | /// See for a base implementation. 13 | /// 14 | public interface ITemplateKey 15 | { 16 | /// 17 | /// The name of the template (ie. when used in a @Include) 18 | /// 19 | string Name { get; } 20 | 21 | /// 22 | /// The layer where the template is to be resolved. 23 | /// 24 | ResolveType TemplateType { get; } 25 | 26 | /// 27 | /// The context where the template is to be resolved (ie the parent template). 28 | /// 29 | ITemplateKey Context { get; } 30 | 31 | /// 32 | /// Gets a unique string which can be used as key by the caching layer. 33 | /// 34 | /// 35 | string GetUniqueKeyString(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/ITemplateManager.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Templating 2 | { 3 | /// 4 | /// Defines the required contract for implementing a template-manager. 5 | /// 6 | public interface ITemplateManager 7 | { 8 | #region Methods 9 | /// 10 | /// Resolves the template with the specified key. 11 | /// 12 | /// The key which should be resolved to a template source. 13 | /// The template content. 14 | ITemplateSource Resolve(ITemplateKey key); 15 | 16 | /// 17 | /// Get the key of a template. 18 | /// This method has to be implemented so that the manager can control the implementation. 19 | /// This way the cache api can rely on the unique string given by . 20 | /// 21 | /// 22 | /// For example one template manager reads all template from a single folder, then the can simply return the template name. 23 | /// Another template manager can read from different folders depending whether we include a layout or including a template. 24 | /// In that situation the has to take that into account so that templates with the same name can not be confused. 25 | /// 26 | /// The name of the template 27 | /// how the template is resolved 28 | /// gets the context for the current resolve operation. 29 | /// Which template is resolving another template? (null = we search a global template) 30 | /// 31 | /// the key for the template 32 | ITemplateKey GetKey(string name, ResolveType resolveType, ITemplateKey context); 33 | 34 | /// 35 | /// Adds a template dynamically to the current manager. 36 | /// 37 | /// 38 | /// 39 | void AddDynamic(ITemplateKey key, ITemplateSource source); 40 | 41 | #endregion 42 | } 43 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/ITemplateOfT.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Templating 2 | { 3 | /// 4 | /// Defines the required contract for implementing a template with a model. 5 | /// 6 | /// The model type. 7 | public interface ITemplate : ITemplate 8 | { 9 | #region Properties 10 | /// 11 | /// Gets the or sets the model. 12 | /// 13 | T Model { get; set; } 14 | #endregion 15 | } 16 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/ITemplateRunnerOfTModel.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | 3 | namespace RazorEngine.Templating 4 | { 5 | /// 6 | /// Defines the required contract for implementing a typed, runnable template reference. 7 | /// 8 | /// The model type 9 | public interface ITemplateRunner 10 | { 11 | /// 12 | /// Runs the template using the specified . 13 | /// 14 | /// 15 | /// 16 | /// 17 | void Run(TModel model, TextWriter textWriter, DynamicViewBag viewBag = null); 18 | } 19 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/ITemplateSource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace RazorEngine.Templating 9 | { 10 | /// 11 | /// Represents a template source (ie the source code of a template). 12 | /// 13 | public interface ITemplateSource 14 | { 15 | /// 16 | /// When not null this file is used for debugging the template. 17 | /// 18 | string TemplateFile { get; } 19 | 20 | /// 21 | /// The source code of the template. 22 | /// 23 | string Template { get; } 24 | 25 | /// 26 | /// Get a reader to read the template. 27 | /// 28 | /// 29 | TextReader GetTemplateReader(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/InstanceContext.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Templating 2 | { 3 | using System; 4 | using System.Diagnostics.Contracts; 5 | 6 | /// 7 | /// Defines contextual information for a template instance. 8 | /// 9 | public class InstanceContext 10 | { 11 | #region Constructor 12 | /// 13 | /// Initialises a new instance of . 14 | /// 15 | /// The type loader. 16 | /// The template type. 17 | internal InstanceContext(TypeLoader loader, Type templateType) 18 | { 19 | Contract.Requires(loader != null); 20 | Contract.Requires(templateType != null); 21 | 22 | Loader = loader; 23 | TemplateType = templateType; 24 | } 25 | #endregion 26 | 27 | #region Properties 28 | /// 29 | /// Gets the type loader. 30 | /// 31 | public TypeLoader Loader { get; private set; } 32 | 33 | /// 34 | /// Gets the template type. 35 | /// 36 | public Type TemplateType { get; private set; } 37 | #endregion 38 | } 39 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/NameOnlyTemplateKey.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace RazorEngine.Templating 8 | { 9 | /// 10 | /// A simple implementation inheriting from . 11 | /// This implementation assumes that the template-names are unique and returns the name as unique key. 12 | /// (So this implementation is used by and . 13 | /// 14 | [Serializable] 15 | public class NameOnlyTemplateKey : BaseTemplateKey 16 | { 17 | /// 18 | /// Initializes a new instance of the class. 19 | /// 20 | /// 21 | /// 22 | /// 23 | public NameOnlyTemplateKey(string name, ResolveType resolveType, ITemplateKey context) 24 | : base(name, resolveType, context) { } 25 | 26 | /// 27 | /// Returns the name. 28 | /// 29 | /// 30 | public override string GetUniqueKeyString() 31 | { 32 | return this.Name; 33 | } 34 | 35 | /// 36 | /// Checks if the names are equal. 37 | /// 38 | /// 39 | /// 40 | public override bool Equals(object obj) 41 | { 42 | var other = obj as NameOnlyTemplateKey; 43 | if (object.ReferenceEquals(null, other)) 44 | { 45 | return false; 46 | } 47 | return other.Name == Name; 48 | } 49 | 50 | /// 51 | /// Returns a hashcode for the current instance. 52 | /// 53 | /// 54 | public override int GetHashCode() 55 | { 56 | return this.Name.GetHashCode(); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/RazorEngineCoreWithCache.cs: -------------------------------------------------------------------------------- 1 | using RazorEngine.Configuration; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace RazorEngine.Templating 9 | { 10 | internal class RazorEngineCoreWithCache : RazorEngineCore 11 | { 12 | internal RazorEngineCoreWithCache(ReadOnlyTemplateServiceConfiguration config, RazorEngineService cached) 13 | : base(config, cached) 14 | { 15 | } 16 | 17 | internal override ITemplate ResolveInternal(string cacheName, object model, Type modelType, DynamicViewBag viewbag, ResolveType resolveType, ITemplateKey context) 18 | { 19 | var templateKey = GetKey(cacheName, resolveType, context); 20 | ICompiledTemplate compiledTemplate; 21 | if (!Configuration.CachingProvider.TryRetrieveTemplate(templateKey, modelType, out compiledTemplate)) 22 | { 23 | compiledTemplate = Compile(templateKey, modelType); 24 | Configuration.CachingProvider.CacheTemplate(compiledTemplate, templateKey); 25 | } 26 | return CreateTemplate(compiledTemplate, model, viewbag); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/RequireNamespacesAttribute.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Templating 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | /// 7 | /// Allows base templates to define require template imports when 8 | /// generating templates. 9 | /// 10 | [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] 11 | public sealed class RequireNamespacesAttribute : Attribute 12 | { 13 | #region Constructor 14 | /// 15 | /// Initialises a new instance of . 16 | /// 17 | /// The set of required namespace imports. 18 | public RequireNamespacesAttribute(params string[] namespaces) 19 | { 20 | if (namespaces == null) 21 | throw new ArgumentNullException("namespaces"); 22 | 23 | var set = new HashSet(); 24 | foreach (string ns in namespaces) 25 | set.Add(ns); 26 | 27 | Namespaces = set; 28 | } 29 | #endregion 30 | 31 | #region Properties 32 | /// 33 | /// Gets the set of required namespace imports. 34 | /// 35 | public IEnumerable Namespaces { get; private set; } 36 | #endregion 37 | } 38 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/ResolvePathCheckModifiedTimeTemplateManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace RazorEngine.Templating 9 | { 10 | /// 11 | /// A TemplateManager resolving remplates by path, given a list of folders to look into. 12 | /// Uses to save template modification time. 13 | /// 14 | public class ResolvePathCheckModifiedTimeTemplateManager : ResolvePathTemplateManager, ITemplateManager 15 | { 16 | /// 17 | /// Initializes a new TemplateManager 18 | /// 19 | /// the list of folders to look for templates. 20 | public ResolvePathCheckModifiedTimeTemplateManager(IEnumerable layoutRoots) 21 | : base(layoutRoots) 22 | { 23 | } 24 | 25 | /// 26 | /// Get the given key. 27 | /// 28 | /// 29 | /// 30 | /// 31 | /// 32 | public new ITemplateKey GetKey(string name, ResolveType resolveType, ITemplateKey context) 33 | { 34 | var fullPath = ResolveFilePath(name); 35 | var modifiedTime = File.GetLastWriteTimeUtc(fullPath); 36 | 37 | return new FullPathWithModifiedTimeTemplateKey(name, fullPath, modifiedTime, resolveType, context); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/ResolveType.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace RazorEngine.Templating 8 | { 9 | /// 10 | /// The type of a resolve action. 11 | /// 12 | public enum ResolveType 13 | { 14 | /// 15 | /// When we search for a template in as part of TemplateService. 16 | /// 17 | Global, 18 | /// 19 | /// When we search for a template which is included. 20 | /// 21 | Include, 22 | /// 23 | /// When we search for a layout template. 24 | /// 25 | Layout 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/TemplateLoadingException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace RazorEngine.Templating 8 | { 9 | /// 10 | /// Happens when we could compile the template, 11 | /// but are unable to load the resulting assembly! 12 | /// 13 | public class TemplateLoadingException : Exception 14 | { 15 | /// 16 | /// Initialises a new instance of . 17 | /// 18 | /// The message. 19 | internal TemplateLoadingException(string message) 20 | : base(message) { } 21 | /// 22 | /// Initialises a new instance of . 23 | /// 24 | /// The message. 25 | /// The root cause. 26 | internal TemplateLoadingException(string message, Exception inner) 27 | : base(message, inner) { } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/TemplateRunnerExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | 3 | namespace RazorEngine.Templating 4 | { 5 | /// 6 | /// Extensions for the . 7 | /// 8 | public static class TemplateRunnerExtensions 9 | { 10 | /// 11 | /// Runs the template using the specified . 12 | /// 13 | /// 14 | /// 15 | /// 16 | /// 17 | public static string Run(this ITemplateRunner templateRunner, TModel model, DynamicViewBag viewBag = null) 18 | { 19 | using (var textWriter = new StringWriter()) 20 | { 21 | templateRunner.Run(model, textWriter, viewBag); 22 | return textWriter.ToString(); 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/TemplateRunnerOfTModel.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | 3 | namespace RazorEngine.Templating 4 | { 5 | /// 6 | /// Typed, runnable template reference. 7 | /// 8 | /// The model type 9 | internal class TemplateRunner : ITemplateRunner 10 | { 11 | private readonly IRazorEngineService _service; 12 | private readonly ITemplateKey _key; 13 | 14 | /// 15 | /// Initialises a new instance of . 16 | /// 17 | /// 18 | /// 19 | public TemplateRunner(IRazorEngineService service, ITemplateKey key) 20 | { 21 | _service = service; 22 | _key = key; 23 | } 24 | 25 | /// 26 | /// Runs the template using the specified . 27 | /// 28 | /// 29 | /// 30 | /// 31 | public void Run(TModel model, TextWriter textWriter, DynamicViewBag viewBag = null) 32 | { 33 | _service.Run(_key, textWriter, typeof(TModel), model, viewBag); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/TemplateSource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | namespace RazorEngine.Templating 8 | { 9 | /// 10 | /// A simple implementation which represents an in-memory string. 11 | /// 12 | [Serializable] 13 | public class LoadedTemplateSource : ITemplateSource 14 | { 15 | private readonly string _template; 16 | private readonly string _templateFile; 17 | 18 | /// 19 | /// Initializes a new instance. 20 | /// 21 | /// 22 | /// 23 | public LoadedTemplateSource(string template, string templateFile = null) 24 | { 25 | this._template = template; 26 | this._templateFile = templateFile; 27 | } 28 | 29 | /// 30 | /// The in-memory template sourcecode. 31 | /// 32 | public string Template 33 | { 34 | get { return _template; } 35 | } 36 | 37 | /// 38 | /// The template file or null if none exists. 39 | /// 40 | public string TemplateFile 41 | { 42 | get { return _templateFile; } 43 | } 44 | 45 | /// 46 | /// Creates a new to read the in-memory stream. 47 | /// 48 | /// 49 | public TextReader GetTemplateReader() 50 | { 51 | return new StringReader(_template); 52 | } 53 | } 54 | 55 | // FileTemplateSource? 56 | } 57 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Templating/TemplateWriter.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Templating 2 | { 3 | using System; 4 | using System.Diagnostics.Contracts; 5 | using System.Globalization; 6 | using System.IO; 7 | 8 | /// 9 | /// Defines a template writer used for helper templates. 10 | /// 11 | public class TemplateWriter 12 | { 13 | #region Fields 14 | private readonly Action writerDelegate; 15 | #endregion 16 | 17 | #region Constructors 18 | /// 19 | /// Initialises a new instance of . 20 | /// 21 | /// The writer delegate used to write using the specified . 22 | public TemplateWriter(Action writer) 23 | { 24 | if (writer == null) throw new ArgumentNullException("writer"); 25 | 26 | writerDelegate = writer; 27 | } 28 | #endregion 29 | 30 | #region Methods 31 | /// 32 | /// Executes the write delegate and returns the result of this . 33 | /// 34 | /// The string result of the helper template. 35 | public override string ToString() 36 | { 37 | using (var writer = new StringWriter(CultureInfo.InvariantCulture)) 38 | { 39 | writerDelegate(writer); 40 | return writer.ToString(); 41 | } 42 | } 43 | 44 | /// 45 | /// Writes the helper result of the specified text writer. 46 | /// 47 | /// The text writer to write the helper result to. 48 | public void WriteTo(TextWriter writer) 49 | { 50 | writerDelegate(writer); 51 | } 52 | #endregion 53 | } 54 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Text/HtmlEncodedString.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Text 2 | { 3 | using System.Net; 4 | 5 | /// 6 | /// Represents a Html-encoded string. 7 | /// 8 | public class HtmlEncodedString : IEncodedString 9 | { 10 | #region Fields 11 | private readonly string _encodedString; 12 | #endregion 13 | 14 | #region Constructor 15 | /// 16 | /// Initialises a new instance of 17 | /// 18 | /// The raw string to be encoded. 19 | public HtmlEncodedString(string value) 20 | { 21 | if (!string.IsNullOrWhiteSpace(value)) 22 | _encodedString = WebUtility.HtmlEncode(value); 23 | } 24 | #endregion 25 | 26 | #region Methods 27 | /// 28 | /// Gets the encoded string. 29 | /// 30 | /// The encoded string. 31 | public string ToEncodedString() 32 | { 33 | return _encodedString ?? string.Empty; 34 | } 35 | 36 | /// 37 | /// Gets the string representation of this instance. 38 | /// 39 | /// The string representation of this instance. 40 | public override string ToString() 41 | { 42 | return ToEncodedString(); 43 | } 44 | #endregion 45 | } 46 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Text/HtmlEncodedStringFactory.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Text 2 | { 3 | /// 4 | /// Represents a factory that creates instances. 5 | /// 6 | public class HtmlEncodedStringFactory : IEncodedStringFactory 7 | { 8 | #region Methods 9 | /// 10 | /// Creates a instance for the specified raw string. 11 | /// 12 | /// The raw string. 13 | /// An instance of . 14 | public IEncodedString CreateEncodedString(string rawString) 15 | { 16 | return new HtmlEncodedString(rawString); 17 | } 18 | 19 | /// 20 | /// Creates a instance for the specified object instance. 21 | /// 22 | /// The object instance. 23 | /// An instance of . 24 | public IEncodedString CreateEncodedString(object value) 25 | { 26 | if (value == null) 27 | return new HtmlEncodedString(string.Empty); 28 | 29 | var htmlString = value as HtmlEncodedString; 30 | if (htmlString != null) 31 | return htmlString; 32 | 33 | return new HtmlEncodedString(value.ToString()); 34 | } 35 | #endregion 36 | } 37 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Text/IEncodedString.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Text 2 | { 3 | /// 4 | /// Defines the required contract for implementing an encoded string. 5 | /// 6 | public interface IEncodedString 7 | { 8 | #region Methods 9 | /// 10 | /// Gets the encoded string. 11 | /// 12 | /// The encoded string. 13 | string ToEncodedString(); 14 | #endregion 15 | } 16 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Text/IEncodedStringFactory.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Text 2 | { 3 | /// 4 | /// Defines the required contract for implementing a factory for building encoded strings. 5 | /// 6 | public interface IEncodedStringFactory 7 | { 8 | #region Methods 9 | /// 10 | /// Creates a instance for the specified raw string. 11 | /// 12 | /// The raw string. 13 | /// An instance of . 14 | IEncodedString CreateEncodedString(string value); 15 | 16 | /// 17 | /// Creates a instance for the specified object instance. 18 | /// 19 | /// The object instance. 20 | /// An instance of . 21 | IEncodedString CreateEncodedString(object value); 22 | #endregion 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Text/RawString.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Text 2 | { 3 | /// 4 | /// Represents an unencoded string. 5 | /// 6 | public class RawString : IEncodedString 7 | { 8 | #region Fields 9 | private readonly string _value; 10 | #endregion 11 | 12 | #region Constructor 13 | /// 14 | /// Initialises a new instance of 15 | /// 16 | /// The value 17 | public RawString(string value) 18 | { 19 | _value = value; 20 | } 21 | #endregion 22 | 23 | #region Methods 24 | /// 25 | /// Gets the encoded string. 26 | /// 27 | /// The encoded string. 28 | public string ToEncodedString() 29 | { 30 | return _value ?? string.Empty; 31 | } 32 | 33 | /// 34 | /// Gets the string representation of this instance. 35 | /// 36 | /// The string representation of this instance. 37 | public override string ToString() 38 | { 39 | return ToEncodedString(); 40 | } 41 | #endregion 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/Text/RawStringFactory.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Text 2 | { 3 | /// 4 | /// Represents a factory that creates instances. 5 | /// 6 | public class RawStringFactory : IEncodedStringFactory 7 | { 8 | #region Methods 9 | /// 10 | /// Creates a instance for the specified raw string. 11 | /// 12 | /// Thevalue. 13 | /// An instance of . 14 | public IEncodedString CreateEncodedString(string value) 15 | { 16 | return new RawString(value); 17 | } 18 | 19 | /// 20 | /// Creates a instance for the specified object instance. 21 | /// 22 | /// The value. 23 | /// An instance of . 24 | public IEncodedString CreateEncodedString(object value) 25 | { 26 | return (value == null) 27 | ? new RawString(string.Empty) 28 | : new RawString(value.ToString()); 29 | } 30 | #endregion 31 | } 32 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Core/paket.references: -------------------------------------------------------------------------------- 1 | group Net45 2 | Microsoft.AspNet.Razor 3 | Microsoft.CodeAnalysis 4 | System.Collections.Immutable 5 | System.Reflection.Metadata 6 | 7 | group Net40 8 | Microsoft.AspNet.Razor 9 | 10 | group Razor4 11 | Microsoft.AspNetCore.Razor 12 | Microsoft.CodeAnalysis 13 | System.Collections.Immutable 14 | System.Reflection.Metadata -------------------------------------------------------------------------------- /src/source/RazorEngine.Hosts.Console/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Hosts.Console/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("RazorEngine.Hosts.Console")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("RazorEngine.Hosts.Console")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("037a08ca-030c-45f5-88a1-985c07a2468a")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /src/source/RazorEngine.Mvc/MvcTemplateBase.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Mvc 2 | { 3 | using System; 4 | using System.Web; 5 | using System.Web.Mvc; 6 | 7 | using Templating; 8 | 9 | /// 10 | /// Provides a base implementation of an MVC-compatible template. 11 | /// 12 | public abstract class MvcTemplateBase : TemplateBase 13 | { 14 | #region Properties 15 | /// 16 | /// Gets the for this template. 17 | /// 18 | public HtmlHelper Html { get; private set; } 19 | 20 | /// 21 | /// Gets the for this template. 22 | /// 23 | public UrlHelper Url { get; private set; } 24 | #endregion 25 | 26 | #region Methods 27 | /// 28 | /// Initializes the template. 29 | /// 30 | public void InitHelpers() 31 | { 32 | var httpContext = new HttpContextWrapper(HttpContext.Current); 33 | var handler = httpContext.CurrentHandler as MvcHandler; 34 | if (handler == null) 35 | throw new InvalidOperationException("Unable to run template outside of ASP.NET MVC"); 36 | } 37 | #endregion 38 | } 39 | } -------------------------------------------------------------------------------- /src/source/RazorEngine.Mvc/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("RazorEngine.Mvc")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("RazorEngine.Mvc")] 13 | [assembly: AssemblyCopyright("Copyright © 2011")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("e6e3699c-9a7a-4ac8-9b48-fa8292240f0b")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /src/source/codeAnalysis.ruleset: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core.Roslyn/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // Allgemeine Informationen über eine Assembly werden über die folgenden 6 | // Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, 7 | // die mit einer Assembly verknüpft sind. 8 | [assembly: AssemblyTitle("Test.RazorEngine.Core.Roslyn")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Test.RazorEngine.Core.Roslyn")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar 18 | // für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von 19 | // COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest. 20 | [assembly: ComVisible(false)] 21 | 22 | // Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird 23 | [assembly: Guid("ab8396cc-9fd1-42d3-b49f-f228752631a7")] 24 | 25 | // Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: 26 | // 27 | // Hauptversion 28 | // Nebenversion 29 | // Buildnummer 30 | // Revision 31 | // 32 | // Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern 33 | // übernehmen, indem Sie "*" eingeben: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core.Roslyn/RazorEngine.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Antaris/RazorEngine/aa5dd2661fe040ce39020949ebd33d98a530ebe4/src/test/Test.RazorEngine.Core.Roslyn/RazorEngine.snk -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core.Roslyn/paket.references: -------------------------------------------------------------------------------- 1 | group Net45 2 | Microsoft.AspNet.Razor 3 | Microsoft.CodeAnalysis 4 | 5 | group Net40 6 | Microsoft.AspNet.Razor 7 | 8 | group Razor4 9 | Microsoft.AspNetCore.Razor 10 | Microsoft.CodeAnalysis 11 | 12 | group Test 13 | NUnit -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/ActivatorTestFixture.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Tests 2 | { 3 | using System; 4 | using System.IO; 5 | 6 | 7 | using Moq; 8 | using NUnit.Framework; 9 | 10 | using Compilation; 11 | using Configuration; 12 | using Templating; 13 | using TestTypes; 14 | using TestTypes.Activation; 15 | using Text; 16 | #if NET45 17 | using Autofac; 18 | using Autofac.Features.ResolveAnything; 19 | 20 | /// 21 | /// Defines a test fixture that provides tests for the type. 22 | /// 23 | [TestFixture] 24 | public class ActivatorTestFixture 25 | { 26 | #region Tests 27 | /// 28 | /// Tests that a custom activator can be used. In this test case, we're using Autofac 29 | /// to handle a instantiation of a custom activator. 30 | /// 31 | [Test] 32 | public void TemplateService_CanSupportCustomActivator_WithAutofac() 33 | { 34 | #if RAZOR4 35 | Assert.Ignore("We need to add roslyn to generate custom constructors!"); 36 | #endif 37 | 38 | var container = new ContainerBuilder(); 39 | container.RegisterType() 40 | .AsSelf() 41 | .As(); 42 | container.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource()); 43 | 44 | var config = new TemplateServiceConfiguration 45 | { 46 | Activator = new AutofacTemplateActivator(container.Build()), 47 | BaseTemplateType = typeof(CustomTemplateBase<>), 48 | CompilerServiceFactory = new DefaultCompilerServiceFactory() 49 | }; 50 | 51 | using (var service = RazorEngineService.Create(config)) 52 | { 53 | const string template = "

Hello @Format(Model.Forename)

"; 54 | const string expected = "

Hello ttaM

"; 55 | 56 | var model = new Person { Forename = "Matt" }; 57 | string result = service.RunCompile(templateSource: template, name: "template", model: model); 58 | 59 | Assert.That(result == expected, "Result does not match expected: " + result); 60 | } 61 | } 62 | #endregion 63 | } 64 | #endif 65 | } -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/CodeInspectorTestFixture.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Tests 2 | { 3 | using System; 4 | 5 | using NUnit.Framework; 6 | 7 | #if !RAZOR4 8 | using Compilation.Inspectors; 9 | using Configuration; 10 | using Templating; 11 | using TestTypes.Inspectors; 12 | 13 | /// 14 | /// Defines a test fixture that provides tests for the type. 15 | /// 16 | [TestFixture] 17 | [Obsolete("Removed eventually.")] 18 | public class CodeInspectorTestFixture 19 | { 20 | #region Tests 21 | /// 22 | /// Tests that a code inspector supports add a custom inspector. 23 | /// 24 | [Test] 25 | public void CodeInspector_SupportsAddingCustomInspector() 26 | { 27 | var config = new TemplateServiceConfiguration(); 28 | config.CodeInspectors.Add(new ThrowExceptionCodeInspector()); 29 | 30 | using (var service = RazorEngineService.Create(config)) 31 | { 32 | const string template = "Hello World"; 33 | 34 | Assert.Throws(() => service.RunCompile(templateSource: template, name: "template")); 35 | } 36 | } 37 | #endregion 38 | } 39 | #endif 40 | } -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("RazorEngine.Core.Tests")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("Microsoft")] 12 | [assembly: AssemblyProduct("RazorEngine.Core.Tests")] 13 | [assembly: AssemblyCopyright("Copyright © Microsoft 2011")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("279faf7a-64a4-4537-b4cd-2023f6652acf")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/RazorEngine.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Antaris/RazorEngine/aa5dd2661fe040ce39020949ebd33d98a530ebe4/src/test/Test.RazorEngine.Core/RazorEngine.snk -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/Templating/TemplateRunnerTestFixture.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using NUnit.Framework; 3 | using RazorEngine.Configuration; 4 | using RazorEngine.Templating; 5 | using RazorEngine.Tests.TestTypes; 6 | 7 | namespace Test.RazorEngine.Templating 8 | { 9 | /// 10 | /// Defines a test fixture that provides tests for the type. 11 | /// 12 | [TestFixture] 13 | public class TemplateRunnerTestFixture 14 | { 15 | /// 16 | /// Tests that the template runner can run the template. 17 | /// 18 | [Test] 19 | public void TemplateRunner_CanRunTemplateString() 20 | { 21 | const string template = "Hello @Model.Forename, welcome to RazorEngine!"; 22 | 23 | var configuration = new TemplateServiceConfiguration { Debug = true }; 24 | using (var service = RazorEngineService.Create(configuration)) 25 | { 26 | var runner = service.CompileRunner(template); 27 | var output = runner.Run(new Person { Forename = "Max" }); 28 | 29 | Assert.AreEqual("Hello Max, welcome to RazorEngine!", output); 30 | } 31 | } 32 | 33 | /// 34 | /// Tests that the template runner can run the template on a text writer. 35 | /// 36 | [Test] 37 | public void TemplateRunner_CanRunTemplateTextWriter() 38 | { 39 | const string template = "Hello @Model.Forename, welcome to RazorEngine!"; 40 | 41 | var configuration = new TemplateServiceConfiguration { Debug = true }; 42 | using (var service = RazorEngineService.Create(configuration)) 43 | using (var writer = new StringWriter()) 44 | { 45 | var runner = service.CompileRunner(template); 46 | runner.Run(new Person { Forename = "Max" }, writer); 47 | 48 | Assert.AreEqual("Hello Max, welcome to RazorEngine!", writer.ToString()); 49 | } 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/Templating/Templates/Layout.cshtml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 |

8 | This is a Layout 9 |

10 |
11 | @RenderBody() 12 |
13 | 14 | -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/Templating/Templates/Model.cs: -------------------------------------------------------------------------------- 1 | namespace Test.RazorEngine.Templating.Templates 2 | { 3 | /// 4 | /// Model used for EmbeddedResourceTemplateManager testing 5 | /// 6 | public class Model 7 | { 8 | /// 9 | /// Model string property 10 | /// 11 | public string Name { get; set; } 12 | 13 | /// 14 | /// Model int property 15 | /// 16 | public int Answer { get; set; } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/Templating/Templates/NoModel.cshtml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 |

8 | Template without Model 9 |

10 | 11 | -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/Templating/Templates/Partial.cshtml: -------------------------------------------------------------------------------- 1 | @model System.String 2 | 3 | Hello @Model! 4 | -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/Templating/Templates/WithLayout.cshtml: -------------------------------------------------------------------------------- 1 | @model Test.RazorEngine.Templating.Templates.Model 2 | 3 | @{ 4 | Layout = "Templates.Layout"; 5 | } 6 | 7 |

8 | Hello @Model.Name! Your answer is @Model.Answer. 9 |

-------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/Templating/Templates/WithModel.cshtml: -------------------------------------------------------------------------------- 1 | @model Test.RazorEngine.Templating.Templates.Model 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |

10 | Hello @Model.Name! Your answer is @Model.Answer. 11 |

12 | 13 | -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/Templating/Templates/WithPartial.cshtml: -------------------------------------------------------------------------------- 1 | @model Test.RazorEngine.Templating.Templates.Model 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | @Include("Templates.Partial", Model.Name) 11 | 12 | Your answer is @Model.Answer. 13 |
14 | 15 | -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/TestTypes/Activation/AutofacTemplateActivator.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Tests.TestTypes.Activation 2 | { 3 | using System; 4 | #if NET45 5 | using Autofac; 6 | 7 | using Templating; 8 | 9 | /// 10 | /// Defines an activator that supports Unity. 11 | /// 12 | public class AutofacTemplateActivator : IActivator 13 | { 14 | #region Fields 15 | private readonly IContainer _container; 16 | #endregion 17 | 18 | #region Constructor 19 | /// 20 | /// Initialises a new instance of . 21 | /// 22 | /// The unity container. 23 | public AutofacTemplateActivator(IContainer container) 24 | { 25 | if (container == null) 26 | throw new ArgumentNullException("container"); 27 | 28 | _container = container; 29 | } 30 | #endregion 31 | 32 | #region Methods 33 | /// 34 | /// Creates an instance of the specifed template. 35 | /// 36 | /// The instance context. 37 | /// An instance of . 38 | public ITemplate CreateInstance(InstanceContext context) 39 | { 40 | return (ITemplate)_container.Resolve(context.TemplateType); 41 | } 42 | #endregion 43 | } 44 | #endif 45 | } -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/TestTypes/Activation/CustomTemplateBase.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Tests.TestTypes.Activation 2 | { 3 | using System; 4 | 5 | using Templating; 6 | 7 | /// 8 | /// Defines a test template base. 9 | /// 10 | /// The model type. 11 | public class CustomTemplateBase : TemplateBase 12 | { 13 | #region Fields 14 | private readonly ITextFormatter _formatter; 15 | #endregion 16 | 17 | #region Methods 18 | /// 19 | /// Initialises a new instance of 20 | /// 21 | /// The formatter. 22 | public CustomTemplateBase(ITextFormatter formatter) 23 | { 24 | if (formatter == null) 25 | throw new ArgumentNullException("formatter"); 26 | 27 | _formatter = formatter; 28 | } 29 | #endregion 30 | 31 | #region Methods 32 | /// 33 | /// Formats the specified object. 34 | /// 35 | /// The value to format. 36 | /// The string formatted value. 37 | public string Format(object value) 38 | { 39 | return _formatter.Format(value.ToString()); 40 | } 41 | #endregion 42 | } 43 | } -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/TestTypes/Activation/ITextFormatter.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Tests.TestTypes.Activation 2 | { 3 | /// 4 | /// Defines the required contract for implementing a text formatter. 5 | /// 6 | public interface ITextFormatter 7 | { 8 | #region Methods 9 | /// 10 | /// Formats the specified value. 11 | /// 12 | /// The value to format. 13 | /// The formatted value. 14 | string Format(string value); 15 | #endregion 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/TestTypes/Activation/ReverseTextFormatter.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Tests.TestTypes.Activation 2 | { 3 | using System; 4 | 5 | /// 6 | /// Reverses the contents of the specified string, 7 | /// 8 | public class ReverseTextFormatter : ITextFormatter 9 | { 10 | #region Methods 11 | /// 12 | /// Formats the specified value. 13 | /// 14 | /// The value to format. 15 | /// The formatted value. 16 | public string Format(string value) 17 | { 18 | char[] content = value.ToCharArray(); 19 | Array.Reverse(content); 20 | return new string(content); 21 | } 22 | #endregion 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/TestTypes/Animal.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Tests.TestTypes 2 | { 3 | /// 4 | /// Defines an animal. 5 | /// 6 | public class Animal 7 | { 8 | #region Properties 9 | /// 10 | /// Gets the animal type. 11 | /// 12 | public string Type { get; set; } 13 | #endregion 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/TestTypes/AnimalViewModel.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Tests.TestTypes 2 | { 3 | /// 4 | /// Defines a complex view model. 5 | /// 6 | public class AnimalViewModel 7 | { 8 | /// 9 | /// Test class. 10 | /// 11 | public Animal[] Animals { get; set; } 12 | } 13 | } -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/TestTypes/BaseTypes/NestedBaseClass.cs: -------------------------------------------------------------------------------- 1 | using RazorEngine.Templating; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Test.RazorEngine.TestTypes.BaseTypes 9 | { 10 | /// 11 | /// Test class. 12 | /// 13 | public class HostingClass 14 | { 15 | /// 16 | /// Test class. 17 | /// 18 | public class NestedBaseClass : TemplateBase 19 | { 20 | /// 21 | /// Test class. 22 | /// 23 | public string TestProperty { get { return "mytest"; } } 24 | } 25 | 26 | /// 27 | /// Test class. 28 | /// 29 | public class NonGenericNestedBaseClass : TemplateBase 30 | { 31 | /// 32 | /// Test class. 33 | /// 34 | public string TestProperty { get { return "mytest"; } } 35 | } 36 | 37 | /// 38 | /// Test class. 39 | /// 40 | public class NestedClass 41 | { 42 | /// 43 | /// Test class. 44 | /// 45 | public string TestProperty { get; set; } 46 | } 47 | 48 | /// 49 | /// Test class. 50 | /// 51 | public class GenericNestedClass 52 | { 53 | /// 54 | /// Test class. 55 | /// 56 | public T TestProperty { get; set; } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/TestTypes/BaseTypes/NonGenericTemplateBase.cs: -------------------------------------------------------------------------------- 1 | using RazorEngine.Templating; 2 | 3 | namespace RazorEngine.Tests.TestTypes.BaseTypes 4 | { 5 | /// 6 | /// Test class. 7 | /// 8 | public abstract class NonGenericTemplateBase : TemplateBase 9 | { 10 | /// 11 | /// Test class. 12 | /// 13 | public string GetHelloWorldText() 14 | { 15 | return "Hello World"; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/TestTypes/Employee.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Tests.TestTypes 2 | { 3 | /// 4 | /// Defines an employee. 5 | /// 6 | public class Employee : Person 7 | { 8 | #region Properties 9 | /// 10 | /// Gets or sets the department. 11 | /// 12 | public string Department { get; set; } 13 | #endregion 14 | } 15 | } -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/TestTypes/InlineTemplateModel.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Tests.TestTypes 2 | { 3 | using System; 4 | using RazorEngine.Templating; 5 | 6 | /// 7 | /// Test class. 8 | /// 9 | public class InlineTemplateModel 10 | { 11 | /// 12 | /// Test class. 13 | /// 14 | public Func InlineTemplate { get; set; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/TestTypes/Inspectors/ThrowExceptionCodeInspector.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Tests.TestTypes.Inspectors 2 | { 3 | using System.CodeDom; 4 | 5 | #if !RAZOR4 6 | using Compilation.Inspectors; 7 | 8 | /// 9 | /// Defines a code inspector that will insert a throw statement into the generated code. 10 | /// 11 | #pragma warning disable 0618 // Fine because we still want to test if 12 | public class ThrowExceptionCodeInspector : ICodeInspector 13 | #pragma warning restore 0618 14 | { 15 | #region Methods 16 | /// 17 | /// Inspects the specified code unit. 18 | /// 19 | /// The code unit. 20 | /// The code namespace declaration. 21 | /// The code type declaration. 22 | /// The code method declaration for the Execute method. 23 | public void Inspect(CodeCompileUnit unit, CodeNamespace ns, CodeTypeDeclaration type, CodeMemberMethod executeMethod) 24 | { 25 | var statement = new CodeThrowExceptionStatement( 26 | new CodeObjectCreateExpression( 27 | new CodeTypeReference(typeof(System.InvalidOperationException)), new CodeExpression[] {})); 28 | 29 | executeMethod.Statements.Insert(0, statement); 30 | } 31 | #endregion 32 | } 33 | #endif 34 | } -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/TestTypes/Person.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Tests.TestTypes 2 | { 3 | using System; 4 | 5 | /// 6 | /// Defines a person. 7 | /// 8 | [Serializable] 9 | public class Person 10 | { 11 | #region Properties 12 | /// 13 | /// Gets or sets the age. 14 | /// 15 | public int Age { get; set; } 16 | 17 | /// 18 | /// Gets or sets the forename. 19 | /// 20 | public string Forename { get; set; } 21 | 22 | /// 23 | /// Gets or sets the surname. 24 | /// 25 | public string Surname { get; set; } 26 | #endregion 27 | } 28 | } -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/TestTypes/ThreadPoolItem.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Tests.TestTypes 2 | { 3 | using System; 4 | using System.Threading; 5 | 6 | /// 7 | /// Defines a thread pool item. 8 | /// 9 | /// The model type. 10 | public class ThreadPoolItem 11 | { 12 | #region Fields 13 | private readonly Action _action; 14 | #endregion 15 | 16 | #region Constructor 17 | /// 18 | /// Initialises a new instance of . 19 | /// 20 | /// The model instance. 21 | /// The reset event. 22 | /// The action to run. 23 | public ThreadPoolItem(T model, ManualResetEvent resetEvent, Action action) 24 | { 25 | Model = model; 26 | ResetEvent = resetEvent; 27 | _action = action; 28 | } 29 | #endregion 30 | 31 | #region Methods 32 | /// 33 | /// The callback method invoked by the threadpool. 34 | /// 35 | /// Any current state. 36 | public void ThreadPoolCallback(object state) 37 | { 38 | _action(Model); 39 | ResetEvent.Set(); 40 | } 41 | #endregion 42 | 43 | #region Properties 44 | /// 45 | /// Gets the model. 46 | /// 47 | public T Model { get; private set; } 48 | 49 | /// 50 | /// Gets the reset event. 51 | /// 52 | public ManualResetEvent ResetEvent { get; private set; } 53 | #endregion 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/TestTypes/ValueObject.cs: -------------------------------------------------------------------------------- 1 | namespace RazorEngine.Tests.TestTypes 2 | { 3 | using System.Collections.Generic; 4 | using System.Dynamic; 5 | 6 | /// 7 | /// Test class. 8 | /// 9 | public class ValueObject : DynamicObject 10 | { 11 | #region Fields 12 | private readonly IDictionary _values; 13 | #endregion 14 | 15 | #region Constructor 16 | /// 17 | /// Test class. 18 | /// 19 | public ValueObject(IDictionary values) 20 | { 21 | _values = values; 22 | } 23 | #endregion 24 | 25 | #region Methods 26 | /// 27 | /// Test class. 28 | /// 29 | public override bool TryGetMember(GetMemberBinder binder, out object result) 30 | { 31 | if (_values.ContainsKey(binder.Name)) 32 | { 33 | result = _values[binder.Name]; 34 | return true; 35 | } 36 | 37 | result = null; 38 | return false; 39 | } 40 | #endregion 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/VariousTestsFixture.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using RazorEngine; 7 | using NUnit.Framework; 8 | 9 | namespace Test.RazorEngine 10 | { 11 | /// 12 | /// Various general tests. 13 | /// 14 | [TestFixture] 15 | public class VariousTestsFixture 16 | { 17 | /// 18 | /// Test if we can call GetTypes on the RazorEngine assembly. 19 | /// This will make sure all SecurityCritical attributes are valid. 20 | /// 21 | [Test] 22 | public void AssemblyIsScannable() 23 | { 24 | typeof(Engine).Assembly.GetTypes(); 25 | } 26 | /* 27 | /// 28 | /// Check that Contracts are enabled and work on this build machine. 29 | /// 30 | [Test] 31 | [ExpectedException(typeof(ArgumentException))] 32 | public void ConstractsWork() 33 | { 34 | System.Diagnostics.Contracts.Contract.Requires(false); 35 | } 36 | 37 | [Test] 38 | //[ExpectedException(typeof(Exception))] 39 | public void ConstractsWork_2() 40 | { 41 | System.Diagnostics.Contracts.Contract.Requires(false); 42 | }*/ 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/paket.references: -------------------------------------------------------------------------------- 1 | group Net45 2 | Microsoft.AspNet.Razor 3 | System.Reflection.Metadata 4 | 5 | group Net40 6 | Microsoft.AspNet.Razor 7 | 8 | group Razor4 9 | Microsoft.AspNetCore.Razor 10 | System.Reflection.Metadata 11 | 12 | group Test 13 | Moq 14 | NUnit 15 | Autofac -------------------------------------------------------------------------------- /src/test/Test.RazorEngine.Core/test/TestHelper.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Antaris/RazorEngine/aa5dd2661fe040ce39020949ebd33d98a530ebe4/src/test/Test.RazorEngine.Core/test/TestHelper.dll -------------------------------------------------------------------------------- /src/test/TestHelper/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // Allgemeine Informationen über eine Assembly werden über die folgenden 6 | // Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, 7 | // die mit einer Assembly verknüpft sind. 8 | [assembly: AssemblyTitle("TestHelper")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("TestHelper")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar 18 | // für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von 19 | // COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest. 20 | [assembly: ComVisible(false)] 21 | 22 | // Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird 23 | [assembly: Guid("655a0836-d3ca-4652-a585-d32c5a795192")] 24 | 25 | // Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: 26 | // 27 | // Hauptversion 28 | // Nebenversion 29 | // Buildnummer 30 | // Revision 31 | // 32 | // Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern 33 | // übernehmen, indem Sie "*" eingeben: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /src/test/TestHelper/TestClass.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace TestHelper 8 | { 9 | /// 10 | /// A helper library to test the case that a assembly is only referenced by the template. 11 | /// 12 | public class TestClass 13 | { 14 | /// 15 | /// Test property. 16 | /// 17 | public string TestProperty 18 | { 19 | get { return "TestPropert"; } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/TestHelper/TestHelper.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Net45 7 | {7899C0AE-844D-4A34-B3BC-96819C73EA68} 8 | Library 9 | Properties 10 | TestHelper 11 | TestHelper 12 | v4.5 13 | 512 14 | ..\..\ 15 | true 16 | true 17 | 18 | 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 57 | -------------------------------------------------------------------------------- /src/test/TestRunnerHelper/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 |
11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/test/TestRunnerHelper/Program.cs: -------------------------------------------------------------------------------- 1 | using RazorEngine; 2 | using RazorEngine.Templating; 3 | using System; 4 | using System.Dynamic; 5 | using RazorEngine.Compilation; 6 | using RazorEngine.Configuration; 7 | 8 | namespace TestRunnerHelper 9 | { 10 | public class Program 11 | { 12 | public class TemplateContext : TemplateBase 13 | { 14 | public string Section1 15 | { 16 | set { 17 | ((CustomDataHolder)ViewBag.DataHolder).Section1 = value; 18 | } 19 | } 20 | } 21 | 22 | public class CustomDataHolder 23 | { 24 | public string Section1 { get; set; } 25 | } 26 | 27 | static void Main(string[] args) 28 | { 29 | using (var service = RazorEngineService.Create 30 | (new TemplateServiceConfiguration() { Debug = true })) 31 | { 32 | const string template = @" 33 | @{ Layout = ""extractLayouts""; } 34 | @section section1{ 35 | sample content 36 | } 37 | "; 38 | const string sectionExtracting = @" 39 | @inherits TestRunnerHelper.Program.TemplateContext 40 | @{ 41 | string result; 42 | using (var mem = new System.IO.StringWriter()) 43 | { 44 | System.Diagnostics.Debugger.Break(); 45 | var section1 = RenderSection(""section1""); 46 | section1.WriteTo(mem); 47 | mem.Flush(); 48 | Section1 = mem.ToString(); 49 | } 50 | } 51 | 52 | @RenderSection(""section1"")"; 53 | service.Compile(sectionExtracting, "extractLayouts", null); 54 | 55 | var holder = new CustomDataHolder(); 56 | dynamic viewbag = new DynamicViewBag(); 57 | viewbag.DataHolder = holder; 58 | // Mono CSC seems to be confused and needs the casts. 59 | var body = service.RunCompile(template, "templateKey", (Type)null, (object)null, (DynamicViewBag)viewbag); 60 | 61 | 62 | if (!holder.Section1.Contains("sample content")) 63 | { 64 | throw new Exception("Expected section content"); 65 | } 66 | } 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/test/TestRunnerHelper/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // Allgemeine Informationen über eine Assembly werden über die folgenden 6 | // Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, 7 | // die mit einer Assembly verknüpft sind. 8 | [assembly: AssemblyTitle("TestRunnerHelper")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("TestRunnerHelper")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar 18 | // für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von 19 | // COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest. 20 | [assembly: ComVisible(false)] 21 | 22 | // Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird 23 | [assembly: Guid("550ef6a5-feec-43bf-bf2e-ee635a17b153")] 24 | 25 | // Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: 26 | // 27 | // Hauptversion 28 | // Nebenversion 29 | // Buildnummer 30 | // Revision 31 | // 32 | // Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern 33 | // übernehmen, indem Sie "*" eingeben: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | --------------------------------------------------------------------------------