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

View File

@ -52,56 +52,29 @@ namespace zaaReloaded2.Importer
"Automatic laboratory detection requires a document.");
}
// TODO: Try to make this algorithm more elegant.
Paragraph start = null;
Paragraph end = null;
int i = 1;
int startParagraph = 1;
if (document.Bookmarks.Exists("Labor"))
{
Logger.Info("Detect: Found lab bookmark");
i = GetParagraphIndex(
startParagraph = GetParagraphIndex(
document,
document.Bookmarks["Labor"].Range.Paragraphs[1]);
}
while (i <= document.Paragraphs.Count)
return DetectRange(document, startParagraph, document.Paragraphs.Count);
}
public bool Detect(Selection selection)
{
if (selection == null)
{
// 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++;
throw new ArgumentNullException(
"Automatic laboratory detection requires a selection.");
}
if (start != null)
{
Logger.Info("Detect: Determining lab block");
end = start;
while (i <= document.Paragraphs.Count - 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++;
}
document.Range(start.Range.Start, end.Range.End).Select();
return true;
}
Logger.Warn("Detect: Did not find lab block!");
return false;
return DetectRange(selection.Document,
GetParagraphIndex(selection.Document, selection.Paragraphs.First),
GetParagraphIndex(selection.Document, selection.Paragraphs.Last) + 1);
}
public IImporter CreateImporter()
@ -121,6 +94,79 @@ namespace zaaReloaded2.Importer
#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>
/// Returns true if a paragraph is a time stamp line.
/// </summary>