rimloc_parsers_xml/
lib.rs1pub use rimloc_core::parse_simple_po as parse_po_string;
2
3use quick_xml::events::Event;
4use quick_xml::Reader;
5use rimloc_core::{Result as CoreResult, TransUnit};
6use std::borrow::Cow;
7use std::convert::TryFrom;
8use std::fs;
9use std::path::Path;
10
11pub fn scan_keyed_xml(root: &Path) -> CoreResult<Vec<TransUnit>> {
16 use walkdir::WalkDir;
17 let mut out: Vec<TransUnit> = Vec::new();
18
19 fn line_for_offset(offset: usize, starts: &[usize]) -> Option<usize> {
20 if starts.is_empty() {
21 return None;
22 }
23 match starts.binary_search(&offset) {
24 Ok(idx) => Some(idx + 1),
25 Err(idx) if idx > 0 => Some(idx),
26 _ => Some(1),
27 }
28 }
29
30 for entry in WalkDir::new(root).into_iter().filter_map(|e| e.ok()) {
31 let p = entry.path();
32 if !p.is_file() {
33 continue;
34 }
35 if p.extension()
36 .and_then(|e| e.to_str())
37 .map_or(true, |ext| !ext.eq_ignore_ascii_case("xml"))
38 {
39 continue;
40 }
41 let p_str = p.to_string_lossy();
43 if !(p_str.contains("/Languages/") || p_str.contains("\\Languages\\")) {
44 continue;
45 }
46 if !(p_str.contains("/Keyed/") || p_str.contains("\\Keyed\\")) {
47 continue;
48 }
49
50 let content = match fs::read_to_string(p) {
51 Ok(s) => s,
52 Err(_) => continue,
53 };
54 let mut line_starts = Vec::new();
55 line_starts.push(0usize);
56 for (idx, _) in content.match_indices('\n') {
57 line_starts.push(idx + 1);
58 }
59
60 let mut reader = Reader::from_str(&content);
61 reader.config_mut().trim_text(true);
62 let mut buf = Vec::new();
63 struct ElementFrame {
64 name: String,
65 line: Option<usize>,
66 has_text: bool,
67 }
68 let mut stack: Vec<ElementFrame> = Vec::new();
69
70 loop {
71 match reader.read_event_into(&mut buf) {
72 Ok(Event::Start(e)) => {
73 let name = String::from_utf8_lossy(e.name().as_ref()).into_owned();
74 let offset = reader.buffer_position();
75 let offset = usize::try_from(offset).unwrap_or(usize::MAX);
76 let line = line_for_offset(offset, &line_starts);
77 stack.push(ElementFrame {
78 name,
79 line,
80 has_text: false,
81 });
82 }
83 Ok(Event::End(_)) => {
84 if let Some(frame) = stack.pop() {
85 if stack.len() == 1 && !frame.has_text && !frame.name.is_empty() {
86 out.push(TransUnit {
87 key: frame.name,
88 source: Some(String::new()),
89 path: p.to_path_buf(),
90 line: frame.line,
91 });
92 }
93 }
94 }
95 Ok(Event::Text(t)) => {
96 let text = t
97 .unescape()
98 .unwrap_or_else(|_| {
99 Cow::Owned(String::from_utf8_lossy(t.as_ref()).into_owned())
100 })
101 .trim()
102 .to_string();
103 if stack.len() == 2 {
104 if let Some(frame) = stack.last_mut() {
105 frame.has_text = true;
106 out.push(TransUnit {
107 key: frame.name.clone(),
108 source: Some(text),
109 path: p.to_path_buf(),
110 line: frame.line,
111 });
112 }
113 }
114 }
115 Ok(Event::CData(t)) => {
116 let text = String::from_utf8_lossy(t.as_ref()).trim().to_string();
117 if stack.len() == 2 {
118 if let Some(frame) = stack.last_mut() {
119 frame.has_text = true;
120 out.push(TransUnit {
121 key: frame.name.clone(),
122 source: Some(text),
123 path: p.to_path_buf(),
124 line: frame.line,
125 });
126 }
127 }
128 }
129 Ok(Event::Eof) => break,
130 Err(_) => break,
131 _ => {}
132 }
133 buf.clear();
134 }
135 }
136
137 Ok(out)
138}