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