using System; using System.Collections.Generic; using System.IO; using ScrewTurn.Wiki.PluginFramework; using System.Text.RegularExpressions; namespace ScrewTurn.Wiki { /// /// Manages snippets. /// public static class Snippets { /// /// Gets the complete list of the Snippets. /// /// The snippets, sorted by name. public static List GetSnippets() { List allSnippets = new List(50); // Retrieve all snippets from Pages Provider foreach(IPagesStorageProviderV30 provider in Collectors.PagesProviderCollector.AllProviders) { allSnippets.AddRange(provider.GetSnippets()); } allSnippets.Sort(new SnippetNameComparer()); return allSnippets; } /// /// Finds a Snippet. /// /// The Name of the Snippet to find. /// The Snippet or null if it is not found. public static Snippet Find(string name) { List allSnippets = GetSnippets(); int result = allSnippets.BinarySearch(new Snippet(name, "", null), new SnippetNameComparer()); if(allSnippets.Count > 0 && result >= 0) return allSnippets[result]; else return null; } /// /// Creates a new Snippet. /// /// The name of the Snippet. /// The content of the Snippet. /// The Provider to use to store the Snippet (null for the default provider). /// True if the Snippets has been addedd successfully. public static bool AddSnippet(string name, string content, IPagesStorageProviderV30 provider) { if(Find(name) != null) return false; if(provider == null) provider = Collectors.PagesProviderCollector.GetProvider(Settings.DefaultPagesProvider); Snippet newSnippet = provider.AddSnippet(name, content); if(newSnippet != null) { Log.LogEntry("Snippet " + name + " created", EntryType.General, Log.SystemUsername); Content.ClearPseudoCache(); Content.InvalidateAllPages(); } else Log.LogEntry("Creation failed for Snippet " + name, EntryType.Error, Log.SystemUsername); return newSnippet != null; } /// /// Removes a Snippet. /// /// The Snippet to remove. /// True if the Snippet has been removed successfully. public static bool RemoveSnippet(Snippet snippet) { bool done = snippet.Provider.RemoveSnippet(snippet.Name); if(done) { Log.LogEntry("Snippet " + snippet.Name + " deleted", EntryType.General, Log.SystemUsername); Content.ClearPseudoCache(); Content.InvalidateAllPages(); } else Log.LogEntry("Deletion failed for Snippet " + snippet.Name, EntryType.Error, Log.SystemUsername); return done; } /// /// Modifies the Content of a Snippet. /// /// The Snippet to update. /// The new Content. /// True if the Snippet has been updated successfully. public static bool ModifySnippet(Snippet snippet, string content) { Snippet newSnippet = snippet.Provider.ModifySnippet(snippet.Name, content); if(newSnippet != null) { Log.LogEntry("Snippet " + snippet.Name + " updated", EntryType.General, Log.SystemUsername); Content.ClearPseudoCache(); Content.InvalidateAllPages(); } else Log.LogEntry("Modification failed for Snippet " + snippet.Name, EntryType.Error, Log.SystemUsername); return newSnippet != null; } /// /// The regular expression to use for extracting parameters. /// public static readonly Regex ParametersRegex = new Regex("\\?[a-zA-Z0-9_-]+\\?", RegexOptions.Compiled | RegexOptions.CultureInvariant); /// /// Counts the parameters in a snippet. /// /// The snippet. /// The number of parameters. public static int CountParameters(Snippet snippet) { return ExtractParameterNames(snippet).Length; } /// /// Finds the parameters in a snippet. /// /// The snippet. /// The parameter names. public static string[] ExtractParameterNames(Snippet snippet) { List parms = new List(); foreach(Match m in ParametersRegex.Matches(snippet.Content)) { string value = m.Value.Substring(1, m.Value.Length - 2); if(m.Success && !parms.Contains(value)) parms.Add(value); } return parms.ToArray(); } } }