Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RtfToTLParser : RtfSarParser
- {
- private bool _bold;
- private bool _italic;
- private bool _firstPard;
- private string _groupText;
- private string _lastKey;
- private int? _field;
- private int _level = -1;
- private int _length;
- private Stack<TLMessageEntityBase> _entities;
- public IReadOnlyList<TLMessageEntityBase> Entities { get; private set; }
- public override void StartRtfDocument()
- {
- _entities = new Stack<TLMessageEntityBase>();
- Entities = null;
- _bold = false;
- _italic = false;
- _firstPard = false;
- _groupText = null;
- _lastKey = null;
- _field = null;
- _level = -1;
- _length = 0;
- }
- public override void StartRtfGroup()
- {
- if (_level >= 0 && !_firstPard) return;
- if (_field.HasValue)
- _field++;
- WriteLine($"StartRtfGroup:\tlevel: {++_level}", _level);
- }
- public override void RtfControl(string key, bool hasParameter, int parameter)
- {
- if (_firstPard && key == "'" && hasParameter)
- {
- if (_field.HasValue && _lastKey == "fldinst")
- {
- _groupText += (char)parameter;
- }
- else if (_field.HasValue && _lastKey.Equals("fldrslt"))
- {
- _groupText += (char)parameter;
- }
- else if (_bold || _italic)
- {
- _groupText += (char)parameter;
- }
- else
- {
- _groupText += (char)parameter;
- HandleBasicText();
- _groupText = string.Empty;
- }
- }
- }
- public override void RtfKeyword(string key, bool hasParameter, int parameter)
- {
- if (key.Equals("pard"))
- {
- _firstPard = true;
- }
- else if (key.Equals("field"))
- {
- _field = !hasParameter || (hasParameter && parameter == 1) ? new int?(0) : null;
- }
- else if (key.Equals("b"))
- {
- if (!hasParameter || (hasParameter && parameter == 1))
- {
- _groupText = string.Empty;
- _bold = true;
- }
- else
- {
- HandleBoldText();
- _groupText = string.Empty;
- _bold = false;
- }
- }
- else if (key.Equals("i"))
- {
- if (!hasParameter || (hasParameter && parameter == 1))
- {
- _groupText = string.Empty;
- _italic = true;
- }
- else
- {
- HandleItalicText();
- _groupText = string.Empty;
- _italic = false;
- }
- }
- else if (key.Equals("fldinst") || key.Equals("fldrslt"))
- {
- _lastKey = key;
- }
- }
- public override void RtfText(string text)
- {
- if (_firstPard)
- {
- if (_field.HasValue && _lastKey == "fldinst")
- {
- if (text.IndexOf("HYPERLINK") == 0)
- _groupText += text.Substring("HYPERLINK ".Length);
- else
- _groupText += text;
- }
- else if (_field.HasValue && _lastKey.Equals("fldrslt"))
- {
- _groupText += text;
- }
- else if (_bold || _italic)
- {
- _groupText += text;
- }
- else
- {
- _groupText += text;
- HandleBasicText();
- _groupText = string.Empty;
- }
- }
- }
- public override void EndRtfGroup()
- {
- if (_firstPard)
- {
- if (_bold) HandleBoldText();
- else if (_italic) HandleItalicText();
- else if (_field.HasValue && _field == 2 && _lastKey.Equals("fldinst")) HandleHyperlinkUrl();
- else if (_field.HasValue && _field == 2 && _lastKey.Equals("fldrslt")) HandleHyperlinkText();
- else if (string.IsNullOrEmpty(_groupText) == false) HandleBasicText();
- _bold = false;
- _italic = false;
- _lastKey = string.Empty;
- _groupText = string.Empty;
- if (_field.HasValue)
- _field--;
- if (_field.HasValue && _field < 0)
- _field = null;
- WriteLine($"EndRtfGroup:\tlevel: {_level--}", _level + 1);
- }
- }
- private void HandleBoldText()
- {
- _entities.Push(new TLMessageEntityBold { Offset = _length, Length = _groupText.Length });
- _length += _groupText.Length;
- WriteLine("BOLD TEXT:\t\"" + _groupText + "\"", _level + 1);
- }
- private void HandleItalicText()
- {
- _entities.Push(new TLMessageEntityItalic { Offset = _length, Length = _groupText.Length });
- _length += _groupText.Length;
- WriteLine("ITALIC TEXT:\t\"" + _groupText + "\"", _level + 1);
- }
- private void HandleHyperlinkUrl()
- {
- var userId = int.Parse(_groupText.Trim().Trim('"'));
- //var user = InMemoryCacheService.Current.GetUser(userId) as TLUser;
- //if (user != null)
- //{
- _entities.Push(new TLInputMessageEntityMentionName { UserId = userId });
- //}
- WriteLine("HYPERLINK URL:\t" + _groupText, _level + 1);
- }
- private void HandleHyperlinkText()
- {
- var mention = _entities.Peek() as TLInputMessageEntityMentionName;
- if (mention != null)
- {
- mention.Offset = _length;
- mention.Length = _groupText.Length;
- }
- _length += _groupText.Length;
- WriteLine("HYPERLINK TEXT:\t\"" + _groupText + "\"", _level + 1);
- }
- private void HandleBasicText()
- {
- _length += _groupText.Length;
- WriteLine("BASIC TEXT:\t\"" + _groupText + "\"", _level + 1);
- }
- public override void EndRtfDocument()
- {
- if (Entities == null)
- {
- Entities = new List<TLMessageEntityBase>(_entities);
- }
- Debugger.Break();
- }
- private void WriteLine(string str, int level)
- {
- for (int i = 0; i < level; i++)
- {
- str = '\t' + str;
- }
- Debug.WriteLine(str);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement