using System;
using System.Collections.Generic;
using ScrewTurn.Wiki.PluginFramework;
using System.Web;
namespace ScrewTurn.Wiki {
///
/// Contains methods for formatting content using a pipeline paradigm.
///
public static class FormattingPipeline {
///
/// Gets the formatter providers list sorted by priority.
///
/// The list.
private static IList GetSortedFormatters() {
List providers = new List(Collectors.FormatterProviderCollector.AllProviders);
// Sort by priority, then by name
providers.Sort((x, y) => {
int preliminaryResult = x.ExecutionPriority.CompareTo(y.ExecutionPriority);
if(preliminaryResult != 0) return preliminaryResult;
else return x.Information.Name.CompareTo(y.Information.Name);
});
return providers;
}
///
/// Performs the Phases 1 and 2 of the formatting process.
///
/// The raw WikiMarkup to format.
/// A value indicating whether the formatting is being done for content indexing.
/// The formatting context.
/// The current Page, if any.
/// The formatted content.
public static string FormatWithPhase1And2(string raw, bool forIndexing, FormattingContext context, PageInfo current) {
string[] tempLinks;
return FormatWithPhase1And2(raw, forIndexing, context, current, out tempLinks);
}
///
/// Performs the Phases 1 and 2 of the formatting process.
///
/// The raw WikiMarkup to format.
/// A value indicating whether the formatting is being done for content indexing.
/// The formatting context.
/// The current Page, if any.
/// The Pages linked by the current Page.
/// The formatted content.
public static string FormatWithPhase1And2(string raw, bool forIndexing, FormattingContext context, PageInfo current, out string[] linkedPages) {
ContextInformation info = null;
string username = SessionFacade.CurrentUsername;
info = new ContextInformation(forIndexing, false, context, current, System.Threading.Thread.CurrentThread.CurrentCulture.Name, HttpContext.Current,
username, SessionFacade.GetCurrentGroupNames());
IList providers = GetSortedFormatters();
// Phase 1
foreach(IFormatterProviderV30 provider in providers) {
if(provider.PerformPhase1) {
try {
raw = provider.Format(raw, info, FormattingPhase.Phase1);
}
catch(Exception ex) {
Log.LogEntry("Provider " + provider.Information.Name + " failed to perform Phase1 (silently resuming from next provider): " + ex.ToString(), EntryType.Error, Log.SystemUsername);
}
}
}
raw = Formatter.Format(raw, forIndexing, context, current, out linkedPages);
// Phase 2
foreach(IFormatterProviderV30 provider in providers) {
if(provider.PerformPhase2) {
try {
raw = provider.Format(raw, info, FormattingPhase.Phase2);
}
catch(Exception ex) {
Log.LogEntry("Provider " + provider.Information.Name + " failed to perform Phase2 (silently resuming from next provider): " + ex.ToString(), EntryType.Error, Log.SystemUsername);
}
}
}
return raw;
}
///
/// Performs the Phase 3 of the formatting process.
///
/// The raw WikiMarkup to format.
/// The formatting context.
/// The current Page, if any.
/// The formatted content.
public static string FormatWithPhase3(string raw, FormattingContext context, PageInfo current) {
raw = Formatter.FormatPhase3(raw, context, current);
ContextInformation info = null;
string username = SessionFacade.CurrentUsername;
info = new ContextInformation(false, false, context, current, System.Threading.Thread.CurrentThread.CurrentCulture.Name, HttpContext.Current,
username, SessionFacade.GetCurrentGroupNames());
// Phase 3
foreach(IFormatterProviderV30 provider in GetSortedFormatters()) {
if(provider.PerformPhase3) {
try {
raw = provider.Format(raw, info, FormattingPhase.Phase3);
}
catch(Exception ex) {
Log.LogEntry("Provider " + provider.Information.Name + " failed to perform Phase3 (silently resuming from next provider): " + ex.ToString(), EntryType.Error, Log.SystemUsername);
}
}
}
return raw;
}
///
/// Prepares the title of an item for display.
///
/// The input title.
/// A value indicating whether the formatting is being done for content indexing.
/// The context information.
/// The current page, if any.
/// The prepared title, properly sanitized.
public static string PrepareTitle(string title, bool forIndexing, FormattingContext context, PageInfo current) {
string temp = title;
ContextInformation info = new ContextInformation(forIndexing, false, context, current, System.Threading.Thread.CurrentThread.CurrentCulture.Name,
HttpContext.Current, SessionFacade.GetCurrentUsername(), SessionFacade.GetCurrentGroupNames());
foreach(IFormatterProviderV30 prov in GetSortedFormatters()) {
temp = prov.PrepareTitle(temp, info);
}
return PrepareItemTitle(temp);
}
///
/// Prepares the title of an item for safe display.
///
/// The title.
/// The sanitized title.
private static string PrepareItemTitle(string title) {
return Formatter.StripHtml(title)
.Replace("\"", """)
.Replace("'", "'")
.Replace("<", "<").Replace(">", ">")
.Replace("[", "[").Replace("]", "]"); // This avoid endless loops in Formatter
}
}
}