This project has moved. For the latest updates, please go here.

@Razor syntax support

Nov 23, 2013 at 2:59 AM
Is there any way of plugging in support for cshtml files?
Coordinator
Nov 23, 2013 at 6:55 AM
Hello, Daniel!

Support of @Razor in WebMarkupMin will not be implemented, because the @Razor syntax is very difficult to parse.

Instead, I recommend you to try a WebMarkupMin extensions for ASP.NET: WebMarkupMin.Web, WebMarkupMin.Mvc and WebMarkupMin.WebForms
Nov 23, 2013 at 1:15 PM
Edited Nov 23, 2013 at 1:16 PM
Hi Taritsyn
That’s exactly what I figured this morning as I was thinking about the RegEx. However, after breakfast I had a quick spelunk and came up with this PoC that properly strips out all the C# code blocks from a CSHTML razor file and spits out the markup. I don’t know how best to implement this in WebMarkupMin, or whether you’d want to, but I thought I’d share.

I also have a fork of html-minifier (https://git01.codeplex.com/forks/danieljsinclair/webmarkupmin) that I’ve converted to use WebMarkupMin that would suit this as a pre-processing step very easily.
using System;
//using System.Linq;
//using System.Collections;
//using System.Collections.Generic;
//using System.Diagnostics;
using System.IO;
using System.Web.Razor.Parser;
using System.Web.Razor.Text;
using System.Web.Razor.Parser.SyntaxTree;

namespace QuickTest
{
    public class MyRazorVisitor : ParserVisitor
    {
        public override void ThrowIfCanceled()
        {
            base.ThrowIfCanceled();
        }
        public override void VisitSpan(Span span)
        {
            switch (span.Kind)
            {
                case SpanKind.Comment:
                case SpanKind.Transition:
                case SpanKind.MetaCode:
                case SpanKind.Code:
                    // ignore all code blocks
                    break;

                case SpanKind.Markup:
                    Console.Write(span.Content);
                    break;
            }
            base.VisitSpan(span);
        }

        public override void OnComplete()
        {
            base.OnComplete();
        }
        public override void VisitError(RazorError err)
        {
            base.VisitError(err);
        }

    }//class MyRazorParserVisitor

    class Program
    {
        public static void Main(string[] argv)
        {
            var cshtmlFile = argv[0];
            var fileText = File.ReadAllText(cshtmlFile);
            var textReader = new StringReader(fileText);
            var textDocument = new BufferingTextReader(textReader);
            var parser = new RazorParser(new CSharpCodeParser(), new HtmlMarkupParser());
            var visitor = new MyRazorVisitor();
            parser.Parse(textDocument, visitor); // for some reason this just hangs waiting for something if I give it the textReader directly
        }

    }//class Program
}
Coordinator
Nov 23, 2013 at 1:28 PM
I think this is a bad idea.
Nov 23, 2013 at 3:00 PM
In what way?
Coordinator
Nov 23, 2013 at 5:37 PM
Better to use my first recommendation and minify the output HTML code:
  1. In ASP.NET MVC use a action filters from WebMarkupMin.Mvc.
  2. In ASP.NET Web Pages use a HTTP modules from WebMarkupMin.Web.
Nov 24, 2013 at 1:38 PM

Hi Taritsyn

But don’t those compress (using regex) the output on a per request basis?

Coordinator
Nov 24, 2013 at 1:50 PM
Hello, Daniel!

By WebMarkupMin (using parser)!

Image
Nov 24, 2013 at 2:10 PM
Edited Nov 24, 2013 at 2:50 PM
Sorry, I don't understand your comment? Oh - you mean you're not using RegEx? But still, don't those action filters have to run that for every request?

Also, I'm having a kind of wierd issue with the HtmlParser.Parser() at present. It's escaping spaces in tags into but only when running in ASP.Net. Any idea what could cause that?