Example #1
0
        private string ResolveHref(string pathToFile, string originalPathToFile, FileModel model, IDocumentBuildContext context, string propertyName)
        {
            if (!Utility.IsSupportedRelativeHref(pathToFile))
            {
                return(pathToFile);
            }

            var index = pathToFile.IndexOfAny(QueryStringOrAnchor);

            if (index == 0)
            {
                throw new DocumentException($"Invalid toc link for {propertyName}: {originalPathToFile}.");
            }

            string href = index == -1
                ? context.GetFilePath(pathToFile)
                : context.GetFilePath(pathToFile.Remove(index));


            if (href == null)
            {
                Logger.LogInfo($"Unable to find file \"{originalPathToFile}\" for {propertyName} referenced by TOC file \"{model.LocalPathFromRoot}\"");
                return(originalPathToFile);
            }

            var relativePath = GetRelativePath(href, model.File);
            var path         = ((TypeForwardedToRelativePath)relativePath).UrlEncode().ToString();

            if (index >= 0)
            {
                path += pathToFile.Substring(index);
            }
            return(path);
        }
Example #2
0
        private string ResolveHref(string pathToFile, string originalPathToFile, FileModel model, IDocumentBuildContext context)
        {
            if (!Utility.IsSupportedRelativeHref(pathToFile))
            {
                return(pathToFile);
            }

            var index = pathToFile.IndexOf('#');

            if (index == 0)
            {
                throw new DocumentException($"Invalid toc link: {originalPathToFile}.");
            }
            string href = index == -1
                ? context.GetFilePath(pathToFile)
                : context.GetFilePath(pathToFile.Remove(index));


            if (href == null)
            {
                Logger.LogWarning($"Unable to find file \"{originalPathToFile}\" referenced by TOC file \"{model.LocalPathFromRepoRoot}\"");
                return(originalPathToFile);
            }

            var relativePath = GetRelativePath(href, model.File);
            var path         = ((RelativePath)relativePath).UrlEncode().ToString();

            if (index >= 0)
            {
                path += pathToFile.Substring(index);
            }
            return(path);
        }
Example #3
0
        public SystemMetadata Generate(ManifestItem item)
        {
            var attrs = new SystemMetadata
            {
                Language = Constants.DefaultLanguage,
            };

            string key  = GetFileKey(item.Key);
            var    file = (RelativePath)item.ModelFile;

            attrs.RelativePathToRoot = (RelativePath.Empty).MakeRelativeTo(file);
            attrs.PathFromRoot       = file.RemoveWorkingFolder();

            // 1. Root Toc is always in the top directory of output folder
            var rootToc = _toc.FirstOrDefault();

            if (rootToc != null)
            {
                var rootTocPath = rootToc.RemoveWorkingFolder();
                if (rootTocPath.SubdirectoryCount == 0)
                {
                    attrs.RootTocPath         = rootTocPath;
                    attrs.RootTocRelativePath = attrs.RootTocPath == null ? null : rootTocPath.MakeRelativeTo(file);
                    Logger.LogVerbose($"Root TOC file {rootTocPath} is found.");
                }
                else
                {
                    Logger.LogVerbose($"Root TOC file from output folder is not found, the toppest TOC file is {rootTocPath}");
                }
            }

            // 2. The algorithm of toc current article belongs to:
            //    a. If toc can be found in TocMap, return that toc
            //    b. Elsewise, get the nearest toc, **nearest** means nearest toc in **OUTPUT** folder
            var parentTocFiles = _context.GetTocFileKeySet(key)?.Select(s => _context.GetFilePath(s));
            var parentToc      = GetNearestToc(parentTocFiles);

            if (parentToc == null)
            {
                parentToc = GetDefaultToc(key);
            }

            if (parentToc != null)
            {
                var parentTocPath = parentToc.RemoveWorkingFolder();
                attrs.TocPath         = parentTocPath;
                attrs.TocRelativePath = attrs.TocPath == null ? null : parentTocPath.MakeRelativeTo(file);
                Logger.LogVerbose($"TOC file {parentTocPath} is found for {item.LocalPathFromRepoRoot}.");
            }
            else
            {
                Logger.LogVerbose($"TOC file for {item.LocalPathFromRepoRoot} is not found.");
            }

            return(attrs);
        }
