1pub mod anchor_styles;
6pub mod atomic_write;
7pub mod blockquote;
8pub mod code_block_utils;
9pub mod emphasis_utils;
10pub mod fix_utils;
11pub mod frontmatter_values;
12pub mod header_id_utils;
13pub mod html_block;
14pub mod jinja_utils;
15pub mod kramdown_utils;
16pub mod line_ending;
17pub mod mdg;
18pub mod mkdocs_admonitions;
19pub mod mkdocs_attr_list;
20pub mod mkdocs_common;
21pub mod mkdocs_config;
22pub mod mkdocs_critic;
23pub mod mkdocs_definition_lists;
24pub mod mkdocs_extensions;
25pub mod mkdocs_footnotes;
26pub mod mkdocs_html_markdown;
27pub mod mkdocs_icons;
28pub mod mkdocs_patterns;
29pub mod mkdocs_snippets;
30pub mod mkdocs_tabs;
31pub mod mkdocstrings_refs;
32pub mod obsidian_config;
33pub mod pandoc;
34pub mod parser_options;
35pub mod project_root;
36pub mod pymdown_blocks;
37pub mod quarto_chunks;
38pub mod range_utils;
39pub mod regex_cache;
40pub mod sentence_utils;
41pub mod skip_context;
42pub mod table_utils;
43pub mod text_reflow;
44pub mod thematic_break;
45pub mod unicode;
46pub mod upward_walk;
47pub mod utf8_offsets;
48
49pub use code_block_utils::CodeBlockUtils;
50pub use line_ending::{
51 LineEnding, detect_line_ending, detect_line_ending_enum, ensure_consistent_line_endings, get_line_ending_str,
52 normalize_line_ending,
53};
54pub use parser_options::rumdl_parser_options;
55pub use range_utils::LineIndex;
56
57pub fn calculate_indentation_width(indent_str: &str, tab_width: usize) -> usize {
61 let mut width = 0;
62 for ch in indent_str.chars() {
63 if ch == '\t' {
64 width = ((width / tab_width) + 1) * tab_width;
65 } else if ch == ' ' {
66 width += 1;
67 } else {
68 break;
69 }
70 }
71 width
72}
73
74pub fn calculate_indentation_width_default(indent_str: &str) -> usize {
76 calculate_indentation_width(indent_str, 4)
77}
78
79pub fn is_definition_list_item(line: &str) -> bool {
89 let trimmed = line.trim_start();
90 trimmed.starts_with(": ")
91 || (trimmed.starts_with(':') && trimmed.len() > 1 && trimmed.chars().nth(1).is_some_and(char::is_whitespace))
92}
93
94pub fn is_template_directive_only(line: &str) -> bool {
104 let trimmed = line.trim();
105 if trimmed.is_empty() {
106 return false;
107 }
108 (trimmed.starts_with("{{") && trimmed.ends_with("}}")) || (trimmed.starts_with("{%") && trimmed.ends_with("%}"))
109}
110
111pub trait StrExt {
113 fn replace_trailing_spaces(&self, replacement: &str) -> String;
115
116 fn has_trailing_spaces(&self) -> bool;
118
119 fn trailing_spaces(&self) -> usize;
121}
122
123impl StrExt for str {
124 fn replace_trailing_spaces(&self, replacement: &str) -> String {
125 let (content, ends_with_newline) = if let Some(stripped) = self.strip_suffix('\n') {
129 (stripped, true)
130 } else {
131 (self, false)
132 };
133
134 let mut non_space_len = content.len();
136 for c in content.chars().rev() {
137 if c == ' ' {
138 non_space_len -= 1;
139 } else {
140 break;
141 }
142 }
143
144 let mut result = String::with_capacity(non_space_len + replacement.len() + usize::from(ends_with_newline));
146 result.push_str(&content[..non_space_len]);
147 result.push_str(replacement);
148 if ends_with_newline {
149 result.push('\n');
150 }
151
152 result
153 }
154
155 fn has_trailing_spaces(&self) -> bool {
156 self.trailing_spaces() > 0
157 }
158
159 fn trailing_spaces(&self) -> usize {
160 let content = self.strip_suffix('\n').unwrap_or(self);
164
165 let mut space_count = 0;
167 for c in content.chars().rev() {
168 if c == ' ' {
169 space_count += 1;
170 } else {
171 break;
172 }
173 }
174
175 space_count
176 }
177}
178
179use std::collections::hash_map::DefaultHasher;
180use std::hash::{Hash, Hasher};
181
182pub fn fast_hash(content: &str) -> u64 {
195 let mut hasher = DefaultHasher::new();
196 content.hash(&mut hasher);
197 hasher.finish()
198}
199
200#[cfg(test)]
201mod tests {
202 use super::*;
203
204 #[test]
205 fn test_detect_line_ending_pure_lf() {
206 let content = "First line\nSecond line\nThird line\n";
208 assert_eq!(detect_line_ending(content), "\n");
209 }
210
211 #[test]
212 fn test_detect_line_ending_pure_crlf() {
213 let content = "First line\r\nSecond line\r\nThird line\r\n";
215 assert_eq!(detect_line_ending(content), "\r\n");
216 }
217
218 #[test]
219 fn test_detect_line_ending_mixed_more_lf() {
220 let content = "First line\nSecond line\r\nThird line\nFourth line\n";
222 assert_eq!(detect_line_ending(content), "\n");
223 }
224
225 #[test]
226 fn test_detect_line_ending_mixed_more_crlf() {
227 let content = "First line\r\nSecond line\r\nThird line\nFourth line\r\n";
229 assert_eq!(detect_line_ending(content), "\r\n");
230 }
231
232 #[test]
233 fn test_detect_line_ending_empty_string() {
234 let content = "";
236 assert_eq!(detect_line_ending(content), "\n");
237 }
238
239 #[test]
240 fn test_detect_line_ending_single_line_no_ending() {
241 let content = "This is a single line with no line ending";
243 assert_eq!(detect_line_ending(content), "\n");
244 }
245
246 #[test]
247 fn test_detect_line_ending_equal_lf_and_crlf() {
248 let content = "Line 1\r\nLine 2\nLine 3\r\nLine 4\n";
252 assert_eq!(detect_line_ending(content), "\n");
253 }
254
255 #[test]
256 fn test_detect_line_ending_single_lf() {
257 let content = "Line 1\n";
259 assert_eq!(detect_line_ending(content), "\n");
260 }
261
262 #[test]
263 fn test_detect_line_ending_single_crlf() {
264 let content = "Line 1\r\n";
266 assert_eq!(detect_line_ending(content), "\r\n");
267 }
268
269 #[test]
270 fn test_detect_line_ending_embedded_cr() {
271 let content = "Line 1\rLine 2\nLine 3\r\nLine 4\n";
274 assert_eq!(detect_line_ending(content), "\n");
276 }
277}