PageRenderTime 45ms CodeModel.GetById 30ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/BlogEngine/BlogEngine.NET/themes/RazorHost/site.master.cs

#
C# | 62 lines | 50 code | 11 blank | 1 comment | 6 complexity | 9c4f025d4c35492ce8b5ca3e190558b3 MD5 | raw file
 1using System;
 2using System.Web;
 3using System.Web.UI;
 4using System.Web.UI.HtmlControls;
 5using BlogEngine.Core;
 6using System.IO;
 7using System.Web.Compilation;
 8using System.Text.RegularExpressions;
 9
10public partial class RazorHostSite : System.Web.UI.MasterPage
11{
12    protected void Page_Load(object sender, EventArgs e)
13    {
14        ParseAndInjectRazor();
15
16    }
17
18    private void ParseAndInjectRazor()
19    {
20        string parsedRazor = RazorHelpers.ParseRazor(RazorHelpers.RAZOR_HOST_PAGE_VPATH, null);
21
22        if (!string.IsNullOrWhiteSpace(parsedRazor))
23        {
24            Regex headRgx = new Regex("<head\\b[^>]*>(.*?)</head>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
25            Match headMatch = headRgx.Match(parsedRazor);
26            if (headMatch.Success)
27            {
28                string headContent = headMatch.Groups[1].Value;
29
30                // remove the <title> tag.
31                headContent = new Regex("<title\\b[^>]*>(.*?)</title>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Replace(headContent, string.Empty);
32
33                phRazorHead.Controls.Add(new LiteralControl(headContent));
34            }
35
36            Regex bodyRgx = new Regex("<body\\b([^>]*)>(.*?)</body>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
37            Match bodyMatch = bodyRgx.Match(parsedRazor);
38            if (bodyMatch.Success)
39            {
40                string bodyAttributes = bodyMatch.Groups[1].Value.Trim();
41                if (!string.IsNullOrWhiteSpace(bodyAttributes))
42                {
43                    this.BodyAttributes = " " + bodyAttributes;
44                }
45
46                string body = bodyMatch.Groups[2].Value;
47
48                int placeHolderPos = body.IndexOf(RazorHelpers.PAGE_BODY_MARKER, StringComparison.OrdinalIgnoreCase);
49                if (placeHolderPos != -1)
50                {
51                    string beforePlaceholderMarkup = body.Substring(0, placeHolderPos);
52                    string afterPlaceholderMarkup = body.Substring(placeHolderPos + RazorHelpers.PAGE_BODY_MARKER.Length);
53
54                    phBeforePageBody.Controls.Add(new LiteralControl(beforePlaceholderMarkup));
55                    phAfterPageBody.Controls.Add(new LiteralControl(afterPlaceholderMarkup));
56                }
57            }
58        }
59    }
60
61    public string BodyAttributes { get; set; }
62}