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
//! Document counting logic for Typst documents.
//!
//! This module provides functionality to count words and characters in compiled
//! Typst documents by traversing the document's element tree and extracting
//! rendered text content.
use Introspector;
use EquationElem;
use ;
use FileId;
use ;
/// Result of counting words and characters in a document.
/// Counts words and characters in a compiled Typst document.
///
/// This function traverses all elements in the document using the introspector
/// and extracts plain text content. It handles the following cases:
///
/// - **Text styling**: Skips styling elements (bold, italic, etc.) to avoid
/// double-counting since their text is already included in parent elements.
/// - **Math equations**: Skips mathematical notation to avoid counting math symbols as words.
/// - **Imports**: Optionally excludes text from imported/included files.
/// - **Rendered content**: Only counts text that appears in the final rendered
/// document, ignoring code, comments, and markup syntax.
///
/// # Arguments
///
/// * `introspector` - The Typst introspector providing access to document elements
/// * `exclude_imports` - If `true`, only counts text from the main file
/// * `main_file_id` - File ID of the main document (used when `exclude_imports` is `true`)
///
/// # Returns
///
/// A `Count` struct containing the word and character counts.
///
/// # Examples
///
/// ```ignore
/// use typst_count::count_document;
///
/// let count = count_document(&introspector, false, main_file_id);
/// println!("Words: {}, Characters: {}", count.words, count.characters);
/// ```
///
/// # Counting Method
///
/// - **Words**: Split by Unicode whitespace (equivalent to Rust's `split_whitespace()`)
/// - **Characters**: Total Unicode scalar values (equivalent to Rust's `chars().count()`)
///
/// # Avoiding Double-Counting
///
/// Typst's document tree includes both container elements and their styled children.
/// For example, `*bold text*` creates:
/// - A paragraph element containing "bold text"
/// - A `strong` element also containing "bold text"
///
/// To avoid counting the same text twice, we skip known styling elements whose
/// content is already included in their parent elements.
/// Checks if an element is a text styling element that should be skipped during counting.
///
/// Text styling elements (like bold, italic, underline) wrap text content but don't
/// add new text. Their content is already included in parent elements, so counting
/// them would result in double-counting.
///
/// # Arguments
///
/// * `element` - The element to check
///
/// # Returns
///
/// `true` if the element is a styling element that should be skipped, `false` otherwise.
///
/// # Styling Elements
///
/// The following element types are considered styling elements:
/// - `strong` - Bold text (`*bold*`)
/// - `emph` - Italic/emphasis text (`_italic_`)
/// - `underline` - Underlined text
/// - `strike` - Strikethrough text
/// - `overline` - Overlined text
/// - `sub` - Subscript text
/// - `super` - Superscript text
/// - `highlight` - Highlighted text
/// - `equation` - Math equations (`$...$` or `$ ... $`)
/// - `raw` - code blocks `code`
///
/// # Examples
///
/// ```ignore
/// if is_styling_element(&element) {
/// // Skip this element to avoid double-counting
/// continue;
/// }
/// ```