Example #4
0
        private static void UpdateHref(HtmlAgilityPack.HtmlNode link, string attribute, IDocumentBuildContext context, string relativePath)
        {
            var    key = link.GetAttributeValue(attribute, null);
            string path;

            if (RelativePath.TryGetPathWithoutWorkingFolderChar(key, out path))
            {
                var href   = context.GetFilePath(key);
                var anchor = link.GetAttributeValue("anchor", null);

                if (href != null)
                {
                    href = ((RelativePath)UpdateFilePath(href, relativePath)).UrlEncode();

                    if (!string.IsNullOrEmpty(anchor))
                    {
                        href += anchor;
                        link.Attributes.Remove("anchor");
                    }
                    link.SetAttributeValue(attribute, href);
                }
                else
                {
                    Logger.LogWarning($"File {path} is not found in {relativePath}.");
                    // TODO: what to do if file path not exists?
                    // CURRENT: fallback to the original one
                    if (!string.IsNullOrEmpty(anchor))
                    {
                        path += anchor;
                        link.Attributes.Remove("anchor");
                    }
                    link.SetAttributeValue(attribute, path);
                }
            }
        }
        private string ResolveHref(string pathToFile, string originalPathToFile, FileModel model, IDocumentBuildContext context, string propertyName)
        {
            if (!Utility.IsSupportedRelativeHref(pathToFile))
            {
                return(pathToFile);
            }

            var index = pathToFile.IndexOfAny(QueryStringOrAnchor);

            if (index == 0)
            {
                throw new DocumentException($"Invalid toc link for {propertyName}: {originalPathToFile}.");
            }

            var path     = UriUtility.GetPath(pathToFile);
            var segments = UriUtility.GetQueryStringAndFragment(pathToFile);

            string href = context.GetFilePath(HttpUtility.UrlDecode(path));

            // original path to file can be null for files generated by docfx in PreBuild
            var displayFilePath = string.IsNullOrEmpty(originalPathToFile) ? pathToFile : originalPathToFile;

            if (href == null)
            {
                Logger.LogInfo($"Unable to find file \"{displayFilePath}\" for {propertyName} referenced by TOC file \"{model.LocalPathFromRoot}\"");
                return(originalPathToFile);
            }

            var relativePath = GetRelativePath(href, model.File);
            var resolvedHref = ((RelativePath)relativePath).UrlEncode().ToString() + segments;

            return(resolvedHref);
        }
Example #6
0
        private static void UpdateHref(HtmlAgilityPack.HtmlNode link, string attribute, IDocumentBuildContext context, string relativePath)
        {
            var originalHref = link.GetAttributeValue(attribute, null);
            var anchor       = link.GetAttributeValue("anchor", null);

            link.Attributes.Remove("anchor");
            string href;
            var    path = TypeForwardedToRelativePath.TryParse(originalHref);

            if (path?.IsFromWorkingFolder() == true)
            {
                var targetPath = (TypeForwardedToRelativePath)context.GetFilePath(path.UrlDecode());

                if (targetPath != null)
                {
                    href = (targetPath.RemoveWorkingFolder() - (TypeForwardedToRelativePath)relativePath).UrlEncode();
                }
                else
                {
                    Logger.LogWarning($"File {path} is not found in {relativePath}.");
                    // TODO: what to do if file path not exists?
                    // CURRENT: fallback to the original one
                    href = (path.UrlDecode().RemoveWorkingFolder() - (TypeForwardedToRelativePath)relativePath).UrlEncode();
                }
                link.SetAttributeValue(attribute, href + anchor);
            }
        }
        public SystemMetadataGenerator(IDocumentBuildContext context)
        {
            _context = context ?? throw new ArgumentNullException(nameof(context));

            // Order toc files by the output folder depth
            _toc = context.GetTocInfo()
                   .Select(s => new FileInfo(s.TocFileKey, context.GetFilePath(s.TocFileKey)))
                   .Where(s => s.RelativePath != null)
                   .OrderBy(s => s.RelativePath.SubdirectoryCount);
        }
Example #8
0
        private void UpdateHref(HtmlNode link, string attribute, IDocumentBuildContext context, string sourceFilePath, string destFilePath)
        {
            var originalHref = link.GetAttributeValue(attribute, null);
            var anchor       = link.GetAttributeValue("anchor", null);

            link.Attributes.Remove("anchor");
            var originalPath = UriUtility.GetPath(originalHref);
            var path         = RelativePath.TryParse(originalPath);

            if (path == null)
            {
                if (!string.IsNullOrEmpty(anchor))
                {
                    link.SetAttributeValue(attribute, originalHref + anchor);
                }

                return;
            }

            var fli = new FileLinkInfo
            {
                FromFileInSource = sourceFilePath,
                FromFileInDest   = destFilePath,
            };

            if (path.IsFromWorkingFolder())
            {
                var targetInSource = path.UrlDecode();
                fli.ToFileInSource   = targetInSource.RemoveWorkingFolder();
                fli.ToFileInDest     = RelativePath.GetPathWithoutWorkingFolderChar(context.GetFilePath(targetInSource));
                fli.FileLinkInSource = targetInSource - (RelativePath)sourceFilePath;
                if (fli.ToFileInDest != null)
                {
                    var resolved = (RelativePath)fli.ToFileInDest - (RelativePath)destFilePath;
                    fli.FileLinkInDest = resolved;
                    fli.Href           = resolved.UrlEncode();
                }
                else
                {
                    fli.Href = (targetInSource.RemoveWorkingFolder() - ((RelativePath)sourceFilePath).RemoveWorkingFolder()).UrlEncode();
                }
            }
            else
            {
                fli.FileLinkInSource = path.UrlDecode();
                fli.ToFileInSource   = ((RelativePath)sourceFilePath + path).RemoveWorkingFolder();
                fli.FileLinkInDest   = fli.FileLinkInSource;
                fli.Href             = originalPath;
            }
            var href = _settings.HrefGenerator?.GenerateHref(fli) ?? fli.Href;

            link.SetAttributeValue(attribute, href + UriUtility.GetQueryString(originalHref) + (anchor ?? UriUtility.GetFragment(originalHref)));
        }
Example #9
0
        public SystemMetadataGenerator(IDocumentBuildContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            _context = context;

            // Order toc files by the output folder depth
            _toc = context.GetTocInfo()
                .Select(s => new FileInfo(s.TocFileKey, (RelativePath)context.GetFilePath(s.TocFileKey)))
                .Where(s => s.File != null)
                .OrderBy(s => s.File.SubdirectoryCount);
        }
Example #10
0
        private static Manifest GenerateManifest(IDocumentBuildContext context, List <TemplateManifestItem> items)
        {
            var toc       = context.GetTocInfo();
            var homepages = toc
                            .Where(s => !string.IsNullOrEmpty(s.Homepage))
                            .Select(s => new HomepageInfo
            {
                Homepage = RelativePath.GetPathWithoutWorkingFolderChar(s.Homepage),
                TocPath  = RelativePath.GetPathWithoutWorkingFolderChar(context.GetFilePath(s.TocFileKey))
            }).ToList();

            return(new Manifest
            {
                Homepages = homepages,
                Files = items,
            });
        }
Example #11
0
        private string GetUpdatedHref(string originalPathToFile, FileModel model, IDocumentBuildContext context)
        {
            if (!PathUtility.IsRelativePath(originalPathToFile))
            {
                return(originalPathToFile);
            }

            string href = context.GetFilePath(originalPathToFile);

            if (href == null)
            {
                throw new DocumentException($"Unable to find file \"{originalPathToFile}\" referenced by TOC file \"{model.LocalPathFromRepoRoot}\"");
            }

            var relativePath = GetRelativePath(href, model.File);

            return(((RelativePath)relativePath).UrlEncode());
        }
Example #12
0
        private string ResolveHref(string originalPathToFile, FileModel model, IDocumentBuildContext context)
        {
            if (!Utility.IsSupportedRelativeHref(originalPathToFile))
            {
                return(originalPathToFile);
            }

            string href = context.GetFilePath(originalPathToFile);

            if (href == null)
            {
                Logger.LogWarning($"Unable to find file \"{originalPathToFile}\" referenced by TOC file \"{model.LocalPathFromRepoRoot}\"");
                return(originalPathToFile);
            }

            var relativePath = GetRelativePath(href, model.File);

            return(((RelativePath)relativePath).UrlEncode());
        }
Example #13
0
        private static void UpdateHref(HtmlAgilityPack.HtmlNode link, string attribute, IDocumentBuildContext context, string relativePath)
        {
            var    key = link.GetAttributeValue(attribute, null);
            string path;

            if (PathUtility.TryGetPathFromWorkingFolder(key, out path))
            {
                string href;
                // For href, # may be appended, remove # before search file from map
                var anchorIndex = key.IndexOf("#");
                var anchor      = string.Empty;
                if (anchorIndex == 0)
                {
                    return;
                }
                if (anchorIndex > 0)
                {
                    anchor = key.Substring(anchorIndex);
                    key    = key.Remove(anchorIndex);
                }

                href = context.GetFilePath(HttpUtility.UrlDecode(key));
                if (href != null)
                {
                    href  = ((RelativePath)UpdateFilePath(href, relativePath)).UrlEncode();
                    href += anchor;
                    link.SetAttributeValue(attribute, href);
                }
                else
                {
                    Logger.LogWarning($"File {path} is not found in {relativePath}.");
                    // TODO: what to do if file path not exists?
                    // CURRENT: fallback to the original one
                    link.SetAttributeValue(attribute, path);
                }
            }
        }
Example #14
0
        private string ResolveHref(string pathToFile, string originalPathToFile, FileModel model, IDocumentBuildContext context, string propertyName)
        {
            if (!Utility.IsSupportedRelativeHref(pathToFile))
            {
                return pathToFile;
            }

            var index = pathToFile.IndexOfAny(QueryStringOrAnchor);
            if (index == 0)
            {
                throw new DocumentException($"Invalid toc link for {propertyName}: {originalPathToFile}.");
            }

            string href = index == -1
                ? context.GetFilePath(pathToFile)
                : context.GetFilePath(pathToFile.Remove(index));


            if (href == null)
            {
                Logger.LogInfo($"Unable to find file \"{originalPathToFile}\" for {propertyName} referenced by TOC file \"{model.LocalPathFromRoot}\"");
                return originalPathToFile;
            }

            var relativePath = GetRelativePath(href, model.File);
            var path = ((TypeForwardedToRelativePath)relativePath).UrlEncode().ToString();
            if (index >= 0)
            {
                path += pathToFile.Substring(index);
            }
            return path;
        }
Example #15
0
 private static RelativePath GetFinalFilePath(string key, IDocumentBuildContext context)
 {
     var fileKey = GetFileKey(key);
     return ((RelativePath)context.GetFilePath(fileKey)).RemoveWorkingFolder();
 }
Example #16
0
        private static void UpdateHref(HtmlAgilityPack.HtmlNode link, string attribute, IDocumentBuildContext context, string relativePath)
        {
            var originalHref = link.GetAttributeValue(attribute, null);
            var anchor = link.GetAttributeValue("anchor", null);
            link.Attributes.Remove("anchor");
            string href;
            var path = RelativePath.TryParse(originalHref);

            if (path?.IsFromWorkingFolder() == true)
            {
                var targetPath = (RelativePath)context.GetFilePath(path.UrlDecode());

                if (targetPath != null)
                {
                    href = (targetPath.RemoveWorkingFolder() - (RelativePath)relativePath).UrlEncode();
                }
                else
                {
                    Logger.LogInfo($"File {path} is not found in {relativePath}.");
                    // TODO: what to do if file path not exists?
                    // CURRENT: fallback to the original one
                    href = (path.UrlDecode().RemoveWorkingFolder() - (RelativePath)relativePath).UrlEncode();
                }
                link.SetAttributeValue(attribute, href + anchor);
            }
        }
Example #17
0
        private static RelativePath GetFinalFilePath(string key, IDocumentBuildContext context)
        {
            var fileKey = GetFileKey(key);

            return(((RelativePath)context.GetFilePath(fileKey)).RemoveWorkingFolder());
        }
Example #18
0
        public static FileLinkInfo Create(string fromFileInSource, string fromFileInDest, string href, IDocumentBuildContext context)
        {
            if (fromFileInSource == null)
            {
                throw new ArgumentNullException(nameof(fromFileInSource));
            }
            if (fromFileInDest == null)
            {
                throw new ArgumentNullException(nameof(fromFileInDest));
            }
            if (href == null)
            {
                throw new ArgumentNullException(nameof(href));
            }
            if (UriUtility.HasFragment(href) || UriUtility.HasQueryString(href))
            {
                throw new ArgumentException("fragment and query string is not supported", nameof(href));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var path = RelativePath.TryParse(href)?.UrlDecode();

            if (path == null)
            {
                throw new ArgumentException("only relative path is supported", nameof(href));
            }

            var fli = new FileLinkInfo
            {
                FromFileInSource = fromFileInSource,
                FromFileInDest   = fromFileInDest,
                GroupInfo        = context.GroupInfo,
            };

            if (path.IsFromWorkingFolder())
            {
                var targetInSource = path;
                fli.ToFileInSource   = targetInSource.RemoveWorkingFolder();
                fli.ToFileInDest     = RelativePath.GetPathWithoutWorkingFolderChar(context.GetFilePath(targetInSource));
                fli.FileLinkInSource = targetInSource - (RelativePath)fromFileInSource;
                if (fli.ToFileInDest != null)
                {
                    var resolved = (RelativePath)fli.ToFileInDest - (RelativePath)fromFileInDest;
                    fli.FileLinkInDest = resolved;
                    fli.Href           = resolved.UrlEncode();
                }
                else
                {
                    fli.Href = (targetInSource.RemoveWorkingFolder() - ((RelativePath)fromFileInSource).RemoveWorkingFolder()).UrlEncode();
                }
            }
            else
            {
                fli.FileLinkInSource = path;
                fli.ToFileInSource   = ((RelativePath)fromFileInSource + path).RemoveWorkingFolder();
                fli.FileLinkInDest   = fli.FileLinkInSource;
                fli.Href             = href;
            }

            return(fli);
        }
Example #19
0
        private string GetUpdatedHref(string originalPathToFile, FileModel model, IDocumentBuildContext context)
        {
            if (!PathUtility.IsRelativePath(originalPathToFile)) return originalPathToFile;

            string href = context.GetFilePath(originalPathToFile);

            if (href == null)
            {
                throw new DocumentException($"Unalbe to find file \"{originalPathToFile}\" referenced by TOC file \"{model.LocalPathFromRepoRoot}\"");
            }

            var relativePath = GetRelativePath(href, model.File);
            return relativePath;
        }