Skip to content

Commit

Permalink
860 file into notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencohn committed Feb 11, 2023
1 parent 31be729 commit a2dde8e
Show file tree
Hide file tree
Showing 17 changed files with 276 additions and 42 deletions.
1 change: 1 addition & 0 deletions OneMore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
register-onemore.reg = register-onemore.reg
setdevreg.ps1 = setdevreg.ps1
setversion.ps1 = setversion.ps1
stop.ps1 = stop.ps1
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Themes", "Themes", "{B33891C1-D812-4C1D-B6DC-2175A68C4ADC}"
Expand Down
145 changes: 139 additions & 6 deletions OneMore/Commands/File/FileQuickNotesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ namespace River.OneMoreAddIn.Commands
using River.OneMoreAddIn.Models;
using River.OneMoreAddIn.Settings;
using System;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;

Expand Down Expand Up @@ -53,11 +55,117 @@ public override async Task Execute(params object[] args)

private async Task FileIntoNotebook(string notebookID, int grouping)
{
one = new OneNote();

var unfiled = await LoadQuickNotes();
if (unfiled == null)
{
return;
}

var notebook = await one.GetNotebook(notebookID, OneNote.Scope.Sections);
var ns = notebook.GetNamespaceOfPrefix(OneNote.Prefix);
var count = 0;
string sectionID = null; // keep track of last section used

unfiled.Descendants(ns + "Page").ForEach(async e =>
{
e.GetAttributeValue("name", out var name, string.Empty);
e.GetAttributeValue("dateTime", out var dateTime);
e.GetAttributeValue("lastModifiedTime", out var lastModifiedTime);

var page = one.GetPage(e.Attribute("ID").Value, OneNote.PageDetail.All);
var section = await FindFilingSection(notebook, grouping, page, DateTime.Parse(dateTime));
sectionID = section.Attribute("ID").Value;

AddHeader(page, name, dateTime);

logger.WriteLine($"moving quick note [{name}] to section [{section.Attribute("name").Value}]");
var pageID = await CopyPage(page, sectionID);
Timewarp(sectionID, pageID, dateTime, lastModifiedTime);
count++;
});

if (count > 0 && sectionID != null)
{
EmptyQuickNotes(unfiled);
await one.NavigateTo(sectionID);
}
}


private async Task<XElement> FindFilingSection(
XElement notebook, int grouping, Page page, DateTime dateTime)
{
string name = null;
switch (grouping)
{
// work week (2023-02-13 Wk 7)
case 0:
var week = new GregorianCalendar(GregorianCalendarTypes.Localized)
.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday);

name = $"{dateTime:yyyy-MM-dd} W{week}";
break;

// 1:month (2023-02)
case 1:
name = dateTime.ToString("yyyy-MM");
break;

// quarter (2023 Q1)
case 2:
name = $"{dateTime:yyyy} Q{(dateTime.Month + 2) / 3}";
break;

// year (2023)
case 3:
name = dateTime.ToString("yyyy");
break;

// #keyword
case 4:
var text = page.Root.Descendants(page.Namespace + "OE").First()?.TextValue();
if (!string.IsNullOrWhiteSpace(text))
{
var match = Regex.Match(text, @"#[^\s]+");
if (match.Success)
{
name = match.Value.Substring(1).Replace("/", "|");
break;
}
}
name = dateTime.ToString("yyyy-MM");
break;
}

var ns = notebook.GetNamespaceOfPrefix(OneNote.Prefix);
var section = notebook.Elements(ns + "Section")
.FirstOrDefault(e => e.Attribute("name").Value == name);

if (section == null)
{
section = new XElement(ns + "Section", new XAttribute("name", name));

var bin = notebook.Elements(ns + "SectionGroup")
.FirstOrDefault(e => e.Attribute("isRecycleBin") != null);

if (bin == null)
notebook.Add(section);
else
bin.AddBeforeSelf(section);

one.UpdateHierarchy(notebook);
var book = await one.GetNotebook(notebook.Attribute("ID").Value);
section = book.Elements(ns + "Section")
.First(e => e.Attribute("name").Value == name);

notebook.Elements(ns + "Section")
.First(e => e.Attribute("name").Value == name)
.ReplaceWith(section);
}

return section;
}


Expand All @@ -72,7 +180,7 @@ private async Task FileIntoSection(string sectionID)
}

