chat_sdk-whatsapp 0.7.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f3c28086e09ba4878d59fb136e601ef30f56a60e6baa4f0cb8bb88cd65937da
4
- data.tar.gz: beaf809c9f76782afa2b06066f3f326b2b37d6a676d40656779f2fe6ef56c122
3
+ metadata.gz: 0d7c52b157faa0646077dd3044014702c05649965374072a2e961cf61d356dd0
4
+ data.tar.gz: 3bda0cd18ee75406ad07085e28d7ee61e92af98061a2761c0e8c8847c87a1fa3
5
5
  SHA512:
6
- metadata.gz: 68b9eb36df3a4202f27fc9d6536cb00208e7f55b57fd6faec07d195c84b3680b0242225e29696112ec9fbccfc291ef13881fbf263092056deff315a6ae87485e
7
- data.tar.gz: cba377fb4474be0d2f6128b61baacea40913a3a9d260fade70b77ea8890c70036da5b818f3f52889143e63460e5f19e32d73bd6ece96c2a1523120a778616d20
6
+ metadata.gz: 789e7d79d27f136c9c5cde8df138a75cb128e1783c49b3528fea765e16c39a346eabff9c32d8bc988f3cfda7483a4ac06f307f705d5d6a2f7000a02fc2cd9274
7
+ data.tar.gz: d5977f541e427eb4a68939489004f35eecb407201d4a057f6bc00afb4cbf0b1a5114c313ca21611da4eafd35550aad3ca6f1249b2bf994380887d2d793c6a997
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatSDK
4
+ module WhatsApp
5
+ class FormatConverter < ChatSDK::Format::Converter
6
+ # WhatsApp → Markdown
7
+ def to_markdown(platform_text)
8
+ text = platform_text.to_s
9
+ return "" if text.empty?
10
+
11
+ parts = split_code_blocks(text)
12
+ parts.map.with_index { |part, i| i.odd? ? part : convert_whatsapp_to_md(part) }.join
13
+ end
14
+
15
+ # Markdown → WhatsApp
16
+ def from_markdown(markdown)
17
+ text = markdown.to_s
18
+ return "" if text.empty?
19
+
20
+ parts = split_code_blocks(text)
21
+ parts.map.with_index { |part, i| i.odd? ? part : convert_md_to_whatsapp(part) }.join
22
+ end
23
+
24
+ private
25
+
26
+ def convert_whatsapp_to_md(text)
27
+ result = text
28
+
29
+ # Bold: *text* → **text** (must come before italic)
30
+ result = result.gsub(/(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)/m, '**\1**')
31
+
32
+ # Italic: _text_ → *text*
33
+ result = result.gsub(/(?<![a-zA-Z0-9])_(.+?)_(?![a-zA-Z0-9])/m, '*\1*')
34
+
35
+ # Strikethrough: ~text~ → ~~text~~
36
+ result.gsub(/(?<!~)~(?!~)(.+?)(?<!~)~(?!~)/m, '~~\1~~')
37
+ end
38
+
39
+ def convert_md_to_whatsapp(text)
40
+ result = text
41
+
42
+ # Bold/italic with placeholder to prevent double conversion
43
+ result = convert_bold_and_italic_from_md(result, bold_char: "*", italic_char: "_")
44
+
45
+ # Strikethrough: ~~text~~ → ~text~
46
+ result.gsub(/~~(.+?)~~/m, '~\1~')
47
+ end
48
+ end
49
+ end
50
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chat_sdk-whatsapp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Quentin Rousseau
@@ -49,6 +49,7 @@ files:
49
49
  - lib/chat_sdk/whatsapp/adapter.rb
50
50
  - lib/chat_sdk/whatsapp/api_client.rb
51
51
  - lib/chat_sdk/whatsapp/event_parser.rb
52
+ - lib/chat_sdk/whatsapp/format_converter.rb
52
53
  - lib/chat_sdk/whatsapp/interactive_renderer.rb
53
54
  homepage: https://github.com/rootlyhq/chat-sdk
54
55
  licenses: