Fix mode detection for selections.

This commit is contained in:
daniel 2017-09-18 21:52:15 +02:00
parent 443a6b48e7
commit ec8e277b85
2 changed files with 93 additions and 41 deletions

View File

@ -204,12 +204,12 @@ namespace zaaReloaded2
Word.Selection selection = activeWindow.Selection; Word.Selection selection = activeWindow.Selection;
Word.Paragraphs paragraphs = selection.Paragraphs; Word.Paragraphs paragraphs = selection.Paragraphs;
Importer.IImporter importer = null; Importer.IImporter importer = null;
Importer.AutoDetector autoDetector = new Importer.AutoDetector();
if (!(paragraphs.Count > 1 if (!(paragraphs.Count > 1
|| (selection.Text.Length > 1 && selection.Text.EndsWith("\r")))) || (selection.Text.Length > 1 && selection.Text.EndsWith("\r"))))
{ {
Logger.Info("DoFormat: Attempting to auto-detect"); Logger.Info("DoFormat: Attempting to auto-detect");
Word.Document doc = activeWindow.Document; Word.Document doc = activeWindow.Document;
Importer.AutoDetector autoDetector = new Importer.AutoDetector();
if (!autoDetector.Detect(doc)) if (!autoDetector.Detect(doc))
{ {
Logger.Info("DoFormat: Automatic detection failed"); Logger.Info("DoFormat: Automatic detection failed");
@ -224,6 +224,12 @@ namespace zaaReloaded2
// Bovender.ComHelpers.ReleaseComObject(doc); // Bovender.ComHelpers.ReleaseComObject(doc);
importer = autoDetector.CreateImporter(); importer = autoDetector.CreateImporter();
} }
else if (paragraphs.Count >= 1)
{
Logger.Info("DoFormat: Detecting mode of selection");
autoDetector.Detect(selection);
}
importer = autoDetector.CreateImporter();
Logger.Info("DoFormat: Importing"); Logger.Info("DoFormat: Importing");
importer.Import(selection.Text); importer.Import(selection.Text);

View File

@ -52,56 +52,29 @@ namespace zaaReloaded2.Importer
"Automatic laboratory detection requires a document."); "Automatic laboratory detection requires a document.");
} }
// TODO: Try to make this algorithm more elegant. int startParagraph = 1;
Paragraph start = null;
Paragraph end = null;
int i = 1;
if (document.Bookmarks.Exists("Labor")) if (document.Bookmarks.Exists("Labor"))
{ {
Logger.Info("Detect: Found lab bookmark"); Logger.Info("Detect: Found lab bookmark");
i = GetParagraphIndex( startParagraph = GetParagraphIndex(
document, document,
document.Bookmarks["Labor"].Range.Paragraphs[1]); document.Bookmarks["Labor"].Range.Paragraphs[1]);
} }
while (i <= document.Paragraphs.Count) return DetectRange(document, startParagraph, document.Paragraphs.Count);
{
// Expect the first paragraph of a Lauris block to be
// a time stamp. This prevents erroneous detection of
// lines such as "Tel. (09 31) 201-39432; -39126", which
// happen to structurally resemble a paragraph with
// laboratory items.
if (IsTimeStampParagraph(document.Paragraphs[i]))
{
start = document.Paragraphs[i];
Logger.Info("Detect: Found time stamp line in paragraph #{0}", i);
break;
}
i++;
} }
if (start != null) public bool Detect(Selection selection)
{ {
Logger.Info("Detect: Determining lab block"); if (selection == null)
end = start;
while (i <= document.Paragraphs.Count - 1)
{ {
Paragraph p = document.Paragraphs[i+1]; throw new ArgumentNullException(
if (!IsLabParagraph(p) && !IsEmptyParagraph(p)) "Automatic laboratory detection requires a selection.");
{
Logger.Info("Detect: Last lab paragraph is #{0}", i);
end = document.Paragraphs[i];
break;
}
i++;
} }
document.Range(start.Range.Start, end.Range.End).Select(); return DetectRange(selection.Document,
return true; GetParagraphIndex(selection.Document, selection.Paragraphs.First),
} GetParagraphIndex(selection.Document, selection.Paragraphs.Last) + 1);
Logger.Warn("Detect: Did not find lab block!");
return false;
} }
public IImporter CreateImporter() public IImporter CreateImporter()
@ -121,6 +94,79 @@ namespace zaaReloaded2.Importer
#region Private methods #region Private methods
private bool DetectRange(Document document, int startParagraph, int endParagraph)
{
if (document == null)
{
throw new ArgumentNullException(
"Automatic laboratory detection requires a document.");
}
if (startParagraph < 1 || startParagraph > document.Paragraphs.Count)
{
Logger.Fatal("Start paragraph index must be between {0} and {1}, was {2}!",
1, document.Paragraphs.Count, startParagraph);
throw new ArgumentOutOfRangeException("startParagraph");
}
if (endParagraph < startParagraph || endParagraph > document.Paragraphs.Count)
{
Logger.Fatal("End paragraph index must be between {0} and {1}, was {2}!",
startParagraph, document.Paragraphs.Count, endParagraph);
throw new ArgumentOutOfRangeException("endParagraph");
}
Logger.Info("DetectRange: Start paragraph is #{0}, end is #{1}, document has #{2} paragraphs",
startParagraph, endParagraph, document.Paragraphs.Count);
// TODO: Try to make this algorithm more elegant.
Paragraph start = null;
Paragraph end = null;
int i = startParagraph;
while (i <= endParagraph)
{
// Expect the first paragraph of a Lauris block to be
// a time stamp. This prevents erroneous detection of
// lines such as "Tel. (09 31) 201-39432; -39126", which
// happen to structurally resemble a paragraph with
// laboratory items.
if (IsTimeStampParagraph(document.Paragraphs[i]))
{
start = document.Paragraphs[i];
Logger.Info("DetectRange: Found time stamp line in paragraph #{0}", i);
break;
}
i++;
}
if (start != null)
{
Logger.Info("DetectRange: Determining lab block");
while (i <= endParagraph - 1)
{
Paragraph p = document.Paragraphs[i + 1];
if (!IsLabParagraph(p) && !IsEmptyParagraph(p))
{
Logger.Info("Detect: Last lab paragraph is #{0}", i);
end = document.Paragraphs[i];
break;
}
i++;
}
if (end == null)
{
end = document.Paragraphs[endParagraph];
}
document.Range(start.Range.Start, end.Range.End).Select();
return true;
}
Logger.Warn("DetectRange: Did not find lab block!");
return false;
}
/// <summary> /// <summary>
/// Returns true if a paragraph is a time stamp line. /// Returns true if a paragraph is a time stamp line.
/// </summary> /// </summary>