Skip to content

Commit

Permalink
1736 resolve embedded tagdefs (#1740)
Browse files Browse the repository at this point in the history
* resolve embedded tagdefs with TagMapper
#1736

* clean
  • Loading branch information
stevencohn authored Dec 30, 2024
1 parent fc83324 commit f79ff42
Show file tree
Hide file tree
Showing 11 changed files with 328 additions and 256 deletions.
5 changes: 3 additions & 2 deletions OneMore/Commands/Clean/RemoveTagsCommand.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//************************************************************************************************
// Copyright © 2021 Steven M Cohn. All rights reserved.
// Copyright © 2021 Steven M Cohn. All rights reserved.
//************************************************************************************************

namespace River.OneMoreAddIn.Commands
{
using River.OneMoreAddIn.Models;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
Expand Down Expand Up @@ -31,7 +32,7 @@ public override async Task Execute(params object[] args)

var updated = false;
var reminders = new ReminderSerializer().LoadReminders(page);
var tagdefs = page.GetTagDefMap().Select(m => m.TagDef);
var tagdefs = TagMapper.GetTagDefs(page);

foreach (var tag in tags)
{
Expand Down
201 changes: 108 additions & 93 deletions OneMore/Commands/References/EmbedSubpageCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace River.OneMoreAddIn.Commands
{
using OneMoreAddIn.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -65,64 +66,97 @@ public override async Task Execute(params object[] args)
var sourceId = args.Length > 1 ? args[1] as string : null;
var linkId = args.Length > 2 ? args[2] as string : null;
await UpdateContent(sourceId, linkId);
// prevent replay
IsCancelled = true;
return;
}

await EmbedContent();
}


//========================================================================================
// Update...
private async Task EmbedContent()
{
await using var o = new OneNote();
o.SelectLocation(
Resx.EmbedSubpageCommand_Select,
Resx.EmbedSubpageCommand_SelectIntro,
OneNote.Scope.Pages, Callback);
}

private async Task UpdateContent(string sourceId, string linkId)

private async Task Callback(string sourceId)
{
await using (one = new OneNote(out page, out ns))
if (string.IsNullOrEmpty(sourceId))
{
// find all embedded sections...

var metas = page.Root.Descendants(ns + "Meta")
.Where(e => e.Attribute("name").Value == EmbeddedMetaName);
// cancelled
return;
}

if (!string.IsNullOrEmpty(sourceId))
await using (one = new OneNote(out page, out ns))
{
var source = await GetSource(sourceId, null);
if (source is null || !source.Snippets.Any())
{
// refine filter by source pageId
metas = metas.Where(e => e.Attribute("content").Value == sourceId);
return;
}

if (!metas.Any())
page.EnsureContentContainer();

var container = page.Root.Elements(ns + "Outline")
.Descendants(ns + "T")
.Where(e => e.Attribute("selected")?.Value == "all")
.Ancestors(ns + "OEChildren")
.FirstOrDefault();

if (container is null)
{
ShowError(Resx.EmbedSubpageCommand_NoEmbedded);
ShowError(Resx.Error_BodyContext);
return;
}

// updated each section...
var table = new Table(ns, 1, 1)
{
BordersVisible = true,
};

var updated = false;
foreach (var meta in metas)
// is cursor positioned in an existing table cell?
XElement hostCell = container.Parent;
while (hostCell != null && hostCell.Name.LocalName != "Cell")
{
var tableRoot = meta.ElementsAfterSelf(ns + "Table").FirstOrDefault();
if (tableRoot is not null)
{
sourceId = meta.Attribute("content").Value;
var source = await GetSource(sourceId, linkId);
if (source is null || !source.Snippets.Any())
{
// error reading source
ShowError(Resx.EmbedSubpageCommand_NoEmbedded);
return;
}
hostCell = hostCell.Parent;
}

var table = new Table(tableRoot);
FillCell(table[0][0], source.Snippets, source.Page);
updated = true;
}
if (hostCell is null)
{
// set width to width of source page outline
var width = source.Outline.GetWidth();
table.SetColumnWidth(0, width == 0 ? 500 : width);
}

if (updated)
try
{
await one.Update(page);
FillCell(table[0][0], source.Snippets, source.Page);
}
catch (Exception exc)
{
logger.WriteLine("error in FillCell", exc);
}

try
{
var editor = new PageEditor(page);
editor.AddNextParagraph(new Paragraph(
new Meta(EmbeddedMetaName, source.Page.PageId),
table.Root
));
}
catch (Exception exc)
{
logger.WriteLine("error adding paragraph", exc);
}

await one.Update(page);
}
}

Expand Down Expand Up @@ -164,12 +198,11 @@ private async Task<SourceInfo> GetSource(string sourceId, string linkId)
var outRoot = source.BodyOutlines.FirstOrDefault();
if (outRoot is null)
{
var schema = new PageSchema();

// couldn't find an Outline but page may contain other valid content items
var child = source.Root.Elements().FirstOrDefault(e =>
e.Name.LocalName == "Image" ||
e.Name.LocalName == "InkDrawing" ||
e.Name.LocalName == "InsertedFile" ||
e.Name.LocalName == "MediaFile");
var child = source.Root.Elements()
.FirstOrDefault(e => e.Name.LocalName.In(schema.OeContent));

if (child is not null)
{
Expand Down Expand Up @@ -219,7 +252,9 @@ private void FillCell(TableCell cell, IEnumerable<XElement> snippets, Page sourc
var match = Regex.Match(link, @"page-id=({[^}]+?})");
var linkId = match.Success ? match.Groups[1].Value : string.Empty;

var tagmap = page.MergeTagDefs(source);
var mapper = new TagMapper(page);
mapper.MergeTagDefsFrom(source);

var quickmap = page.MergeQuickStyles(source);
var citationIndex = page.GetQuickStyle(Styles.StandardStyles.Citation).Index;

Expand All @@ -238,84 +273,64 @@ private void FillCell(TableCell cell, IEnumerable<XElement> snippets, Page sourc
foreach (var snippet in snippets)
{
page.ApplyStyleMapping(quickmap, snippet);
page.ApplyTagDefMapping(tagmap, snippet);
mapper.RemapTags(snippet);

cell.Root.Add(snippet);
}
}


//========================================================================================
// Embed...

private async Task EmbedContent()
{
await using var o = new OneNote();
o.SelectLocation(
Resx.EmbedSubpageCommand_Select,
Resx.EmbedSubpageCommand_SelectIntro,
OneNote.Scope.Pages, Callback);
}

// Update...

private async Task Callback(string sourceId)
private async Task UpdateContent(string sourceId, string linkId)
{
if (string.IsNullOrEmpty(sourceId))
{
// cancelled
return;
}

await using (one = new OneNote(out page, out ns))
{
var source = await GetSource(sourceId, null);
if (source is null || !source.Snippets.Any())
{
return;
}
// find all embedded sections...

page.EnsureContentContainer();
var metas = page.Root.Descendants(ns + "Meta")
.Where(e => e.Attribute("name").Value == EmbeddedMetaName);

var container = page.Root.Elements(ns + "Outline")
.Descendants(ns + "T")
.Where(e => e.Attribute("selected")?.Value == "all")
.Ancestors(ns + "OEChildren")
.FirstOrDefault();
if (!string.IsNullOrEmpty(sourceId))
{
// refine filter by source pageId
metas = metas.Where(e => e.Attribute("content").Value == sourceId);
}

if (container is null)
if (!metas.Any())
{
ShowError(Resx.Error_BodyContext);
ShowError(Resx.EmbedSubpageCommand_NoEmbedded);
return;
}

var table = new Table(ns, 1, 1)
{
BordersVisible = true,
};
// updated each section...

// is cursor positioned in an existing table cell?
XElement hostCell = container.Parent;
while (hostCell != null && hostCell.Name.LocalName != "Cell")
var updated = false;
foreach (var meta in metas)
{
hostCell = hostCell.Parent;
var tableRoot = meta.ElementsAfterSelf(ns + "Table").FirstOrDefault();
if (tableRoot is not null)
{
sourceId = meta.Attribute("content").Value;
var source = await GetSource(sourceId, linkId);
if (source is null || !source.Snippets.Any())
{
// error reading source
ShowError(Resx.EmbedSubpageCommand_NoEmbedded);
return;
}

var table = new Table(tableRoot);
FillCell(table[0][0], source.Snippets, source.Page);
updated = true;
}
}

if (hostCell is null)
if (updated)
{
// set width to width of source page outline
var width = source.Outline.GetWidth();
table.SetColumnWidth(0, width == 0 ? 500 : width);
await one.Update(page);
}

FillCell(table[0][0], source.Snippets, source.Page);

var editor = new PageEditor(page);
editor.AddNextParagraph(new Paragraph(
new Meta(EmbeddedMetaName, source.Page.PageId),
table.Root
));

await one.Update(page);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions OneMore/Commands/References/EmbedSubpageProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal class EmbedSubpageProxy : Command
{
public EmbedSubpageProxy()
{
IsCancelled = true;
}


Expand Down
8 changes: 3 additions & 5 deletions OneMore/Commands/Tables/Formulas/Processor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private void GetCellValue(object sender, GetCellValueEventArgs e)
var index = tagx.Attribute("index").Value;
if (index != null)
{
tags ??= DiscoverTags();
tags ??= DiscoverToDoTags();

var tag = tags.Find(t => t.Index == index);
if (tag != null)
Expand All @@ -121,7 +121,7 @@ private void GetCellValue(object sender, GetCellValueEventArgs e)
}


private List<TagDef> DiscoverTags()
private List<TagDef> DiscoverToDoTags()
{
var pageElement = table.Root.Ancestors().FirstOrDefault(e => e.Name.LocalName == "Page");
if (pageElement == null)
Expand All @@ -130,9 +130,7 @@ private List<TagDef> DiscoverTags()
}

var page = new Page(pageElement);
var map = page.GetTagDefMap();
var list = map.Where(m => m.TagDef.IsToDo()).Select(m => m.TagDef).ToList();
return list;
return TagMapper.GetTagDefs(page).Where(d => d.IsToDo()).ToList();
}


Expand Down
Loading

0 comments on commit f79ff42

Please sign in to comment.