Compile Mustache template using c# library without rendering data and sections - c#

Is there a way to "compile" a mustache template using Stubble without inlining all the data? What I mean by compiling is the following:
given the template Main
{{ Foo }}
{{ > SomePartial}}
{{ #SomeSection }} world {{ /SomeSection }}
and partial template SomePartial
hello
I would like to get a following template as a result of some functions calls:
{{ Foo }}
hello
{{ #SomeSection }} world {{ /SomeSection }}
If it is not possible with Stubble, I'm open to another library

Related

How to generate monaco completion proposals from C# assembly?

I have a DLL with a set of classes I want to get code completion in Monaco code editor UI. They require them to be in format like this:
{
label: '"lodash"',
kind: monaco.languages.CompletionItemKind.Function,
documentation: "The Lodash library exported as Node.js modules.",
insertText: '"lodash": "*"',
range: range
}
So how to generate Monaco completion proposals from C# assembly?
Notes:
this may be related "Using Roslyn C# Completion Service programmatically" yet how to make them work together?
If one could generate something like this using reflection:
'namespace test{',
'declare interface custom {',
'id :string;',
'};',
'',
'declare function MyCustomFunction(i :custom) :void;',
'}'
It would solve the problem as shown here

Custom tag helper isn't interpreted on razor page

I try to add a custom tag helper to my project that would convert a markdown text to html.
For the conversion I tried using both Makrdig.Markdown and CommonMarkConverter.Convert, without success, but I think that the problem lies in not detecting my implementation by the razor page.
My TagHelper:
using Markdig;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace CustomTagHelpers.Helpers
{
[HtmlTargetElement("markdown")]
public class MarkdownTagHelper : TagHelper
{
[HtmlAttributeName("for-content")]
public ModelExpression Content { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagMode = TagMode.SelfClosing;
output.TagName = "markdown-helper";
var markdown = Content.Model.ToString();
//var html = CommonMarkConverter.Convert(markdown);
var html = Markdig.Markdown.ToHtml(markdown);
output.Content.SetHtmlContent(html);
}
}
I've added it to the _ViewImports file
#addTagHelper *, CustomTagHelpers.Helpers MarkdownTagHelper
And that's the code using my tag helper in my view, HelloMarkdown is an attribute located in my viewmodel linked to the current view, it's a public string with [BindProperty]
<markdown for-content="HelloMarkdown"> </markdown>
Lastly, if I inspect the html code in my browser, it seems to interpret it literally, so it's the same as in the code:
<markdown for-content="HelloMarkdown"> </markdown>
While I would like to get something like that
<markdown-helper><p><em>Hello World</em></p></markdown-helper>
Welcome to StackOverflow!
I dig into Adam Freeman's ASP.NET Core MVC 2 book as I found Your _ViewImports.cshtml declaration strange looking and also found confirmation in the docs that You are most probably use the invalid syntax there.
Based on the documentation it should be something like this (note the ., not space):
#addTagHelper *, MarkdownTagHelper
as the second argument sholud point to either FQN or the short name of the assembly and then the first argument is telling us which classes from the given assembly we want to use. Your current declaration is saying "import all classes from the CustomTagHelpers.Helpers assembly" which does not exists (it is a namespace). As for the 3ed part after the whitespace honestly I don't know how it would be interpreted, most probably ignored.
You can also use this syntax:
#addTagHelper CustomTagHelpers.Helpers, MarkdownTagHelper
but I guess Your intention was the first one, to import all helpers from Your assembly.
Please check it out if it would help? I think it might be it.
EDIT:
I've created a short exampe to verify and yes - it was a matter of the invalid #addTagHelper syntax.
After creating a new project (dotnet new mvc -o Sample), creating a lib for Your tag helpers (an assembly) (dotnet new classlib CustomTagHelpers), adding a reference of that lib to the main project I end up with this:
| Sample (sln)
|- CustomTagHelpers (classlib)
|-- Helpers (dir)
|--- MarkdownTagHelper.cs (Your code with CustomTagHelpers.Helpers namespace)
|- Sample (mvc project)
...
and _ViewImports.cshtml file:
#addTagHelper CustomTagHelpers.Helpers.MarkdownTagHelper, CustomTagHelpers
After rebuilding the tag helper is visible:
Of course You can also use #addTagHelper *, CustomTagHelpers to import all classes fro the assembly.
And what about the default #addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers which is seems to be a namespace (docs)? This is actually the name of the dll file (assembly). You can find it here: %USERPROFILE%\.nuget\packages\microsoft.aspnetcore.mvc.taghelpers\2.0.0\lib\netstandard2.0 or similar directory. Here is the source code for it: link. The Microsoft.AspNetCore.Mvc.TagHelpers is just a name of the classlib/assembly. We can also do the same trick by changing our project's structire into this:
| Sample (sln)
|- CustomTagHelpers.Helpers (classlib)
|-- MarkdownTagHelper.cs (Your code with CustomTagHelpers.Helpers namespace)
|- Sample (mvc project)
...
and then we can use #addTagHelper *, CustomTagHelpers.Helpers.

Ironpython in C# Import Module Error import csv module

I'm trying to execute a Python script from within a C# application but when it tries to launch the script, I receive the error ImportException was unhandled No module name csv. I checked the Ironpython folder and there is a csv.py file...?
Code I'm using to call the script:
IDictionary<string, object> options = new Dictionary<string, object>();
options["Argument"] = new[] { filePath, profile };
var pyEngine = Python.CreateEngine(options);
var pyScope = pyEngine.CreateScope();
string script = "xccdf-xml2tsv.py";
pyScope.SetVariable(profile, options);
pyEngine.ExecuteFile(script, pyScope);
python file:
#!/usr/bin/env python
###
# (C) 2010 Adam Crosby
# Licensed under:
# http://creativecommons.org/licenses/by-nc-sa/3.0/
##
import csv
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import xml.etree.ElementTree as ET
xmlns = "http://checklists.nist.gov/xccdf/1.1"
...
The IronPython engine you created is unaware of the standard library implementation it should use. You can specify it by adding something like
var paths = pyEngine.GetSearchPaths();
paths.Add(#"C:\Program Files (x86)\IronPython 2.7\Lib");
pyEngine.SetSearchPaths(paths);
You could either point it to a locally installed iron python (as in my example) or use the appropriate NuGet package to directly add it to your project. You might also have to deploy it to your output folder/installer/...
Please also take note of this answer as well as this answer and comments as they might provide additional information.

How to use EF default code generation strategy with separate files per object?

My project is using the "Default" code generation strategy for converting my EDMX into C# objects.
I don't want to change anything about the way the code is generated, except that I want to generate a separate file per generated class, rather than one big honkin' mutha of close on 100,000 lines.
What's the easiest way to achieve that?
EDIT: found the default template online, made a little tweak to it so that the generated output is identical to the original. Code is too long to post here; you can view it here; now I need to know what modifications to make to it so that every generated class gets created in its own separate file.
If you can get to the .tt code generation template behind that EDMX, then you can set it to begin a new file per entity, something like this:
foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
{
fileManager.StartNewFile(entity.Name + ".cs");
BeginNamespace(code);
#>
(rest of class generation code here...)
where that fileManager is created thus:
var fileManager = EntityFrameworkTemplateFileManager.Create(this);
and BeginNamespace should be further down the template; if you're missing it then it looks like this:
public void BeginNamespace(CodeGenerationTools code)
{
var codeNamespace = code.VsNamespaceSuggestion();
if (!String.IsNullOrEmpty(codeNamespace))
{
#>
namespace <#=code.EscapeNamespace(codeNamespace)#>
{
<#+
PushIndent(" ");
}
}
public void EndNamespace(CodeGenerationTools code)
{
if (!String.IsNullOrEmpty(code.VsNamespaceSuggestion()))
{
PopIndent();
#>
}
<#+
}
}
For reference, this is from the standard EF5 DbContext generator, which can be found here.

Text templates for string generation at run-time (like Razor or T4)

Is there any tool on the web that can be used to generate strings from a template, i'm looking for something similar to Razor.
The strings should be able to be generated at run time, and don't depend on Visual Studio (like T4). And the framework should work in Silverlight.
RazorEngine is a framework that meets the requeriments but doesn't work in Silverlight.
Thanks in advance.
Hopefully I understood what you asked for but I would argue that you can make T4 work in SL as well. T4 can be asked to generate what is sometimes called runtime templates. I have defined my template (very simple) and added it to my Silverlight project.
<#
for (var iter = 0; iter < 10; ++iter)
{
#>
This is just a test: #<#=iter#>
<#
}
#>
Normally it would generate an output like this:
This is just a test: #0
This is just a test: #1
This is just a test: #2
This is just a test: #3
This is just a test: #4
This is just a test: #5
This is just a test: #6
This is just a test: #7
This is just a test: #8
This is just a test: #9
But in this case I like to generate the code that generates that output ie a runtime template. In order to do that I switch the custom tool to: TextTemplatingFilePreprocessor
Now the template generates the code that generates that output. If you stay clear of hostspecific=true you don't get Visual Studio dependencies. By extending the partial class with member variables and referencing them from the template file you can modify the behavior on the template in runtime.
The problem in Silverlight is that silverlight lacks the classes: System.CodeDom.Compiler.CompilerError and System.CodeDom.Compiler.CompilerErrorCollection.
I worked around that by creating my own classes for that (just for this purpose):
namespace System.CodeDom.Compiler
{
public class CompilerError
{
public string ErrorText;
public bool IsWarning;
}
public class CompilerErrorCollection : List<CompilerError>
{
}
}
Now my template compiles and I just it like this from my Silverlight app to produce the output:
var runtimeTemplate = new MyRuntimeTemplate();
string output = runtimeTemplate.TransformText();

Categories

Resources