private IList<IContentData> GetAllContentAreaContents(IContentData contentData)
        {
            PropertyInfo[] props = contentData.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            var blocks = new List<IContentData>();

            foreach (PropertyInfo prop in props)
            {
                if (prop.PropertyType != typeof(ContentArea))
                    continue;

                object value = prop.GetValue(contentData, null);

                var contentArea = value as ContentArea;

                if (contentArea == null || !contentArea.FilteredItems.Any())
                    continue;

                var blockLinks = contentArea.FilteredItems.Select(i => i.ContentLink);

                blocks.AddRange(blockLinks.Select(b => _contentRepository.Get<IContentData>(b)));
            }

            return blocks;
        }
Esempio n. 2
0
        public Uri Save(IContentData contentData)
        {
            IContentTypeProvider contentTypeProvider = new ContentTypeProvider();
            IContentSerializer   contentSerializer   = new FrontMatterContentSerializer(contentTypeProvider);

            if (string.IsNullOrEmpty(contentData.Layout))
            {
                contentData.Layout = contentData.GetType().Name;
            }

            var fileContent = contentSerializer.Serialize(contentData);

            string contentPath;

            if (contentData.ContentUri != null)
            {
                // if the slug has been updated, rename the file
                if (!contentData.ContentUri.Segments.Last().Equals(contentData.Slug, StringComparison.InvariantCultureIgnoreCase))
                {
                    contentData.ContentUri = RenameContentAssets(contentData.ContentUri, contentData.Slug);
                }

                contentPath = ContentUri.GetAbsolutePath(contentData.ContentUri, true);
            }
            else if (contentData.ParentUri != null)
            {
                var parentPath = ContentUri.GetAbsolutePath(contentData.ParentUri);

                if (string.IsNullOrEmpty(contentData.Slug) && string.IsNullOrEmpty(contentData.Title))
                {
                    throw new InvalidOperationException("Could not create a URL slug for content. Please supply either Title or Slug to save the content.");
                }

                if (string.IsNullOrEmpty(contentData.Slug))
                {
                    contentData.Slug = contentData.Title.ToUrlSlug();
                }

                contentPath = Path.Combine(parentPath, Path.ChangeExtension(contentData.Slug, _fileExtension));
            }
            else
            {
                throw new InvalidOperationException("Cannot save content when both ParentUri and ContentUri is missing.");
            }

            var directoryPath = Path.GetDirectoryName(contentPath);

            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            File.WriteAllText(contentPath, fileContent);

            return(NormalizeContentUri(new Uri(contentPath)));
        }
        public IEnumerable <PropertyInfo> GetProperties(IContentData contentData)
        {
            var type = contentData.GetType();

            if (_cachedContentTypes.ContainsKey(type))
            {
                return(_cachedContentTypes[type]);
            }

            var properties = type.GetProperties().Where(ShouldBeIncluded);

            _cachedContentTypes[type] = properties;
            return(properties);
        }
        private bool ResolveProperty(IContentData content, string propertyName)
        {
            //First try to find property on typed model
            var property = content.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);

            if (property != null && property.CanRead && property.CanWrite)
            {
                return(true);
            }

            //Then check if property is in PropertyCollection
            var propertyData = content.Property[propertyName];

            if (propertyData != null)
            {
                return(true);
            }

            //Finally check if it is defined on an interface
            return(GetInterfaceForPropertyName(propertyName));
        }
Esempio n. 5
0
        public static IEnumerable <PropertyData> GetIndexableProperties(this IContentData content, Func <PropertyData, bool> propertyPredicate = null)
        {
            var predicate = propertyPredicate ?? (_ => true);
            var propertyDataCollection = content.Property.Where(predicate);

            var contentType = content.GetType();

            foreach (var propertyData in propertyDataCollection)
            {
                var property = contentType.GetProperty(propertyData.Name);
                if (property == null)
                {
                    continue;
                }

                var indexableAttribute = property.GetCustomAttribute <IndexableAttribute>();
                if (indexableAttribute != null && indexableAttribute.IsIndexable)
                {
                    yield return(propertyData);
                }
            }
        }
        private bool PropertyExist(IContentData content, string propertyName)
        {
            var resolvedPropertiesOnType = _propertyCache.GetOrAdd(content.GetType(), t => new ConcurrentDictionary <string, bool>(StringComparer.OrdinalIgnoreCase));

            return(resolvedPropertiesOnType.GetOrAdd(propertyName, prop => ResolveProperty(content, prop)));
        }
 private static IEnumerable <PropertyInfo> GetColorProperties(IContentData image)
 {
     return(image.GetType().GetProperties()
            .Where(prop => Attribute.IsDefined(prop, typeof(DominantColorAttribute))));
 }
        private IList<ContentReference> GetAllImages(IContentData contentData)
        {
            PropertyInfo[] props = contentData.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            IList<ContentReference> images = new List<ContentReference>();

            foreach (PropertyInfo prop in props)
            {
                if (prop.PropertyType != typeof (ContentReference) || !IsImage(prop))
                    continue;

                object value = prop.GetValue(contentData, null);

                var image = value as ContentReference;

                if (!ContentReference.IsNullOrEmpty(image))
                    images.Add(image);
            }

            return images;
        }