Beispiel #1
0
        public async Task <Dictionary <string, object> > GetDatasourcesAsync(IFileInput source, string path)
        {
            var datasources = new Dictionary <string, object>();
            var files       = await source.GetFilesAsync(path);

            var data = files
                       .Select(p => new InputFile(p))
                       .Where(f => f.InputType == InputType.Data)
                       .ToList();

            foreach (var file in data)
            {
                datasources.Add(file.Section, await ParseDataAsync(source, file));
            }

            return(datasources);
        }
Beispiel #2
0
        public async Task <Page <TMetadata> > ParsePageAsync <TMetadata>(IFileInput source, string path, string kind)
        {
            var paths = await source.GetFilesAsync(path);

            var files = paths.Select(p => new InputFile(p)).OrderBy(f => f.Section).ThenBy(f => f.Order);
            var slug  = GetSlug(path);
            var page  = new Page <TMetadata>()
            {
                Kind      = kind,
                Slug      = slug,
                Thumbnail = new Media()
                {
                    Sources = files
                              .Where(f => f.InputType == InputType.Media && f.Section == "thumbnail")
                              .Select(mapFileToSource)
                              .ToArray()
                },
                Sections = new Dictionary <string, Section>()
            };

            var metadataFile = files.Where(f => f.Section == "metadata").FirstOrDefault();

            if (metadataFile != null)
            {
                page.Metadata = await ParseMetadataAsync <TMetadata>(source, metadataFile);
            }

            var ignoreList = new string[] { "metadata", "thumbnail", "thumbs", "icon" };

            foreach (var sectionGroup in files.Where(s => !ignoreList.Contains(s.Section)).GroupBy(f => f.Section))
            {
                Section section;

                // section metadata
                var sectionMetadataFile = sectionGroup.Where(f => f.InputType == InputType.Metadata).FirstOrDefault();

                if (sectionMetadataFile != null)
                {
                    section = await ParseMetadataAsync <Section>(source, sectionMetadataFile);
                }
                else
                {
                    section = new Section();
                }

                // section html or markdown content
                var contentPath = sectionGroup.Where(f => f.InputType == InputType.Content).Select(f => f.Path).FirstOrDefault();

                if (contentPath != null)
                {
                    section.Content = await ReadFileAsStringAsync(source, contentPath);
                }

                // section media
                var media = sectionGroup
                            .Where(s => s.InputType == InputType.Media)
                            .GroupBy(s => s.Order)
                            .Select(g => new Media()
                {
                    Sources = g.Select(mapFileToSource).ToList()
                })
                            .ToList();

                if (section.Media != null)
                {
                    section.Media = MergeMediaLists(media, section.Media);
                }
                else
                {
                    section.Media = media;
                }

                page.Sections.Add(sectionGroup.Key, section);
            }

            return(page);
        }