1pub mod anchor_styles;
6pub mod atomic_write;
7pub mod blank_lines;
8pub mod blockquote;
9pub mod code_block_utils;
10pub mod emphasis_utils;
11pub mod fix_utils;
12pub mod frontmatter_values;
13pub(crate) mod gh_aw;
14pub(crate) mod hard_break;
15pub mod header_id_utils;
16pub mod html_block;
17pub mod html_elements;
18pub mod jinja_utils;
19pub mod kramdown_utils;
20pub mod line_ending;
21pub(crate) mod list_fix_guard;
22pub mod list_indent_shift;
23pub mod mdg;
24pub mod mkdocs_admonitions;
25pub mod mkdocs_attr_list;
26pub mod mkdocs_common;
27pub mod mkdocs_config;
28pub mod mkdocs_critic;
29pub mod mkdocs_definition_lists;
30pub mod mkdocs_extensions;
31pub mod mkdocs_footnotes;
32pub mod mkdocs_html_markdown;
33pub mod mkdocs_icons;
34pub mod mkdocs_patterns;
35pub mod mkdocs_snippets;
36pub mod mkdocs_tabs;
37pub mod mkdocstrings_refs;
38pub mod obsidian_config;
39pub mod obsidian_tag;
40pub mod pandoc;
41pub mod parser_options;
42pub mod project_root;
43pub mod pymdown_blocks;
44pub mod quarto_chunks;
45pub mod range_utils;
46pub mod regex_cache;
47pub mod sentence_utils;
48pub mod skip_context;
49pub mod table_utils;
50pub mod text_reflow;
51pub mod thematic_break;
52pub mod unicode;
53pub mod upward_walk;
54pub mod utf8_offsets;
55
56pub use code_block_utils::CodeBlockUtils;
57pub use line_ending::{
58 LineEnding, NormalizedLineEndingMap, detect_line_ending, detect_line_ending_enum, ensure_consistent_line_endings,
59 get_line_ending_str, line_ending_before, normalize_line_ending,
60};
61pub use parser_options::rumdl_parser_options;
62pub use range_utils::LineIndex;
63
64pub fn calculate_indentation_width(indent_str: &str, tab_width: usize) -> usize {
68 let mut width = 0;
69 for ch in indent_str.chars() {
70 if ch == '\t' {
71 width = ((width / tab_width) + 1) * tab_width;
72 } else if ch == ' ' {
73 width += 1;
74 } else {
75 break;
76 }
77 }
78 width
79}
80
81pub fn calculate_indentation_width_default(indent_str: &str) -> usize {
83 calculate_indentation_width(indent_str, 4)
84}
85
86pub fn is_definition_list_item(line: &str) -> bool {
96 let trimmed = line.trim_start();
97 trimmed.starts_with(": ")
98 || (trimmed.starts_with(':') && trimmed.len() > 1 && trimmed.chars().nth(1).is_some_and(char::is_whitespace))
99}
100
101pub fn is_template_directive_only(line: &str) -> bool {
111 let trimmed = line.trim();
112 if trimmed.is_empty() {
113 return false;
114 }
115 (trimmed.starts_with("{{") && trimmed.ends_with("}}")) || (trimmed.starts_with("{%") && trimmed.ends_with("%}"))
116}
117
118pub trait StrExt {
120 fn replace_trailing_spaces(&self, replacement: &str) -> String;
122
123 fn has_trailing_spaces(&self) -> bool;
125
126 fn trailing_spaces(&self) -> usize;
128}
129
130impl StrExt for str {
131 fn replace_trailing_spaces(&self, replacement: &str) -> String {
132 let (content, ends_with_newline) = if let Some(stripped) = self.strip_suffix('\n') {
136 (stripped, true)
137 } else {
138 (self, false)
139 };
140
141 let mut non_space_len = content.len();
143 for c in content.chars().rev() {
144 if c == ' ' {
145 non_space_len -= 1;
146 } else {
147 break;
148 }
149 }
150
151 let mut result = String::with_capacity(non_space_len + replacement.len() + usize::from(ends_with_newline));
153 result.push_str(&content[..non_space_len]);
154 result.push_str(replacement);
155 if ends_with_newline {
156 result.push('\n');
157 }
158
159 result
160 }
161
162 fn has_trailing_spaces(&self) -> bool {
163 self.trailing_spaces() > 0
164 }
165
166 fn trailing_spaces(&self) -> usize {
167 let content = self.strip_suffix('\n').unwrap_or(self);
171
172 let mut space_count = 0;
174 for c in content.chars().rev() {
175 if c == ' ' {
176 space_count += 1;
177 } else {
178 break;
179 }
180 }
181
182 space_count
183 }
184}
185
186use std::collections::hash_map::DefaultHasher;
187use std::hash::{Hash, Hasher};
188
189pub fn fast_hash(content: &str) -> u64 {
202 let mut hasher = DefaultHasher::new();
203 content.hash(&mut hasher);
204 hasher.finish()
205}
206
207pub fn warning_label(kind: &str) -> String {
211 let label = format!("[{kind}]");
212 #[cfg(feature = "colored")]
213 {
214 use colored::Colorize;
215 label.yellow().to_string()
216 }
217 #[cfg(not(feature = "colored"))]
218 {
219 label
220 }
221}
222
223#[cfg(test)]
224mod tests {
225 use super::*;
226
227 #[test]
228 fn test_detect_line_ending_pure_lf() {
229 let content = "First line\nSecond line\nThird line\n";
231 assert_eq!(detect_line_ending(content), "\n");
232 }
233
234 #[test]
235 fn test_detect_line_ending_pure_crlf() {
236 let content = "First line\r\nSecond line\r\nThird line\r\n";
238 assert_eq!(detect_line_ending(content), "\r\n");
239 }
240
241 #[test]
242 fn test_detect_line_ending_mixed_more_lf() {
243 let content = "First line\nSecond line\r\nThird line\nFourth line\n";
245 assert_eq!(detect_line_ending(content), "\n");
246 }
247
248 #[test]
249 fn test_detect_line_ending_mixed_more_crlf() {
250 let content = "First line\r\nSecond line\r\nThird line\nFourth line\r\n";
252 assert_eq!(detect_line_ending(content), "\r\n");
253 }
254
255 #[test]
256 fn test_detect_line_ending_empty_string() {
257 let content = "";
259 assert_eq!(detect_line_ending(content), "\n");
260 }
261
262 #[test]
263 fn test_detect_line_ending_single_line_no_ending() {
264 let content = "This is a single line with no line ending";
266 assert_eq!(detect_line_ending(content), "\n");
267 }
268
269 #[test]
270 fn test_detect_line_ending_equal_lf_and_crlf() {
271 let content = "Line 1\r\nLine 2\nLine 3\r\nLine 4\n";
275 assert_eq!(detect_line_ending(content), "\n");
276 }
277
278 #[test]
279 fn test_detect_line_ending_single_lf() {
280 let content = "Line 1\n";
282 assert_eq!(detect_line_ending(content), "\n");
283 }
284
285 #[test]
286 fn test_detect_line_ending_single_crlf() {
287 let content = "Line 1\r\n";
289 assert_eq!(detect_line_ending(content), "\r\n");
290 }
291
292 #[test]
293 fn test_detect_line_ending_embedded_cr() {
294 let content = "Line 1\rLine 2\nLine 3\r\nLine 4\n";
297 assert_eq!(detect_line_ending(content), "\n");
299 }
300}