Ejemplo n.º 1
0
        private static void UpdateHref(HtmlAgilityPack.HtmlNode link, Dictionary<string, string> map, Func<string, string> updater)
        {
            string attribute = "href";
            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);
                }

                if (map.TryGetValue(key, out href))
                {
                    href = updater(href);
                    href += anchor;
                    link.SetAttributeValue(attribute, href);
                }
                else
                {
                    Logger.Log(LogLevel.Warning, $"File {path} is not found.");
                    // TODO: what to do if file path not exists?
                    // CURRENT: fallback to the original one
                    link.SetAttributeValue(attribute, path);
                }
            }
        }
Ejemplo n.º 2
0
 private static void UpdateSrc(HtmlAgilityPack.HtmlNode link, Dictionary<string, string> map, Func<string, string> updater)
 {
     string attribute = "src";
     var key = link.GetAttributeValue(attribute, null);
     string path;
     if (PathUtility.TryGetPathFromWorkingFolder(key, out path))
     {
         string xrefValue;
         if (map.TryGetValue(key, out xrefValue))
         {
             xrefValue = updater(xrefValue);
             link.SetAttributeValue(attribute, xrefValue);
         }
         else
         {
             Logger.Log(LogLevel.Warning, $"File {path} is not found.");
             // TODO: what to do if file path not exists?
             // CURRENT: fallback to the original one
             link.SetAttributeValue(attribute, path);
         }
     }
 }
Ejemplo n.º 3
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);
            }
        }