Advertisement
dr_forse

Untitled

Dec 9th, 2019
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.85 KB | None | 0 0
  1. async def cut_message(message_text, limitation):
  2.     try:
  3.         x = 0
  4.         y = 0
  5.         italic_op = 0
  6.         bold_op = 0
  7.         italic_cl = 0
  8.         bold_cl = 0
  9.         cuted_text = ''
  10.         added_tag = None
  11.         it = list(message_text)
  12.         for i in it:
  13.             if y <= limitation:
  14.                 if limitation - y <= 2 and italic_op == italic_cl and bold_op == bold_cl:
  15.                     if i == 'i':
  16.                         cuted_text += 'i></i>'
  17.                     elif i == 'b':
  18.                         cuted_text += 'b></b>'
  19.                     elif i == '>':
  20.                         cuted_text += f'{i}</{x[-1]}>'
  21.                     x += 1
  22.                     break
  23.                 else:
  24.                     cuted_text += i
  25.                 if it[x-2] + it[x-1] + i not in ['<i>', '<b>'] and it[x-3] + it[x-2] + it[x-1] + i not in ['</i>', '</b>']:
  26.                     y += 1
  27.                 elif it[x-2] + it[x-1] + i == '<i>':
  28.                     italic_op += 1
  29.                 elif it[x-2] + it[x-1] + i == '<b>':
  30.                     bold_op += 1
  31.                 elif it[x-3] + it[x-2] + it[x-1] + i == '</i>':
  32.                     italic_cl += 1
  33.                 elif it[x-3] + it[x-2] + it[x-1] + i == '</b>':
  34.                     bold_cl += 1
  35.                 x += 1
  36.             elif italic_op == italic_cl and bold_op == bold_cl:
  37.                     break
  38.             else:
  39.                 if italic_op != italic_cl:
  40.                     if i == '<':
  41.                         cuted_text += '</i>'
  42.                         x += 4
  43.                     elif i == '/':
  44.                         cuted_text += '/i>'
  45.                         x += 3
  46.                     elif i == 'i':
  47.                         cuted_text += 'i>'
  48.                         x += 2
  49.                     elif i == '>':
  50.                         cuted_text += '>'
  51.                         x += 1
  52.                     else:
  53.                         cuted_text += '</i>'
  54.                         added_tag = '</i>'
  55.                 if bold_op != bold_cl:
  56.                     if i == '<':
  57.                         cuted_text += '</b>'
  58.                         x += 4
  59.                     elif i == '/':
  60.                         cuted_text += '/b>'
  61.                         x += 3
  62.                     elif i == 'b':
  63.                         cuted_text += 'b>'
  64.                         x += 2
  65.                     elif i == '>':
  66.                         cuted_text += '>'
  67.                         x += 1
  68.                     else:
  69.                         cuted_text += '</b>'
  70.                         added_tag = '</b>'
  71.                 break
  72.         return {'cuted': cuted_text,
  73.                 'len_cuted': x,
  74.                 'added_tag_at_end': added_tag}
  75.     except:
  76.         print(traceback.format_exc())
  77.         raise Exception('MessageNotCuted')
  78.  
  79.  
  80. async def cut_for_messages(message_text, limitation):
  81.     try:
  82.         currently_cuted = 0
  83.         message_parts = []
  84.         added_tag_at_last = None
  85.         while len(message_text) > currently_cuted:
  86.             cut_message_result = await cut_message(message_text, limitation)
  87.             currently_cuted += cut_message_result['len_cuted']
  88.             if added_tag_at_last:
  89.                 cuted_part = added_tag_at_last.replace('/', '')
  90.                 cuted_part += cut_message_result['cuted']
  91.                 added_tag_at_last = None
  92.             else:
  93.                 cuted_part = cut_message_result['cuted']
  94.             if cut_message_result['added_tag_at_end']:
  95.                 added_tag_at_last = cut_message_result['added_tag_at_end']
  96.             message_parts.append(cuted_part)
  97.             message_text = message_text[currently_cuted:]
  98.             limitation = len(message_text) if limitation < len(message_text) else limitation
  99.         return message_parts
  100.     except:
  101.         print(traceback.format_exc())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement