-
-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5921bd0
commit 84973c8
Showing
7 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
//************************************************************************************************ | ||
// Copyright © 2022 Steven M Cohn. All rights reserved. | ||
//************************************************************************************************ | ||
|
||
namespace River.OneMoreAddIn.Models | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Xml.Linq; | ||
|
||
internal class PageReader | ||
{ | ||
#region Types | ||
|
||
private sealed class DeepNode | ||
{ | ||
public XElement Element; | ||
public int Depth; | ||
} | ||
|
||
#endregion Types | ||
|
||
private Page page; | ||
private XNamespace ns; | ||
|
||
|
||
/// <summary> | ||
/// Initialize a new editor for the given page | ||
/// </summary> | ||
/// <param name="page"></param> | ||
public PageReader(Page page) | ||
{ | ||
this.page = page; | ||
ns = page.Namespace; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Removes the selected content on the current page, wraps it inside a new OEChildren | ||
/// container, and returns the container. | ||
/// </summary> | ||
/// <returns>A new OEChildren element containing the extracted content</returns> | ||
/// <remarks> | ||
/// Normalizes the content to include selected items in a partially selected bullet or | ||
/// number list and excludes the unselected items. Similarlly, this applies to indented | ||
/// paragraphs where not all sibilings or even child thereof are selected. | ||
/// </remarks> | ||
public XElement ExtractSelectedContent() | ||
{ | ||
var content = new XElement(ns + "OEChildren"); | ||
|
||
var selections = page.Root.Elements(ns+ "Outline") | ||
.Descendants(ns+ "T") | ||
.Where(e => e.Attributes().Any(a => a.Name == "selected" && a.Value == "all")) | ||
.Select(e => new DeepNode | ||
{ | ||
Element = e, | ||
Depth = e.Ancestors().Count() | ||
}) | ||
.ToList(); | ||
|
||
if (!selections.Any()) | ||
{ | ||
// no T runs selected but check for selected images | ||
selections = page.Root.Elements(ns + "Outline") | ||
.Descendants(ns + "Image") | ||
.Where(e => e.Attributes().Any(a => a.Name == "selected" && a.Value == "all")) | ||
.Select(e => new DeepNode | ||
{ | ||
Element = e, | ||
Depth = e.Ancestors().Count() | ||
}) | ||
.ToList(); | ||
} | ||
|
||
if (!selections.Any()) | ||
{ | ||
return content; | ||
} | ||
|
||
var minDepth = selections.Min(e => e.Depth); | ||
|
||
return content; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.