mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-14 19:04:51 +08:00
Hooking up a content capture mechanism (doesn't yet work, but doesn't break anything).
--HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4044272
This commit is contained in:
21
src/Orchard/Mvc/Html/ContentCaptureBlock.cs
Normal file
21
src/Orchard/Mvc/Html/ContentCaptureBlock.cs
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
15
src/Orchard/Mvc/Html/ContentCaptureExtensions.cs
Normal file
15
src/Orchard/Mvc/Html/ContentCaptureExtensions.cs
Normal file
@@ -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<IContentCapture>();
|
||||
|
||||
//htmlHelper.ViewContext.HttpContext.Response.Filter.Flush();
|
||||
contentCapture.BeginContentCapture(captureName);
|
||||
|
||||
return new ContentCaptureBlock(htmlHelper.ViewContext, contentCapture);
|
||||
}
|
||||
}
|
||||
}
|
@@ -66,5 +66,9 @@ namespace Orchard.Mvc.Html {
|
||||
public static void RegisterFootScript(this HtmlHelper html, string fileName) {
|
||||
html.Resolve<IResourceManager>().RegisterFootScript(fileName, html);
|
||||
}
|
||||
|
||||
public static ContentCaptureBlock RegisterInlineScript(this HtmlHelper html, string name) {
|
||||
return html.CaptureContent("inlinescript:" + name);
|
||||
}
|
||||
}
|
||||
}
|
@@ -125,6 +125,8 @@
|
||||
<Compile Include="Environment\IOrchardShellEvents.cs" />
|
||||
<Compile Include="Extensions\ExtensionDescriptor.cs" />
|
||||
<Compile Include="Extensions\ExtensionEntry.cs" />
|
||||
<Compile Include="Mvc\Html\ContentCaptureBlock.cs" />
|
||||
<Compile Include="Mvc\Html\ContentCaptureExtensions.cs" />
|
||||
<Compile Include="Mvc\Html\FileRegistrationContext.cs" />
|
||||
<Compile Include="Themes\ExtensionManagerExtensions.cs" />
|
||||
<Compile Include="Extensions\Helpers\PathHelpers.cs" />
|
||||
@@ -225,11 +227,14 @@
|
||||
<Compile Include="Mvc\ViewEngines\WebFormsViewEngineProvider.cs" />
|
||||
<Compile Include="Mvc\ViewModels\AdminViewModel.cs" />
|
||||
<Compile Include="Mvc\ViewModels\BaseViewModel.cs" />
|
||||
<Compile Include="UI\ContentCapture\ContentCaptureFilter.cs" />
|
||||
<Compile Include="UI\ContentCapture\IContentCapture.cs" />
|
||||
<Compile Include="UI\PageTitle\IPageTitleBuilder.cs" />
|
||||
<Compile Include="UI\PageTitle\PageTitleBuilder.cs" />
|
||||
<Compile Include="UI\Resources\IResourceManager.cs" />
|
||||
<Compile Include="UI\Resources\ResourceFilter.cs" />
|
||||
<Compile Include="UI\Resources\ResourceManager.cs" />
|
||||
<Compile Include="UI\ContentCapture\ContentCaptureStream.cs" />
|
||||
<Compile Include="UI\Zones\DelegateZoneItem.cs" />
|
||||
<Compile Include="UI\Zones\ItemDisplayZoneItem.cs" />
|
||||
<Compile Include="UI\Zones\IZoneManager.cs" />
|
||||
|
24
src/Orchard/UI/ContentCapture/ContentCaptureFilter.cs
Normal file
24
src/Orchard/UI/ContentCapture/ContentCaptureFilter.cs
Normal file
@@ -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) {
|
||||
}
|
||||
}
|
||||
}
|
92
src/Orchard/UI/ContentCapture/ContentCaptureStream.cs
Normal file
92
src/Orchard/UI/ContentCapture/ContentCaptureStream.cs
Normal file
@@ -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<byte> _contentBuffer;
|
||||
private string _contentCaptureName;
|
||||
private readonly Dictionary<string, string> _contents;
|
||||
|
||||
public ContentCaptureStream() {
|
||||
_contents = new Dictionary<string, string>(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<string, string> GetContents() {
|
||||
if (_contentCaptureName != null)
|
||||
throw new ApplicationException("Can not get contents while capturing content");
|
||||
|
||||
Dictionary<string, string> contents = new Dictionary<string, string>(_contents.Count);
|
||||
|
||||
foreach (KeyValuePair<string, string> 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<byte>(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;
|
||||
}
|
||||
}
|
||||
}
|
11
src/Orchard/UI/ContentCapture/IContentCapture.cs
Normal file
11
src/Orchard/UI/ContentCapture/IContentCapture.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace Orchard.UI.ContentCapture {
|
||||
public interface IContentCapture : IDependency {
|
||||
Stream CaptureStream { get; set; }
|
||||
Dictionary<string, string> GetContents();
|
||||
void BeginContentCapture(string name);
|
||||
void EndContentCapture();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user