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 pandoc;
30pub mod parser_options;
31pub mod project_root;
32pub mod pymdown_blocks;
33pub mod quarto_chunks;
34pub mod range_utils;
35pub mod regex_cache;
36pub mod sentence_utils;
37pub mod skip_context;
38pub mod string_interner;
39pub mod table_utils;
40pub mod text_reflow;
41pub mod thematic_break;
42pub mod upward_walk;
43pub mod utf8_offsets;
44
45pub use code_block_utils::CodeBlockUtils;
46pub use line_ending::{
47 LineEnding, detect_line_ending, detect_line_ending_enum, ensure_consistent_line_endings, get_line_ending_str,
48 normalize_line_ending,
49};
50pub use parser_options::rumdl_parser_options;
51pub use range_utils::LineIndex;
52
53pub fn calculate_indentation_width(indent_str: &str, tab_width: usize) -> usize {
57 let mut width = 0;
58 for ch in indent_str.chars() {
59 if ch == '\t' {
60 width = ((width / tab_width) + 1) * tab_width;
61 } else if ch == ' ' {
62 width += 1;
63 } else {
64 break;
65 }
66 }
67 width
68}
69
70pub fn calculate_indentation_width_default(indent_str: &str) -> usize {
72 calculate_indentation_width(indent_str, 4)
73}
74
75pub fn is_definition_list_item(line: &str) -> bool {
85 let trimmed = line.trim_start();
86 trimmed.starts_with(": ")
87 || (trimmed.starts_with(':') && trimmed.len() > 1 && trimmed.chars().nth(1).is_some_and(char::is_whitespace))
88}
89
90pub fn is_template_directive_only(line: &str) -> bool {
100 let trimmed = line.trim();
101 if trimmed.is_empty() {
102 return false;
103 }
104 (trimmed.starts_with("{{") && trimmed.ends_with("}}")) || (trimmed.starts_with("{%") && trimmed.ends_with("%}"))
105}
106
107pub trait StrExt {
109 fn replace_trailing_spaces(&self, replacement: &str) -> String;
111
112 fn has_trailing_spaces(&self) -> bool;
114
115 fn trailing_spaces(&self) -> usize;
117}
118
119impl StrExt for str {
120 fn replace_trailing_spaces(&self, replacement: &str) -> String {
121 let (content, ends_with_newline) = if let Some(stripped) = self.strip_suffix('\n') {
125 (stripped, true)
126 } else {
127 (self, false)
128 };
129
130 let mut non_space_len = content.len();
132 for c in content.chars().rev() {
133 if c == ' ' {
134 non_space_len -= 1;
135 } else {
136 break;
137 }
138 }
139
140 let mut result = String::with_capacity(non_space_len + replacement.len() + usize::from(ends_with_newline));
142 result.push_str(&content[..non_space_len]);
143 result.push_str(replacement);
144 if ends_with_newline {
145 result.push('\n');
146 }
147
148 result
149 }
150
151 fn has_trailing_spaces(&self) -> bool {
152 self.trailing_spaces() > 0
153 }
154
155 fn trailing_spaces(&self) -> usize {
156 let content = self.strip_suffix('\n').unwrap_or(self);
160
161 let mut space_count = 0;
163 for c in content.chars().rev() {
164 if c == ' ' {
165 space_count += 1;
166 } else {
167 break;
168 }
169 }
170
171 space_count
172 }
173}
174
175use std::collections::hash_map::DefaultHasher;
176use std::hash::{Hash, Hasher};
177
178pub fn fast_hash(content: &str) -> u64 {
191 let mut hasher = DefaultHasher::new();
192 content.hash(&mut hasher);
193 hasher.finish()
194}
195
196#[cfg(test)]
197mod tests {
198 use super::*;
199
200 #[test]
201 fn test_detect_line_ending_pure_lf() {
202 let content = "First line\nSecond line\nThird line\n";
204 assert_eq!(detect_line_ending(content), "\n");
205 }
206
207 #[test]
208 fn test_detect_line_ending_pure_crlf() {
209 let content = "First line\r\nSecond line\r\nThird line\r\n";
211 assert_eq!(detect_line_ending(content), "\r\n");
212 }
213
214 #[test]
215 fn test_detect_line_ending_mixed_more_lf() {
216 let content = "First line\nSecond line\r\nThird line\nFourth line\n";
218 assert_eq!(detect_line_ending(content), "\n");
219 }
220
221 #[test]
222 fn test_detect_line_ending_mixed_more_crlf() {
223 let content = "First line\r\nSecond line\r\nThird line\nFourth line\r\n";
225 assert_eq!(detect_line_ending(content), "\r\n");
226 }
227
228 #[test]
229 fn test_detect_line_ending_empty_string() {
230 let content = "";
232 assert_eq!(detect_line_ending(content), "\n");
233 }
234
235 #[test]
236 fn test_detect_line_ending_single_line_no_ending() {
237 let content = "This is a single line with no line ending";
239 assert_eq!(detect_line_ending(content), "\n");
240 }
241
242 #[test]
243 fn test_detect_line_ending_equal_lf_and_crlf() {
244 let content = "Line 1\r\nLine 2\nLine 3\r\nLine 4\n";
248 assert_eq!(detect_line_ending(content), "\n");
249 }
250
251 #[test]
252 fn test_detect_line_ending_single_lf() {
253 let content = "Line 1\n";
255 assert_eq!(detect_line_ending(content), "\n");
256 }
257
258 #[test]
259 fn test_detect_line_ending_single_crlf() {
260 let content = "Line 1\r\n";
262 assert_eq!(detect_line_ending(content), "\r\n");
263 }
264
265 #[test]
266 fn test_detect_line_ending_embedded_cr() {
267 let content = "Line 1\rLine 2\nLine 3\r\nLine 4\n";
270 assert_eq!(detect_line_ending(content), "\n");
272 }
273}