PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

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