OneNoteConversionTool.FormatReaders.EpubReader.UseFullPathForLinks C# (CSharp) Method

UseFullPathForLinks() private method

Replace the relative paths with full paths
private UseFullPathForLinks ( HtmlAgilityPack.HtmlDocument htmlDoc, string htmlPath ) : void
htmlDoc HtmlAgilityPack.HtmlDocument
htmlPath string
return void
		private void UseFullPathForLinks(HtmlDocument htmlDoc, string htmlPath)
		{
			HtmlNodeCollection links = htmlDoc.DocumentNode.SelectNodes("//*[@background or @lowsrc or @src or @href]");
			if (links == null)
			{
				return;
			}

			// Get the directory where the html path exists
			string htmlDir = Path.GetDirectoryName(htmlPath);
			if (htmlDir == null)
			{
				return;
			}

			foreach (var link in links)
			{
				// Background
				if (link.Attributes["background"] != null)
				{
					link.Attributes["background"].Value = Path.GetFullPath(Path.Combine(htmlDir, link.Attributes["background"].Value));
				}

				// Lowsrc
				if (link.Attributes["lowsrc"] != null)
				{
					link.Attributes["lowsrc"].Value = Path.GetFullPath(Path.Combine(htmlDir, link.Attributes["lowsrc"].Value));
				}

				// src
				if (link.Attributes["src"] != null)
				{
					link.Attributes["src"].Value = Path.GetFullPath(Path.Combine(htmlDir, link.Attributes["src"].Value));
				}

				// href
				if (link.Attributes["href"] != null && File.Exists(Path.Combine(htmlDir, link.Attributes["href"].Value)))
				{
					link.Attributes["href"].Value = Path.GetFullPath(Path.Combine(htmlDir, link.Attributes["href"].Value));
				}
				else if (link.Attributes["href"] != null && link.Attributes["href"].Value.Contains('#'))
				{
					int indexOfHash = link.Attributes["href"].Value.LastIndexOf('#');
					string relativePath = link.Attributes["href"].Value.Substring(0, indexOfHash);
					string fullPath = File.Exists(Path.Combine(htmlDir, relativePath))
						? Path.GetFullPath(Path.Combine(htmlDir, relativePath))
						: link.Attributes["href"].Value;
					link.Attributes["href"].Value = indexOfHash == 0 ? htmlPath : fullPath;
				}
			}
		}