/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using Everest.Library.ExtensionMethod;
using Everest.Library.Web;
using System.Web.Mvc;
namespace Everest.CmsServices.MvcHelper{
/// <summary>
///
/// </summary>
public class ThemeRulesInterpretor
{
const string ThemeRulesFileName = "CSSRules.txt";
static Regex hrefReg = new Regex(@"href=""(?<FilePath>[^""]+)""", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private string themePath;
List<string> files = new List<string>();
public ThemeRulesInterpretor(string themePath, UrlHelper urlHelper)
{
this.themePath = themePath;
string themeCssRuleFilePath = Path.Combine(themePath, ThemeRulesFileName);
if (File.Exists(themeCssRuleFilePath))
{
string text = null;
using (FileStream fs = new FileStream(themeCssRuleFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
text = fs.ReadString();
}
if (!StringExtensions.IsNullOrEmptyTrim(text))
{
StringBuilder textBuilder = new StringBuilder(text);
MatchCollection hrefs = hrefReg.Matches(text);
foreach (Match match in hrefs)
{
if (!StringExtensions.IsNullOrEmptyTrim(match.Groups["FilePath"].Value))
{
string filePath = Path.Combine(themePath, match.Groups["FilePath"].Value);
//add file to ignored.
files.Add(filePath);
string repleacedHref = "href=\"" + urlHelper.ResolveUrl(UrlConvertor.AbsolutePathToRelativeUrl(filePath)) + "\"";
textBuilder.Replace(match.Value, repleacedHref);
}
}
RepleacedText = textBuilder.ToString();
}
}
}
public IEnumerable<string> Files
{
get
{
return files;
}
}
public string RepleacedText { get; private set; }
}
}
|