chat_sdk-whatsapp 0.7.0 → 0.8.0
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 +4 -4
- data/lib/chat_sdk/whatsapp/format_converter.rb +60 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e420bdbdf90d49945a4bedf2459b2ad52d3aa3702ed73f3ef531cb327a15fb5c
|
|
4
|
+
data.tar.gz: 7bba518a3f18eca2254c52f97c2dcb1af54ca2bdd823cef3a92dd4c3fae3d7e7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 81e7ff4856c14a188378a97269635076b0e2bf00038eeace77147b15a7ee996b7c03aa8efd8a74f0f82a52f4f06c858aa639be5339152f353da717239c7c60fb
|
|
7
|
+
data.tar.gz: e6fbb0668012855875d2dac4c00b612fdf4d61aa36906398fedfaae7031f7e928834f00c89c637863e1bdd243a573fb4eb3e91f5ddc9b1790f0134559469515d
|
|
@@ -0,0 +1,60 @@
|
|
|
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 split_code_blocks(text)
|
|
27
|
+
text.split(/(```[\s\S]*?```|`[^`]+`)/)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def convert_whatsapp_to_md(text)
|
|
31
|
+
result = text
|
|
32
|
+
|
|
33
|
+
# Bold: *text* → **text** (must come before italic)
|
|
34
|
+
result = result.gsub(/(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)/m, '**\1**')
|
|
35
|
+
|
|
36
|
+
# Italic: _text_ → *text*
|
|
37
|
+
result = result.gsub(/(?<![a-zA-Z0-9])_(.+?)_(?![a-zA-Z0-9])/m, '*\1*')
|
|
38
|
+
|
|
39
|
+
# Strikethrough: ~text~ → ~~text~~
|
|
40
|
+
result.gsub(/(?<!~)~(?!~)(.+?)(?<!~)~(?!~)/m, '~~\1~~')
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def convert_md_to_whatsapp(text)
|
|
44
|
+
result = text
|
|
45
|
+
|
|
46
|
+
# Bold: **text** → *text* (placeholder to prevent italic pass from catching it)
|
|
47
|
+
result = result.gsub(/\*\*(.+?)\*\*/m, "\x00BOLD\\1BOLD\x00")
|
|
48
|
+
|
|
49
|
+
# Italic: *text* → _text_ (only single asterisks remaining after bold placeholder)
|
|
50
|
+
result = result.gsub(/(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)/m, '_\1_')
|
|
51
|
+
|
|
52
|
+
# Restore bold placeholders
|
|
53
|
+
result = result.gsub(/\x00BOLD(.+?)BOLD\x00/m, '*\1*')
|
|
54
|
+
|
|
55
|
+
# Strikethrough: ~~text~~ → ~text~
|
|
56
|
+
result.gsub(/~~(.+?)~~/m, '~\1~')
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
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.
|
|
4
|
+
version: 0.8.0
|
|
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:
|