Example #1
0
        private static string Get(cmSite site, string lang, string path)
        {
            if (site == null)
            {
                site = SiteManager.Current;
            }
            if (string.IsNullOrEmpty(lang))
            {
                lang = MultilingualMgr.GetCurrentCulture();
            }

            string cacheKey = string.Format("snippet_{0}_{1}_{2}", site.DistinctName, lang, path);
            string value    = HttpRuntime.Cache[cacheKey] as string;

            if (value != null)
            {
                return(value);
            }

            PreparsedContent preparsedContent = GetPreparsedContent(site, path);
            StringBuilder    content          = new StringBuilder();

            int startIndex   = 0;
            int endIndex     = 0;
            int nextTagIndex = 0;

            PreparsedContentTag tag = null;

            if (!string.IsNullOrEmpty(preparsedContent.Content))
            {
                for (; ;)
                {
                    if (nextTagIndex < preparsedContent.Tags.Count)
                    {
                        tag      = preparsedContent.Tags[nextTagIndex];
                        endIndex = tag.StartIndex;
                    }
                    else
                    {
                        tag      = null;
                        endIndex = preparsedContent.Content.Length;
                    }

                    if (endIndex > startIndex)
                    {
                        content.Append(preparsedContent.Content.Substring(startIndex, endIndex - startIndex));
                    }

                    if (tag == null)
                    {
                        break;
                    }

                    string metadata = Metadata.Get(site, tag.MetadataPath, lang);
                    switch (tag.Method)
                    {
                    case "htmlencode":
                        content.Append(metadata.SafeHtmlEncode());
                        break;

                    case "scriptencode":
                        content.Append(metadata.SafeJavascriptStringEncode());
                        break;

                    default:
                        content.Append(metadata);
                        break;
                    }

                    startIndex = tag.StartIndex + tag.Length;
                    nextTagIndex++;
                }
            }

            HttpRuntime.Cache.Insert(cacheKey
                                     , content
                                     , new CacheDependencyEx(preparsedContent.DependedFiles.ToArray(), false)
                                     , Cache.NoAbsoluteExpiration
                                     , Cache.NoSlidingExpiration
                                     , CacheItemPriority.NotRemovable
                                     , null
                                     );


            return(content.ToString());
        }
Example #2
0
        private static PreparsedContent GetPreparsedContent(cmSite site, string path)
        {
            string           cacheKey         = string.Format("snippet_{0}_preparsed_{1}", site.DistinctName, path);
            PreparsedContent preparsedContent = HttpRuntime.Cache[cacheKey] as PreparsedContent;

            if (preparsedContent != null)
            {
                return(preparsedContent);
            }

            preparsedContent = new PreparsedContent();

            List <string> paths = new List <string>();

            paths.Add(string.Format(CultureInfo.InvariantCulture, "~/Views/{0}/{1}", site.DistinctName, path.TrimStart('/')));
            if (!string.IsNullOrEmpty(site.TemplateDomainDistinctName))
            {
                paths.Add(string.Format(CultureInfo.InvariantCulture, "~/Views/{0}/{1}", site.TemplateDomainDistinctName, path.TrimStart('/')));
            }

            string content = null;

            foreach (string relativePath in paths)
            {
                string physicalPath = HostingEnvironment.MapPath(relativePath);
                preparsedContent.DependedFiles.Add(physicalPath);
                content = WinFileIO.ReadWithoutLock(physicalPath);
                if (content == null)
                {
                    continue;
                }
                break;
            }

            if (!string.IsNullOrEmpty(content))
            {
                // remove comments
                content = Regex.Replace(content
                                        , @"(\<\!\-\-)(.*?)(\-\-\>)"
                                        , string.Empty
                                        , RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.Singleline
                                        );
                preparsedContent.Content = content;

                // parse tags
                string currentMetadataPath = Regex.Replace(path
                                                           , @"(\/[^\/]+)$"
                                                           , delegate(Match m) { return(string.Format(CultureInfo.InvariantCulture, "/_{0}", Regex.Replace(m.ToString().TrimStart('/'), @"[^\w\-_]", "_", RegexOptions.Compiled))); }
                                                           , RegexOptions.Compiled);

                MatchCollection matches = Regex.Matches(content
                                                        , @"\[(\s*)metadata(\s*)\:(\s*)(?<method>((htmlencode)|(scriptencode)|(value)))(\s*)\((\s*)(?<path>(\w|\/|\-|\_|\.)+)(\s*)\)(\s*)\]"
                                                        , RegexOptions.Multiline | RegexOptions.ECMAScript | RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant
                                                        );
                foreach (Match match in matches)
                {
                    PreparsedContentTag tag = new PreparsedContentTag()
                    {
                        StartIndex   = match.Index,
                        Length       = match.Length,
                        Method       = match.Groups["method"].Value.ToLowerInvariant(),
                        MetadataPath = Metadata.ResolvePath(currentMetadataPath, match.Groups["path"].Value)
                    };
                    preparsedContent.Tags.Add(tag);
                }
                preparsedContent.Tags.Sort((a, b) =>
                {
                    if (a.StartIndex > b.StartIndex)
                    {
                        return(1);
                    }
                    else if (a.StartIndex < b.StartIndex)
                    {
                        return(-1);
                    }
                    else
                    {
                        return(0);
                    }
                });
            }

            HttpRuntime.Cache.Insert(cacheKey
                                     , preparsedContent
                                     , new CacheDependencyEx(preparsedContent.DependedFiles.ToArray(), false)
                                     , Cache.NoAbsoluteExpiration
                                     , Cache.NoSlidingExpiration
                                     , CacheItemPriority.NotRemovable
                                     , null
                                     );

            return(preparsedContent);
        }