Example #1
0
        public static HrefType GetHrefType(string href)
        {
            var hrefWithoutAnchor = href != null?UriUtility.GetPath(href) : href;

            if (!PathUtility.IsRelativePath(hrefWithoutAnchor))
            {
                return(HrefType.AbsolutePath);
            }
            var fileName = Path.GetFileName(hrefWithoutAnchor);

            if (string.IsNullOrEmpty(fileName))
            {
                return(HrefType.RelativeFolder);
            }

            var tocFileType = GetTocFileType(hrefWithoutAnchor);

            if (tocFileType == TocFileType.Markdown)
            {
                return(HrefType.MarkdownTocFile);
            }

            if (tocFileType == TocFileType.Yaml)
            {
                return(HrefType.YamlTocFile);
            }

            return(HrefType.RelativeFile);
        }
Example #2
0
        public override IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            if (parser.Options.LegacyMode)
            {
                return(TryMatchOld(parser, context));
            }
            var match = context.Match(DfmFencesMatcher);

            if (match?.Length > 0)
            {
                var sourceInfo = context.Consume(match.Length);

                // [!code-lang[name](href "optionalTitle")]
                var name  = StringHelper.UnescapeMarkdown(match["name"].GetValue());
                var href  = StringHelper.UnescapeMarkdown(match["href"].GetValue());
                var lang  = match.GetGroup("lang")?.GetValue() ?? string.Empty;
                var title = StringHelper.UnescapeMarkdown(match.GetGroup("title")?.GetValue() ?? string.Empty);
                var queryStringAndFragment = UriUtility.GetQueryStringAndFragment(href);
                var path            = UriUtility.GetPath(href);
                var pathQueryOption =
                    !string.IsNullOrEmpty(queryStringAndFragment) ?
                    ParsePathQueryString(queryStringAndFragment.Remove(1), queryStringAndFragment.Substring(1)) :
                    null;
                return(new DfmFencesBlockToken(this, parser.Context, name, path, sourceInfo, lang, title, pathQueryOption));
            }
            return(null);
        }
Example #3
0
        private void UpdateHref(HtmlNode link, string attribute, IDocumentBuildContext context, string sourceFilePath, string destFilePath)
        {
            var originalHref   = link.GetAttributeValue(attribute, null);
            var path           = UriUtility.GetPath(originalHref);
            var anchorFromNode = link.GetAttributeValue("anchor", null);
            var segments       = anchorFromNode ?? UriUtility.GetQueryStringAndFragment(originalHref);

            link.Attributes.Remove("anchor");
            if (RelativePath.TryParse(path) == null)
            {
                if (!string.IsNullOrEmpty(anchorFromNode))
                {
                    link.SetAttributeValue(attribute, originalHref + anchorFromNode);
                }
                return;
            }
            var fli = FileLinkInfo.Create(sourceFilePath, destFilePath, path, context);

            // fragment and query in original href takes precedence over the one from hrefGenerator
            var href = _settings.HrefGenerator?.GenerateHref(fli);

            link.SetAttributeValue(
                attribute,
                href == null ? fli.Href + segments : UriUtility.MergeHref(href, segments));
        }
        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)
            {
                var message = $"Invalid toc link for {propertyName}: {originalPathToFile}.";
                Logger.LogError(message, code: ErrorCodes.Toc.InvalidTocLink);
                throw new DocumentException(message);
            }

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

            var fli  = FileLinkInfo.Create(model.LocalPathFromRoot, model.File, path, context);
            var href = context.HrefGenerator?.GenerateHref(fli);

            if (fli.ToFileInDest == null && href == null)
            {
                // original path to file can be null for files generated by docfx in PreBuild
                var displayFilePath = string.IsNullOrEmpty(originalPathToFile) ? pathToFile : originalPathToFile;
                Logger.LogInfo($"Unable to find file \"{displayFilePath}\" for {propertyName} referenced by TOC file \"{model.LocalPathFromRoot}\"");
                return(originalPathToFile);
            }

            // fragment and query in original href takes precedence over the one from hrefGenerator
            return(href == null ? fli.Href + segments : UriUtility.MergeHref(href, segments));
        }
Example #5
0
            private string GetHrefFromRoot(string originalHref, HandleModelAttributesContext context)
            {
                if (context.FileAndType == null || string.IsNullOrEmpty(originalHref) || !RelativePath.IsRelativePath(originalHref))
                {
                    return(originalHref);
                }

                var result = originalHref;
                var ft     = context.FileAndType;
                var path   = (RelativePath)ft.File + (RelativePath)UriUtility.GetPath(originalHref);
                var file   = path.GetPathFromWorkingFolder().UrlDecode();

                if (context.Host.SourceFiles.ContainsKey(file))
                {
                    result = file.UrlEncode().ToString() + UriUtility.GetQueryStringAndFragment(originalHref);
                }

                List <LinkSourceInfo> sources;

                if (!context.FileLinkSources.TryGetValue(file, out sources))
                {
                    sources = new List <LinkSourceInfo>();
                    context.FileLinkSources[file] = sources;
                }
                sources.Add(new LinkSourceInfo
                {
                    Target     = file,
                    Anchor     = UriUtility.GetFragment(originalHref),
                    SourceFile = ft.File,
                });
                context.LinkToFiles.Add(file);

                return(result);
            }
Example #6
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}.");
            }

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

            var fli    = FileLinkInfo.Create(model.LocalPathFromRoot, model.File, path, context);
            var result = context.HrefGenerator?.GenerateHref(fli);

            if (fli.ToFileInDest == null && result == null)
            {
                // original path to file can be null for files generated by docfx in PreBuild
                var displayFilePath = string.IsNullOrEmpty(originalPathToFile) ? pathToFile : originalPathToFile;
                Logger.LogInfo($"Unable to find file \"{displayFilePath}\" for {propertyName} referenced by TOC file \"{model.LocalPathFromRoot}\"");
                return(originalPathToFile);
            }

            return((result ?? fli.Href) + segments);
        }
        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 #8
0
        private string UpdateHrefCore(string href, RelativePath fromFile)
        {
            if (href.Length == 0)
            {
                return(string.Empty);
            }
            var path = UriUtility.GetPath(href);

            if (path.Length == 0)
            {
                return(href);
            }
            var qf = UriUtility.GetQueryStringAndFragment(href);
            var rp = RelativePath.TryParse(path);

            if (rp == null || !rp.IsFromWorkingFolder())
            {
                return(href);
            }
            if (!FileMap.TryGetValue(rp.UrlDecode(), out string filePath))
            {
                return(href);
            }
            string modifiedPath;

            if (fromFile == null)
            {
                modifiedPath = ((RelativePath)filePath).UrlEncode();
            }
            else
            {
                modifiedPath = ((RelativePath)filePath - fromFile).UrlEncode();
            }
            return(modifiedPath + qf);
        }
        internal static bool IsPackageFile(PackagePart part)
        {
            var path = UriUtility.GetPath(part.Uri);

            return(!ExcludePaths.Any(p => path.StartsWith(p, StringComparison.OrdinalIgnoreCase)) &&
                   !PackageUtility.IsManifest(path));
        }
Example #10
0
 public void TestUriUtility(string input, string path, string queryString, string fragment, string queryStringAndFragment, string nonFragment)
 {
     Assert.Equal(path, UriUtility.GetPath(input));
     Assert.Equal(queryString, UriUtility.GetQueryString(input));
     Assert.Equal(fragment, UriUtility.GetFragment(input));
     Assert.Equal(queryStringAndFragment, UriUtility.GetQueryStringAndFragment(input));
     Assert.Equal(nonFragment, UriUtility.GetNonFragment(input));
 }
Example #11
0
        public object Interpret(BaseSchema schema, object value, IProcessContext context, string path)
        {
            if (value == null || !CanInterpret(schema))
            {
                return(value);
            }

            if (!(value is string val))
            {
                throw new ArgumentException($"{value.GetType()} is not supported type string.");
            }

            if (!Uri.TryCreate(val, UriKind.RelativeOrAbsolute, out Uri uri))
            {
                var message = $"{val} is not a valid href";
                Logger.LogError(message, code: ErrorCodes.Build.InvalidHref);
                throw new DocumentException(message);
            }

            // "/" is also considered as absolute to us
            if (uri.IsAbsoluteUri || val.StartsWith("/", StringComparison.Ordinal))
            {
                return(Helper.RemoveHostName(val, _siteHostName));
            }

            // sample value: a/b/c?hello
            var filePath  = UriUtility.GetPath(val);
            var fragments = UriUtility.GetQueryStringAndFragment(val);
            var relPath   = RelativePath.TryParse(filePath);

            if (relPath != null)
            {
                var originalFile = context.GetOriginalContentFile(path);
                var currentFile  = (RelativePath)originalFile.File;
                relPath = (currentFile + relPath.UrlDecode()).GetPathFromWorkingFolder();
                if (_exportFileLink)
                {
                    (context.FileLinkSources).AddFileLinkSource(new LinkSourceInfo
                    {
                        Target     = relPath,
                        Anchor     = UriUtility.GetFragment(val),
                        SourceFile = originalFile.File
                    });
                }

                if (_updateValue && context.BuildContext != null)
                {
                    var resolved = (RelativePath)context.BuildContext.GetFilePath(relPath);
                    if (resolved != null)
                    {
                        val = resolved.MakeRelativeTo(((RelativePath)context.FileAndType.File).GetPathFromWorkingFolder()).UrlEncode() + fragments;
                    }
                }
            }

            return(val);
        }
Example #12
0
        public object Interpret(BaseSchema schema, object value, IProcessContext context, string path)
        {
            if (value == null || !CanInterpret(schema))
            {
                return(value);
            }

            if (!(value is string val))
            {
                throw new ArgumentException($"{value.GetType()} is not supported type string.");
            }

            if (!Uri.TryCreate(val, UriKind.RelativeOrAbsolute, out Uri uri))
            {
                throw new DocumentException($"{val} is not a valid href");
            }

            // "/" is also considered as absolute to us
            if (uri.IsAbsoluteUri || val.StartsWith("/"))
            {
                return(value);
            }

            // sample value: a/b/c?hello
            var filePath  = UriUtility.GetPath(val);
            var fragments = UriUtility.GetQueryStringAndFragment(val);
            var relPath   = RelativePath.TryParse(filePath);

            if (relPath != null)
            {
                var currentFile = (RelativePath)context.Model.OriginalFileAndType.File;
                relPath = (currentFile + relPath.UrlDecode()).GetPathFromWorkingFolder();
                if (_exportFileLink)
                {
                    ((Dictionary <string, List <LinkSourceInfo> >)context.Properties.FileLinkSources).AddFileLinkSource(new LinkSourceInfo
                    {
                        Target     = relPath,
                        Anchor     = UriUtility.GetFragment(val),
                        SourceFile = context.Model.OriginalFileAndType.File
                    });
                }

                if (_updateValue && context.BuildContext != null)
                {
                    var resolved = (RelativePath)context.BuildContext.GetFilePath(relPath);
                    if (resolved != null)
                    {
                        val = resolved.MakeRelativeTo(((RelativePath)context.Model.File).GetPathFromWorkingFolder()).UrlEncode() + fragments;
                    }
                }
            }

            return(val);
        }
        public void GetPathFromUri()
        {
            // Arrange
            Uri uri = new Uri("/a/b.txt", UriKind.Relative);

            // Act
            string path = UriUtility.GetPath(uri);

            // Assert
            Assert.Equal(@"a\b.txt", path);
        }
        public void GetPathFromUriWithEncodedSpacesDecodesSpaces()
        {
            // Arrange
            Uri uri = new Uri("/a/b/This%20is%20a%20test/c.txt", UriKind.Relative);

            // Act
            string path = UriUtility.GetPath(uri);

            // Assert
            Assert.Equal(@"a\b\This is a test\c.txt", path);
        }
Example #15
0
        public async Task SubFeed_InitMultipleFeedsVerifyDestroyDoesNotModifyOthers()
        {
            using (var packagesFolder = new TestFolder())
                using (var testContext = new AzureTestContext())
                    using (var testContext2 = new AzureTestContext())
                    {
                        // Use a subfeed for the filesystem
                        var subFeedName  = "testSubFeedA";
                        var subFeedName2 = "testSubFeedB";
                        var root         = UriUtility.GetPath(testContext.Uri, subFeedName);
                        var root2        = UriUtility.GetPath(testContext.Uri, subFeedName2);
                        testContext.FileSystem  = new AzureFileSystem(testContext.LocalCache, root, root, testContext.StorageAccount, testContext.ContainerName, feedSubPath: subFeedName);
                        testContext2.FileSystem = new AzureFileSystem(testContext.LocalCache, root2, root2, testContext.StorageAccount, testContext.ContainerName, feedSubPath: subFeedName2);

                        await testContext.InitAsync();

                        await testContext2.InitAsync();

                        var testPackage = new TestNupkg("packageA", "1.0.0");
                        var zipFile     = testPackage.Save(packagesFolder.Root);

                        var result = await InitCommand.RunAsync(testContext.LocalSettings,
                                                                testContext.FileSystem,
                                                                enableCatalog : true,
                                                                enableSymbols : true,
                                                                log : testContext.Logger,
                                                                token : CancellationToken.None);

                        result &= await InitCommand.RunAsync(testContext.LocalSettings,
                                                             testContext2.FileSystem,
                                                             enableCatalog : true,
                                                             enableSymbols : true,
                                                             log : testContext2.Logger,
                                                             token : CancellationToken.None);

                        // Destroy feed2
                        result &= await DestroyCommand.RunAsync(testContext.LocalSettings,
                                                                testContext2.FileSystem,
                                                                testContext2.Logger);

                        // Validate feed1
                        result &= await ValidateCommand.RunAsync(testContext.LocalSettings,
                                                                 testContext.FileSystem,
                                                                 testContext.Logger);


                        result.Should().BeTrue();

                        await testContext.CleanupAsync();

                        await testContext2.CleanupAsync();
                    }
        }
Example #16
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 #17
0
        public void SubFeed_VerifySubFeedPath()
        {
            using (var target = new TestFolder())
                using (var cache = new LocalCache())
                {
                    var root       = UriUtility.CreateUri(target.Root, "feedA");
                    var fileSystem = new PhysicalFileSystem(cache, root, root, feedSubPath: "feedA");

                    fileSystem.Root.Should().Be(UriUtility.EnsureTrailingSlash(root));
                    fileSystem.LocalRoot.Should().StartWith(Path.Combine(target.Root, "feedA"));

                    fileSystem.Get("index.json").EntityUri.Should().Be(UriUtility.GetPath(root, "index.json"));
                }
        }
Example #18
0
        private void ValidateHref(TocItemViewModel item)
        {
            if (item.Href == null)
            {
                return;
            }
            var hrefType = Utility.GetHrefType(item.Href);

            if ((hrefType == HrefType.MarkdownTocFile || hrefType == HrefType.YamlTocFile || hrefType == HrefType.RelativeFolder) &&
                (UriUtility.HasFragment(item.Href) || UriUtility.HasQueryString(item.Href)))
            {
                Logger.LogWarning($"Illegal href: {item.Href}.`#` or `?` aren't allowed when referencing toc file.");
                item.Href = UriUtility.GetPath(item.Href);
            }
        }
Example #19
0
        private void BuildCore(TocItemViewModel item, FileModel model, IHostService hostService, string includedFrom = null)
        {
            if (item == null)
            {
                return;
            }

            var linkToUids      = new HashSet <string>();
            var linkToFiles     = new HashSet <string>();
            var uidLinkSources  = new Dictionary <string, ImmutableList <LinkSourceInfo> >();
            var fileLinkSources = new Dictionary <string, ImmutableList <LinkSourceInfo> >();

            if (Utility.IsSupportedRelativeHref(item.Href))
            {
                UpdateDependencies(linkToFiles, fileLinkSources, item.Href);
            }
            if (Utility.IsSupportedRelativeHref(item.Homepage))
            {
                UpdateDependencies(linkToFiles, fileLinkSources, item.Homepage);
            }
            if (!string.IsNullOrEmpty(item.TopicUid))
            {
                UpdateDependencies(linkToUids, uidLinkSources, item.TopicUid);
            }

            model.LinkToUids      = model.LinkToUids.Union(linkToUids);
            model.LinkToFiles     = model.LinkToFiles.Union(linkToFiles);
            model.UidLinkSources  = model.UidLinkSources.Merge(uidLinkSources);
            model.FileLinkSources = model.FileLinkSources.Merge(fileLinkSources);

            includedFrom = item.IncludedFrom ?? includedFrom;
            if (item.Items != null)
            {
                foreach (var i in item.Items)
                {
                    BuildCore(i, model, hostService, includedFrom);
                }
            }

            void UpdateDependencies(HashSet <string> linkTos, Dictionary <string, ImmutableList <LinkSourceInfo> > linkSources, string link)
            {
                var path   = UriUtility.GetPath(link);
                var anchor = UriUtility.GetFragment(link);

                linkTos.Add(path);
                AddOrUpdate(linkSources, path, GetLinkSourceInfo(path, anchor, model.File, includedFrom));
            }
        }
Example #20
0
        public string GetHrefFromRoot(string originalHref, string sourceFileKey)
        {
            if (string.IsNullOrEmpty(sourceFileKey) || string.IsNullOrEmpty(originalHref) || !RelativePath.IsRelativePath(originalHref))
            {
                return(originalHref);
            }
            var path = (RelativePath)sourceFileKey + (RelativePath)UriUtility.GetPath(originalHref);
            var file = path.GetPathFromWorkingFolder().UrlDecode();

            if (!_context.AllSourceFiles.ContainsKey(file))
            {
                Logger.LogWarning($"Invalid file link: ({originalHref})", file: sourceFileKey, code: WarningCodes.Build.InvalidFileLink);
                return(originalHref);
            }
            return(file.UrlEncode().ToString() + UriUtility.GetQueryStringAndFragment(originalHref));
        }
Example #21
0
        public async Task SubFeed_PushAndVerifyNoFilesInRoot()
        {
            using (var packagesFolder = new TestFolder())
                using (var testContext = new AmazonS3TestContext())
                {
                    // Use a subfeed for the filesystem
                    var subFeedName = "testSubFeed";
                    var root        = UriUtility.GetPath(testContext.Uri, subFeedName);
                    testContext.FileSystem = new AmazonS3FileSystem(
                        testContext.LocalCache, root, root,
                        testContext.Client, testContext.BucketName,
                        ServerSideEncryptionMethod.None, feedSubPath: subFeedName
                        );

                    await testContext.InitAsync();

                    var testPackage = new TestNupkg("packageA", "1.0.0");
                    var zipFile     = testPackage.Save(packagesFolder.Root);

                    var result = await InitCommand.RunAsync(testContext.LocalSettings,
                                                            testContext.FileSystem,
                                                            enableCatalog : true,
                                                            enableSymbols : true,
                                                            log : testContext.Logger,
                                                            token : CancellationToken.None);

                    result &= await PushCommand.RunAsync(testContext.LocalSettings,
                                                         testContext.FileSystem,
                                                         new List <string>() { zipFile.FullName },
                                                         force : false,
                                                         skipExisting : false,
                                                         log : testContext.Logger);

                    result &= await ValidateCommand.RunAsync(testContext.LocalSettings,
                                                             testContext.FileSystem,
                                                             testContext.Logger);

                    result.Should().BeTrue();

                    var files = await AmazonS3FileSystemAbstraction.GetFilesAsync(testContext.Client, testContext.BucketName, CancellationToken.None);

                    files.Where(e => e.Key.IndexOf(subFeedName, StringComparison.OrdinalIgnoreCase) < 0).Should().BeEmpty();
                    files.Where(e => e.Key.IndexOf(subFeedName, StringComparison.OrdinalIgnoreCase) > -1).Should().NotBeEmpty();

                    await testContext.CleanupAsync();
                }
        }
Example #22
0
            private async Task <List <XRefSpec> > HandleCoreAsync(Task <List <XRefSpec> > task, string name, string value)
            {
                var list = await task;

                foreach (var item in list)
                {
                    if (string.IsNullOrEmpty(item.Href))
                    {
                        continue;
                    }
                    var mvc = HttpUtility.ParseQueryString(UriUtility.GetQueryString(item.Href));
                    mvc[name] = value;
                    item.Href = UriUtility.GetPath(item.Href) +
                                "?" + mvc.ToString() +
                                UriUtility.GetFragment(item.Href);
                }
                return(list);
            }
Example #23
0
        protected override void RegisterTocMapToContext(TocItemViewModel item, FileModel model, IDocumentBuildContext context)
        {
            var key = model.Key;
            // If tocHref is set, href is originally RelativeFolder type, and href is set to the homepage of TocHref,
            // So in this case, TocHref should be used to in TocMap
            // TODO: what if user wants to set TocHref?
            var tocHref     = item.TocHref;
            var tocHrefType = Utility.GetHrefType(tocHref);

            if (tocHrefType == HrefType.MarkdownTocFile || tocHrefType == HrefType.YamlTocFile)
            {
                context.RegisterToc(key, UriUtility.GetPath(tocHref));
            }
            else
            {
                var href = item.Href; // Should be original href from working folder starting with ~
                if (Utility.IsSupportedRelativeHref(href))
                {
                    context.RegisterToc(key, UriUtility.GetPath(href));
                }
            }
        }
Example #24
0
        private void UpdateNearestToc(IHostService host, TocItemViewModel item, FileModel toc, ConcurrentDictionary <string, RelativeInfo> nearest)
        {
            var tocHref = item.TocHref;
            var type    = Utility.GetHrefType(tocHref);

            if (type == HrefType.MarkdownTocFile || type == HrefType.YamlTocFile)
            {
                UpdateNearestTocCore(host, UriUtility.GetPath(tocHref), toc, nearest);
            }
            else if (item.TopicUid == null && Utility.IsSupportedRelativeHref(item.Href))
            {
                UpdateNearestTocCore(host, UriUtility.GetPath(item.Href), toc, nearest);
            }

            if (item.Items != null && item.Items.Count > 0)
            {
                foreach (var i in item.Items)
                {
                    UpdateNearestToc(host, i, toc, nearest);
                }
            }
        }
Example #25
0
        public int Extract(string packageFile, string directory, bool suppressNestedScriptWarning)
        {
            int filesExtracted = 0;

            using (var package = Package.Open(packageFile, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                var files =
                    from part in package.GetParts()
                    where IsPackageFile(part)
                    select part;

                foreach (var part in files)
                {
                    filesExtracted++;
                    var path = UriUtility.GetPath(part.Uri);

                    if (!suppressNestedScriptWarning)
                    {
                        WarnIfScriptInSubFolder(path);
                    }

                    path = Path.Combine(directory, path);

                    var parent = Path.GetDirectoryName(path);
                    if (parent != null && !Directory.Exists(parent))
                    {
                        Directory.CreateDirectory(parent);
                    }

                    using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
                        using (var stream = part.GetStream())
                        {
                            stream.CopyTo(fileStream);
                            fileStream.Flush();
                        }
                }
            }
            return(filesExtracted);
        }
        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  = FileLinkInfo.Create(sourceFilePath, destFilePath, originalPath, context);
            var href = _settings.HrefGenerator?.GenerateHref(fli) ?? fli.Href;

            link.SetAttributeValue(attribute, href + UriUtility.GetQueryString(originalHref) + (anchor ?? UriUtility.GetFragment(originalHref)));
        }
Example #27
0
 public void UriUtility_GetPath(string root, string path, string expected)
 {
     Assert.Equal(expected, UriUtility.GetPath(new Uri(root), path).AbsoluteUri);
 }
