From 590613786da737ea737d2350e3b330ecb875c1c7 Mon Sep 17 00:00:00 2001 From: Renaud Paquay Date: Sun, 25 Jul 2010 11:50:23 -0700 Subject: [PATCH] Fix issue with Contoso and gallery pictures We now support the BBCode syntax for static html files serving as the basis for Widgets. All this stuff is still futures work, but we needed a temporary solution for Alpha. Here is the new syntax for one of the Widget file:
--HG-- branch : dev --- .../Core/Common/Drivers/BodyPartDriver.cs | 40 ++++++-------- .../Core/Common/Services/BbcodeFilter.cs | 52 +++++++++++++++++++ src/Orchard.Web/Core/Orchard.Core.csproj | 1 + .../Controllers/AdminController.cs | 15 ++++-- .../DesignerNotes/ZoneManagerEvents.cs | 28 ++++++++-- .../Contoso/Zones/Home-Hero-Gallery.html | 6 +-- .../Corporate/Zones/Home-Hero-Gallery.html | 3 +- .../Themes/Corporate/Zones/Home-Hero.html | 10 ++-- src/Orchard/Orchard.Framework.csproj | 1 + src/Orchard/Services/IHtmlFilter.cs | 5 ++ 10 files changed, 121 insertions(+), 40 deletions(-) create mode 100644 src/Orchard.Web/Core/Common/Services/BbcodeFilter.cs create mode 100644 src/Orchard/Services/IHtmlFilter.cs diff --git a/src/Orchard.Web/Core/Common/Drivers/BodyPartDriver.cs b/src/Orchard.Web/Core/Common/Drivers/BodyPartDriver.cs index bc49bf5e6..6576ee9a5 100644 --- a/src/Orchard.Web/Core/Common/Drivers/BodyPartDriver.cs +++ b/src/Orchard.Web/Core/Common/Drivers/BodyPartDriver.cs @@ -1,33 +1,42 @@ -using System.Text.RegularExpressions; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; using JetBrains.Annotations; using Orchard.ContentManagement; using Orchard.ContentManagement.Aspects; using Orchard.ContentManagement.Drivers; using Orchard.Core.Common.Models; +using Orchard.Core.Common.Services; using Orchard.Core.Common.Settings; using Orchard.Core.Common.ViewModels; using Orchard.Core.ContentsLocation.Models; using Orchard.Core.Routable.Models; +using Orchard.Services; namespace Orchard.Core.Common.Drivers { [UsedImplicitly] public class BodyPartDriver : ContentPartDriver { - public IOrchardServices Services { get; set; } + private readonly IEnumerable _htmlFilters; + private const string TemplateName = "Parts/Common.Body"; private const string DefaultTextEditorTemplate = "TinyMceTextEditor"; private const string PlainTextEditorTemplate = "PlainTextEditor"; - public BodyPartDriver(IOrchardServices services) { + public BodyPartDriver(IOrchardServices services, IEnumerable htmlFilters) { + _htmlFilters = htmlFilters; Services = services; } + public IOrchardServices Services { get; set; } + protected override string Prefix { get { return "Body"; } } // \/\/ Hackalicious on many accounts - don't copy what has been done here for the wrapper \/\/ protected override DriverResult Display(BodyPart part, string displayType) { - var model = new BodyDisplayViewModel { BodyPart = part, Text = BbcodeReplace(part.Text) }; + var bodyText = _htmlFilters.Aggregate(part.Text, (text, filter) => filter.ProcessContent(text)); + var model = new BodyDisplayViewModel { BodyPart = part, Text = bodyText }; var location = part.GetLocation(displayType); return Combined( @@ -36,7 +45,7 @@ namespace Orchard.Core.Common.Drivers { ContentPartTemplate(model, TemplateName, Prefix).LongestMatch(displayType, "Summary", "SummaryAdmin").Location(location), Services.Authorizer.Authorize(Permissions.ChangeOwner) ? ContentPartTemplate(model, "Parts/Common.Body.ManageWrapperPost").LongestMatch(displayType, "SummaryAdmin").Location(location) : null); } - + protected override DriverResult Editor(BodyPart part) { var model = BuildEditorViewModel(part); var location = part.GetLocation("Editor"); @@ -59,7 +68,7 @@ namespace Orchard.Core.Common.Drivers { return new BodyEditorViewModel { BodyPart = part, TextEditorTemplate = GetFlavor(part) == "html" ? DefaultTextEditorTemplate : PlainTextEditorTemplate, - AddMediaPath= new PathBuilder(part).AddContentType().AddContainerSlug().AddSlug().ToString() + AddMediaPath = new PathBuilder(part).AddContentType().AddContainerSlug().AddSlug().ToString() }; } @@ -118,25 +127,6 @@ namespace Orchard.Core.Common.Drivers { else _path = _path + "/" + segment; } - - } - - - // Can be moved somewhere else once we have IoC enabled body text filters. - private static string BbcodeReplace(string bodyText) { - - if ( string.IsNullOrEmpty(bodyText) ) - return string.Empty; - - Regex urlRegex = new Regex(@"\[url\]([^\]]+)\[\/url\]"); - Regex urlRegexWithLink = new Regex(@"\[url=([^\]]+)\]([^\]]+)\[\/url\]"); - Regex imgRegex = new Regex(@"\[img\]([^\]]+)\[\/img\]"); - - bodyText = urlRegex.Replace(bodyText, "$1"); - bodyText = urlRegexWithLink.Replace(bodyText, "$2"); - bodyText = imgRegex.Replace(bodyText, ""); - - return bodyText; } } } \ No newline at end of file diff --git a/src/Orchard.Web/Core/Common/Services/BbcodeFilter.cs b/src/Orchard.Web/Core/Common/Services/BbcodeFilter.cs new file mode 100644 index 000000000..9574520d7 --- /dev/null +++ b/src/Orchard.Web/Core/Common/Services/BbcodeFilter.cs @@ -0,0 +1,52 @@ +using System.Linq; +using System.Text.RegularExpressions; +using System.Web; +using Orchard.FileSystems.VirtualPath; +using Orchard.Services; + +namespace Orchard.Core.Common.Services { + public class BbcodeFilter : IHtmlFilter { + private readonly IVirtualPathProvider _virtualPathProvider; + + public BbcodeFilter(IVirtualPathProvider virtualPathProvider) { + _virtualPathProvider = virtualPathProvider; + } + + public string ProcessContent(string text) { + return BbcodeReplace(text); + } + + // Can be moved somewhere else once we have IoC enabled body text filters. + private string BbcodeReplace(string text) { + if (string.IsNullOrEmpty(text)) + return string.Empty; + + Regex urlRegex = new Regex(@"\[url\]([^\]]+)\[\/url\]"); + Regex urlRegexWithLink = new Regex(@"\[url=([^\]]+)\]([^\]]+)\[\/url\]"); + Regex imgRegex = new Regex(@"\[img\]([^\]]+)\[\/img\]"); + + text = urlRegex.Replace(text, "$1"); + text = urlRegexWithLink.Replace(text, "$2"); + //text = imgRegex.Replace(text, ""); + + var matches = imgRegex.Matches(text).OfType().OrderByDescending(m => m.Groups[0].Index); + foreach(var match in matches) { + var index = match.Groups[0].Index; + var length = match.Groups[0].Length; + var url = match.Groups[1].Value.Trim(); + if (!string.IsNullOrEmpty(url)) { + if (url[0]=='~') + url = VirtualPathUtility.ToAbsolute(url); + text = + text.Substring(0, index) + + string.Format("", url) + + text.Substring(index + length); + } + } + + text = imgRegex.Replace(text, ""); + + return text; + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Core/Orchard.Core.csproj b/src/Orchard.Web/Core/Orchard.Core.csproj index 18eafc8b7..0ace309a2 100644 --- a/src/Orchard.Web/Core/Orchard.Core.csproj +++ b/src/Orchard.Web/Core/Orchard.Core.csproj @@ -68,6 +68,7 @@ + diff --git a/src/Orchard.Web/Modules/Futures.Widgets/Controllers/AdminController.cs b/src/Orchard.Web/Modules/Futures.Widgets/Controllers/AdminController.cs index 42f4c53d7..660213447 100644 --- a/src/Orchard.Web/Modules/Futures.Widgets/Controllers/AdminController.cs +++ b/src/Orchard.Web/Modules/Futures.Widgets/Controllers/AdminController.cs @@ -1,17 +1,23 @@ -using System.Web.Mvc; +using System.Collections.Generic; +using System.Linq; +using System.Web.Mvc; using Futures.Widgets.Models; using Futures.Widgets.ViewModels; using Orchard; using Orchard.ContentManagement; using Orchard.Core.Common.Models; using Orchard.Localization; +using Orchard.Services; using Orchard.Settings; using Orchard.UI.Notify; namespace Futures.Widgets.Controllers { [ValidateInput(false)] public class AdminController : Controller, IUpdateModel { - public AdminController(IOrchardServices services) { + private readonly IEnumerable _htmlFilters; + + public AdminController(IOrchardServices services, IEnumerable htmlFilters) { + _htmlFilters = htmlFilters; Services = services; T = NullLocalizer.Instance; } @@ -31,11 +37,14 @@ namespace Futures.Widgets.Controllers { return Redirect(returnUrl); } + var fileText = _htmlFilters + .Aggregate(System.IO.File.ReadAllText(physicalPath), (text, filter) => filter.ProcessContent(text)); + var widget = Services.ContentManager.Create("HtmlWidget", init => { init.Record.Scope = hasWidgetsRecord; init.Record.Zone = zoneName; init.Record.Position = "1"; - init.As().Text = System.IO.File.ReadAllText(physicalPath); + init.As().Text = fileText; }); return RedirectToAction("Edit", new { widget.ContentItem.Id, returnUrl }); diff --git a/src/Orchard.Web/Modules/Orchard.Themes/DesignerNotes/ZoneManagerEvents.cs b/src/Orchard.Web/Modules/Orchard.Themes/DesignerNotes/ZoneManagerEvents.cs index bd2aa9f00..72e0922ef 100644 --- a/src/Orchard.Web/Modules/Orchard.Themes/DesignerNotes/ZoneManagerEvents.cs +++ b/src/Orchard.Web/Modules/Orchard.Themes/DesignerNotes/ZoneManagerEvents.cs @@ -1,17 +1,28 @@ -using System.IO; +using System.Collections.Generic; +using System.IO; using System.Linq; using System.Web.Mvc.Html; +using Orchard.Environment.Descriptor; using Orchard.Security; +using Orchard.Services; using Orchard.UI.Zones; namespace Orchard.Themes.DesignerNotes { public class ZoneManagerEvents : IZoneManagerEvents { private readonly IThemeService _themeService; private readonly IAuthorizationService _authorizationService; + private readonly IShellDescriptorManager _shellDescriptorManager; + private readonly IEnumerable _htmlFilters; + + public ZoneManagerEvents(IThemeService themeService, + IAuthorizationService authorizationService, + IShellDescriptorManager shellDescriptorManager, + IEnumerable htmlFilters) { - public ZoneManagerEvents(IThemeService themeService, IAuthorizationService authorizationService) { _themeService = themeService; _authorizationService = authorizationService; + _shellDescriptorManager = shellDescriptorManager; + _htmlFilters = htmlFilters; } public virtual IUser CurrentUser { get; set; } @@ -29,6 +40,13 @@ namespace Orchard.Themes.DesignerNotes { var accessAdminPanel = _authorizationService.TryCheckAccess( StandardPermissions.AccessAdminPanel, CurrentUser, null); + if (accessAdminPanel) { + //Temporary: Don't show "edit" button if "Futures.Widgets" is not enabled. + accessAdminPanel = _shellDescriptorManager + .GetShellDescriptor() + .Features + .Any(f => f.Name == "Futures.Widgets"); + } var writer = context.Html.ViewContext.Writer; if (accessAdminPanel) { @@ -42,7 +60,11 @@ namespace Orchard.Themes.DesignerNotes { })); writer.Write(""); } - writer.Write(File.ReadAllText(physicalPath)); + + var fileText = _htmlFilters + .Aggregate(File.ReadAllText(physicalPath), (text, filter) => filter.ProcessContent(text)); + + writer.Write(fileText); if (accessAdminPanel) { writer.Write(""); } diff --git a/src/Orchard.Web/Themes/Contoso/Zones/Home-Hero-Gallery.html b/src/Orchard.Web/Themes/Contoso/Zones/Home-Hero-Gallery.html index 4a51637e1..e364c1027 100644 --- a/src/Orchard.Web/Themes/Contoso/Zones/Home-Hero-Gallery.html +++ b/src/Orchard.Web/Themes/Contoso/Zones/Home-Hero-Gallery.html @@ -1,7 +1,7 @@ 
    -
  • Orchard Rocks
  • -
  • Orchard FTW
  • -
  • Orchard Time
  • +
  • [img]~/Themes/Contoso/Content/Images/Gallery/feature01.jpg[/img]
  • +
  • [img]~/Themes/Contoso/Content/Images/Gallery/feature02.jpg[/img]
  • +
  • [img]~/Themes/Contoso/Content/Images/Gallery/feature03.jpg[/img]
diff --git a/src/Orchard.Web/Themes/Corporate/Zones/Home-Hero-Gallery.html b/src/Orchard.Web/Themes/Corporate/Zones/Home-Hero-Gallery.html index c25ef50b4..97ee3f409 100644 --- a/src/Orchard.Web/Themes/Corporate/Zones/Home-Hero-Gallery.html +++ b/src/Orchard.Web/Themes/Corporate/Zones/Home-Hero-Gallery.html @@ -1,2 +1,3 @@ 
-
\ No newline at end of file + [img]~/Themes/Corporate/Content/Images/jumping-people.jpg[/img] + \ No newline at end of file diff --git a/src/Orchard.Web/Themes/Corporate/Zones/Home-Hero.html b/src/Orchard.Web/Themes/Corporate/Zones/Home-Hero.html index e724d418b..68a352c4a 100644 --- a/src/Orchard.Web/Themes/Corporate/Zones/Home-Hero.html +++ b/src/Orchard.Web/Themes/Corporate/Zones/Home-Hero.html @@ -9,16 +9,16 @@ diff --git a/src/Orchard/Orchard.Framework.csproj b/src/Orchard/Orchard.Framework.csproj index 2b5a64c78..2969c3374 100644 --- a/src/Orchard/Orchard.Framework.csproj +++ b/src/Orchard/Orchard.Framework.csproj @@ -369,6 +369,7 @@
+ diff --git a/src/Orchard/Services/IHtmlFilter.cs b/src/Orchard/Services/IHtmlFilter.cs new file mode 100644 index 000000000..40ad26301 --- /dev/null +++ b/src/Orchard/Services/IHtmlFilter.cs @@ -0,0 +1,5 @@ +namespace Orchard.Services { + public interface IHtmlFilter : IDependency { + string ProcessContent(string text); + } +} \ No newline at end of file