-
Notifications
You must be signed in to change notification settings - Fork 3
/
Unicode.pm
executable file
·202 lines (187 loc) · 5.74 KB
/
Unicode.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
################################################################
# $Id: Unicode.pm 25638 2022-02-06 14:21:21Z Adimarantis $
# Maintainer: Adimarantis
# Library to convert ASCII into formatted Unicode and apply styles like bold or emoticons
#
# This script free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# The GNU General Public License can be found at
# http://www.gnu.org/copyleft/gpl.html.
# A copy is found in the textfile GPL.txt and important notices to the license
# from the author is found in LICENSE.txt distributed with these scripts.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
################################################################
package FHEM::Text::Unicode;
use strict;
use warnings;
use Exporter 'import';
our @EXPORT_OK = qw (formatTextUnicode formatStringUnicode demoUnicode demoUnicodeHTML);
our %EXPORT_TAGS = (ALL => [@EXPORT_OK]);
#define globally for demo
#Emoticons
my @mrep = (
[":\\)","\x{1F600}"],
[":-\\)","\x{1F600}"],
[":\\(","\x{1F641}"],
["<3","\x{2665}"],
[";-\\)","\x{1F609}"],
[":\\+1:","\x{1F44D}"],
[":smile:","\x{1F600}"],
[":sad:","\x{1F641}"],
[":heart:","\x{2665}"],
[":wink:","\x{1F609}"],
[":thumbsup:","\x{1F44D}"],
);
#html like styles
my @htags = (
["bold" ,"<b>","</b>"],
["italic","<i>","</i>"],
["bold-italic","<bi>","</bi>"],
["mono","<tt>","</tt>"],
["mono","<code>","</code>"],
["underline","<u>","</u>"],
["strikethrough","<s>","</s>"],
["fraktur","<fraktur>","</fraktur>"],
["script","<script>","</script>"],
["square","<square>","</square>"],
);
#Single replacements in html mode
my @hrep = (
["<br>","\n"],
);
#Markdown styles
my @mtags = (
["italic","__","__"],
["strikethrough","~~","~~"],
["bold","\\*\\*","\\*\\*"],
["mono","``","``"],
);
#Convert text with Markdown/html to Unicode
#Arguments:
#$format:
# html: Only apply HTML-like formatting like <x>....</x>
# markdown: Only apply Markdown formatting like __text__ and emoticon replacements
# both: Apply both formatting styles
#$msg: ASCII String that should be replaced
#returns: Unicode string with applied replacements
#To display all replacements use the demoUnicode() or demoUnicodeHTML() function
sub formatTextUnicode($$) {
my ($format,$msg) = @_;
my @tags;
my @reps;
if ($format eq "markdown" || $format eq "both") {
push @tags, @mtags;
push @reps, @mrep;
}
if ($format eq "html" || $format eq "both") {
push @tags, @htags;
push @reps, @hrep;
}
#First pass, replace singe special characters
foreach my $arr (@reps) {
my @val=@$arr;
$msg =~ s/$val[0]/$val[1]/sg;
}
my $found=1;
my $matches=0;
my $text;
while ($found && $matches<100) {
$matches++;
$found=0;
foreach my $arr (@tags) {
my @val=@$arr;
$msg =~ /$val[1](.*?)$val[2]/s;
if (defined $1) {
$text=formatStringUnicode($val[0],$1);
if (defined $text) {
$msg =~ s/$val[1].*?$val[2]/$text/s;
$found=1;
}
}
}
}
return $msg;
}
#Converts normal ASCII into unicode with a special font or style
#$font: Style to be applied: underline, strikethrough, bold, italic, bold-italic, script, fraktur, square, mono
#$str: ASCII String that should be converted
#returns: Unicode String with style applied
sub formatStringUnicode($$) {
my ($font,$str) = @_;
if ($font eq "underline") {
$str =~ s/./$&\x{332}/g;
return $str;
}
if ($font eq "strikethrough") {
$str =~ s/./$&\x{336}/g;
return $str;
}
my %uc = (
"bold" => [0x1d41a,0x1d400,0x1d7ce],
"italic" => [0x1d44e,0x1d434,0x30],
"bold-italic" => [0x1d482,0x1d468,0x30],
"script" => [0x1d4ea,0x1d4d0,0x30], #Using boldface since normal misses some letters
"fraktur" => [0x1d586,0x1d56c,0x30],#Using boldface since normal misses some letters
"square" => [0x1f130,0x1f130,0x30],
"mono" => [0x1d68a,0x1d670,0x1d7f6],
);
return undef if (! defined $uc{$font});
my $rep=chr($uc{$font}[0])."-".chr($uc{$font}[0]+25).chr($uc{$font}[1])."-".chr($uc{$font}[1]+25).chr($uc{$font}[2])."-".chr($uc{$font}[2]+9);
$_=$str;
#"no warnings" to prevent a bug in older Perl versions (seen in 5.28) that warns about
#"Replacement list is longer than search list" when using ASCII->Unicode replacements
eval "{no warnings; tr/a-zA-Z0-9/$rep/}";
return undef if $@;
#Special handling for characters missing in some fonts
# 0x1d455 => 0x1d629, #italic h -> italic sans-serif h or 0x210e (planck constant)
# 0x1d4ba => 0x1d452, #script e -> serif e (not used -> using bold script charset which is complete
$_ =~ tr/\x{1d455}\x{1d4ba}/\x{1d629}\x{1d452}/;
return $_;
}
# Returns a String that is can be embedded in HTML (e.g. FHEM "get") and showcases all possible replacements and their syntax
sub demoUnicodeHTML {
my $str=demoUnicode();
$str =~ s/</</sg;
$str =~ s/</>/sg;
$str =~ s/\n/<br>/sg;
return $str;
}
# Returns a printable String that showcases all possible replacements and their syntax
sub demoUnicode {
my $str;
$str.="HTML style formatting:\n";
foreach my $arr (@htags) {
my @val=@$arr;
$str .= formatStringUnicode($val[0],$val[0]." TEXT 123").": $val[1]text$val[2]\n";
}
$str.="newline: <br>\n";
$str.="\nMarkdown style formatting:\n";
foreach my $arr (@mtags) {
my @val=@$arr;
my $md= formatStringUnicode($val[0],$val[0]." TEXT 123").": $val[1]text$val[2]\n";
$md =~ s/\\//g;
$str.=$md;
}
my $i=0;
foreach my $arr (@mrep) {
my @val=@$arr;
my $emo=$val[0];
$emo =~ s/\\//g;
$str.="$val[1]=$emo ";
$i++;
if ($i>5) {
$str.="\n";
$i=0;
}
}
return $str;
}
1;