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 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); }