using System;
using System.IO;
using umbraco.BusinessLogic.Actions;
using System.Text.RegularExpressions;
namespace umbracoUtilities
{
///
/// Summary description for saveStatic.
///
public class saveStatic : umbraco.BusinessLogic.Actions.IActionHandler
{
public saveStatic() {}
#region IActionHandler Members
public string HandlerName()
{
return "SaveStatic";
}
public bool Execute(umbraco.cms.businesslogic.web.Document documentObject, umbraco.main.IAction action)
{
// If you wanted to you could check which action is parsed and do different stuff according to that.
// This is pretty easy when just publishing, but on delete, move and sort you might need to update other pages
// (because of lists and navigation)
// This is the root of our static pages
string root = "/static";
// The pageID is normally updated through the requestHandler which is byparsed here.
// Therefore we need to set it manually as it's used by some xslt helper functions
System.Web.HttpContext.Current.Items["pageID"] = documentObject.Id;
// Get the current page object from the document object
umbraco.page p = new umbraco.page(documentObject.Id, documentObject.Version);
// Find url name
string urlName = umbraco.library.NiceUrl(documentObject.Id);
// Replace url name with .html
urlName = urlName.Replace(".aspx", ".html");
// Render the contents of the page controls
// into a stringwriter and save the content of the stringwriter into a string
System.IO.StringWriter writer = new StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(writer);
p.PageContentControl.RenderControl(hw);
string pageContents = writer.ToString();
// Replace internal url names (from aspx to .html)
MatchCollection links = Regex.Matches(pageContents, "]*href=\"(/[^>]*.aspx)\">", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
foreach (Match link in links)
{
pageContents = pageContents.Replace(link.Value, link.Value.Replace(link.Groups[0].Value, link.Groups[0].Value.Replace(".aspx", ".html")));
}
// Maybe create subdirectories
string[] dirs = urlName.Substring(1, urlName.Length-1).Split("/".ToCharArray());
string currDir = root;
if (dirs.Length > 0)
{
for (int i=0; i