Example #1
0
        public HtmlLink GenerateCssFileTag(string relToRootPath, string templateBaseUrl, Dictionary <string, string> attributes = null, bool useUrlAsIs = false)
        {
            HtmlLink tag = new HtmlLink();

            var url = relToRootPath;

            if (!useUrlAsIs)
            {
                url = URIHelper.ConvertToAbsUrl(LoadFileServiceUrl) + URIHelper.ConvertToAbsUrl(relToRootPath);
            }
            //url = URIHelper.ConvertToAbsUrl(LoadFileServiceUrl) + URIHelper.ConvertToAbsUrl(relToRootPath) + "&templateBaseUrl=" + templateBaseUrl;

            if (!url.Contains("?"))
            {
                url = url.Replace("&", "?");
            }

            tag.Href = url;

            tag.Attributes.Add("type", "text/css");
            tag.Attributes.Add("rel", "stylesheet");

            tag = AddAttributesToTag(tag, attributes);

            return(tag);
        }
Example #2
0
        public static void WriteRss(Rss rss, Stream stream)
        {
            XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);

            writer.WriteStartDocument();
            writer.WriteStartElement("rss");
            writer.WriteAttributeString("version", "2.0");
            writer.WriteStartElement("channel");
            writer.WriteElementString("title", rss.Title);
            writer.WriteElementString("link", URIHelper.ConvertToAbsUrl(rss.Link));
            writer.WriteElementString("description", rss.Description);
            writer.WriteElementString("copyright", rss.Copyright);
            writer.WriteElementString("ttl", rss.TTL);

            foreach (RssItem item in rss.Items)
            {
                WriteRssItem(item, writer);
            }

            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();

            writer.Flush();
            writer.Close();
            stream.Close();
        }
