Implement text styles.

- NEU: Überschriften und pathologische Werte werden jetzt besonders ausgezeichnet (fett).
This commit is contained in:
Daniel Kraus
2015-08-15 20:14:24 +02:00
parent 4cb2999297
commit 92a8faa972
8 changed files with 113 additions and 29 deletions

View File

@ -31,8 +31,19 @@ namespace zaaReloaded2.Formatter
/// depending on whether there is text in the buffer or not.
/// </summary>
/// <remarks>
/// <para>
/// Linking several DocumentWriters permits a cascading work flow
/// with several buffers.
/// </para>
/// <para>
/// Markup support: The DocumentWriter supports basic markup to control
/// the text styles in the Word document.
/// </para>
/// <list type="unordered">
/// <item>&lt;b&gt; and &lt;/b&gt; - bold/unbold</item>
/// <item>&lt;style:NAME&gt; - set the paragraph or character style</item>
/// <item>&lt;/style&gt; - remove *character* style</item>
/// </list>
/// </remarks>
class DocumentWriter
{
@ -54,12 +65,6 @@ namespace zaaReloaded2.Formatter
/// </summary>
public bool HasBufferedText { get { return _buffer.Length > 0; } }
/// <summary>
/// Gets or sets the desired paragraph style when flushing into
/// a Document.
/// </summary>
public string ParagraphStyle { get; set; }
/// <summary>
/// Returns text without markup from the buffer.
/// </summary>
@ -137,10 +142,6 @@ namespace zaaReloaded2.Formatter
Selection s = Document.ActiveWindow.Selection;
s.ClearCharacterDirectFormatting();
s.ClearParagraphDirectFormatting();
if (!string.IsNullOrEmpty(ParagraphStyle))
{
s.set_Style(ParagraphStyle);
}
MarkupToDocument(_buffer.ToString());
}
if (Parent != null)
@ -187,22 +188,33 @@ namespace zaaReloaded2.Formatter
/// Parses a string containing markup (e.g., "&lt;b&gt;", "&lt;/b&gt;")
/// and writes formatted text to the current Document.
/// </summary>
/// <param name="text"></param>
void MarkupToDocument(string text)
{
string[] substrings = _markupRegex.Split(text);
Selection sel = Document.ActiveWindow.Selection;
foreach (string substring in substrings)
{
switch (substring)
{
case "<b>":
Document.ActiveWindow.Selection.Font.Bold = 1;
sel.Font.Bold = 1;
break;
case "</b>":
Document.ActiveWindow.Selection.Font.Bold = 0;
sel.Font.Bold = 0;
break;
case "</style>":
sel.ClearCharacterStyle();
break;
default:
Document.ActiveWindow.Selection.TypeText(substring);
Match styleMatch = _styleRegex.Match(substring);
if (styleMatch.Success)
{
sel.set_Style(styleMatch.Groups["style"].Value);
}
else
{
sel.TypeText(substring);
}
break;
}
}
@ -214,7 +226,10 @@ namespace zaaReloaded2.Formatter
StringBuilder _buffer;
// Put pattern in parentheses so they will not be discarded by Regex.Split
static readonly Regex _markupRegex = new Regex(@"(</?b>)");
// The splitting pattern must not contain subgroups!
static readonly Regex _markupRegex = new Regex(@"(<[^>]+>)");
static readonly Regex _styleRegex = new Regex(@"<style:(?<style>[^>]+)>");
#endregion
}