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

GetPagesLevel() public method

Gets the level of each page as dictionary where the key in the dictionary is the full path of the xhtml page
public GetPagesLevel ( ) : int>.Dictionary
return int>.Dictionary
		public Dictionary<string, int> GetPagesLevel()
		{
			var pagesLevel = new Dictionary<string, int>();

			// Gets the opf package document
			var opfDoc = new XmlDocument();
			opfDoc.Load(GetOpfFilePath());
			if (opfDoc.DocumentElement == null)
			{
				return pagesLevel;
			}

			// Gets the Navigation page
			XmlNode xmlNavNode = opfDoc.DocumentElement.SelectSingleNode("//*[@properties=\"nav\"]");
			if (xmlNavNode == null || xmlNavNode.Attributes == null || xmlNavNode.Attributes["href"] == null)
			{
				return pagesLevel;
			}

			// Load the navigation page as HtmlDocument
			var navDoc = new HtmlDocument();
			string navPath = Path.Combine(_mContentDir, xmlNavNode.Attributes["href"].Value);
			navDoc.Load(navPath);

			// Gets the directory of all the xhtml files
			string htmlDir = Path.GetDirectoryName(navPath);
			if (htmlDir == null)
			{
				return pagesLevel;
			}

			// Gets the page levels
			HtmlNodeCollection nodes = navDoc.DocumentNode.SelectNodes("//*[@href]");
			foreach (var node in nodes)
			{
				if (!File.Exists(Path.Combine(htmlDir, node.Attributes["href"].Value)))
				{
					continue;
				}
				string filePath = Path.GetFullPath(Path.Combine(htmlDir, node.Attributes["href"].Value));
				int pageLevel = node.AncestorsAndSelf().Count(e => e.Name.Equals("li"));
				if (!pagesLevel.ContainsKey(filePath))
				{
					pagesLevel.Add(filePath, pageLevel);
				}
			}

			return pagesLevel;
		}
		#endregion

Usage Example

		/// <summary>
		/// Converts the ePub document input file into OneNote
		/// </summary>
		/// <param name="inputFile"></param>
		/// <param name="outputDir"></param>
		/// <returns></returns>
		public virtual bool ConvertEpubToOneNote(string inputFile, string outputDir)
		{
			try
			{
				// Initialize epub reader class
				var epub = new EpubReader(inputFile, outputDir);

				// Get the page contents
				List<string> pageTitles = epub.GetPageTitles();
				List<HtmlDocument> pagesHtml = epub.GetPagesAsHtmlDocuments();
				List<string> pagePaths = epub.GetPagePaths();
				Dictionary<string, int> pagesLevel = epub.GetPagesLevel();

				// Create a new OneNote Notebook
				var note = new OneNoteGenerator(outputDir);
				string notebookId = note.CreateNotebook(GetSupportedInputFormat());
				string sectionId = note.CreateSection(epub.GetTitle(), notebookId);

				// Create pages
				var pageIds = pageTitles.Select(pageTitle => note.CreatePage(pageTitle, sectionId)).ToArray();

				// Get links to pages
				var pageLinks = pageIds.Select(pageId => note.GetHyperLinkToObject(pageId)).ToList();

				// Replace links to .html with .one
				ReplaceEpubLinksFromHtmlToOneNote(pagesHtml, pagePaths, pageLinks);

				// Add content to pages
				for (var i = 0; i < pageIds.Length; i++)
				{
					note.AppendPageContentAsHtmlBlock(pageIds[i], pagesHtml[i].DocumentNode.OuterHtml);
					if (pagesLevel.ContainsKey(pagePaths[i]))
					{
						note.SetPageLevel(pageIds[i], pagesLevel[pagePaths[i]]);
					}
				}

				return true;
			}
			catch (Exception e)
			{
				Console.WriteLine(@"Error in ConvertEpubToOneNote for file {0}: {1}", inputFile, e.Message);
				return false;
			}
		}