1pub mod anchor_styles;
6pub mod blockquote;
7pub mod code_block_utils;
8pub mod emphasis_utils;
9pub mod fix_utils;
10pub mod header_id_utils;
11pub mod jinja_utils;
12pub mod kramdown_utils;
13pub mod line_ending;
14pub mod mkdocs_admonitions;
15pub mod mkdocs_attr_list;
16pub mod mkdocs_common;
17pub mod mkdocs_config;
18pub mod mkdocs_critic;
19pub mod mkdocs_definition_lists;
20pub mod mkdocs_extensions;
21pub mod mkdocs_footnotes;
22pub mod mkdocs_html_markdown;
23pub mod mkdocs_icons;
24pub mod mkdocs_patterns;
25pub mod mkdocs_snippets;
26pub mod mkdocs_tabs;
27pub mod mkdocstrings_refs;
28pub mod obsidian_config;
29pub mod parser_options;
30pub mod pymdown_blocks;
31pub mod quarto_divs;
32pub mod range_utils;
33pub mod regex_cache;
34pub mod sentence_utils;
35pub mod skip_context;
36pub mod string_interner;
37pub mod table_utils;
38pub mod text_reflow;
39pub mod thematic_break;
40pub mod utf8_offsets;
41
42pub use code_block_utils::CodeBlockUtils;
43pub use line_ending::{
44 LineEnding, detect_line_ending, detect_line_ending_enum, ensure_consistent_line_endings, get_line_ending_str,
45 normalize_line_ending,
46};
47pub use parser_options::rumdl_parser_options;
48pub use range_utils::LineIndex;
49
50pub fn calculate_indentation_width(indent_str: &str, tab_width: usize) -> usize {
54 let mut width = 0;
55 for ch in indent_str.chars() {
56 if ch == '\t' {
57 width = ((width / tab_width) + 1) * tab_width;
58 } else if ch == ' ' {
59 width += 1;
60 } else {
61 break;
62 }
63 }
64 width
65}
66
67pub fn calculate_indentation_width_default(indent_str: &str) -> usize {
69 calculate_indentation_width(indent_str, 4)
70}
71
72pub fn is_definition_list_item(line: &str) -> bool {
82 let trimmed = line.trim_start();
83 trimmed.starts_with(": ")
84 || (trimmed.starts_with(':') && trimmed.len() > 1 && trimmed.chars().nth(1).is_some_and(char::is_whitespace))
85}
86
87pub fn is_template_directive_only(line: &str) -> bool {
97 let trimmed = line.trim();
98 if trimmed.is_empty() {
99 return false;
100 }
101 (trimmed.starts_with("{{") && trimmed.ends_with("}}")) || (trimmed.starts_with("{%") && trimmed.ends_with("%}"))
102}
103
104pub trait StrExt {
106 fn replace_trailing_spaces(&self, replacement: &str) -> String;
108
109 fn has_trailing_spaces(&self) -> bool;
111
112 fn trailing_spaces(&self) -> usize;
114}
115
116impl StrExt for str {
117 fn replace_trailing_spaces(&self, replacement: &str) -> String {
118 let (content, ends_with_newline) = if let Some(stripped) = self.strip_suffix('\n') {
122 (stripped, true)
123 } else {
124 (self, false)
125 };
126
127 let mut non_space_len = content.len();
129 for c in content.chars().rev() {
130 if c == ' ' {
131 non_space_len -= 1;
132 } else {
133 break;
134 }
135 }
136
137 let mut result = String::with_capacity(non_space_len + replacement.len() + usize::from(ends_with_newline));
139 result.push_str(&content[..non_space_len]);
140 result.push_str(replacement);
141 if ends_with_newline {
142 result.push('\n');
143 }
144
145 result
146 }
147
148 fn has_trailing_spaces(&self) -> bool {
149 self.trailing_spaces() > 0
150 }
151
152 fn trailing_spaces(&self) -> usize {
153 let content = self.strip_suffix('\n').unwrap_or(self);
157
158 let mut space_count = 0;
160 for c in content.chars().rev() {
161 if c == ' ' {
162 space_count += 1;
163 } else {
164 break;
165 }
166 }
167
168 space_count
169 }
170}
171
172use std::collections::hash_map::DefaultHasher;
173use std::hash::{Hash, Hasher};
174
175pub fn fast_hash(content: &str) -> u64 {
188 let mut hasher = DefaultHasher::new();
189 content.hash(&mut hasher);
190 hasher.finish()
191}
192
193#[cfg(test)]
194mod tests {
195 use super::*;
196
197 #[test]
198 fn test_detect_line_ending_pure_lf() {
199 let content = "First line\nSecond line\nThird line\n";
201 assert_eq!(detect_line_ending(content), "\n");
202 }
203
204 #[test]
205 fn test_detect_line_ending_pure_crlf() {
206 let content = "First line\r\nSecond line\r\nThird line\r\n";
208 assert_eq!(detect_line_ending(content), "\r\n");
209 }
210
211 #[test]
212 fn test_detect_line_ending_mixed_more_lf() {
213 let content = "First line\nSecond line\r\nThird line\nFourth line\n";
215 assert_eq!(detect_line_ending(content), "\n");
216 }
217
218 #[test]
219 fn test_detect_line_ending_mixed_more_crlf() {
220 let content = "First line\r\nSecond line\r\nThird line\nFourth line\r\n";
222 assert_eq!(detect_line_ending(content), "\r\n");
223 }
224
225 #[test]
226 fn test_detect_line_ending_empty_string() {
227 let content = "";
229 assert_eq!(detect_line_ending(content), "\n");
230 }
231
232 #[test]
233 fn test_detect_line_ending_single_line_no_ending() {
234 let content = "This is a single line with no line ending";
236 assert_eq!(detect_line_ending(content), "\n");
237 }
238
239 #[test]
240 fn test_detect_line_ending_equal_lf_and_crlf() {
241 let content = "Line 1\r\nLine 2\nLine 3\r\nLine 4\n";
245 assert_eq!(detect_line_ending(content), "\n");
246 }
247
248 #[test]
249 fn test_detect_line_ending_single_lf() {
250 let content = "Line 1\n";
252 assert_eq!(detect_line_ending(content), "\n");
253 }
254
255 #[test]
256 fn test_detect_line_ending_single_crlf() {
257 let content = "Line 1\r\n";
259 assert_eq!(detect_line_ending(content), "\r\n");
260 }
261
262 #[test]
263 fn test_detect_line_ending_embedded_cr() {
264 let content = "Line 1\rLine 2\nLine 3\r\nLine 4\n";
267 assert_eq!(detect_line_ending(content), "\n");
269 }
270}