diff --git a/src/Orchard/Mvc/Html/ContentCaptureBlock.cs b/src/Orchard/Mvc/Html/ContentCaptureBlock.cs new file mode 100644 index 000000000..fe3a1b11a --- /dev/null +++ b/src/Orchard/Mvc/Html/ContentCaptureBlock.cs @@ -0,0 +1,21 @@ +using System.Web.Mvc; +using System.Web.Mvc.Html; +using Orchard.UI.ContentCapture; + +namespace Orchard.Mvc.Html { + public class ContentCaptureBlock : MvcForm { + private readonly ViewContext _viewContext; + private readonly IContentCapture _contentCapture; + + public ContentCaptureBlock(ViewContext viewContext, IContentCapture contentCapture) + : base(viewContext) { + _viewContext = viewContext; + _contentCapture = contentCapture; + } + + protected override void Dispose(bool disposing) { + //_viewContext.HttpContext.Response.Filter.Flush(); + _contentCapture.EndContentCapture(); + } + } +} \ No newline at end of file diff --git a/src/Orchard/Mvc/Html/ContentCaptureExtensions.cs b/src/Orchard/Mvc/Html/ContentCaptureExtensions.cs new file mode 100644 index 000000000..7757e6f90 --- /dev/null +++ b/src/Orchard/Mvc/Html/ContentCaptureExtensions.cs @@ -0,0 +1,15 @@ +using System.Web.Mvc; +using Orchard.UI.ContentCapture; + +namespace Orchard.Mvc.Html { + public static class ContentCaptureExtensions { + public static ContentCaptureBlock CaptureContent(this HtmlHelper htmlHelper, string captureName) { + IContentCapture contentCapture = htmlHelper.Resolve(); + + //htmlHelper.ViewContext.HttpContext.Response.Filter.Flush(); + contentCapture.BeginContentCapture(captureName); + + return new ContentCaptureBlock(htmlHelper.ViewContext, contentCapture); + } + } +} \ No newline at end of file diff --git a/src/Orchard/Mvc/Html/LayoutExtensions.cs b/src/Orchard/Mvc/Html/LayoutExtensions.cs index c9b7a574c..e78184e36 100644 --- a/src/Orchard/Mvc/Html/LayoutExtensions.cs +++ b/src/Orchard/Mvc/Html/LayoutExtensions.cs @@ -66,5 +66,9 @@ namespace Orchard.Mvc.Html { public static void RegisterFootScript(this HtmlHelper html, string fileName) { html.Resolve().RegisterFootScript(fileName, html); } + + public static ContentCaptureBlock RegisterInlineScript(this HtmlHelper html, string name) { + return html.CaptureContent("inlinescript:" + name); + } } } \ No newline at end of file diff --git a/src/Orchard/Orchard.csproj b/src/Orchard/Orchard.csproj index d75c4f1bc..b5302fa08 100644 --- a/src/Orchard/Orchard.csproj +++ b/src/Orchard/Orchard.csproj @@ -125,6 +125,8 @@ + + @@ -225,11 +227,14 @@ + + + diff --git a/src/Orchard/UI/ContentCapture/ContentCaptureFilter.cs b/src/Orchard/UI/ContentCapture/ContentCaptureFilter.cs new file mode 100644 index 000000000..08cc93332 --- /dev/null +++ b/src/Orchard/UI/ContentCapture/ContentCaptureFilter.cs @@ -0,0 +1,24 @@ +using System.IO; +using System.Web.Mvc; +using Orchard.Mvc.Filters; + +namespace Orchard.UI.ContentCapture { + public class ContentCaptureFilter : FilterProvider, IResultFilter { + private readonly IContentCapture _contentCapture; + + public ContentCaptureFilter(IContentCapture contentCapture) { + _contentCapture = contentCapture; + } + + public void OnResultExecuting(ResultExecutingContext filterContext) { + if (filterContext.Result is ViewResult) { + _contentCapture.CaptureStream = filterContext.HttpContext.Response.Filter; + filterContext.HttpContext.Response.Filter = _contentCapture as Stream; + filterContext.HttpContext.Response.Buffer = false; + } + } + + public void OnResultExecuted(ResultExecutedContext filterContext) { + } + } +} \ No newline at end of file diff --git a/src/Orchard/UI/ContentCapture/ContentCaptureStream.cs b/src/Orchard/UI/ContentCapture/ContentCaptureStream.cs new file mode 100644 index 000000000..5647ec31b --- /dev/null +++ b/src/Orchard/UI/ContentCapture/ContentCaptureStream.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace Orchard.UI.ContentCapture { + public class ContentCaptureStream : Stream, IContentCapture { + private List _contentBuffer; + private string _contentCaptureName; + private readonly Dictionary _contents; + + public ContentCaptureStream() { + _contents = new Dictionary(20); + } + + public Stream CaptureStream { get; set; } + + public override bool CanRead { + get { return CaptureStream.CanRead; } + } + + public override bool CanSeek { + get { return CaptureStream.CanSeek; } + } + + public override bool CanWrite { + get { return CaptureStream.CanWrite; } + } + + public override long Length { + get { return CaptureStream.Length; } + } + + public override long Position { + get { return CaptureStream.Position; } + set { CaptureStream.Position = value; } + } + + public override void Flush() { + CaptureStream.Flush(); + } + + public override int Read(byte[] buffer, int offset, int count) { + return CaptureStream.Read(buffer, offset, count); + } + + public override long Seek(long offset, SeekOrigin origin) { + return CaptureStream.Seek(offset, origin); + } + + public override void SetLength(long value) { + CaptureStream.SetLength(value); + } + + public override void Write(byte[] buffer, int offset, int count) { + if (_contentCaptureName != null) + _contentBuffer.AddRange(buffer); + else + CaptureStream.Write(buffer, 0, buffer.Length); + } + + public Dictionary GetContents() { + if (_contentCaptureName != null) + throw new ApplicationException("Can not get contents while capturing content"); + + Dictionary contents = new Dictionary(_contents.Count); + + foreach (KeyValuePair content in contents) + contents.Add(content.Key, content.Value); + _contents.Clear(); + + return contents; + } + + public void BeginContentCapture(string name) { + if (_contentCaptureName != null) + throw new ApplicationException("There is already content being captured"); + + _contentBuffer = new List(5000); + _contentCaptureName = name; + } + + public void EndContentCapture() { + if (_contentCaptureName == null) + throw new ApplicationException("There is currently no content being captured"); + + _contents.Add(_contentCaptureName, Encoding.UTF8.GetString(_contentBuffer.ToArray(), 0, _contentBuffer.Count)); + _contentCaptureName = null; + _contentBuffer = null; + } + } +} \ No newline at end of file diff --git a/src/Orchard/UI/ContentCapture/IContentCapture.cs b/src/Orchard/UI/ContentCapture/IContentCapture.cs new file mode 100644 index 000000000..7cecb254f --- /dev/null +++ b/src/Orchard/UI/ContentCapture/IContentCapture.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using System.IO; + +namespace Orchard.UI.ContentCapture { + public interface IContentCapture : IDependency { + Stream CaptureStream { get; set; } + Dictionary GetContents(); + void BeginContentCapture(string name); + void EndContentCapture(); + } +} \ No newline at end of file