Example #1
0
    public static RazorHtmlDocument GetHtmlDocument(RazorCodeDocument codeDocument)
    {
        var options = codeDocument.GetCodeGenerationOptions();

        if (options == null || !options.DesignTime)
        {
            // Not needed in run time. This pass generates the backing HTML document that is used to provide HTML intellisense.
            return(null);
        }

        var writer     = new RazorHtmlWriter(codeDocument.Source);
        var syntaxTree = codeDocument.GetSyntaxTree();

        writer.Visit(syntaxTree.Root);

        var generatedHtml = writer.Builder.ToString();

        Debug.Assert(
            writer.Source.Length == writer.Builder.Length,
            $"The backing HTML document should be the same length as the original document. Expected: {writer.Source.Length} Actual: {writer.Builder.Length}");

        var razorHtmlDocument = new DefaultRazorHtmlDocument(generatedHtml, options);

        return(razorHtmlDocument);
    }