var section = one.GetSection(sectionID);
var ns = one.GetNamespace(section);
var ns = section.GetNamespaceOfPrefix(OneNote.Prefix);
var count = 0;

unfiled.Descendants(ns + "Page").ForEach(async e =>
Expand All @@ -95,14 +203,15 @@ private async Task FileIntoSection(string sectionID)
if (count > 0)
{
EmptyQuickNotes(unfiled);
await one.NavigateTo(sectionID);
}
}


private async Task<XElement> LoadQuickNotes()
{
var books = await one.GetNotebooks();
var ns = one.GetNamespace(books);
var ns = books.GetNamespaceOfPrefix(OneNote.Prefix);

// Quick Notes are stored in the singular one:UnfiledNotes notebook node
var book = books.Elements(ns + "UnfiledNotes").FirstOrDefault();
Expand All @@ -124,14 +233,35 @@ private async Task<XElement> LoadQuickNotes()

private void AddHeader(Page page, string name, string dateTime)
{
if (!titled)
if (titled)
{
// extract text from body, possibly removing #keyword first line
if (Regex.IsMatch(name, @"^#[^\s]+$"))
{
// skip first line and grab name from remaining content
var next = page.Root.Descendants(page.Namespace + "OE")
.Skip(1)
.FirstOrDefault(e => e.TextValue().Length != 0);

if (next != null)
{
name = next.Value;
if (name.Length > 20)
{
name = name.Substring(20) + "...";
}
}
}
}
else
{
// do not extract text from body
name = string.Empty;
}

if (stamped && DateTime.TryParse(dateTime, out var dttm))
{
name = $"{dttm.ToString("yyyy-MM-dd")} {name}";
name = $"{dttm:yyyy-MM-dd} {name}";
}

page.SetTitle(name);
Expand Down Expand Up @@ -173,7 +303,7 @@ private void Timewarp(
string sectionID, string pageID, string dateTime, string lastModifiedTime)
{
var section = one.GetSection(sectionID);
var ns = one.GetNamespace(section);
var ns = section.GetNamespaceOfPrefix(OneNote.Prefix);

var page = section.Descendants(ns + "Page")
.FirstOrDefault(e => e.Attribute("ID")?.Value == pageID);
Expand All @@ -190,7 +320,10 @@ private void Timewarp(

private void EmptyQuickNotes(XElement unfiled)
{
var ns = one.GetNamespace(unfiled);
var ns = unfiled.GetNamespaceOfPrefix(OneNote.Prefix);

// need to delete individual pages in Quick Notes section because that
// section is considered read-only by OneNote so would throw and exception

unfiled.Descendants(ns + "Page").ForEach(e =>
{
Expand Down
5 changes: 4 additions & 1 deletion OneMore/Commands/Settings/QuickNotesSheet.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion OneMore/Commands/Settings/QuickNotesSheet.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//************************************************************************************************
// Copyright © 2021 teven M. Cohn. All Rights Reserved.
// Copyright © 2023 teven M. Cohn. All Rights Reserved.
//************************************************************************************************

namespace River.OneMoreAddIn.Settings
Expand Down
17 changes: 2 additions & 15 deletions OneMore/Commands/Settings/SheetBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

namespace River.OneMoreAddIn.Settings
{
using System.Linq;
using System.Threading;
using River.OneMoreAddIn.UI;
using System.Windows.Forms;
using Resx = River.OneMoreAddIn.Properties.Resources;


/// <summary>
Expand Down Expand Up @@ -61,18 +59,7 @@ protected SheetBase(SettingsProvider provider)
/// <param name="keys">An collection of strings specifying the control names</param>
protected void Localize(string[] keys)
{
foreach (var key in keys)
{
var control = Controls.Find(key, true).FirstOrDefault();
if (control != null)
{
var text = Resx.ResourceManager.GetString($"{Name}_{key}.Text", AddIn.Culture);
if (text != null)
{
control.Text = text;
}
}
}
TranslationHelper.Localize(this, keys);
}


Expand Down
3 changes: 2 additions & 1 deletion OneMore/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion OneMore/Properties/Resources.ar-SA.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4766,9 +4766,10 @@ ISO-code then comma then language name</comment>
<comment>error message</comment>
</data>
<data name="QuickNotesSheet_groupingBox.Text" xml:space="preserve">
<value>أسبوع العمل (2023-02-13 أسبوع 7)
<value>أسبوع العمل (2023-02-13 W7)
الشهر (2023-02)
ربع (2023 Q1)
عام (2023)
# الكلمة الرئيسية في السطر الأول من الملاحظة</value>
<comment>combobox</comment>
</data>
Expand Down Expand Up @@ -4834,4 +4835,12 @@ ISO-code then comma then language name</comment>
<data name="word_Section" xml:space="preserve">
<value>قسم</value>
</data>
<data name="ribFileQuickNotesButton_Label" xml:space="preserve">
<value>ملاحظات سريعة للملف</value>
<comment>ribbon file</comment>
</data>
<data name="ribFileQuickNotesButton_Screentip" xml:space="preserve">
<value>تنظيم الملاحظات السريعة في دفتر ملاحظات أو قسم</value>
<comment>ribbon file</comment>
</data>
</root>
11 changes: 10 additions & 1 deletion OneMore/Properties/Resources.de-DE.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4753,9 +4753,10 @@ In diesem Abschnitt</value>
<comment>error message</comment>
</data>
<data name="QuickNotesSheet_groupingBox.Text" xml:space="preserve">
<value>Arbeitswoche (2023-02-13 Woche 7)
<value>Arbeitswoche (2023-02-13 W7)
Monat (2023-02)
Quartal (2023 Q1)
Jahr (2023)
#Keyword in der ersten Notizzeile</value>
<comment>combobox</comment>
</data>
Expand Down Expand Up @@ -4821,4 +4822,12 @@ Quartal (2023 Q1)
<data name="word_Section" xml:space="preserve">
<value>Abschnitt</value>
</data>
<data name="ribFileQuickNotesButton_Label" xml:space="preserve">
<value>Schnelle Notizen ablegen</value>
<comment>ribbon file</comment>
</data>
<data name="ribFileQuickNotesButton_Screentip" xml:space="preserve">
<value>Organisieren Sie Quick Notes in einem Notizbuch oder Abschnitt</value>
<comment>ribbon file</comment>
</data>
</root>
11 changes: 10 additions & 1 deletion OneMore/Properties/Resources.es-ES.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4766,9 +4766,10 @@ En esta sección</value>
<comment>error message</comment>
</data>
<data name="QuickNotesSheet_groupingBox.Text" xml:space="preserve">
<value>Semana laboral (2023-02-13 Semana 7)
<value>Semana laboral (2023-02-13 W7)
Mes (2023-02)
Trimestre (2023 Q1)
Año (2023)
#Palabra clave en la primera línea de la nota</value>
<comment>combobox</comment>
</data>
Expand Down Expand Up @@ -4834,4 +4835,12 @@ Trimestre (2023 Q1)
<data name="word_Section" xml:space="preserve">
<value>Sección</value>
</data>
<data name="ribFileQuickNotesButton_Label" xml:space="preserve">
<value>Archivo de notas rápidas</value>
<comment>ribbon file</comment>
</data>
<data name="ribFileQuickNotesButton_Screentip" xml:space="preserve">
<value>Organice Quick Notes en un cuaderno o sección</value>
<comment>ribbon file</comment>
</data>
</root>
11 changes: 10 additions & 1 deletion OneMore/Properties/Resources.fr-FR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4760,9 +4760,10 @@ Dans cette section</value>
<comment>error message</comment>
</data>
<data name="QuickNotesSheet_groupingBox.Text" xml:space="preserve">
<value>Semaine de travail (2023-02-13 Semaine 7)
<value>Semaine de travail (2023-02-13 W7)
Mois (2023-02)
Trimestre (2023 Q1)
Année (2023)
#Mot clé dans la première ligne de la note</value>
<comment>combobox</comment>
</data>
Expand Down Expand Up @@ -4828,4 +4829,12 @@ Trimestre (2023 Q1)
<data name="word_Section" xml:space="preserve">
<value>Section</value>
</data>
<data name="ribFileQuickNotesButton_Label" xml:space="preserve">
<value>Déposer des notes rapides</value>
<comment>ribbon file</comment>
</data>
<data name="ribFileQuickNotesButton_Screentip" xml:space="preserve">
<value>Organiser les notes rapides dans un bloc-notes ou une section</value>
<comment>ribbon file</comment>
</data>
</root>
Loading

0 comments on commit a2dde8e

Please sign in to comment.