Skip to content

Commit aad4ec5

Browse files
committed
No Regexp, no Unicode-problems
While working fine on my Arch GNU/Linux the regular expression-code failed to parse the text right on my Debian stable. All "special chars" were just replaced with question marks.
1 parent cc35809 commit aad4ec5

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ WeeChat + Weeaboo faces = *Weechataboo*
33

44
A [WeeChat](https://weechat.org/)-Script to replace emotion-tags with
55
random emoticon. Emotion tags in text are starting with `~~`. The text
6-
`hello ~~hug` will be `hello (っ´▽`)っ`. \
6+
`hello ~~hug` will be `hello (っ´▽`)っ`.
7+
78
Oh. It's written in Scheme.
89

910
```

weechataboo.scm

+16-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
; For usage see `/help weechataboo`
1818

1919
(use-modules (srfi srfi-1))
20-
(use-modules (ice-9 regex))
2120

2221
; -> List
2322
; Returns a list of available emotions.
@@ -37,10 +36,22 @@
3736
; Replaces in the given string every ~~EMOTION with a fitting emoticon.
3837
(define (emoticonize-line line)
3938
(let*
40-
((match->emoticon (lambda (m) (emotion->emoticon (string-drop m 2))))
41-
(match-func (lambda (m) (match->emoticon (match:substring m))))
42-
(regexp (string-append "~~(" (string-join (emotion-categories) "|") ")")))
43-
(regexp-substitute/global #f regexp line 'pre match-func 'post)))
39+
((as-tag (lambda (emo) (string-append "~~" emo)))
40+
(has-emotions? (lambda (txt)
41+
(any (lambda (emo)
42+
(number? (string-contains txt (as-tag emo))))
43+
(emotion-categories))))
44+
(replace (lambda (emo txt)
45+
(let ((pos (string-contains txt (as-tag emo))))
46+
(if (number? pos)
47+
(string-replace
48+
txt (emotion->emoticon emo)
49+
pos (+ (string-length (as-tag emo)) pos))
50+
txt))))
51+
(new-line (fold replace line (emotion-categories))))
52+
(if (has-emotions? new-line)
53+
(emoticonize-line new-line)
54+
new-line)))
4455

4556
; Pointer String String -> Weechat-Return
4657
; This function was registered to be called when an input was submitted and

0 commit comments

Comments
 (0)