1pub mod anchor_styles;
6pub mod blockquote;
7pub mod code_block_utils;
8pub mod early_returns;
11pub mod element_cache;
12pub mod emphasis_utils;
13pub mod fix_utils;
14pub mod header_id_utils;
15pub mod jinja_utils;
16pub mod kramdown_utils;
17pub mod line_ending;
18pub mod markdown_elements;
19pub mod mkdocs_abbreviations;
20pub mod mkdocs_admonitions;
21pub mod mkdocs_attr_list;
22pub mod mkdocs_common;
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 mkdocs_test_utils;
33pub mod mkdocstrings_refs;
34pub mod quarto_divs;
35pub mod range_utils;
36pub mod regex_cache;
37pub mod sentence_utils;
38pub mod skip_context;
39pub mod string_interner;
40pub mod table_utils;
41pub mod text_reflow;
42pub mod utf8_offsets;
43
44pub use code_block_utils::CodeBlockUtils;
45pub 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 markdown_elements::{ElementQuality, ElementType, MarkdownElement, MarkdownElements};
51pub use range_utils::LineIndex;
52
53pub fn is_definition_list_item(line: &str) -> bool {
63 let trimmed = line.trim_start();
64 trimmed.starts_with(": ")
65 || (trimmed.starts_with(':') && trimmed.len() > 1 && trimmed.chars().nth(1).is_some_and(|c| c.is_whitespace()))
66}
67
68pub trait StrExt {
70 fn replace_trailing_spaces(&self, replacement: &str) -> String;
72
73 fn has_trailing_spaces(&self) -> bool;
75
76 fn trailing_spaces(&self) -> usize;
78}
79
80impl StrExt for str {
81 fn replace_trailing_spaces(&self, replacement: &str) -> String {
82 let (content, ends_with_newline) = if let Some(stripped) = self.strip_suffix('\n') {
86 (stripped, true)
87 } else {
88 (self, false)
89 };
90
91 let mut non_space_len = content.len();
93 for c in content.chars().rev() {
94 if c == ' ' {
95 non_space_len -= 1;
96 } else {
97 break;
98 }
99 }
100
101 let mut result =
103 String::with_capacity(non_space_len + replacement.len() + if ends_with_newline { 1 } else { 0 });
104 result.push_str(&content[..non_space_len]);
105 result.push_str(replacement);
106 if ends_with_newline {
107 result.push('\n');
108 }
109
110 result
111 }
112
113 fn has_trailing_spaces(&self) -> bool {
114 self.trailing_spaces() > 0
115 }
116
117 fn trailing_spaces(&self) -> usize {
118 let content = self.strip_suffix('\n').unwrap_or(self);
122
123 let mut space_count = 0;
125 for c in content.chars().rev() {
126 if c == ' ' {
127 space_count += 1;
128 } else {
129 break;
130 }
131 }
132
133 space_count
134 }
135}
136
137use std::collections::hash_map::DefaultHasher;
138use std::hash::{Hash, Hasher};
139
140pub fn fast_hash(content: &str) -> u64 {
153 let mut hasher = DefaultHasher::new();
154 content.hash(&mut hasher);
155 hasher.finish()
156}
157
158#[cfg(test)]
159mod tests {
160 use super::*;
161
162 #[test]
163 fn test_detect_line_ending_pure_lf() {
164 let content = "First line\nSecond line\nThird line\n";
166 assert_eq!(detect_line_ending(content), "\n");
167 }
168
169 #[test]
170 fn test_detect_line_ending_pure_crlf() {
171 let content = "First line\r\nSecond line\r\nThird line\r\n";
173 assert_eq!(detect_line_ending(content), "\r\n");
174 }
175
176 #[test]
177 fn test_detect_line_ending_mixed_more_lf() {
178 let content = "First line\nSecond line\r\nThird line\nFourth line\n";
180 assert_eq!(detect_line_ending(content), "\n");
181 }
182
183 #[test]
184 fn test_detect_line_ending_mixed_more_crlf() {
185 let content = "First line\r\nSecond line\r\nThird line\nFourth line\r\n";
187 assert_eq!(detect_line_ending(content), "\r\n");
188 }
189
190 #[test]
191 fn test_detect_line_ending_empty_string() {
192 let content = "";
194 assert_eq!(detect_line_ending(content), "\n");
195 }
196
197 #[test]
198 fn test_detect_line_ending_single_line_no_ending() {
199 let content = "This is a single line with no line ending";
201 assert_eq!(detect_line_ending(content), "\n");
202 }
203
204 #[test]
205 fn test_detect_line_ending_equal_lf_and_crlf() {
206 let content = "Line 1\r\nLine 2\nLine 3\r\nLine 4\n";
210 assert_eq!(detect_line_ending(content), "\n");
211 }
212
213 #[test]
214 fn test_detect_line_ending_single_lf() {
215 let content = "Line 1\n";
217 assert_eq!(detect_line_ending(content), "\n");
218 }
219
220 #[test]
221 fn test_detect_line_ending_single_crlf() {
222 let content = "Line 1\r\n";
224 assert_eq!(detect_line_ending(content), "\r\n");
225 }
226
227 #[test]
228 fn test_detect_line_ending_embedded_cr() {
229 let content = "Line 1\rLine 2\nLine 3\r\nLine 4\n";
232 assert_eq!(detect_line_ending(content), "\n");
234 }
235}