Skip to content

Commit 7261c1c

Browse files
committed
Add emoji support
1 parent b4140f2 commit 7261c1c

File tree

4 files changed

+121
-2
lines changed

4 files changed

+121
-2
lines changed

lib/node_modules/@stdlib/repl/presentation/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,18 @@ For example,
227227
228228
```
229229

230+
#### Emoji
231+
232+
To include an [emoji][@stdlib/datasets/emoji], insert the relevant [emoji code][@stdlib/datasets/emoji-code-picto]. For example,
233+
234+
```text
235+
236+
:books: :grinning: :+1: :man: :woman:
237+
238+
```
239+
240+
Note, however, that [emoji][@stdlib/datasets/emoji] support is **limited** to [emoji][@stdlib/datasets/emoji] which are **two** columns wide. Accordingly, not all [emoji][@stdlib/datasets/emoji] are supported.
241+
230242
</section>
231243

232244
<!-- /.intro -->
@@ -1470,6 +1482,10 @@ $ stdlib-repl-presentation ./path/to/presentation.txt
14701482

14711483
[@stdlib/repl]: https://github.com/stdlib-js/stdlib
14721484

1485+
[@stdlib/datasets/emoji]: https://github.com/stdlib-js/stdlib
1486+
1487+
[@stdlib/datasets/emoji-code-picto]: https://github.com/stdlib-js/stdlib
1488+
14731489
</section>
14741490

14751491
<!-- /.links -->

lib/node_modules/@stdlib/repl/presentation/examples/presentation.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Speaker notes.
1616

1717
---
1818

19-
| standard library for JavaScript and Node.js
19+
| :books: standard library for JavaScript and Node.js :books:
2020
\s
2121

2222
--
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var replace = require( '@stdlib/string/replace' );
24+
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
25+
var table = require( '@stdlib/datasets/emoji-code-picto' );
26+
27+
28+
// VARIABLES //
29+
30+
var TABLE = table();
31+
32+
33+
// MAIN //
34+
35+
/**
36+
* Replaces emoji codes with rendered emoji in a line of slide text.
37+
*
38+
* ## Notes
39+
*
40+
* - The input line object is mutated.
41+
*
42+
* @private
43+
* @param {Object} line - line
44+
* @returns {Object} rendered line
45+
*/
46+
function emoji( line ) {
47+
var RE_EMOJI_CODE;
48+
var len;
49+
50+
// Create a regular expression for matching emoji codes (code schema: `:<short_name>[::<modifier>[, <modifier>[, ...]]]`):
51+
RE_EMOJI_CODE = /:([\w+\-]+)(?::{2}([\w+\-,\s]+))?:/g; // eslint-disable-line no-useless-escape
52+
53+
// Initialize a length adjustment factor:
54+
len = 0;
55+
56+
// Replace emoji codes with rendered emojis:
57+
line.text = replace( line.text, RE_EMOJI_CODE, replacer );
58+
59+
// Update the effective line length:
60+
line.length += len;
61+
62+
// Return the rendered line:
63+
return line;
64+
65+
/**
66+
* Callback invoked for each match.
67+
*
68+
* @private
69+
* @param {string} match - matched substring
70+
* @param {string} p1 - first string found by a parenthesized capture group
71+
* @param {(string|void)} p2 - second string found by a parenthesized capture group
72+
* @returns {string} replacement string
73+
*/
74+
function replacer( match, p1, p2 ) {
75+
var code;
76+
var out;
77+
78+
// Reconstitute the emoji code in order to normalize the code string:
79+
code = ':' + p1 + ':';
80+
if ( p2 ) {
81+
p2 = p2.split( /,\s*/ ).join( ', ' ); // normalize any modifiers by ensuring consistent modifier spacing
82+
code += ':' + p2 + ':';
83+
}
84+
if ( hasOwnProp( TABLE, code ) ) {
85+
out = TABLE[ code ];
86+
87+
// Note: we limit ourselves to emojis that are two columns wide--emoji being considered wide characters--hence, minus two. Depending on Unicode support, other emoji may not render as a single glyph.
88+
if ( out.length === 2 ) {
89+
len -= match.length - 2;
90+
return out;
91+
}
92+
}
93+
// If unable to convert an emoji code to a rendered emoji, return the matched substring unchanged:
94+
return match;
95+
}
96+
}
97+
98+
99+
// EXPORTS //
100+
101+
module.exports = emoji;

lib/node_modules/@stdlib/repl/presentation/lib/parse.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var RE_EOL = require( '@stdlib/regexp/eol' );
2424
var trim = require( '@stdlib/string/trim' );
2525
var max = require( '@stdlib/math/base/special/max' );
2626
var style = require( './style_line.js' );
27+
var emoji = require( './emoji.js' );
2728

2829

2930
// VARIABLES //
@@ -109,7 +110,8 @@ function parse( out, str ) {
109110
'length': line.length
110111
});
111112

112-
// TODO: apply emojis!
113+
// Apply emojis:
114+
line = emoji( line );
113115

114116
// Append the line to the fragment:
115117
f.push( line );

0 commit comments

Comments
 (0)