1pub mod anchor_styles;
6pub mod code_block_utils;
7pub mod early_returns;
10pub mod element_cache;
11pub mod emphasis_utils;
12pub mod fix_utils;
13pub mod header_id_utils;
14pub mod jinja_utils;
15pub mod kramdown_utils;
16pub mod line_ending;
17pub mod markdown_elements;
18pub mod mkdocs_abbreviations;
19pub mod mkdocs_admonitions;
20pub mod mkdocs_attr_list;
21pub mod mkdocs_common;
22pub mod mkdocs_critic;
23pub mod mkdocs_definition_lists;
24pub mod mkdocs_extensions;
25pub mod mkdocs_footnotes;
26pub mod mkdocs_icons;
27pub mod mkdocs_patterns;
28pub mod mkdocs_snippets;
29pub mod mkdocs_tabs;
30pub mod mkdocs_test_utils;
31pub mod mkdocstrings_refs;
32pub mod quarto_divs;
33pub mod range_utils;
34pub mod regex_cache;
35pub mod sentence_utils;
36pub mod skip_context;
37pub mod string_interner;
38pub mod table_utils;
39pub mod text_reflow;
40pub mod utf8_offsets;
41
42pub use code_block_utils::CodeBlockUtils;
43pub use line_ending::{
45 LineEnding, detect_line_ending, detect_line_ending_enum, ensure_consistent_line_endings, get_line_ending_str,
46 normalize_line_ending,
47};
48pub use markdown_elements::{ElementQuality, ElementType, MarkdownElement, MarkdownElements};
49pub use range_utils::LineIndex;
50
51pub fn is_definition_list_item(line: &str) -> bool {
61 let trimmed = line.trim_start();
62 trimmed.starts_with(": ")
63 || (trimmed.starts_with(':') && trimmed.len() > 1 && trimmed.chars().nth(1).is_some_and(|c| c.is_whitespace()))
64}
65
66pub trait StrExt {
68 fn replace_trailing_spaces(&self, replacement: &str) -> String;
70
71 fn has_trailing_spaces(&self) -> bool;
73
74 fn trailing_spaces(&self) -> usize;
76}
77
78impl StrExt for str {
79 fn replace_trailing_spaces(&self, replacement: &str) -> String {
80 let (content, ends_with_newline) = if let Some(stripped) = self.strip_suffix('\n') {
84 (stripped, true)
85 } else {
86 (self, false)
87 };
88
89 let mut non_space_len = content.len();
91 for c in content.chars().rev() {
92 if c == ' ' {
93 non_space_len -= 1;
94 } else {
95 break;
96 }
97 }
98
99 let mut result =
101 String::with_capacity(non_space_len + replacement.len() + if ends_with_newline { 1 } else { 0 });
102 result.push_str(&content[..non_space_len]);
103 result.push_str(replacement);
104 if ends_with_newline {
105 result.push('\n');
106 }
107
108 result
109 }
110
111 fn has_trailing_spaces(&self) -> bool {
112 self.trailing_spaces() > 0
113 }
114
115 fn trailing_spaces(&self) -> usize {
116 let content = self.strip_suffix('\n').unwrap_or(self);
120
121 let mut space_count = 0;
123 for c in content.chars().rev() {
124 if c == ' ' {
125 space_count += 1;
126 } else {
127 break;
128 }
129 }
130
131 space_count
132 }
133}
134
135use std::collections::hash_map::DefaultHasher;
136use std::hash::{Hash, Hasher};
137
138pub fn fast_hash(content: &str) -> u64 {
151 let mut hasher = DefaultHasher::new();
152 content.hash(&mut hasher);
153 hasher.finish()
154}
155
156#[cfg(test)]
157mod tests {
158 use super::*;
159
160 #[test]
161 fn test_detect_line_ending_pure_lf() {
162 let content = "First line\nSecond line\nThird line\n";
164 assert_eq!(detect_line_ending(content), "\n");
165 }
166
167 #[test]
168 fn test_detect_line_ending_pure_crlf() {
169 let content = "First line\r\nSecond line\r\nThird line\r\n";
171 assert_eq!(detect_line_ending(content), "\r\n");
172 }
173
174 #[test]
175 fn test_detect_line_ending_mixed_more_lf() {
176 let content = "First line\nSecond line\r\nThird line\nFourth line\n";
178 assert_eq!(detect_line_ending(content), "\n");
179 }
180
181 #[test]
182 fn test_detect_line_ending_mixed_more_crlf() {
183 let content = "First line\r\nSecond line\r\nThird line\nFourth line\r\n";
185 assert_eq!(detect_line_ending(content), "\r\n");
186 }
187
188 #[test]
189 fn test_detect_line_ending_empty_string() {
190 let content = "";
192 assert_eq!(detect_line_ending(content), "\n");
193 }
194
195 #[test]
196 fn test_detect_line_ending_single_line_no_ending() {
197 let content = "This is a single line with no line ending";
199 assert_eq!(detect_line_ending(content), "\n");
200 }
201
202 #[test]
203 fn test_detect_line_ending_equal_lf_and_crlf() {
204 let content = "Line 1\r\nLine 2\nLine 3\r\nLine 4\n";
208 assert_eq!(detect_line_ending(content), "\n");
209 }
210
211 #[test]
212 fn test_detect_line_ending_single_lf() {
213 let content = "Line 1\n";
215 assert_eq!(detect_line_ending(content), "\n");
216 }
217
218 #[test]
219 fn test_detect_line_ending_single_crlf() {
220 let content = "Line 1\r\n";
222 assert_eq!(detect_line_ending(content), "\r\n");
223 }
224
225 #[test]
226 fn test_detect_line_ending_embedded_cr() {
227 let content = "Line 1\rLine 2\nLine 3\r\nLine 4\n";
230 assert_eq!(detect_line_ending(content), "\n");
232 }
233}