public ProcessingPriority GetProcessingPriority(FileAndType file)
        {
            switch (file.Type)
            {
                case DocumentType.Article:
                    if (".yml".Equals(Path.GetExtension(file.File), StringComparison.OrdinalIgnoreCase))
                    {
                        return ProcessingPriority.Normal;
                    }

                    if (".csyml".Equals(Path.GetExtension(file.File), StringComparison.OrdinalIgnoreCase) ||
                        ".csyaml".Equals(Path.GetExtension(file.File), StringComparison.OrdinalIgnoreCase))
                    {
                        return ProcessingPriority.Normal;
                    }
                    break;
                case DocumentType.Override:
                    if (".md".Equals(Path.GetExtension(file.File), StringComparison.OrdinalIgnoreCase))
                    {
                        return ProcessingPriority.Normal;
                    }
                    break;
                default:
                    break;
            }
            return ProcessingPriority.NotSupportted;
        }
Exemple #2
0
 public FileModel(FileAndType ft, object content, FileAndType original = null, IFormatter serializer = null)
 {
     OriginalFileAndType = original ?? ft;
     FileAndType = ft;
     _content = content;
     _serializer = serializer;
 }
Exemple #3
0
 private static IEnumerable<YamlHtmlPart> MarkupMultiple(IHostService host, string markdown, FileAndType ft)
 {
     try
     {
         var html = host.Markup(markdown, ft, true);
         var parts = YamlHtmlPart.SplitYamlHtml(html);
         foreach (var part in parts)
         {
             var mr = host.Parse(part.ToMarkupResult(), ft);
             part.Conceptual = mr.Html;
             part.LinkToFiles = mr.LinkToFiles;
             part.LinkToUids = mr.LinkToUids;
             part.YamlHeader = mr.YamlHeader;
             part.FileLinkSources = mr.FileLinkSources;
             part.UidLinkSources = mr.UidLinkSources;
         }
         return parts;
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.Fail("Markup failed!");
         var message = $"Markup failed: {ex.Message}.";
         Logger.LogError(message);
         throw new DocumentException(message, ex);
     }
 }
        public override FileModel Load(FileAndType file, ImmutableDictionary<string, object> metadata)
        {
            if (file.Type != DocumentType.Article)
            {
                throw new NotSupportedException();
            }
            var content = MarkdownReader.ReadMarkdownAsConceptual(file.BaseDir, file.File);
            foreach (var item in metadata)
            {
                if (!content.ContainsKey(item.Key))
                {
                    content[item.Key] = item.Value;
                }
            }

            var displayLocalPath = PathUtility.MakeRelativePath(EnvironmentContext.BaseDirectory, file.FullPath);

            return new FileModel(
                file,
                content,
                serializer: Environment.Is64BitProcess ? null : new BinaryFormatter())
            {
                LocalPathFromRepoRoot = (content["source"] as SourceDetail)?.Remote?.RelativePath,
                LocalPathFromRoot = displayLocalPath
            };
        }
Exemple #5
0
 public static IEnumerable<OverwriteDocumentModel> ReadMarkdownAsOverwrite(IHostService host, FileAndType ft)
 {
     // Order the list from top to bottom
     var markdown = File.ReadAllText(ft.FullPath);
     var parts = MarkupMultiple(host, markdown, ft);
     return parts.Select(part => TransformModel(ft.FullPath, part));
 }
 public override FileModel Load(IDocumentProcessor processor, ImmutableDictionary<string, object> metadata, FileMetadata fileMetadata, FileAndType file)
 {
     using (new LoggerFileScope(file.File))
     {
         if (CanProcessorIncremental(processor))
         {
             ChangeKindWithDependency ck;
             string fileKey = ((RelativePath)file.File).GetPathFromWorkingFolder().ToString();
             if (IncrementalContext.ChangeDict.TryGetValue(fileKey, out ck))
             {
                 Logger.LogDiagnostic($"Processor {processor.Name}, File {file.FullPath}, ChangeType {ck}.");
                 if (ck == ChangeKindWithDependency.Deleted)
                 {
                     return null;
                 }
                 if (ck == ChangeKindWithDependency.None)
                 {
                     Logger.LogDiagnostic($"Processor {processor.Name}, File {file.FullPath}: Check incremental...");
                     if (processor.BuildSteps.Cast<ISupportIncrementalBuildStep>().All(step => step.CanIncrementalBuild(file)))
                     {
                         Logger.LogDiagnostic($"Processor {processor.Name}, File {file.FullPath}: Skip build by incremental.");
                         return null;
                     }
                     Logger.LogDiagnostic($"Processor {processor.Name}, File {file.FullPath}: Incremental not available.");
                 }
             }
         }
         return base.Load(processor, metadata, fileMetadata, file);
     }
 }
Exemple #7
0
 public TocItemInfo(FileAndType file, TocItemViewModel item)
 {
     Content = item;
     File = file;
     IsResolved = false;
     IsReferenceToc = false;
 }
Exemple #8
0
        public override ProcessingPriority GetProcessingPriority(FileAndType file)
        {
            if (file.Type == DocumentType.Article && Utility.IsSupportedFile(file.File))
            {
                return ProcessingPriority.High;
            }

            return ProcessingPriority.NotSupported;
        }
Exemple #9
0
 public ProcessingPriority GetProcessingPriority(FileAndType file)
 {
     if (file.Type == DocumentType.Article &&
         ".rtf".Equals(Path.GetExtension(file.File), StringComparison.OrdinalIgnoreCase))
     {
         return ProcessingPriority.Normal;
     }
     return ProcessingPriority.NotSupported;
 }
Exemple #10
0
 public FileModel Load(FileAndType file, ImmutableDictionary<string, object> metadata)
 {
     var content = new Dictionary<string, object>
     {
         ["conceptual"] = File.ReadAllText(Path.Combine(file.BaseDir, file.File)),
         ["type"] = "Conceptual",
         ["path"] = file.File,
     };
     return new FileModel(file, content);
 }
Exemple #11
0
 public MarkupResult Markup(string markdown, FileAndType ft)
 {
     try
     {
         return MarkupCore(markdown, ft);
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.Fail("Markup failed!");
         Logger.LogWarning($"Markup failed:{Environment.NewLine}  Markdown: {markdown}{Environment.NewLine}  Details:{ex.ToString()}");
         return new MarkupResult { Html = markdown };
     }
 }
        public FileModel Load(FileAndType file, ImmutableDictionary<string, object> metadata)
        {
            var filePath = Path.Combine(file.BaseDir, file.File);
            TocViewModel toc = LoadSingleToc(filePath);

            var repoDetail = GitUtility.GetGitDetail(filePath);

            // todo : metadata.
            return new FileModel(file, toc)
            {
                Uids = new[] { file.File }.ToImmutableArray(),
                LocalPathFromRepoRoot = repoDetail?.RelativePath
            };
        }
 public ProcessingPriority GetProcessingPriority(FileAndType file)
 {
     if (file.Type == DocumentType.Article)
     {
         if ("toc.md".Equals(Path.GetFileName(file.File), StringComparison.OrdinalIgnoreCase))
         {
             return ProcessingPriority.High;
         }
         if ("toc.yml".Equals(Path.GetFileName(file.File), StringComparison.OrdinalIgnoreCase))
         {
             return ProcessingPriority.High;
         }
     }
     return ProcessingPriority.NotSupportted;
 }
Exemple #14
0
        public FileModel(FileAndType ft, object content, FileAndType original = null, IFormatter serializer = null)
        {
            OriginalFileAndType = original ?? ft;

            if (OriginalFileAndType.File.StartsWith("~/"))
            {
                Key = OriginalFileAndType.File;
            }
            else
            {
                Key = "~/" + OriginalFileAndType.File;
            }

            FileAndType = ft;
            ModelWithCache = new ModelWithCache(content, serializer);
        }
 public override ProcessingPriority GetProcessingPriority(FileAndType file)
 {
     if (file.Type == DocumentType.Resource)
     {
         return ProcessingPriority.Normal;
     }
     if (file.Type == DocumentType.Article)
     {
         foreach (var config in Configs)
         {
             if (config.IsResourceFile(Path.GetExtension(file.File)))
             {
                 return ProcessingPriority.Lowest;
             }
         }
     }
     return ProcessingPriority.NotSupported;
 }
        public override FileModel Load(FileAndType file, ImmutableDictionary <string, object> metadata)
        {
            switch (file.Type)
            {
            case DocumentType.Article:
                // TODO: Support dynamic in YAML deserializer
                try
                {
                    // MUST be a dictionary
                    var obj = YamlUtility.Deserialize <Dictionary <string, object> >(file.File);
                    foreach (var pair in metadata)
                    {
                        if (!obj.ContainsKey(pair.Key))
                        {
                            obj[pair.Key] = pair.Value;
                        }
                    }
                    var content = ConvertToObjectHelper.ConvertToDynamic(obj);

                    var localPathFromRoot = PathUtility.MakeRelativePath(EnvironmentContext.BaseDirectory, EnvironmentContext.FileAbstractLayer.GetPhysicalPath(file.File));

                    var fm = new FileModel(
                        file,
                        content,
                        serializer: new BinaryFormatter())
                    {
                        LocalPathFromRoot = localPathFromRoot,
                    };

                    fm.Properties.Schema = _schema;
                    return(fm);
                }
                catch (YamlDotNet.Core.YamlException e)
                {
                    throw new DocumentException($"{file.File} is not in supported format: {e.Message}", e);
                }

            case DocumentType.Overwrite:
                return(OverwriteDocumentReader.Read(file));

            default:
                throw new NotSupportedException();
            }
        }
Exemple #17
0
 private MarkupResult MarkupCore(string markdown, FileAndType ft, bool omitParse)
 {
     try
     {
         var mr = MarkdownService.Markup(markdown, ft.File);
         if (omitParse)
         {
             return(mr);
         }
         return(Parse(mr, ft));
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.Fail("Markup failed!");
         var message = $"Markup failed: {ex.Message}.";
         Logger.LogError(message);
         throw new DocumentException(message, ex);
     }
 }
Exemple #18
0
        public override FileModel Load(FileAndType file, ImmutableDictionary <string, object> metadata)
        {
            switch (file.Type)
            {
            case DocumentType.Article:
                var page = YamlUtility.Deserialize <PageViewModel>(Path.Combine(file.BaseDir, file.File));
                if (page.Metadata == null)
                {
                    page.Metadata = metadata.ToDictionary(p => p.Key, p => p.Value);
                }
                else
                {
                    foreach (var item in metadata)
                    {
                        if (!page.Metadata.ContainsKey(item.Key))
                        {
                            page.Metadata[item.Key] = item.Value;
                        }
                    }
                }

                // Item's source is the path for the original code, should not be used here
                var displayLocalPath = Path.Combine(file.BaseDir, file.File).ToDisplayPath();
                return(new FileModel(file, page, serializer: new BinaryFormatter())
                {
                    Uids = (from item in page.Items select new UidDefinition(item.Uid, displayLocalPath)).ToImmutableArray(),

                    Properties =
                    {
                        LinkToFiles = new HashSet <string>(),
                        LinkToUids  = new HashSet <string>(),
                    },
                    LocalPathFromRepoRoot = displayLocalPath,
                });

            case DocumentType.Overwrite:
                // TODO: Refactor current behavior that overwrite file is read multiple times by multiple processors
                return(OverwriteDocumentReader.Read(file));

            default:
                throw new NotSupportedException();
            }
        }
Exemple #19
0
        public virtual FileModel Load(IDocumentProcessor processor, ImmutableDictionary<string, object> metadata, FileMetadata fileMetadata, FileAndType file)
        {
            using (new LoggerFileScope(file.File))
            {
                Logger.LogDiagnostic($"Processor {processor.Name}, File {file.FullPath}: Loading...");

                var path = Path.Combine(file.BaseDir, file.File);
                metadata = ApplyFileMetadata(path, metadata, fileMetadata);
                try
                {
                    return processor.Load(file, metadata);
                }
                catch (Exception)
                {
                    Logger.LogError($"Unable to load file: {file.File} via processor: {processor.Name}.");
                    throw;
                }
            }
        }
 public FileModel Load(FileAndType file, ImmutableDictionary<string, object> metadata)
 {
     switch (file.Type)
     {
         case DocumentType.Article:
             var page = YamlUtility.Deserialize<PageViewModel>(Path.Combine(file.BaseDir, file.File));
             if (page.Metadata == null)
             {
                 page.Metadata = metadata.ToDictionary(p => p.Key, p => p.Value);
             }
             else
             {
                 foreach (var item in metadata)
                 {
                     if (!page.Metadata.ContainsKey(item.Key))
                     {
                         page.Metadata[item.Key] = item.Value;
                     }
                 }
             }
             var result = new FileModel(file, page, serializer: new BinaryFormatter())
             {
                 Uids = (from item in page.Items select item.Uid).ToImmutableArray(),
             };
             result.Properties.LinkToFiles = new HashSet<string>();
             result.Properties.LinkToUids = new HashSet<string>();
             return result;
         case DocumentType.Override:
             var overrides = MarkdownReader.ReadMarkdownAsOverride(file.BaseDir, file.File);
             return new FileModel(file, overrides, serializer: new BinaryFormatter())
             {
                 Uids = (from item in overrides
                         select item.Uid).ToImmutableArray(),
                 Properties =
                 {
                     LinkToFiles = new HashSet<string>(),
                     LinkToUids = new HashSet<string>(),
                 }
             };
         default:
             throw new NotSupportedException();
     }
 }
        public override FileModel Load(FileAndType file, ImmutableDictionary <string, object> metadata)
        {
            var filePath     = file.FullPath;
            var tocViewModel = Utility.LoadSingleToc(filePath);
            var toc          = new TocItemViewModel
            {
                Items = tocViewModel
            };

            var repoDetail       = GitUtility.TryGetFileDetail(filePath);
            var displayLocalPath = PathUtility.MakeRelativePath(EnvironmentContext.BaseDirectory, file.FullPath);

            // todo : metadata.
            return(new FileModel(file, toc)
            {
                Uids = new[] { new UidDefinition(file.File, displayLocalPath) }.ToImmutableArray(),
                LocalPathFromRoot = displayLocalPath
            });
        }
        public override FileModel Load(FileAndType file, ImmutableDictionary <string, object> metadata)
        {
            switch (file.Type)
            {
            case DocumentType.Article:
                var filePath       = Path.Combine(file.BaseDir, file.File);
                var swaggerContent = File.ReadAllText(filePath);
                var swagger        = SwaggerJsonParser.Parse(swaggerContent);
                swagger.Metadata[DocumentTypeKey] = RestApiDocumentType;
                swagger.Raw = swaggerContent;
                var repoInfo = GitUtility.GetGitDetail(filePath);
                if (repoInfo != null)
                {
                    swagger.Metadata["source"] = new SourceDetail()
                    {
                        Remote = repoInfo
                    };
                }

                swagger.Metadata = MergeMetadata(swagger.Metadata, metadata);
                var vm = SwaggerModelConverter.FromSwaggerModel(swagger);
                var displayLocalPath = repoInfo?.RelativePath ?? filePath.ToDisplayPath();
                return(new FileModel(file, vm, serializer: new BinaryFormatter())
                {
                    Uids = new[] { new UidDefinition(vm.Uid, displayLocalPath) }
                    .Concat(from item in vm.Children select new UidDefinition(item.Uid, displayLocalPath))
                    .Concat(from tag in vm.Tags select new UidDefinition(tag.Uid, displayLocalPath)).ToImmutableArray(),
                    LocalPathFromRepoRoot = displayLocalPath,
                    Properties =
                    {
                        LinkToFiles = new HashSet <string>(),
                        LinkToUids  = new HashSet <string>(),
                    },
                });

            case DocumentType.Overwrite:
                // TODO: Refactor current behavior that overwrite file is read multiple times by multiple processors
                return(OverwriteDocumentReader.Read(file));

            default:
                throw new NotSupportedException();
            }
        }
