1#[allow(
39 unused_imports,
40 clippy::wildcard_imports,
41 reason = "Standard workspace module prelude"
42)]
43use crate::utilities::*;
44
45use super::Feed;
46use ctb_formats_html::entities::{
47 to_entities_html32, to_entities_netscape_1999,
48};
49use ctb_formats_utf8::to_ascii_translit;
50use jiff::Timestamp;
51
52const TIME_FMT: &str = "%a, %d %b %Y %H:%M:%S GMT";
53
54fn write_feed_dates_to_strings(feed: &Feed) -> (String, String) {
55 let now_date = Timestamp::now().strftime(TIME_FMT).to_string();
59
60 let pub_date = if let Some(entry) = feed.entries().first() {
61 entry.try_updated_try_format(TIME_FMT)
62 } else {
63 now_date.clone()
64 };
65
66 (pub_date, now_date)
67}
68
69fn push_open(out: &mut String, indent: usize, name: &str) {
70 out.push_str(&"\t".repeat(indent));
71 out.push('<');
72 out.push_str(name);
73 out.push_str(">\n");
74}
75
76fn push_close(out: &mut String, indent: usize, name: &str) {
77 out.push_str(&"\t".repeat(indent));
78 out.push_str("</");
79 out.push_str(name);
80 out.push_str(">\n");
81}
82
83fn push_text_element_raw(
84 out: &mut String,
85 indent: usize,
86 name: &str,
87 value: &str,
88) {
89 out.push_str(&"\t".repeat(indent));
90 out.push('<');
91 out.push_str(name);
92 out.push('>');
93 out.push_str(value);
94 out.push_str("</");
95 out.push_str(name);
96 out.push_str(">\n");
97}
98
99fn prep_string_10(input: &str) -> Result<String> {
105 let input = to_entities_html32(input.to_string())?;
106 Ok(to_ascii_translit(&input))
107}
108
109pub fn render_scripting_news_10(feed: &Feed) -> Result<String> {
117 let mut out = String::new();
118
119 out.push_str(r#"<?XML VERSION="1.0"?>"#);
121 out.push('\n');
122 out.push_str(
123 r#"<!DOCTYPE scriptingNews SYSTEM "http://www.scripting.com/dtd/scriptingNews.dtd">"#,
124 );
125 out.push('\n');
126
127 push_open(&mut out, 0, "scriptingNews");
128 push_open(&mut out, 1, "header");
129
130 push_text_element_raw(&mut out, 2, "scriptingNewsVersion", "1.0a2");
136
137 let (pub_date, last_build_date) = write_feed_dates_to_strings(feed);
138 push_text_element_raw(&mut out, 2, "pubDate", &pub_date);
139 push_text_element_raw(&mut out, 2, "lastBuildDate", &last_build_date);
140
141 let docs_url = feed.home_page_url().unwrap_or("");
143 push_text_element_raw(&mut out, 2, "docs", &prep_string_10(docs_url)?);
144
145 push_close(&mut out, 1, "header");
146
147 for entry in feed.entries() {
148 push_open(&mut out, 1, "item");
149
150 let text = to_ascii_translit(entry.body());
154 push_text_element_raw(&mut out, 2, "text", &text);
155
156 push_open(&mut out, 2, "link");
157
158 let url = entry.url().unwrap_or(entry.id());
160 push_text_element_raw(&mut out, 3, "url", &prep_string_10(url)?);
161 push_text_element_raw(
162 &mut out,
163 3,
164 "linetext",
165 &prep_string_10(entry.title())?,
166 );
167
168 push_close(&mut out, 2, "link");
169 push_close(&mut out, 1, "item");
170 }
171
172 push_close(&mut out, 0, "scriptingNews");
173
174 if !out.bytes().all(|b| b.is_ascii()) {
175 bail!("ScriptingNews 1.0 output contains non-ASCII characters");
176 }
177
178 Ok(out)
179}
180
181fn prep_string_20(input: &str) -> Result<String> {
188 let input = to_entities_netscape_1999(input.to_string())?;
189 Ok(to_ascii_translit(&input))
190}
191
192pub fn render_scripting_news_20(feed: &Feed) -> Result<String> {
204 let mut out = String::new();
205
206 out.push_str(r#"<?xml version="1.0"?>"#);
208 out.push('\n');
209
210 push_open(&mut out, 0, "scriptingNews");
211 push_open(&mut out, 1, "header");
212
213 push_text_element_raw(&mut out, 2, "scriptingNewsVersion", "2.0b1");
219
220 let (pub_date, last_build_date) = write_feed_dates_to_strings(feed);
221 push_text_element_raw(&mut out, 2, "pubDate", &pub_date);
222 push_text_element_raw(&mut out, 2, "lastBuildDate", &last_build_date);
223
224 let docs_url = feed.home_page_url().unwrap_or("");
226 push_text_element_raw(&mut out, 2, "docs", &prep_string_20(docs_url)?);
227
228 push_text_element_raw(
229 &mut out,
230 2,
231 "channelDescription",
232 &prep_string_20(feed.title())?,
233 );
234
235 let channel_link = feed.home_page_url().unwrap_or("");
237 push_text_element_raw(
238 &mut out,
239 2,
240 "channelLink",
241 &prep_string_20(channel_link)?,
242 );
243 push_text_element_raw(
244 &mut out,
245 2,
246 "channelTitle",
247 &prep_string_20(feed.title())?,
248 );
249
250 push_text_element_raw(&mut out, 2, "language", "en-US");
251
252 push_close(&mut out, 1, "header");
253
254 for entry in feed.entries() {
255 push_open(&mut out, 1, "item");
256
257 push_text_element_raw(
259 &mut out,
260 2,
261 "text",
262 &prep_string_20(entry.body())?,
263 );
264
265 push_open(&mut out, 2, "link");
266
267 let url = entry.url().unwrap_or(entry.id());
269 push_text_element_raw(&mut out, 3, "url", &prep_string_20(url)?);
270 push_text_element_raw(
271 &mut out,
272 3,
273 "linetext",
274 &prep_string_20(entry.title())?,
275 );
276
277 push_close(&mut out, 2, "link");
278 push_close(&mut out, 1, "item");
279 }
280
281 push_close(&mut out, 0, "scriptingNews");
282
283 if !out.bytes().all(|b| b.is_ascii()) {
284 bail!("ScriptingNews 2.0 output contains non-ASCII characters");
285 }
286
287 Ok(out)
288}
289
290#[cfg(test)]
295#[allow(
296 clippy::panic,
297 clippy::expect_used,
298 clippy::unwrap_used,
299 clippy::unwrap_in_result,
300 clippy::panic_in_result_fn,
301 clippy::indexing_slicing,
302 clippy::arithmetic_side_effects,
303 reason = "Standard repository test boilerplate"
304)]
305mod tests {
306 use super::*;
307 use crate::Entry;
308
309 #[crate::ctb_test]
314 fn test_scripting_news_10_basic() {
315 let entry = Entry::new(
316 "http://example.org/item1",
317 "Test Item",
318 "The example content. Lorem ipsum dolor sit amet.",
319 "2024-01-15T10:00:00Z",
320 )
321 .with_url_opt(Some("http://example.org/item1".into()));
322
323 let feed = Feed::new("Test Feed", vec![entry])
324 .with_home_page_url_opt(Some("http://example.org/".into()));
325
326 let result = render_scripting_news_10(&feed).unwrap();
327
328 assert!(result.contains(r#"<?XML VERSION="1.0"?>"#));
330 assert!(result.contains(
332 r#"<!DOCTYPE scriptingNews SYSTEM "http://www.scripting.com/dtd/scriptingNews.dtd">"#
333 ));
334 assert!(result.contains("<scriptingNews>"));
336 assert!(result.contains("<header>"));
337 assert!(
338 result
339 .contains("<scriptingNewsVersion>1.0a2</scriptingNewsVersion>")
340 );
341 assert!(result.contains("<item>"));
342 assert!(result.contains(
343 "<text>The example content. Lorem ipsum dolor sit amet.</text>"
344 ));
345 assert!(result.contains("<link>"));
346 assert!(result.contains("<url>http://example.org/item1</url>"));
347 assert!(result.contains("<linetext>Test Item</linetext>"));
348 assert!(result.contains("</scriptingNews>"));
349 }
350
351 #[crate::ctb_test]
352 fn test_scripting_news_10_ascii_only() {
353 let entry = Entry::new(
354 "http://example.org/item1",
355 "Tëst with ümlauts",
356 "Cöntent with spëcial chärs",
357 "2024-01-15",
358 )
359 .with_url_opt(Some("http://example.org/item1".into()));
360
361 let feed = Feed::new("Fëed with spëcial chars", vec![entry])
362 .with_home_page_url_opt(Some("http://example.org/".into()));
363
364 let result = render_scripting_news_10(&feed).unwrap();
365
366 assert!(result.bytes().all(|b| b.is_ascii()));
368 assert!(!result.contains("ë"));
370 assert!(!result.contains("ü"));
371 assert!(!result.contains("ö"));
372 assert!(!result.contains("ä"));
373 }
374
375 #[crate::ctb_test]
376 fn test_scripting_news_10_html_in_text() {
377 let entry = Entry::new(
378 "http://example.org/item1",
379 "Item with HTML",
380 "Content with <i>italic</i> and <b>bold</b>",
381 "2024-01-15",
382 )
383 .with_url_opt(Some("http://example.org/item1".into()));
384
385 let feed = Feed::new("Test Feed", vec![entry])
386 .with_home_page_url_opt(Some("http://example.org/".into()));
387
388 let result = render_scripting_news_10(&feed).unwrap();
389
390 assert!(result.contains("<i>italic</i>"), "Got: '{result}'");
394 assert!(result.contains("<b>bold</b>"), "Got: '{result}'");
395 }
396
397 #[crate::ctb_test]
398 fn test_scripting_news_10_multiple_items() {
399 let entries: Vec<_> = (0..5)
400 .map(|i| {
401 Entry::new(
402 format!("http://example.org/item{i}"),
403 format!("Item {i}"),
404 format!("Content for item {i}"),
405 "",
406 )
407 .with_url_opt(Some(format!("http://example.org/item{i}")))
408 })
409 .collect();
410
411 let feed = Feed::new("Multi-Item Feed", entries)
412 .with_home_page_url_opt(Some("http://example.org/".into()));
413
414 let result = render_scripting_news_10(&feed).unwrap();
415
416 assert!(result.contains("Item 0"));
417 assert!(result.contains("Item 4"));
418 assert!(result.contains("Content for item 2"));
419 }
420
421 #[crate::ctb_test]
426 fn test_scripting_news_20_basic() {
427 let entry = Entry::new(
428 "http://example.org/item1",
429 "Test Item",
430 "The example content. Lorem ipsum dolor sit amet.",
431 "2024-01-15T10:00:00Z",
432 )
433 .with_url_opt(Some("http://example.org/item1".into()));
434
435 let feed = Feed::new("Test Feed", vec![entry])
436 .with_home_page_url_opt(Some("http://example.org/".into()));
437
438 let result = render_scripting_news_20(&feed).unwrap();
439
440 assert!(
442 result.contains(r#"<?xml version="1.0"?>"#),
443 "Got: '{result}'"
444 );
445 assert!(result.contains("<scriptingNews>"), "Got: '{result}'");
447 assert!(result.contains("<header>"), "Got: '{result}'");
448 assert!(
449 result
450 .contains("<scriptingNewsVersion>2.0b1</scriptingNewsVersion>"),
451 "Got: '{result}'"
452 );
453
454 assert!(
456 result.contains("<channelTitle>Test Feed</channelTitle>"),
457 "Got: '{result}'"
458 );
459 assert!(
460 result.contains("<channelLink>http://example.org/</channelLink>"),
461 "Got: '{result}'"
462 );
463 assert!(
464 result
465 .contains("<channelDescription>Test Feed</channelDescription>"),
466 "Got: '{result}'"
467 );
468 assert!(
469 result.contains("<language>en-US</language>"),
470 "Got: '{result}'"
471 );
472
473 assert!(result.contains("<item>"), "Got: '{result}'");
475 assert!(
476 result.contains(
477 "<text>The example content. Lorem ipsum dolor sit amet.</text>"
478 ),
479 "Got: '{result}'"
480 );
481 assert!(result.contains("<link>"), "Got: '{result}'");
482 assert!(
483 result.contains("<url>http://example.org/item1</url>"),
484 "Got: '{result}'"
485 );
486 assert!(
487 result.contains("<linetext>Test Item</linetext>"),
488 "Got: '{result}'"
489 );
490 assert!(result.contains("</scriptingNews>"), "Got: '{result}'");
491 }
492
493 #[crate::ctb_test]
494 fn test_scripting_news_20_ascii_only() {
495 let entry = Entry::new(
496 "http://example.org/item1",
497 "Tëst with ümlauts",
498 "Cöntent with spëcial chärs",
499 "2024-01-15",
500 )
501 .with_url_opt(Some("http://example.org/item1".into()));
502
503 let feed = Feed::new("Fëed with spëcial chars", vec![entry])
504 .with_home_page_url_opt(Some("http://example.org/".into()));
505
506 let result = render_scripting_news_20(&feed).unwrap();
507
508 assert!(result.bytes().all(|b| b.is_ascii()));
510 assert!(!result.contains("ë"));
512 assert!(!result.contains("ü"));
513 assert!(!result.contains("ö"));
514 assert!(!result.contains("ä"));
515 }
516
517 #[crate::ctb_test]
518 fn test_scripting_news_20_no_doctype() {
519 let feed = Feed::empty("Test Feed")
520 .with_home_page_url_opt(Some("http://example.org/".into()));
521
522 let result = render_scripting_news_20(&feed).unwrap();
523
524 assert!(!result.contains("<!DOCTYPE"), "Got: '{result}'");
526 }
527
528 #[crate::ctb_test]
529 fn test_scripting_news_20_html_in_text() {
530 let entry = Entry::new(
531 "http://example.org/item1",
532 "Item with HTML",
533 "Content with <i>italic</i> text",
534 "2024-01-15",
535 )
536 .with_url_opt(Some("http://example.org/item1".into()));
537
538 let feed = Feed::new("Test Feed", vec![entry])
539 .with_home_page_url_opt(Some("http://example.org/".into()));
540
541 let result = render_scripting_news_20(&feed).unwrap();
542
543 assert!(
545 result.contains("<i>italic</i>"),
546 "Got: '{result}'"
547 );
548 }
549
550 #[crate::ctb_test]
551 fn test_scripting_news_20_multiple_items() {
552 let entries: Vec<_> = (0..3)
553 .map(|i| {
554 Entry::new(
555 format!("http://example.org/item{i}"),
556 format!("Item {i}"),
557 format!("Content for item {i}"),
558 "",
559 )
560 .with_url_opt(Some(format!("http://example.org/item{i}")))
561 })
562 .collect();
563
564 let feed = Feed::new("Multi-Item Feed", entries)
565 .with_home_page_url_opt(Some("http://example.org/".into()));
566
567 let result = render_scripting_news_20(&feed).unwrap();
568
569 assert!(result.contains("Item 0"));
570 assert!(result.contains("Item 2"));
571 assert!(result.contains("Content for item 1"));
572 }
573
574 #[crate::ctb_test]
575 fn test_scripting_news_10_vs_20_version_strings() {
576 let feed = Feed::empty("Test")
577 .with_home_page_url_opt(Some("http://example.org/".into()));
578
579 let result_10 = render_scripting_news_10(&feed).unwrap();
580 let result_20 = render_scripting_news_20(&feed).unwrap();
581
582 assert!(
583 result_10
584 .contains("<scriptingNewsVersion>1.0a2</scriptingNewsVersion>"),
585 "Got: '{result_10}'"
586 );
587 assert!(
588 result_20
589 .contains("<scriptingNewsVersion>2.0b1</scriptingNewsVersion>"),
590 "Got: '{result_20}'"
591 );
592 }
593
594 #[crate::ctb_test]
595 fn test_scripting_news_20_has_channel_elements() {
596 let feed = Feed::new("My Channel", vec![])
597 .with_home_page_url_opt(Some("http://example.org/".into()));
598
599 let result_10 = render_scripting_news_10(&feed).unwrap();
600 let result_20 = render_scripting_news_20(&feed).unwrap();
601
602 assert!(!result_10.contains("<channelTitle>"), "Got: '{result_10}'");
604 assert!(!result_10.contains("<channelLink>"), "Got: '{result_10}'");
605 assert!(
606 !result_10.contains("<channelDescription>"),
607 "Got: '{result_10}'"
608 );
609
610 assert!(
612 result_20.contains("<channelTitle>My Channel</channelTitle>"),
613 "Got: '{result_20}'"
614 );
615 assert!(
616 result_20
617 .contains("<channelLink>http://example.org/</channelLink>"),
618 "Got: '{result_20}'"
619 );
620 assert!(
621 result_20.contains(
622 "<channelDescription>My Channel</channelDescription>"
623 ),
624 "Got: '{result_20}'"
625 );
626 }
627}