-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextstats.sty
129 lines (109 loc) · 3.25 KB
/
textstats.sty
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
\RequirePackage{xparse}
\ProvidesExplPackage
{textstats}
{2021/11/27}
{0.1}
{Text statistics}
%%% USAGE %%%
% Shows number of characters and words in a text.
% Optionally, it can show the time it takes to read--both silently and out loud.
%
% 'Record':
% \textstats[id=<id>, show=<bool>, hide=<bool>]{<text>}
%
% Show stats:
% \showstats{id=<id>, show={chars, words, time}, time={read, speak}, wpm/read=250, wpm/speak=130}
\cs_generate_variant:Nn \tl_count_tokens:n { V }
\prop_new:N \l_id_stats_prop
\keys_define:nn { textstats }
{
show .bool_set:N = \l_show_text_bool,
show .default:n = true,
show .initial:n = true,
hide .bool_set_inverse:N = \l_show_text_bool,
hide .default:n = true,
hide .initial:n = false,
id .tl_set:N = \l_block_id_tl,
id .value_required:n = true,
id .initial:n = default,
}
\NewDocumentCommand \textstats { o m }
{
% If optional key-value arguments are given:
\IfValueT { #1 }
{
\keys_set:nn { textstats } { #1 }
}
\tl_set:Nn \l_tmpa_tl { #2 }
% Count characters
\int_set:Nn \l_tmpa_int
{ \tl_count_tokens:V \l_tmpa_tl }
% Count words
\seq_set_split:NnV \l_tmpa_seq { ~ } \l_tmpa_tl
\int_set:Nn \l_tmpb_int
{ \seq_count:N \l_tmpa_seq }
% Save into the DB
\prop_put:NVn \l_id_stats_prop \l_block_id_tl
{ \int_use:N \l_tmpa_int, \int_use:N \l_tmpb_int }
\bool_if:NT \l_show_text_bool { \tl_use:N \l_tmpa_tl }
}
\keys_define:nn { showstats }
{
show .multichoice:,
show / chars .code:n = \bool_set_true:N \l_show_chars_bool,
show / words .code:n = \bool_set_true:N \l_show_words_bool,
show / time .code:n = \bool_set_true:N \l_show_time_bool,
time .multichoice:,
time / read .code:n = \bool_set_true:N \l_time_read_bool,
time / speak .code:n = \bool_set_true:N \l_time_speak_bool,
wpm / read .fp_set:N = \l_wpm_read_fp,
wpm / speak .fp_set:N = \l_wpm_speak_fp,
}
\NewDocumentCommand \showstats { D<>{default} o }
{
\bool_set_false:N \l_show_chars_bool
\bool_set_false:N \l_show_words_bool
\bool_set_false:N \l_show_time_bool
\bool_set_false:N \l_time_read_bool
\bool_set_false:N \l_time_speak_bool
\fp_set:Nn \l_wpm_read_fp { 250 }
\fp_set:Nn \l_wpm_speak_fp { 130 }
\IfValueTF { #2 }
{
\keys_set:nn { showstats } { #2 }
}
{
\keys_set:nn { showstats }
{
show = { words, time },
time = { read },
}
}
\prop_get:NnN \l_id_stats_prop { #1 } \l_tmpa_tl
\clist_set:NV \l_tmpa_clist \l_tmpa_tl
% Character count
\clist_pop:NN \l_tmpa_clist \l_tmpa_tl
% Word count
\clist_pop:NN \l_tmpa_clist \l_tmpb_tl
(
\bool_if:NT \l_show_chars_bool
{ \tl_use:N \l_tmpa_tl{}~chars,~ }
\bool_if:NT \l_show_words_bool
{ \tl_use:N \l_tmpb_tl{}~words,~ }
\bool_if:NT \l_show_time_bool
{
\bool_if:NT \l_time_read_bool
{
\fp_eval:n
{ round ( \l_tmpb_tl / \l_wpm_read_fp , 2 ) }
{}~min~reading~time,~
}
\bool_if:NT \l_time_speak_bool
{
\fp_eval:n
{ round ( \l_tmpb_tl / \l_wpm_speak_fp , 2 ) }
{}~min~speaking~time,~
}
}
)
}