Example #28
0
        public async Task SubFeed_PushAndVerifyWithNestedFeedsVerifySuccess()
        {
            using (var packagesFolder = new TestFolder())
                using (var testContext = new AzureTestContext())
                    using (var testContext2 = new AzureTestContext())
                    {
                        // Use a subfeed for the filesystem
                        var subFeedName = "testSubFeed";
                        var root        = UriUtility.GetPath(testContext.Uri, subFeedName);
                        testContext.FileSystem = new AzureFileSystem(testContext.LocalCache, root, root, testContext.StorageAccount, testContext.ContainerName, feedSubPath: subFeedName);

                        await testContext.InitAsync();

                        await testContext2.InitAsync();

                        var testPackage = new TestNupkg("packageA", "1.0.0");
                        var zipFile     = testPackage.Save(packagesFolder.Root);

                        var result = await InitCommand.RunAsync(testContext.LocalSettings,
                                                                testContext.FileSystem,
                                                                enableCatalog : true,
                                                                enableSymbols : true,
                                                                log : testContext.Logger,
                                                                token : CancellationToken.None);

                        result &= await InitCommand.RunAsync(testContext.LocalSettings,
                                                             testContext2.FileSystem,
                                                             enableCatalog : true,
                                                             enableSymbols : true,
                                                             log : testContext2.Logger,
                                                             token : CancellationToken.None);

                        result &= await PushCommand.RunAsync(testContext.LocalSettings,
                                                             testContext.FileSystem,
                                                             new List <string>() { zipFile.FullName },
                                                             force : false,
                                                             skipExisting : false,
                                                             log : testContext.Logger);

                        result &= await PushCommand.RunAsync(testContext.LocalSettings,
                                                             testContext2.FileSystem,
                                                             new List <string>() { zipFile.FullName },
                                                             force : false,
                                                             skipExisting : false,
                                                             log : testContext2.Logger);

                        result &= await ValidateCommand.RunAsync(testContext.LocalSettings,
                                                                 testContext.FileSystem,
                                                                 testContext.Logger);

                        result &= await ValidateCommand.RunAsync(testContext.LocalSettings,
                                                                 testContext2.FileSystem,
                                                                 testContext2.Logger);

                        result.Should().BeTrue();

                        await testContext.CleanupAsync();

                        await testContext2.CleanupAsync();
                    }
        }
Example #29
0
        protected override void HandleCore(HtmlDocument document, ManifestItem manifestItem, string inputFile, string outputFile)
        {
            _fileMapping[outputFile] = inputFile;

            // RFC 3986: relative-ref = relative-part [ "?" query ] [ "#" fragment ]
            _linksWithBookmark[outputFile] =
                (from node in GetNodesWithAttribute(document, "href")
                 let nocheck = node.GetAttributeValue("nocheck", null)
                               where !"bookmark".Equals(nocheck, StringComparison.OrdinalIgnoreCase)
                               let link = node.GetAttributeValue("href", null)
                                          let bookmark = UriUtility.GetFragment(link).TrimStart('#')
                                                         let decodedLink = RelativePath.TryParse(HttpUtility.UrlDecode(UriUtility.GetPath(link)))
                                                                           where !string.IsNullOrEmpty(bookmark) && !WhiteList.Contains(bookmark)
                                                                           where decodedLink != null
                                                                           select new LinkItem
            {
                Title = node.InnerText,
                Href = TransformPath(outputFile, decodedLink),
                Bookmark = bookmark,
                SourceFragment = WebUtility.HtmlDecode(node.GetAttributeValue("data-raw-source", null)),
                SourceFile = WebUtility.HtmlDecode(node.GetAttributeValue("sourceFile", null)),
                SourceLineNumber = node.GetAttributeValue("sourceStartLineNumber", 0),
                TargetLineNumber = node.Line
            }).ToList();
            var anchors = GetNodeAttribute(document, "id").Concat(GetNodeAttribute(document, "name"));

            _registeredBookmarks[outputFile] = new HashSet <string>(anchors);
        }