Example #3
0
        public static string ReadUrl(string url, bool enableCaching = false, long cacheDurationInSeconds = 86400)
        {
            if (url == null)
            {
                return("");
            }

            var absUrl = URIHelper.ConvertToAbsUrl(url).ToLower();

            var    data    = "";
            string absPath = URIHelper.ConvertToAbsPath(URIHelper.ConvertAbsUrlToTilda(url));

            using (FileStream fs = File.Open(absPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (BufferedStream bs = new BufferedStream(fs))
                    using (StreamReader sr = new StreamReader(bs))
                    {
                        data = sr.ReadToEnd();
                    }

            if (enableCaching)
            {
                ContextHelper.SaveToCache(absUrl, data, DateTime.Now.AddSeconds(cacheDurationInSeconds));
            }

            return(data);
        }
Example #4
0
        public HtmlGenericControl GenerateJsFileTag(string relToRootPath, string templateBaseUrl, Dictionary <string, string> attributes = null, bool useUrlAsIs = false)
        {
            HtmlGenericControl tag = new HtmlGenericControl();

            tag.TagName = "script";

            var url = relToRootPath;

            if (!useUrlAsIs)
            {
                url = URIHelper.ConvertToAbsUrl(LoadFileServiceUrl) + URIHelper.ConvertToAbsUrl(relToRootPath);
            }
            //url = URIHelper.ConvertToAbsUrl(LoadFileServiceUrl) + URIHelper.ConvertToAbsUrl(relToRootPath) + "&templateBaseUrl=" + templateBaseUrl;

            if (!url.Contains("?"))
            {
                url = url.Replace("&", "?");
            }

            tag.Attributes.Add("src", url);
            tag.Attributes.Add("type", "text/javascript");

            tag = AddAttributesToTag(tag, attributes);

            return(tag);
        }
Example #5
0
        public static string GetUrlToDetailsPage(string absListPageUrl, Tag item)
        {
            if (!absListPageUrl.EndsWith("/"))
            {
                absListPageUrl = absListPageUrl + "/";
            }

            return(URIHelper.ConvertToAbsUrl(absListPageUrl) + item.SefTitle);
        }
Example #6
0
        public static void AddToList(string url)
        {
            url = URIHelper.ConvertToAbsUrl(url);

            if (list.Contains(url))
            {
                return;
            }

            list.Add(url);
        }
Example #7
0
        public static bool IsSame(string url1, string url2)
        {
            url1 = URIHelper.ConvertToAbsUrl(url1);
            url2 = URIHelper.ConvertToAbsUrl(url2);

            if (Prepair(url1) == Prepair(url2))
            {
                return(true);
            }

            return(false);
        }
Example #8
0
        public Dictionary <string, object> GetFieldValuePairs()
        {
            var dictionary = new Dictionary <string, object>();

            if (MediaType.ShowInSiteTree)
            {
                dictionary["FriendlyUrl"] = AbsoluteUrl;
            }

            dictionary["PermaUrl"]    = URIHelper.ConvertToAbsUrl(PermaLink);
            dictionary["PermaApiUrl"] = dictionary["PermaUrl"] + "&format=json";

            var children = new List <Dictionary <string, object> >();

            foreach (MediaDetail mediaDetail in ChildMediaDetails.Where(i => i.ShowInMenu))
            {
                children.Add(mediaDetail.GetFieldValuePairs());
            }

            dictionary["Children"] = children;

            foreach (var field in Fields)
            {
                var fieldAssociations = field.GetPublishedFieldAssociations();

                if (!fieldAssociations.Any())
                {
                    dictionary[field.FieldCode] = field.FieldValue;
                }
                else
                {
                    var fieldDoctionary = new List <Dictionary <string, object> >();
                    foreach (var association in fieldAssociations)
                    {
                        var mediaDetail = association.MediaDetail;

                        fieldDoctionary.Add(mediaDetail.GetFieldValuePairs());
                    }

                    dictionary[field.FieldCode] = fieldDoctionary;
                }
            }

            return(dictionary);
        }
Example #9
0
        private static void WriteRssItem(RssItem rssItem, XmlTextWriter writer)
        {
            writer.WriteStartElement("item");
            writer.WriteElementString("title", rssItem.Title);
            writer.WriteElementString("link", URIHelper.ConvertToAbsUrl(rssItem.Link));

            string shortDesc = StringHelper.StripHtmlTags(rssItem.Description);

            if (shortDesc.Length > 255)
            {
                shortDesc = shortDesc.Substring(0, 255) + " ...";
            }

            writer.WriteElementString("description", shortDesc);
            writer.WriteElementString("author", rssItem.Author);
            writer.WriteElementString("pubDate", rssItem.PubDate);
            writer.WriteElementString("updated", rssItem.Updated);
            writer.WriteEndElement();
        }
Example #10
0
        public static string ParseData(string data, object obj, bool compileRazor = true)
        {
            //return data;

            if (obj == null)
            {
                return("");
            }

            data = data.Trim();

            if (data.StartsWith("@") && data.EndsWith("}"))
            {
                data = RunOrCompileRazorCode(data, data, obj, compileRazor);
            }

            var matches = Regex.Matches(data, openToken + "[a-zA-Z0-9-.&=<>/\\;(\n|\r|\r\n)\"#?']+" + closeToken);

            foreach (var item in matches)
            {
                var tag               = item.ToString();
                var tagValue          = "";
                var propertyName      = tag.Replace("{", "").Replace("}", "");
                var queryStringParams = "";

                if (propertyName.Contains("?"))
                {
                    var segments = propertyName.Split('?').ToList();
                    propertyName = segments[0];
                    segments.RemoveAt(0);
                    queryStringParams = string.Join("", segments);
                }

                var nestedProperties = StringHelper.SplitByString(propertyName, ".");

                if (nestedProperties.Length > 0)
                {
                    if (obj == null)
                    {
                        continue;
                    }

                    object tempNestedProperty = obj;
                    var    propertyloopIndex  = 0;

                    foreach (var nestedProperty in nestedProperties)
                    {
                        PropertyInfo tempPropertyInfo = null;
                        MethodInfo   tempMethodInfo   = null;

                        var matchParams = Regex.Matches(nestedProperty, "([a-zA-Z0-9-_]+)");

                        var methodParamsMatches = new List <string>();

                        for (int i = 0; i < matchParams.Count; i++)
                        {
                            var val = matchParams[i].ToString();
                            if (val != "quot")
                            {
                                methodParamsMatches.Add(val);
                            }
                        }

                        if (nestedProperty.Contains("(") && !nestedProperty.Contains("."))
                        {
                            try
                            {
                                tempMethodInfo = tempNestedProperty.GetType().GetMethod(methodParamsMatches[0]);
                            }
                            catch (Exception ex)
                            {
                                ErrorHelper.LogException(ex);
                            }
                        }
                        else
                        {
                            var prop = nestedProperty;

                            var queryParamsSplit = nestedProperty.Split('?');
                            if (queryParamsSplit.Count() > 1)
                            {
                                prop = queryParamsSplit.ElementAt(0);
                            }

                            if (tempNestedProperty is JObject)
                            {
                                var val = (tempNestedProperty as JObject).GetValue(prop);

                                if (val is JArray)
                                {
                                    tempNestedProperty = String.Join(",", val as JArray);
                                }
                                else
                                {
                                    if (val != null)
                                    {
                                        tempNestedProperty = val.ToString();
                                    }
                                }
                            }
                            else
                            {
                                tempPropertyInfo = tempNestedProperty.GetType().GetProperty(prop);
                            }
                        }

                        if (tempPropertyInfo != null || tempMethodInfo != null)
                        {
                            if (tempPropertyInfo != null)
                            {
                                tempNestedProperty = tempPropertyInfo.GetValue(tempNestedProperty, null);
                            }
                            else if (tempMethodInfo != null)
                            {
                                var objParams      = new object[methodParamsMatches.Count - 1];
                                var parametersInfo = tempMethodInfo.GetParameters();

                                for (var i = 0; i < methodParamsMatches.Count - 1; i++)
                                {
                                    if (parametersInfo.Count() > i)
                                    {
                                        objParams[i] = Convert.ChangeType(methodParamsMatches[i + 1], parametersInfo[i].ParameterType);
                                    }
                                }

                                tempNestedProperty = tempMethodInfo.Invoke(tempNestedProperty, objParams.Where(i => i != null)?.ToArray());
                            }

                            if (tempNestedProperty != null)
                            {
                                var  hasEnumerable = tempNestedProperty.GetType().ToString().Contains("Enumerable") || tempNestedProperty.GetType().ToString().Contains("Collection");
                                long tmpIndex      = 0;

                                if (nestedProperties.Count() > propertyloopIndex + 1)
                                {
                                    if (hasEnumerable)
                                    {
                                        if (long.TryParse(nestedProperties[propertyloopIndex + 1], out tmpIndex))
                                        {
                                            var count = 0;
                                            foreach (var nestedItem in tempNestedProperty as IEnumerable <object> )
                                            {
                                                if (count == tmpIndex)
                                                {
                                                    tempNestedProperty = nestedItem;
                                                    break;
                                                }

                                                count++;
                                            }

                                            continue;
                                        }
                                        else
                                        {
                                            var count                = 0;
                                            var returnValue          = "";
                                            var tempPropertiesString = "";

                                            var tmp = nestedProperties.ToList();
                                            tmp.RemoveAt(propertyloopIndex);

                                            var newPropertyString = string.Join(".", tmp);

                                            foreach (var nestedItem in (dynamic)tempNestedProperty)
                                            {
                                                var tmpReturn = ParseData("{" + newPropertyString + "}", nestedItem);
                                                returnValue += ParseData(tmpReturn, nestedItem);

                                                /*var tmpReturn = ParseData("{" + nestedProperties[propertyloopIndex + 1] + "}", nestedItem);
                                                 * returnValue += ParseData(tmpReturn, nestedItem);
                                                 * count++;*/
                                            }

                                            tagValue = returnValue;
                                        }
                                    }
                                }
                                else
                                {
                                    if (nestedProperties.Length < propertyloopIndex + 1)
                                    {
                                        return(ParseData("{" + nestedProperties[propertyloopIndex + 1] + "}", tempNestedProperty));
                                    }
                                    else if (tempNestedProperty is string)
                                    {
                                        if (tempMethodInfo != null)
                                        {
                                            tagValue = tempNestedProperty.ToString();
                                        }
                                        //return ParseData("{" + nestedProperties[propertyloopIndex + 1] + "}", tempNestedProperty);
                                    }
                                }
                            }
                        }
                        else
                        {
                            var splitEq = nestedProperty.ToString().Split('=');
                            if (splitEq.Count() > 1)
                            {
                                tempPropertyInfo = tempNestedProperty.GetType().GetProperty(splitEq[0]);

                                if (tempPropertyInfo != null)
                                {
                                    var returnVal = tempPropertyInfo.GetValue(tempNestedProperty, null);

                                    if (splitEq[1].Replace("\"", "") == returnVal.ToString())
                                    {
                                        var tmp = nestedProperties.ToList();
                                        tmp.RemoveAt(propertyloopIndex);

                                        var newPropertyString = string.Join(".", tmp);

                                        tempNestedProperty = ParseData("{" + newPropertyString + "}", tempNestedProperty);
                                    }
                                    else
                                    {
                                        tempNestedProperty = "";
                                    }
                                }
                            }
                        }

                        propertyloopIndex++;
                    }

                    if (tempNestedProperty is DateTime)
                    {
                        tagValue = data.Replace(item.ToString(), StringHelper.FormatOnlyDate((DateTime)tempNestedProperty));
                    }

                    if (tempNestedProperty is string || tempNestedProperty is bool || tempNestedProperty is long)
                    {
                        var val = tempNestedProperty.ToString();

                        var queryStringSplit = item.ToString().Replace(OpenToken, "").Replace(CloseToken, "").Split('?');

                        if (queryStringSplit.Count() > 1)
                        {
                            var nv = HttpUtility.ParseQueryString(queryStringSplit.ElementAt(1));

                            foreach (string key in nv)
                            {
                                var value = nv[key];
                                val = val.Replace(OpenToken + key + CloseToken, value);
                            }
                        }

                        tagValue = data.Replace(item.ToString(), val);
                    }
                }

                if (tagValue.StartsWith("~/"))
                {
                    tagValue = URIHelper.ConvertToAbsUrl(tagValue);
                }

                if (!string.IsNullOrEmpty(tagValue))
                {
                    data = RunOrCompileRazorCode(tag, tagValue, obj, compileRazor);
                }
            }

            data = RunOrCompileRazorCode(data, data, obj, compileRazor);

            return(data);
        }