| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # (C) Paul Evans, 2014-2017 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package Net::Async::Matrix::Utils; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | our $VERSION = '0.19'; | |||||||||||||||||||||||||||
| 12 | 1 | $VERSION = eval $VERSION; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 16 | parse_formatted_message | ||||||||||||||||||||||||||||
| 17 | build_formatted_message | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use String::Tagged 0.11; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Optionally parse HTML rich-formatted body; but don't get too upset if we | ||||||||||||||||||||||||||||
| 23 | # don't have these installed | ||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use constant CAN_PARSE_HTML => eval { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 1 | require HTML::TreeBuilder; | |||||||||||||||||||||||||||
| 26 | 1 | require Convert::Color::HTML; | |||||||||||||||||||||||||||
| 27 | 1 | }; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # Optionally build HTML rich-formatted body; but don't get too upset if we | ||||||||||||||||||||||||||||
| 30 | # don't have this installed | ||||||||||||||||||||||||||||
| 31 | 1 | ▶ | use constant CAN_BUILD_HTML => eval { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 1 | require String::Tagged::HTML; | |||||||||||||||||||||||||||
| 33 | 1 | require Convert::Color::HTML; | |||||||||||||||||||||||||||
| 34 | 1 | }; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 NAME | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | C<Net::Async::Matrix::Utils> - support utilities for L<Net::Async::Matrix> | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =cut | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head2 parse_formatted_message | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | $st = parse_formatted_message( $content ) | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | Given the content of a C<m.room.message> event of C<m.text> or C<m.emote> | ||||||||||||||||||||||||||||
| 53 | type, returns a L<String::Tagged> instance containing the text of the message | ||||||||||||||||||||||||||||
| 54 | with formatting in L<String::Tagged::Formatting> style. If the message is not | ||||||||||||||||||||||||||||
| 55 | formatted, or the formatting is of a kind not recognised, the plain-text body | ||||||||||||||||||||||||||||
| 56 | is returned in an instance with no tags. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | The following formats are recognised: | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =over 4 | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =item org.matrix.custom.html | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | This format requires the presence of L<HTML::TreeBuilder> to parse; it will be | ||||||||||||||||||||||||||||
| 65 | ignored if this module is not available. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | HTML | String::Tagged::Formatting | ||||||||||||||||||||||||||||
| 68 | ------------------+--------------------------- | ||||||||||||||||||||||||||||
| 69 | <b>, <strong> | 'bold' | ||||||||||||||||||||||||||||
| 70 | <i>, <em> | 'italic' | ||||||||||||||||||||||||||||
| 71 | <u> | 'under' | ||||||||||||||||||||||||||||
| 72 | <strike> | 'strike' | ||||||||||||||||||||||||||||
| 73 | <tt>, <code> | 'monospace' | ||||||||||||||||||||||||||||
| 74 | <font color="..." | 'fg' | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =back | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | sub parse_formatted_message | ||||||||||||||||||||||||||||
| 81 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parse_formatted_message: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 2 | my ( $content ) = @_; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 2 | for my $format ( $content->{format} ) { | |||||||||||||||||||||||||||
| 85 | 2 | ▶ | last if !$format; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | ▶ | return _parse_html_body( $content->{formatted_body} ) if | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | CAN_PARSE_HTML and $format eq "org.matrix.custom.html"; | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | return String::Tagged->new( $content->{body} ); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | sub _parse_html_body | ||||||||||||||||||||||||||||
| 95 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_parse_html_body: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 96 | 1 | my ( $formatted ) = @_; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 1 | return _traverse_html( HTML::TreeBuilder->new_from_content( $formatted ) | |||||||||||||||||||||||||||
| 99 | ->find_by_tag_name( 'body' ) | ||||||||||||||||||||||||||||
| 100 | ); | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | sub _traverse_html | ||||||||||||||||||||||||||||
| 104 | 8 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_traverse_html: called CC=16 SCAR=29.7
| |||||||||||||||||||||||||||||
| 105 | 8 | my ( $node ) = @_; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # Plain text | ||||||||||||||||||||||||||||
| 108 | 8 | ▶ | return String::Tagged->new( $node ) if !ref $node; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 3 | my %tags; | |||||||||||||||||||||||||||
| 111 | 3 | for ( $node->tag ) { | |||||||||||||||||||||||||||
| 112 | 3 | ▶ | ( $_ eq "b" || $_ eq "strong" ) and $tags{bold}++, last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_ eq "b" or $_ eq "strong"
MC/DC50%1/2
$_ eq "b" or $_ eq "strong"
$_ eq "b"
$_ eq "strong"
Truth table66%2/3
A or B
A= $_ eq "b"B= $_ eq "strong"
| |||||||||||||||||||||||||||||
| 113 | 2 | ▶ | ( $_ eq "i" || $_ eq "em" ) and $tags{italic}++, last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_ eq "i" or $_ eq "em"
MC/DC0%0/2
$_ eq "i" or $_ eq "em"
$_ eq "i"
$_ eq "em"
Truth table33%1/3
A or B
A= $_ eq "i"B= $_ eq "em"
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 2 | ▶ | $_ eq "u" and $tags{under}++, last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 2 | ▶ | $_ eq "strike" and $tags{strike}++, last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 2 | ▶ | ( $_ eq "tt" || $_ eq "code" ) and $tags{monospace}++, last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_ eq "tt" or $_ eq "code"
MC/DC0%0/2
$_ eq "tt" or $_ eq "code"
$_ eq "tt"
$_ eq "code"
Truth table33%1/3
A or B
A= $_ eq "tt"B= $_ eq "code"
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 2 | ▶ | if( $_ eq "font" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 1 | my %attrs = $node->all_attr; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | my $fg = defined $attrs{color} ? | ||||||||||||||||||||||||||||
| 124 | 1 | ▶ | eval { Convert::Color::HTML->new( $attrs{color} ) } : | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | undef; | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 1 | ▶ | $tags{fg} = $fg if defined $fg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 1 | last; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 3 | my $ret = String::Tagged->new; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 3 | $ret .= _traverse_html( $_ ) for $node->content_list; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 3 | $ret->apply_tag( 0, length $ret, $_, $tags{$_} ) for keys %tags; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 3 | return $ret; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head2 build_formatted_message | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | $content = build_formatted_message( $str ) | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Given a L<String::Tagged::Formatting> instance or plain string, returns a | ||||||||||||||||||||||||||||
| 147 | content HASH reference encoding the formatting the message. Plain strings are | ||||||||||||||||||||||||||||
| 148 | returned simply as a plain-text body; formatted instances will be output as | ||||||||||||||||||||||||||||
| 149 | formatted content if possible: | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =over 4 | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =item org.matrix.custom.html | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | This format is output for formatted messages if L<String::Tagged::HTML> is | ||||||||||||||||||||||||||||
| 156 | available. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | String::Tagged::Formatting | HTML | ||||||||||||||||||||||||||||
| 159 | ---------------------------+-------------------- | ||||||||||||||||||||||||||||
| 160 | 'bold' | <strong> | ||||||||||||||||||||||||||||
| 161 | 'italic' | <em> | ||||||||||||||||||||||||||||
| 162 | 'under' | <u> | ||||||||||||||||||||||||||||
| 163 | 'strike' | <strike> | ||||||||||||||||||||||||||||
| 164 | 'monospace' | <code> | ||||||||||||||||||||||||||||
| 165 | 'fg' | <font color="..."> | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =back | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | sub build_formatted_message | ||||||||||||||||||||||||||||
| 172 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
build_formatted_message: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 173 | 3 | my ( $str ) = @_; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 3 | ▶ | return { body => $str } if !ref $str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 2 | ▶ | if( $str->tagnames and CAN_BUILD_HTML ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$str->tagnames and !!1
MC/DC100%1/1
$str->tagnames and !!1
$str->tagnames
Truth table100%2/2
A
A= $str->tagnames
| |||||||||||||||||||||||||||||
| 178 | my $html = String::Tagged::HTML->clone( $str, | ||||||||||||||||||||||||||||
| 179 | only_tags => [qw( bold italic under strike monospace fg )], | ||||||||||||||||||||||||||||
| 180 | convert_tags => { | ||||||||||||||||||||||||||||
| 181 | bold => "strong", | ||||||||||||||||||||||||||||
| 182 | italic => "em", | ||||||||||||||||||||||||||||
| 183 | under => "u", | ||||||||||||||||||||||||||||
| 184 | strike => "strike", | ||||||||||||||||||||||||||||
| 185 | monospace => "code", | ||||||||||||||||||||||||||||
| 186 | 1 | ▶ | fg => sub { font => { color => $_[1]->as_html->name } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 187 | }, | ||||||||||||||||||||||||||||
| 188 | 1 | ); | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | return { | ||||||||||||||||||||||||||||
| 191 | 1 | body => $str->str, | |||||||||||||||||||||||||||
| 192 | format => "org.matrix.custom.html", | ||||||||||||||||||||||||||||
| 193 | formatted_body => $html->as_html, | ||||||||||||||||||||||||||||
| 194 | }; | ||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | else { | ||||||||||||||||||||||||||||
| 197 | 1 | return { body => $str->str }; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =cut | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 1 | 0x55AA; | |||||||||||||||||||||||||||