Exemple #23
0
        public override FileModel Load(FileAndType file, ImmutableDictionary <string, object> metadata)
        {
            var filePath     = file.FullPath;
            var tocViewModel = Utility.LoadSingleToc(filePath);
            var toc          = new TocItemViewModel
            {
                Items = tocViewModel
            };

            var repoDetail       = GitUtility.GetGitDetail(filePath);
            var displayLocalPath = repoDetail?.RelativePath ?? filePath;

            // todo : metadata.
            return(new FileModel(file, toc)
            {
                Uids = new[] { new UidDefinition(file.File, displayLocalPath) }.ToImmutableArray(),
                LocalPathFromRepoRoot = displayLocalPath,
            });
        }
Exemple #24
0
        protected override FileModel LoadArticle(FileAndType file, ImmutableDictionary <string, object> metadata)
        {
            if (YamlMime.ReadMime(file.File) == null)
            {
                Logger.LogWarning(
                    "Please add yamlmime in the first line of file, e.g.: `### YamlMime:ManagedReference`, we will decline yaml files without yamlmime in next release.",
                    file: file.File,
                    code: WarningCodes.Yaml.MissingYamlMime);
            }

            var page = YamlUtility.Deserialize <PageViewModel>(file.File);

            if (page.Items == null || page.Items.Count == 0)
            {
                return(null);
            }
            if (page.Metadata == null)
            {
                page.Metadata = metadata.ToDictionary(p => p.Key, p => p.Value);
            }
            else
            {
                foreach (var item in metadata)
                {
                    if (!page.Metadata.ContainsKey(item.Key))
                    {
                        page.Metadata[item.Key] = item.Value;
                    }
                }
            }
            page.Metadata[Constants.PropertyName.SystemKeys] = SystemKeys;

            var localPathFromRoot = PathUtility.MakeRelativePath(EnvironmentContext.BaseDirectory, EnvironmentContext.FileAbstractLayer.GetPhysicalPath(file.File));

            return(new FileModel(file, page, serializer: new BinaryFormatter())
            {
                Uids = (from item in page.Items select item.Uid)
                       .Concat(from item in page.Items where item.Overload != null select item.Overload)
                       .Distinct().Select(s => new UidDefinition(s, localPathFromRoot)).ToImmutableArray(),
                LocalPathFromRoot = localPathFromRoot
            });
        }
        public override ProcessingPriority GetProcessingPriority(FileAndType file)
        {
            switch (file.Type)
            {
            case DocumentType.Article:
                if (".yml".Equals(Path.GetExtension(file.File), StringComparison.OrdinalIgnoreCase) ||
                    ".yaml".Equals(Path.GetExtension(file.File), StringComparison.OrdinalIgnoreCase))
                {
                    var mime = YamlMime.ReadMime(file.File);
                    switch (mime)
                    {
                    case YamlMime.ManagedReference:
                        return(ProcessingPriority.Normal);

                    case null:
                        return(ProcessingPriority.BelowNormal);

                    default:
                        return(ProcessingPriority.NotSupported);
                    }
                }

                if (".csyml".Equals(Path.GetExtension(file.File), StringComparison.OrdinalIgnoreCase) ||
                    ".csyaml".Equals(Path.GetExtension(file.File), StringComparison.OrdinalIgnoreCase))
                {
                    return(ProcessingPriority.Normal);
                }

                break;

            case DocumentType.Overwrite:
                if (".md".Equals(Path.GetExtension(file.File), StringComparison.OrdinalIgnoreCase))
                {
                    return(ProcessingPriority.Normal);
                }
                break;

            default:
                break;
            }
            return(ProcessingPriority.NotSupported);
        }
Exemple #26
0
        public override FileModel Load(FileAndType file, ImmutableDictionary <string, object> metadata)
        {
            switch (file.Type)
            {
            case DocumentType.Article:
                var page = YamlUtility.Deserialize <PageViewModel>(Path.Combine(file.BaseDir, file.File));
                if (page.Items == null || page.Items.Count == 0)
                {
                    return(null);
                }
                if (page.Metadata == null)
                {
                    page.Metadata = metadata.ToDictionary(p => p.Key, p => p.Value);
                }
                else
                {
                    foreach (var item in metadata)
                    {
                        if (!page.Metadata.ContainsKey(item.Key))
                        {
                            page.Metadata[item.Key] = item.Value;
                        }
                    }
                }

                var displayLocalPath = TypeForwardedToPathUtility.MakeRelativePath(EnvironmentContext.BaseDirectory, file.FullPath);

                return(new FileModel(file, page, serializer: Environment.Is64BitProcess?null: new BinaryFormatter())
                {
                    Uids = (from item in page.Items select new UidDefinition(item.Uid, displayLocalPath)).ToImmutableArray(),
                    LocalPathFromRepoRoot = displayLocalPath,
                    LocalPathFromRoot = displayLocalPath
                });

            case DocumentType.Overwrite:
                // TODO: Refactor current behavior that overwrite file is read multiple times by multiple processors
                return(OverwriteDocumentReader.Read(file));

            default:
                throw new NotSupportedException();
            }
        }
Exemple #27
0
 private MarkupResult MarkupCore(string markdown, FileAndType ft, bool omitParse)
 {
     try
     {
         var mr = MarkdownService.Markup(markdown, ft.File);
         if (omitParse)
         {
             return(mr);
         }
         return(Parse(mr, ft));
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.Fail("Markup failed!");
         Logger.LogWarning($"Markup failed:{Environment.NewLine}  Markdown: {markdown}{Environment.NewLine}  Details:{ex.ToString()}");
         return(new MarkupResult {
             Html = markdown
         });
     }
 }
Exemple #28
0
        public override FileModel Load(FileAndType file, ImmutableDictionary<string, object> metadata)
        {
            var filePath = file.FullPath;
            var tocViewModel = Utility.LoadSingleToc(filePath);
            var toc = new TocItemViewModel
            {
                Items = tocViewModel
            };

            var repoDetail = GitUtility.GetGitDetail(filePath);
            var displayLocalPath = TypeForwardedToPathUtility.MakeRelativePath(EnvironmentContext.BaseDirectory, file.FullPath);

            // todo : metadata.
            return new FileModel(file, toc)
            {
                Uids = new[] { new UidDefinition(file.File, displayLocalPath) }.ToImmutableArray(),
                LocalPathFromRepoRoot = repoDetail?.RelativePath ?? filePath,
                LocalPathFromRoot = displayLocalPath
            };
        }
Exemple #29
0
        private MarkupResult ParseCore(MarkupResult markupResult, FileAndType ft)
        {
            var doc = new HtmlDocument();

            doc.LoadHtml(markupResult.Html);
            var result = markupResult.Clone();

            var node = doc.DocumentNode.SelectSingleNode("//yamlheader");

            if (node != null)
            {
                using (var sr = new StringReader(StringHelper.HtmlDecode(node.InnerHtml)))
                {
                    result.YamlHeader = YamlUtility.Deserialize <Dictionary <string, object> >(sr).ToImmutableDictionary();
                }
                node.Remove();
            }

            result.FileLinkSources = GetFileLinkSource(ft, doc);
            result.LinkToFiles     = result.FileLinkSources.Keys.ToImmutableArray();

            result.UidLinkSources = GetUidLinkSources(doc);
            result.LinkToUids     = result.UidLinkSources.Keys.ToImmutableHashSet();

            if (result.Dependency.Length > 0)
            {
                result.Dependency =
                    (from d in result.Dependency
                     select
                         ((RelativePath)ft.File + (RelativePath)d)
                     .GetPathFromWorkingFolder()
                     .ToString()
                    ).ToImmutableArray();
            }
            using (var sw = new StringWriter())
            {
                doc.Save(sw);
                result.Html = sw.ToString();
            }
            return(result);
        }
Exemple #30
0
        public static FileModel Read(FileAndType file)
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            if (file.Type != DocumentType.Overwrite)
            {
                throw new NotSupportedException(file.Type.ToString());
            }

            return(new FileModel(file, null, serializer: new BinaryFormatter())
            {
                Properties =
                {
                    LinkToFiles = new HashSet <string>(),
                    LinkToUids  = new HashSet <string>(),
                }
            });
        }
        public override FileModel Load(FileAndType file, ImmutableDictionary <string, object> metadata)
        {
            var filePath = file.FullPath;
            var toc      = TocHelper.LoadSingleToc(filePath);

            var displayLocalPath = PathUtility.MakeRelativePath(EnvironmentContext.BaseDirectory, file.FullPath);

            // Apply metadata to TOC
            foreach (var pair in metadata)
            {
                if (!toc.Metadata.TryGetValue(pair.Key, out var val))
                {
                    toc.Metadata[pair.Key] = pair.Value;
                }
            }

            return(new FileModel(file, toc)
            {
                LocalPathFromRoot = displayLocalPath
            });
        }
        private FileModel GenerateNewFileModel(FileModel model, PageViewModel newPage, string fileNameWithoutExtension, Dictionary <string, int> existingFileNames)
        {
            var initialFile = model.FileAndType.File;
            var extension   = Path.GetExtension(initialFile);
            var directory   = Path.GetDirectoryName(initialFile);

            // encode file name to clean url so that without server hosting, href can work with file:/// navigation
            var cleanUrlFileName = fileNameWithoutExtension.ToCleanUrlFileName();
            var actualFileName   = GetUniqueFileNameWithSuffix(cleanUrlFileName, existingFileNames);
            var newFileName      = actualFileName + extension;
            var filePath         = Path.Combine(directory, newFileName).ToNormalizedPath();

            var newFileAndType = new FileAndType(model.FileAndType.BaseDir, filePath, model.FileAndType.Type, model.FileAndType.SourceDir, model.FileAndType.DestinationDir);
            var keyForModel    = "~/" + RelativePath.GetPathWithoutWorkingFolderChar(filePath);

            return(new FileModel(newFileAndType, newPage, model.OriginalFileAndType, model.Serializer, keyForModel)
            {
                LocalPathFromRoot = model.LocalPathFromRoot,
                Uids = CalculateUids(newPage, model.LocalPathFromRoot)
            });
        }
        private FileModel GenerateNewFileModel(FileModel model, RestApiRootItemViewModel tagModel)
        {
            var originalFile  = model.FileAndType.File;
            var fileExtension = Path.GetExtension(originalFile);

            // When handling tags in petstore.swagger.json, the tag file path should be petstore/tag.json, to prevent tag name conflict
            var originalFileName = Path.GetFileName(originalFile);
            var subDirectory     = originalFileName.Remove(originalFileName.IndexOf('.'));
            var directory        = Path.GetDirectoryName(originalFile);
            var filePath         = Path.Combine(directory, subDirectory, tagModel.Name + fileExtension).ToNormalizedPath();

            var newFileAndType = new FileAndType(model.FileAndType.BaseDir, filePath, model.FileAndType.Type, model.FileAndType.SourceDir, model.FileAndType.DestinationDir);
            var newKey         = "~/" + RelativePath.GetPathWithoutWorkingFolderChar(filePath);
            var newModel       = new FileModel(newFileAndType, tagModel, model.OriginalFileAndType, model.Serializer, newKey)
            {
                LocalPathFromRoot = model.LocalPathFromRoot,
                Uids = CalculateUids(tagModel).Select(i => new UidDefinition(i, model.LocalPathFromRoot)).ToImmutableArray()
            };

            return(newModel);
        }
Exemple #34
0
        private FileModel GenerateNewFileModel(FileModel model, RestApiRootItemViewModel operationModel)
        {
            var originalFile  = model.FileAndType.File;
            var fileExtension = Path.GetExtension(originalFile);

            // When split into operation for petstore.swagger.json, the operation file path should be petstore/{operationName}.json, to prevent operation name confliction
            var originalFileName = Path.GetFileName(originalFile);
            var subDirectory     = originalFileName.Remove(originalFileName.IndexOf('.'));
            var directory        = Path.GetDirectoryName(originalFile);
            var filePath         = Path.Combine(directory, subDirectory, operationModel.Name + fileExtension).ToNormalizedPath();

            var newFileAndType = new FileAndType(model.FileAndType.BaseDir, filePath, model.FileAndType.Type, model.FileAndType.SourceDir, model.FileAndType.DestinationDir);
            var newKey         = "~/" + RelativePath.GetPathWithoutWorkingFolderChar(filePath);
            var newModel       = new FileModel(newFileAndType, operationModel, model.OriginalFileAndType, model.Serializer, newKey)
            {
                LocalPathFromRoot = model.LocalPathFromRoot,
                Uids = new[] { new UidDefinition(operationModel.Uid, model.LocalPathFromRoot) }.ToImmutableArray()
            };

            return(newModel);
        }
Exemple #35
0
 public override ProcessingPriority GetProcessingPriority(FileAndType file)
 {
     switch (file.Type)
     {
         case DocumentType.Article:
             if (IsSupportedFile(file.FullPath))
             {
                 return ProcessingPriority.Normal;
             }
             break;
         case DocumentType.Overwrite:
             if (".md".Equals(Path.GetExtension(file.File), StringComparison.OrdinalIgnoreCase))
             {
                 return ProcessingPriority.Normal;
             }
             break;
         default:
             break;
     }
     return ProcessingPriority.NotSupported;
 }
        public override FileModel Load(FileAndType file, ImmutableDictionary<string, object> metadata)
        {
            switch (file.Type)
            {
                case DocumentType.Article:
                    var page = YamlUtility.Deserialize<PageViewModel>(Path.Combine(file.BaseDir, file.File));
                    if (page.Items == null || page.Items.Count == 0)
                    {
                        return null;
                    }
                    if (page.Metadata == null)
                    {
                        page.Metadata = metadata.ToDictionary(p => p.Key, p => p.Value);
                    }
                    else
                    {
                        foreach (var item in metadata)
                        {
                            if (!page.Metadata.ContainsKey(item.Key))
                            {
                                page.Metadata[item.Key] = item.Value;
                            }
                        }
                    }

                    var displayLocalPath = PathUtility.MakeRelativePath(EnvironmentContext.BaseDirectory, file.FullPath);

                    return new FileModel(file, page, serializer: Environment.Is64BitProcess ? null : new BinaryFormatter())
                    {
                        Uids = (from item in page.Items select new UidDefinition(item.Uid, displayLocalPath)).ToImmutableArray(),
                        LocalPathFromRepoRoot = displayLocalPath,
                        LocalPathFromRoot = displayLocalPath
                    };
                case DocumentType.Overwrite:
                    // TODO: Refactor current behavior that overwrite file is read multiple times by multiple processors
                    return OverwriteDocumentReader.Read(file);
                default:
                    throw new NotSupportedException();
            }
        }
        public override FileModel Load(FileAndType file, ImmutableDictionary <string, object> metadata)
        {
            string uid = null;
            Dictionary <string, object> content = null;
            var metafile = Path.Combine(file.BaseDir, file.File.TrimEnd('.') + ".meta");

            if (File.Exists(metafile))
            {
                content = YamlUtility.Deserialize <Dictionary <string, object> >(metafile);
                if (content != null)
                {
                    foreach (var item in metadata)
                    {
                        if (!content.ContainsKey(item.Key))
                        {
                            content[item.Key] = item.Value;
                        }
                        if (item.Key == Constants.PropertyName.Uid)
                        {
                            uid = item.Value as string;
                        }
                    }
                }
            }
            if (content == null)
            {
                content = metadata.ToDictionary(p => p.Key, p => p.Value);
            }

            var filePath         = Path.Combine(file.BaseDir, file.File);
            var repoDetail       = GitUtility.GetGitDetail(filePath);
            var displayLocalPath = PathUtility.MakeRelativePath(EnvironmentContext.BaseDirectory, file.FullPath);

            return(new FileModel(file, content)
            {
                Uids = string.IsNullOrEmpty(uid) ? ImmutableArray <UidDefinition> .Empty : ImmutableArray <UidDefinition> .Empty.Add(new UidDefinition(uid, displayLocalPath)),
                LocalPathFromRepoRoot = repoDetail?.RelativePath ?? Path.Combine(file.BaseDir, file.File).ToDisplayPath(),
                LocalPathFromRoot = displayLocalPath
            });
        }
Exemple #38
0
        public override FileModel Load(FileAndType file, ImmutableDictionary <string, object> metadata)
        {
            switch (file.Type)
            {
            case DocumentType.Article:
                var filePath = Path.Combine(file.BaseDir, file.File);
                var swagger  = SwaggerJsonParser.Parse(filePath);
                swagger.Metadata[DocumentTypeKey] = RestApiDocumentType;
                swagger.Raw = EnvironmentContext.FileAbstractLayer.ReadAllText(filePath);
                CheckOperationId(swagger, file.File);

                var repoInfo = GitUtility.TryGetFileDetail(filePath);
                if (repoInfo != null)
                {
                    swagger.Metadata["source"] = new SourceDetail()
                    {
                        Remote = repoInfo
                    };
                }

                swagger.Metadata = MergeMetadata(swagger.Metadata, metadata);
                var vm = SwaggerModelConverter.FromSwaggerModel(swagger);
                var displayLocalPath = PathUtility.MakeRelativePath(EnvironmentContext.BaseDirectory, file.FullPath);

                return(new FileModel(file, vm, serializer: Environment.Is64BitProcess?null: new BinaryFormatter())
                {
                    Uids = new[] { new UidDefinition(vm.Uid, displayLocalPath) }
                    .Concat(from item in vm.Children select new UidDefinition(item.Uid, displayLocalPath))
                    .Concat(from tag in vm.Tags select new UidDefinition(tag.Uid, displayLocalPath)).ToImmutableArray(),
                    LocalPathFromRoot = displayLocalPath
                });

            case DocumentType.Overwrite:
                // TODO: Refactor current behavior that overwrite file is read multiple times by multiple processors
                return(OverwriteDocumentReader.Read(file));

            default:
                throw new NotSupportedException();
            }
        }
Exemple #39
0
        private void ValidateToc(TocItemViewModel item, FileModel model, IHostService hostService)
        {
            if (!PathUtility.IsRelativePath(item.Href))
            {
                return;
            }
            var file = model.File;

            FileAndType originalTocFile = null;

            string fileName = Path.GetFileName(item.Href);

            // Special handle for folder ends with '/'
            if (string.IsNullOrEmpty(fileName))
            {
                var    href    = item.Href + "toc.yml";
                var    absHref = (RelativePath)file + (RelativePath)href;
                string tocPath = absHref.GetPathFromWorkingFolder();
                if (!hostService.SourceFiles.TryGetValue(tocPath, out originalTocFile))
                {
                    href    = item.Href + "toc.md";
                    absHref = (RelativePath)file + (RelativePath)href;
                    tocPath = absHref.GetPathFromWorkingFolder();
                    if (!hostService.SourceFiles.TryGetValue(tocPath, out originalTocFile))
                    {
                        var error = $"Unable to find either toc.yml or toc.md inside {item.Href}. Make sure the file is included in config file docfx.json!";
                        Logger.LogError(error, file: model.LocalPathFromRepoRoot);
                        throw new DocumentException(error);
                    }
                }

                Logger.LogInfo($"TOC file {href} inside {item.Href} is used", file: model.LocalPathFromRepoRoot);
                item.Href         = href;
                item.OriginalHref = item.Href;
            }

            // Set default homepage
            SetDefaultHomepage(item, originalTocFile, model);
        }
        public override ProcessingPriority GetProcessingPriority(FileAndType file)
        {
            if (file.Type != DocumentType.Article)
            {
                return(ProcessingPriority.NotSupported);
            }
            if (!".yml".Equals(Path.GetExtension(file.File), StringComparison.OrdinalIgnoreCase) &&
                !".yaml".Equals(Path.GetExtension(file.File), StringComparison.OrdinalIgnoreCase))
            {
                return(ProcessingPriority.NotSupported);
            }
            var mime = YamlMime.ReadMime(file.File);

            switch (mime)
            {
            case YamlDocumentYamlMime:
                return(ProcessingPriority.Normal);

            default:
                return(ProcessingPriority.NotSupported);
            }
        }
 public FileModel Load(FileAndType file, ImmutableDictionary<string, object> metadata)
 {
     if (file.Type != DocumentType.Article)
     {
         throw new NotSupportedException();
     }
     var content = MarkdownReader.ReadMarkdownAsConceptual(file.BaseDir, file.File);
     foreach (var item in metadata)
     {
         if (!content.ContainsKey(item.Key))
         {
             content[item.Key] = item.Value;
         }
     }
     return new FileModel(
         file,
         content,
         serializer: new BinaryFormatter())
     {
         LocalPathFromRepoRoot = (content["source"] as SourceDetail)?.Remote?.RelativePath
     };
 }
        public void Postbuild(ImmutableList <FileModel> models, IHostService host)
        {
            foreach (FileModel model in models)
            {
                if (model.Content is IDictionary <string, object> content)
                {
                    // Add relative output path
                    FileAndType fileAndType = model.FileAndType;
                    // Copied from DocFx's LinkPhaseHandler.ExportManifest
                    string pageRelPath = Path.ChangeExtension((RelativePath)fileAndType.DestinationDir + (((RelativePath)fileAndType.File) - (RelativePath)fileAndType.SourceDir), null);
                    content.Add("mimo_pageRelPath", pageRelPath);

                    // Copy all options to manifest properties so they are accessible in post processors
                    IDictionary <string, object> manifestProperties = model.ManifestProperties as IDictionary <string, object>;

                    foreach (KeyValuePair <string, object> pair in content)
                    {
                        manifestProperties.Add(pair);
                    }
                }
            }
        }
        public override FileModel Load(FileAndType file, ImmutableDictionary <string, object> metadata)
        {
            string uid = null;
            Dictionary <string, object> content = null;
            var pp       = EnvironmentContext.FileAbstractLayer.GetPhysicalPath(file.File);
            var metafile = pp.TrimEnd('.') + ".meta";

            if (File.Exists(metafile))
            {
                content = YamlUtility.Deserialize <Dictionary <string, object> >(metafile);
                if (content != null)
                {
                    foreach (var item in metadata)
                    {
                        if (!content.ContainsKey(item.Key))
                        {
                            content[item.Key] = item.Value;
                        }
                        if (item.Key == Constants.PropertyName.Uid)
                        {
                            uid = item.Value as string;
                        }
                    }
                }
            }
            if (content == null)
            {
                content = metadata.ToDictionary(p => p.Key, p => p.Value);
            }

            var localPathFromRoot = PathUtility.MakeRelativePath(EnvironmentContext.BaseDirectory, EnvironmentContext.FileAbstractLayer.GetPhysicalPath(file.File));

            return(new FileModel(file, content)
            {
                Uids = string.IsNullOrEmpty(uid) ? ImmutableArray <UidDefinition> .Empty : ImmutableArray <UidDefinition> .Empty.Add(new UidDefinition(uid, localPathFromRoot)),
                LocalPathFromRoot = localPathFromRoot
            });
        }
        public override ProcessingPriority GetProcessingPriority(FileAndType file)
        {
            switch (file.Type)
            {
                case DocumentType.Article:
                    if (".yml".Equals(Path.GetExtension(file.File), StringComparison.OrdinalIgnoreCase) ||
                        ".yaml".Equals(Path.GetExtension(file.File), StringComparison.OrdinalIgnoreCase))
                    {
                        var mime = YamlMime.ReadMime(Path.Combine(file.BaseDir, file.File));
                        switch (mime)
                        {
                            case YamlMime.ManagedReference:
                                return ProcessingPriority.Normal;
                            case null:
                                return ProcessingPriority.BelowNormal;
                            default:
                                return ProcessingPriority.NotSupported;
                        }
                    }

                    if (".csyml".Equals(Path.GetExtension(file.File), StringComparison.OrdinalIgnoreCase) ||
                        ".csyaml".Equals(Path.GetExtension(file.File), StringComparison.OrdinalIgnoreCase))
                    {
                        return ProcessingPriority.Normal;
                    }

                    break;
                case DocumentType.Overwrite:
                    if (".md".Equals(Path.GetExtension(file.File), StringComparison.OrdinalIgnoreCase))
                    {
                        return ProcessingPriority.Normal;
                    }
                    break;
                default:
                    break;
            }
            return ProcessingPriority.NotSupported;
        }
Exemple #45
0
        public FileModel Load(FileAndType file, ImmutableDictionary <string, object> metadata)
        {
            string uid = null;
            Dictionary <string, object> content = null;
            var metafile = Path.Combine(file.BaseDir, file.File.TrimEnd('.') + ".meta");

            if (File.Exists(metafile))
            {
                content = YamlUtility.Deserialize <Dictionary <string, object> >(metafile);
                if (content != null)
                {
                    foreach (var item in metadata)
                    {
                        if (!content.ContainsKey(item.Key))
                        {
                            content[item.Key] = item.Value;
                        }
                        if (item.Key == "uid")
                        {
                            uid = item.Value as string;
                        }
                    }
                }
            }
            if (content == null)
            {
                content = metadata.ToDictionary(p => p.Key, p => p.Value);
            }

            var filePath   = Path.Combine(file.BaseDir, file.File);
            var repoDetail = GitUtility.GetGitDetail(filePath);

            return(new FileModel(file, content)
            {
                Uids = string.IsNullOrEmpty(uid) ? ImmutableArray <string> .Empty : ImmutableArray <string> .Empty.Add(uid),
                LocalPathFromRepoRoot = repoDetail?.RelativePath
            });
        }
Exemple #46
0
        private static FileModel Load(
            IDocumentProcessor processor,
            ImmutableDictionary <string, object> metadata,
            FileMetadata fileMetadata,
            FileAndType file)
        {
            using (new LoggerFileScope(file.File))
            {
                Logger.LogDiagnostic($"Processor {processor.Name}: Loading...");

                var path = Path.Combine(file.BaseDir, file.File);
                metadata = ApplyFileMetadata(path, metadata, fileMetadata);
                try
                {
                    return(processor.Load(file, metadata));
                }
                catch (Exception)
                {
                    Logger.LogError($"Unable to load file: {file.File} via processor: {processor.Name}.");
                    throw;
                }
            }
        }
        public override FileModel Load(FileAndType file, ImmutableDictionary <string, object> metadata)
        {
            if (file.Type != DocumentType.Article)
            {
                throw new NotSupportedException();
            }
            var content = MarkdownReader.ReadMarkdownAsConceptual(file.BaseDir, file.File);

            foreach (var item in metadata)
            {
                if (!content.ContainsKey(item.Key))
                {
                    content[item.Key] = item.Value;
                }
            }
            return(new FileModel(
                       file,
                       content,
                       serializer: new BinaryFormatter())
            {
                LocalPathFromRepoRoot = (content["source"] as SourceDetail)?.Remote?.RelativePath
            });
        }
        public override ProcessingPriority GetProcessingPriority(FileAndType file)
        {
            if (file.Type != DocumentType.Article)
            {
                return(ProcessingPriority.NotSupported);
            }
            if (".md".Equals(Path.GetExtension(file.File), StringComparison.OrdinalIgnoreCase))
            {
                var filenameWithoutExtension = Path.ChangeExtension(file.File, null);

                // exclude overwrite markdown segments
                var subExtension = Path.GetExtension(filenameWithoutExtension);
                if ((".yml".Equals(subExtension, StringComparison.OrdinalIgnoreCase) ||
                     ".yaml".Equals(subExtension, StringComparison.OrdinalIgnoreCase)) &&
                    EnvironmentContext.FileAbstractLayer.Exists(filenameWithoutExtension))
                {
                    return(ProcessingPriority.NotSupported);
                }

                return(ProcessingPriority.Normal);
            }
            return(ProcessingPriority.NotSupported);
        }
        public override FileModel Load(FileAndType file, ImmutableDictionary <string, object> metadata)
        {
            if (file.Type != DocumentType.Article)
            {
                throw new NotSupportedException();
            }

            var content = YamlUtility.Deserialize <YamlDocumentModel>(file.File);

            foreach (var item in metadata.Where(item => !content.Metadata.ContainsKey(item.Key)))
            {
                content.Metadata[item.Key] = item.Value;
            }
            var localPathFromRoot = PathUtility.MakeRelativePath(EnvironmentContext.BaseDirectory, EnvironmentContext.FileAbstractLayer.GetPhysicalPath(file.File));

            return(new FileModel(
                       file,
                       content)
            {
                LocalPathFromRoot = localPathFromRoot,
                DocumentType = content.DocumentType ?? DefaultDocumentType,
            });
        }
        public override FileModel Load(FileAndType file, ImmutableDictionary<string, object> metadata)
        {
            switch (file.Type)
            {
                case DocumentType.Article:
                    var filePath = Path.Combine(file.BaseDir, file.File);
                    var swaggerContent = File.ReadAllText(filePath);
                    var swagger = SwaggerJsonParser.Parse(swaggerContent);
                    swagger.Metadata[DocumentTypeKey] = RestApiDocumentType;
                    swagger.Raw = swaggerContent;
                    CheckOperationId(swagger, file.File);

                    var repoInfo = GitUtility.TryGetFileDetail(filePath);
                    if (repoInfo != null)
                    {
                        swagger.Metadata["source"] = new SourceDetail() { Remote = repoInfo };
                    }

                    swagger.Metadata = MergeMetadata(swagger.Metadata, metadata);
                    var vm = SwaggerModelConverter.FromSwaggerModel(swagger);
                    var displayLocalPath = PathUtility.MakeRelativePath(EnvironmentContext.BaseDirectory, file.FullPath);

                    return new FileModel(file, vm, serializer: Environment.Is64BitProcess ? null : new BinaryFormatter())
                    {
                        Uids = new[] { new UidDefinition(vm.Uid, displayLocalPath) }
                            .Concat(from item in vm.Children select new UidDefinition(item.Uid, displayLocalPath))
                            .Concat(from tag in vm.Tags select new UidDefinition(tag.Uid, displayLocalPath)).ToImmutableArray(),
                        LocalPathFromRepoRoot = repoInfo?.RelativePath ?? StringExtension.ToDisplayPath(filePath),
                        LocalPathFromRoot = displayLocalPath
                    };
                case DocumentType.Overwrite:
                    // TODO: Refactor current behavior that overwrite file is read multiple times by multiple processors
                    return OverwriteDocumentReader.Read(file);
                default:
                    throw new NotSupportedException();
            }
        }
        public static FileModel Read(FileAndType file)
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            if (file.Type != DocumentType.Overwrite)
            {
                throw new NotSupportedException(file.Type.ToString());
            }

            var overwrites = MarkdownReader.ReadMarkdownAsOverwrite(file.BaseDir, file.File);

            if (overwrites == null || overwrites.Count == 0)
            {
                return(null);
            }

            var displayLocalPath = overwrites[0].Documentation?.Remote?.RelativePath ?? Path.Combine(file.BaseDir, file.File).ToDisplayPath();

            return(new FileModel(file, overwrites, serializer: new BinaryFormatter())
            {
                Uids = (from item in overwrites
                        select new UidDefinition(
                            item.Uid,
                            displayLocalPath,
                            item.Documentation.StartLine + 1
                            )).ToImmutableArray(),
                Properties =
                {
                    LinkToFiles = new HashSet <string>(),
                    LinkToUids  = new HashSet <string>(),
                },
                LocalPathFromRepoRoot = displayLocalPath,
            });
        }
        public FileModel Load(FileAndType file, ImmutableDictionary<string, object> metadata)
        {
            string uid = null;
            Dictionary<string, object> content = null;
            var metafile = Path.Combine(file.BaseDir, file.File.TrimEnd('.') + ".meta");
            if (File.Exists(metafile))
            {
                content = YamlUtility.Deserialize<Dictionary<string, object>>(metafile);
                if (content != null)
                {
                    foreach (var item in metadata)
                    {
                        if (!content.ContainsKey(item.Key))
                        {
                            content[item.Key] = item.Value;
                        }
                        if (item.Key == "uid")
                        {
                            uid = item.Value as string;
                        }
                    }
                }
            }
            if (content == null)
            {
                content = metadata.ToDictionary(p => p.Key, p => p.Value);
            }

            var filePath = Path.Combine(file.BaseDir, file.File);
            var repoDetail = GitUtility.GetGitDetail(filePath);

            return new FileModel(file, content)
            {
                Uids = string.IsNullOrEmpty(uid) ? ImmutableArray<string>.Empty : ImmutableArray<string>.Empty.Add(uid),
                LocalPathFromRepoRoot = repoDetail?.RelativePath
            };
        }
        protected override FileModel LoadArticle(FileAndType file, ImmutableDictionary <string, object> metadata)
        {
            var page = YamlUtility.Deserialize <PageViewModel>(file.File);

            if (page.Items == null || page.Items.Count == 0)
            {
                Logger.LogWarning("No items found from YAML file. No output is generated");
                return(null);
            }
            if (page.Items[0].SupportedLanguages == null || page.Items[0].SupportedLanguages.Length == 0)
            {
                throw new ArgumentException($"{nameof(ItemViewModel.SupportedLanguages)} must contain at least one language");
            }
            if (page.Items.Any(i => string.IsNullOrEmpty(i.Uid)))
            {
                throw new ArgumentException($"{nameof(ItemViewModel.Uid)} must not be null or empty");
            }
            if (page.Metadata == null)
            {
                page.Metadata = metadata.ToDictionary(p => p.Key, p => p.Value);
            }
            else
            {
                foreach (var item in metadata.Where(item => !page.Metadata.ContainsKey(item.Key)))
                {
                    page.Metadata[item.Key] = item.Value;
                }
            }

            var localPathFromRoot = PathUtility.MakeRelativePath(EnvironmentContext.BaseDirectory, EnvironmentContext.FileAbstractLayer.GetPhysicalPath(file.File));

            return(new FileModel(file, page, serializer: new BinaryFormatter())
            {
                Uids = (from item in page.Items select new UidDefinition(item.Uid, localPathFromRoot)).ToImmutableArray(),
                LocalPathFromRoot = localPathFromRoot
            });
        }
        public override FileModel Load(FileAndType file, ImmutableDictionary<string, object> metadata)
        {
            string uid = null;
            Dictionary<string, object> content = null;
            var metafile = Path.Combine(file.BaseDir, file.File.TrimEnd('.') + ".meta");
            if (File.Exists(metafile))
            {
                content = YamlUtility.Deserialize<Dictionary<string, object>>(metafile);
                if (content != null)
                {
                    foreach (var item in metadata)
                    {
                        if (!content.ContainsKey(item.Key))
                        {
                            content[item.Key] = item.Value;
                        }
                        if (item.Key == Constants.PropertyName.Uid)
                        {
                            uid = item.Value as string;
                        }
                    }
                }
            }
            if (content == null)
            {
                content = metadata.ToDictionary(p => p.Key, p => p.Value);
            }

            var displayLocalPath = PathUtility.MakeRelativePath(EnvironmentContext.BaseDirectory, file.FullPath);

            return new FileModel(file, content)
            {
                Uids = string.IsNullOrEmpty(uid) ? ImmutableArray<UidDefinition>.Empty : ImmutableArray<UidDefinition>.Empty.Add(new UidDefinition(uid, displayLocalPath)),
                LocalPathFromRoot = displayLocalPath
            };
        }
 public bool CanIncrementalBuild(FileAndType fileAndType) => true;
Exemple #56
0
 public MarkupResult Markup(string markdown, FileAndType ft, bool omitParse)
 {
     if (markdown == null)
     {
         throw new ArgumentNullException(nameof(markdown));
     }
     if (ft == null)
     {
         throw new ArgumentNullException(nameof(ft));
     }
     return MarkupCore(markdown, ft, omitParse);
 }
Exemple #57
0
 private MarkupResult MarkupCore(string markdown, FileAndType ft, bool omitParse)
 {
     try
     {
         var mr = MarkdownService.Markup(markdown, ft.File);
         if (omitParse)
         {
             return mr;
         }
         return Parse(mr, ft);
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.Fail("Markup failed!");
         var message = $"Markup failed: {ex.Message}.";
         Logger.LogError(message);
         throw new DocumentException(message, ex);
     }
 }
Exemple #58
0
 public MarkupResult Parse(MarkupResult markupResult, FileAndType ft)
 {
     if (markupResult == null)
     {
         throw new ArgumentNullException(nameof(markupResult));
     }
     if (ft == null)
     {
         throw new ArgumentNullException(nameof(ft));
     }
     return ParseCore(markupResult, ft);
 }
Exemple #59
0
        private MarkupResult ParseCore(MarkupResult markupResult, FileAndType ft)
        {
            var doc = new HtmlDocument();
            doc.LoadHtml(markupResult.Html);
            var result = markupResult.Clone();

            var node = doc.DocumentNode.SelectSingleNode("//yamlheader");
            if (node != null)
            {
                using (var sr = new StringReader(StringHelper.HtmlDecode(node.InnerHtml)))
                {
                    result.YamlHeader = YamlUtility.Deserialize<Dictionary<string, object>>(sr).ToImmutableDictionary();
                }
                node.Remove();
            }

            result.FileLinkSources = GetFileLinkSource(ft, doc);
            result.LinkToFiles = result.FileLinkSources.Keys.ToImmutableArray();

            result.UidLinkSources = GetUidLinkSources(doc);
            result.LinkToUids = result.UidLinkSources.Keys.ToImmutableHashSet();

            if (result.Dependency.Length > 0)
            {
                result.Dependency =
                    (from d in result.Dependency
                     select
                        ((TypeForwardedToRelativePath)ft.File + (TypeForwardedToRelativePath)d)
                            .GetPathFromWorkingFolder()
                            .ToString()
                    ).ToImmutableArray();
            }
            using (var sw = new StringWriter())
            {
                doc.Save(sw);
                result.Html = sw.ToString();
            }
            return result;
        }
Exemple #60
0
        private ImmutableDictionary<string, ImmutableList<LinkSourceInfo>> GetFileLinkSource(FileAndType ft, HtmlDocument doc)
        {
            var fileLinkSources = new Dictionary<string, List<LinkSourceInfo>>();
            foreach (var pair in (from n in doc.DocumentNode.Descendants()
                                  where !string.Equals(n.Name, "xref", StringComparison.OrdinalIgnoreCase)
                                  from attr in n.Attributes
                                  where string.Equals(attr.Name, "src", StringComparison.OrdinalIgnoreCase) ||
                                        string.Equals(attr.Name, "href", StringComparison.OrdinalIgnoreCase)
                                  where !string.IsNullOrWhiteSpace(attr.Value)
                                  select new { Node = n, Attr = attr }).ToList())
            {
                string anchor = null;
                var link = pair.Attr;
                string linkFile = link.Value;
                var index = linkFile.IndexOfAny(UriFragmentOrQueryString);
                if (index != -1)
                {
                    anchor = linkFile.Substring(index);
                    linkFile = linkFile.Remove(index);
                }
                if (TypeForwardedToRelativePath.IsRelativePath(linkFile))
                {
                    var path = (TypeForwardedToRelativePath)ft.File + (TypeForwardedToRelativePath)linkFile;
                    string file = path.GetPathFromWorkingFolder().UrlDecode();
                    if (SourceFiles.ContainsKey(file))
                    {
                        link.Value = file;
                        if (!string.IsNullOrEmpty(anchor) &&
                            string.Equals(link.Name, "href", StringComparison.OrdinalIgnoreCase))
                        {
                            pair.Node.SetAttributeValue("anchor", anchor);
                        }
                    }

                    List<LinkSourceInfo> sources;
                    if (!fileLinkSources.TryGetValue(file, out sources))
                    {
                        sources = new List<LinkSourceInfo>();
                        fileLinkSources[file] = sources;
                    }
                    sources.Add(new LinkSourceInfo
                    {
                        Target = file,
                        Anchor = anchor,
                        SourceFile = pair.Node.GetAttributeValue("sourceFile", null),
                        LineNumber = pair.Node.GetAttributeValue("sourceStartLineNumber", 0),
                    });
                }
            }
            return fileLinkSources.ToImmutableDictionary(x => x.Key, x => x.Value.ToImmutableList());
        }