Skip to content

Instantly share code, notes, and snippets.

@Xitsa
Created June 15, 2021 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xitsa/6d3cb2001dfcc47131673aee3ffec986 to your computer and use it in GitHub Desktop.
Save Xitsa/6d3cb2001dfcc47131673aee3ffec986 to your computer and use it in GitHub Desktop.
BBLogger
using System;
using System.Drawing;
using CodeKicker.BBCode;
using CodeKicker.BBCode.SyntaxTree;
namespace Xitsa
{
public static class Logger
{
static BBCodeParser GetParser()
{
return new BBCodeParser(ErrorMode.ErrorFree, null, new[]
{
new BBTag("b", "<b>", "</b>"),
new BBTag("i", "<i>", "</i>"),
new BBTag("size", "", "", new BBAttribute("size", ""), new BBAttribute("size", "size")),
new BBTag("colour", "", "", new BBAttribute("name", ""), new BBAttribute("name", "name")),
new BBTag("font", "", "", new BBAttribute("name", ""), new BBAttribute("name", "name")),
});
}
public static void VisitTextNodes(SyntaxTreeNode node, Action<string> visitText, Func<TagNode, bool, bool> tagFilter)
{
if (node == null) throw new ArgumentNullException("node");
if (visitText == null) throw new ArgumentNullException("visitText");
switch (node)
{
case TextNode textNode:
visitText(textNode.Text);
break;
case TagNode tagNode:
if ((tagFilter != null && !tagFilter(tagNode, true))) return; //skip filtered tags
foreach (var subNode in tagNode.SubNodes)
VisitTextNodes(subNode, visitText, tagFilter);
tagFilter(tagNode, false);
break;
default:
foreach (var subNode in node.SubNodes)
VisitTextNodes(subNode, visitText, tagFilter);
break;
}
}
/// <summary>
/// Печатает лог с поддержкой bbcode
/// </summary>
public static void BBLog(System.Windows.Forms.RichTextBox RTB, string Message)
{
bool isFontBold = false;
bool isFontItalic = false;
float? FontSize = null;
Color? FontColour = null;
string FontName = null;
var Parser = GetParser();
var Result = Parser.ParseSyntaxTree(Message);
VisitTextNodes(Result,
(string Text) =>
{
int Start = RTB.TextLength;
RTB.AppendText(Text);
RTB.Select(Start, RTB.TextLength - Start);
// Настройки шрифта
FontStyle Style = FontStyle.Regular;
if (isFontBold)
Style = Style | FontStyle.Bold;
if (isFontItalic)
Style = Style | FontStyle.Italic;
float NewFontSize = RTB.Font.Size;
if (FontSize.HasValue)
NewFontSize = FontSize.Value;
string NewFontName = RTB.Font.Name;
if (FontName != null)
NewFontName = FontName;
RTB.SelectionFont = new Font(NewFontName, NewFontSize, Style);
// Цвет выделения
if (FontColour.HasValue)
RTB.SelectionColor = FontColour.Value;
else
RTB.SelectionColor = RTB.ForeColor;
RTB.DeselectAll();
},
(TagNode Tag, bool isFirst) =>
{
if (Tag.Tag.Name == "b")
isFontBold = isFirst;
else if (Tag.Tag.Name == "i")
isFontItalic = isFirst;
else if (Tag.Tag.Name == "size")
{
if (isFirst)
{
var attr = Tag.Tag.FindAttribute("");
if (Tag.AttributeValues.ContainsKey(attr))
{
var val = Tag.AttributeValues[attr];
float NewFontSize;
if (float.TryParse(val, out NewFontSize))
{
FontSize = NewFontSize;
}
}
}
else
{
FontSize = null;
}
}
else if (Tag.Tag.Name == "colour")
{
if (isFirst)
{
var attr = Tag.Tag.FindAttribute("");
if (Tag.AttributeValues.ContainsKey(attr))
{
var val = Tag.AttributeValues[attr];
FontColour = Color.FromName(val);
}
}
else
{
FontColour = null;
}
}
else if (Tag.Tag.Name == "font")
{
if (isFirst)
{
var attr = Tag.Tag.FindAttribute("");
if (Tag.AttributeValues.ContainsKey(attr))
{
var val = Tag.AttributeValues[attr];
val = val.Replace("_", " ");
FontName = val;
}
}
else
{
FontName = null;
}
}
return true;
}
);
}
}
}
@Xitsa
Copy link
Author

Xitsa commented Jun 15, 2021

For CodeKicker.BBCode 5.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment