Advertisement
FrayxRulez

RtfToTLParser

Sep 25th, 2016
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.20 KB | None | 0 0
  1. public class RtfToTLParser : RtfSarParser
  2. {
  3.     private bool _bold;
  4.     private bool _italic;
  5.     private bool _firstPard;
  6.  
  7.     private string _groupText;
  8.     private string _lastKey;
  9.     private int? _field;
  10.  
  11.     private int _level = -1;
  12.     private int _length;
  13.  
  14.     private Stack<TLMessageEntityBase> _entities;
  15.  
  16.     public IReadOnlyList<TLMessageEntityBase> Entities { get; private set; }
  17.  
  18.     public override void StartRtfDocument()
  19.     {
  20.         _entities = new Stack<TLMessageEntityBase>();
  21.         Entities = null;
  22.  
  23.         _bold = false;
  24.         _italic = false;
  25.         _firstPard = false;
  26.  
  27.         _groupText = null;
  28.         _lastKey = null;
  29.         _field = null;
  30.  
  31.         _level = -1;
  32.         _length = 0;
  33.     }
  34.  
  35.     public override void StartRtfGroup()
  36.     {
  37.         if (_level >= 0 && !_firstPard) return;
  38.  
  39.         if (_field.HasValue)
  40.             _field++;
  41.  
  42.         WriteLine($"StartRtfGroup:\tlevel: {++_level}", _level);
  43.     }
  44.  
  45.     public override void RtfControl(string key, bool hasParameter, int parameter)
  46.     {
  47.         if (_firstPard && key == "'" && hasParameter)
  48.         {
  49.             if (_field.HasValue && _lastKey == "fldinst")
  50.             {
  51.                 _groupText += (char)parameter;
  52.             }
  53.             else if (_field.HasValue && _lastKey.Equals("fldrslt"))
  54.             {
  55.                 _groupText += (char)parameter;
  56.             }
  57.             else if (_bold || _italic)
  58.             {
  59.                 _groupText += (char)parameter;
  60.             }
  61.             else
  62.             {
  63.                 _groupText += (char)parameter;
  64.                 HandleBasicText();
  65.                 _groupText = string.Empty;
  66.             }
  67.         }
  68.     }
  69.  
  70.     public override void RtfKeyword(string key, bool hasParameter, int parameter)
  71.     {
  72.         if (key.Equals("pard"))
  73.         {
  74.             _firstPard = true;
  75.         }
  76.         else if (key.Equals("field"))
  77.         {
  78.             _field = !hasParameter || (hasParameter && parameter == 1) ? new int?(0) : null;
  79.         }
  80.         else if (key.Equals("b"))
  81.         {
  82.             if (!hasParameter || (hasParameter && parameter == 1))
  83.             {
  84.                 _groupText = string.Empty;
  85.                 _bold = true;
  86.             }
  87.             else
  88.             {
  89.                 HandleBoldText();
  90.                 _groupText = string.Empty;
  91.                 _bold = false;
  92.             }
  93.         }
  94.         else if (key.Equals("i"))
  95.         {
  96.             if (!hasParameter || (hasParameter && parameter == 1))
  97.             {
  98.                 _groupText = string.Empty;
  99.                 _italic = true;
  100.             }
  101.             else
  102.             {
  103.                 HandleItalicText();
  104.                 _groupText = string.Empty;
  105.                 _italic = false;
  106.             }
  107.         }
  108.         else if (key.Equals("fldinst") || key.Equals("fldrslt"))
  109.         {
  110.             _lastKey = key;
  111.         }
  112.     }
  113.  
  114.     public override void RtfText(string text)
  115.     {
  116.         if (_firstPard)
  117.         {
  118.             if (_field.HasValue && _lastKey == "fldinst")
  119.             {
  120.                 if (text.IndexOf("HYPERLINK") == 0)
  121.                     _groupText += text.Substring("HYPERLINK ".Length);
  122.                 else
  123.                     _groupText += text;
  124.             }
  125.             else if (_field.HasValue && _lastKey.Equals("fldrslt"))
  126.             {
  127.                 _groupText += text;
  128.             }
  129.             else if (_bold || _italic)
  130.             {
  131.                 _groupText += text;
  132.             }
  133.             else
  134.             {
  135.                 _groupText += text;
  136.                 HandleBasicText();
  137.                 _groupText = string.Empty;
  138.             }
  139.         }
  140.     }
  141.  
  142.     public override void EndRtfGroup()
  143.     {
  144.         if (_firstPard)
  145.         {
  146.             if (_bold) HandleBoldText();
  147.             else if (_italic) HandleItalicText();
  148.             else if (_field.HasValue && _field == 2 && _lastKey.Equals("fldinst")) HandleHyperlinkUrl();
  149.             else if (_field.HasValue && _field == 2 && _lastKey.Equals("fldrslt")) HandleHyperlinkText();
  150.             else if (string.IsNullOrEmpty(_groupText) == false) HandleBasicText();
  151.  
  152.             _bold = false;
  153.             _italic = false;
  154.             _lastKey = string.Empty;
  155.             _groupText = string.Empty;
  156.  
  157.             if (_field.HasValue)
  158.                 _field--;
  159.  
  160.             if (_field.HasValue && _field < 0)
  161.                 _field = null;
  162.  
  163.             WriteLine($"EndRtfGroup:\tlevel: {_level--}", _level + 1);
  164.         }
  165.     }
  166.  
  167.     private void HandleBoldText()
  168.     {
  169.         _entities.Push(new TLMessageEntityBold { Offset = _length, Length = _groupText.Length });
  170.         _length += _groupText.Length;
  171.         WriteLine("BOLD   TEXT:\t\"" + _groupText + "\"", _level + 1);
  172.     }
  173.  
  174.     private void HandleItalicText()
  175.     {
  176.         _entities.Push(new TLMessageEntityItalic { Offset = _length, Length = _groupText.Length });
  177.         _length += _groupText.Length;
  178.         WriteLine("ITALIC TEXT:\t\"" + _groupText + "\"", _level + 1);
  179.     }
  180.  
  181.     private void HandleHyperlinkUrl()
  182.     {
  183.         var userId = int.Parse(_groupText.Trim().Trim('"'));
  184.         //var user = InMemoryCacheService.Current.GetUser(userId) as TLUser;
  185.         //if (user != null)
  186.         //{
  187.         _entities.Push(new TLInputMessageEntityMentionName { UserId = userId });
  188.         //}
  189.         WriteLine("HYPERLINK URL:\t" + _groupText, _level + 1);
  190.     }
  191.  
  192.     private void HandleHyperlinkText()
  193.     {
  194.         var mention = _entities.Peek() as TLInputMessageEntityMentionName;
  195.         if (mention != null)
  196.         {
  197.             mention.Offset = _length;
  198.             mention.Length = _groupText.Length;
  199.         }
  200.  
  201.         _length += _groupText.Length;
  202.         WriteLine("HYPERLINK TEXT:\t\"" + _groupText + "\"", _level + 1);
  203.     }
  204.  
  205.     private void HandleBasicText()
  206.     {
  207.         _length += _groupText.Length;
  208.         WriteLine("BASIC  TEXT:\t\"" + _groupText + "\"", _level + 1);
  209.     }
  210.  
  211.     public override void EndRtfDocument()
  212.     {
  213.         if (Entities == null)
  214.         {
  215.             Entities = new List<TLMessageEntityBase>(_entities);
  216.         }
  217.  
  218.         Debugger.Break();
  219.     }
  220.  
  221.     private void WriteLine(string str, int level)
  222.     {
  223.         for (int i = 0; i < level; i++)
  224.         {
  225.             str = '\t' + str;
  226.         }
  227.  
  228.         Debug.WriteLine(str);
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement