weave-content 0.2.32

Content DSL parser, validator, and builder for OSINT case files
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
use std::collections::HashSet;

use clap::{Parser, Subcommand};

use weave_content::build_cache;
use weave_content::commands;
use weave_content::output;
use weave_content::registry;
use weave_content::{
    build_case_output_tracked, load_registry, resolve_case_files, resolve_content_root,
};

#[cfg(test)]
use weave_content::entity;

/// Content DSL parser, validator, and builder for OSINT case files.
#[derive(Parser)]
#[command(name = "weave-content", version, about)]
struct Cli {
    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// Validate case files (parse and check schema).
    Validate {
        /// Path to case file, or content root directory.
        /// When a directory is given, auto-discovers cases/**/*.md.
        path: Option<String>,

        /// Content root directory (for loading entity registry).
        /// Defaults to the parent of the given path, or current directory.
        #[arg(long)]
        root: Option<String>,

        /// Treat warnings as errors (filename mismatches, qualifier inconsistencies).
        #[arg(long)]
        strict: bool,

        /// Suppress per-entity and per-relationship detail lines.
        #[arg(long, short)]
        quiet: bool,
    },
    /// Verify URLs in case files (HEAD/GET checks).
    Verify {
        /// Path to case file, or content root directory.
        path: Option<String>,

        /// Content root directory.
        #[arg(long)]
        root: Option<String>,

        /// Maximum concurrent requests.
        #[arg(long, default_value_t = 16)]
        concurrency: usize,

        /// Per-URL timeout in seconds.
        #[arg(long, default_value_t = 15)]
        timeout: u64,

        /// Path to URL verification cache file.
        #[arg(long)]
        cache: Option<String>,

        /// Report all as warnings, never fail.
        #[arg(long)]
        warn_only: bool,
    },
    /// Build JSON output from case files.
    Build {
        /// Path to case file, or content root directory.
        path: Option<String>,

        /// Content root directory.
        #[arg(long)]
        root: Option<String>,

        /// Output directory (one JSON per case). Stdout if omitted.
        #[arg(short, long)]
        output: Option<String>,

        /// Also generate static HTML files in the output directory.
        #[arg(long)]
        html: bool,

        /// Base URL for sitemap generation (used with --html).
        #[arg(long, default_value = "https://redberrythread.org")]
        base_url: String,

        /// Base URL for thumbnail images (used with --html).
        ///
        /// When set, thumbnail `src` attributes are rewritten from
        /// original source URLs to `{url}/thumbnails/{sha256}.webp`.
        /// Example: `http://files.web.garage.localhost:3902`
        #[arg(long)]
        thumbnail_base_url: Option<String>,

        /// Force full rebuild, bypassing the incremental build cache.
        #[arg(long)]
        full: bool,
    },
    /// Check cases for staleness (offline structural analysis).
    CheckStaleness {
        /// Path to case file, or content root directory.
        path: Option<String>,

        /// Content root directory.
        #[arg(long)]
        root: Option<String>,

        /// Months before an `under_investigation` case is considered stale.
        #[arg(long, default_value_t = 6)]
        investigation_months: u32,

        /// Months before a 'trial' case is considered stale.
        #[arg(long, default_value_t = 12)]
        trial_months: u32,

        /// Months before an 'appeal' case is considered stale.
        #[arg(long, default_value_t = 12)]
        appeal_months: u32,
    },
}

fn main() {
    let cli = Cli::parse();

    let exit_code = match cli.command {
        Command::Validate {
            ref path,
            ref root,
            strict,
            quiet,
        } => commands::validate(path.as_deref(), root.as_deref(), strict, quiet),
        Command::Verify {
            ref path,
            ref root,
            concurrency,
            timeout,
            ref cache,
            warn_only,
        } => {
            let config = commands::VerifyConfig {
                concurrency,
                timeout,
                cache_path: cache.clone(),
                warn_only,
            };
            commands::verify(path.as_deref(), root.as_deref(), &config)
        }
        Command::Build {
            ref path,
            ref root,
            ref output,
            html,
            ref base_url,
            ref thumbnail_base_url,
            full,
        } => cmd_build(
            path.as_deref(),
            root.as_deref(),
            output.as_deref(),
            html,
            base_url,
            thumbnail_base_url.as_deref(),
            full,
        ),
        Command::CheckStaleness {
            ref path,
            ref root,
            investigation_months,
            trial_months,
            appeal_months,
        } => {
            let config = commands::StalenessConfig {
                investigation_months,
                trial_months,
                appeal_months,
            };
            commands::check_staleness(path.as_deref(), root.as_deref(), &config)
        }
    };

    std::process::exit(exit_code);
}

fn cmd_build(
    path: Option<&str>,
    root: Option<&str>,
    output_dir: Option<&str>,
    generate_html: bool,
    base_url: &str,
    thumbnail_base_url: Option<&str>,
    force_full: bool,
) -> i32 {
    let content_root = resolve_content_root(path, root);
    let reg = match load_registry(&content_root) {
        Ok(r) => r,
        Err(code) => return code,
    };

    let case_files = match resolve_case_files(path, &content_root) {
        Ok(f) => f,
        Err(code) => return code,
    };

    if case_files.is_empty() {
        eprintln!("no case files found");
        return 1;
    }

    let mut build_cache = load_build_cache(&content_root, force_full);
    let file_hashes = hash_content_files(&reg, &case_files);

    let case_nulid_map = match weave_content::build_case_index(&case_files, &content_root) {
        Ok(m) => m,
        Err(code) => return code,
    };

    let (exit_code, all_outputs) = build_cases(
        &case_files,
        &reg,
        &case_nulid_map,
        &file_hashes,
        &mut build_cache,
        output_dir,
        generate_html,
        force_full,
    );

    finalize_build_cache(&mut build_cache, &case_files, &file_hashes);

    let mut exit_code = exit_code;
    if generate_html {
        if let Some(dir) = output_dir {
            let html_result = weave_content::generate_html_output(
                dir,
                &all_outputs,
                base_url,
                thumbnail_base_url,
            );
            if html_result != 0 {
                exit_code = html_result;
            }
        } else {
            eprintln!("--html requires --output directory");
            exit_code = 1;
        }
    }

    exit_code
}

fn load_build_cache(content_root: &std::path::Path, force_full: bool) -> build_cache::BuildCache {
    if force_full {
        build_cache::BuildCache::empty()
    } else {
        match build_cache::BuildCache::load(content_root) {
            Ok(c) => c,
            Err(e) => {
                eprintln!("warning: {e}, starting fresh");
                build_cache::BuildCache::empty()
            }
        }
    }
}

fn hash_content_files(
    reg: &registry::EntityRegistry,
    case_files: &[String],
) -> std::collections::HashMap<String, String> {
    let mut file_hashes = std::collections::HashMap::new();

    for entry in reg.entries() {
        if let Some(path_str) = entry.path.to_str()
            && let Ok(hash) = build_cache::hash_file(&entry.path)
        {
            file_hashes.insert(path_str.to_string(), hash);
        }
    }

    for case_path in case_files {
        if let Ok(hash) = build_cache::hash_file(std::path::Path::new(case_path)) {
            file_hashes.insert(case_path.clone(), hash);
        }
    }

    file_hashes
}

#[allow(clippy::too_many_arguments)]
fn build_cases(
    case_files: &[String],
    reg: &registry::EntityRegistry,
    case_nulid_map: &std::collections::HashMap<String, (String, String)>,
    file_hashes: &std::collections::HashMap<String, String>,
    build_cache: &mut build_cache::BuildCache,
    output_dir: Option<&str>,
    collect_html: bool,
    force_full: bool,
) -> (i32, Vec<output::CaseOutput>) {
    let mut exit_code = 0;
    let mut written_entities = HashSet::new();
    let mut all_outputs = Vec::new();
    let mut skipped = 0usize;

    for case_path in case_files {
        if !force_full
            && !collect_html
            && let Some(current_hash) = file_hashes.get(case_path)
            && build_cache.is_unchanged_with_hashes(case_path, current_hash, file_hashes)
        {
            skipped += 1;
            continue;
        }

        match build_case_output_tracked(case_path, reg, &mut written_entities, case_nulid_map) {
            Ok(case_output) => {
                let write_result =
                    write_case_output(case_path, &case_output.case_id, &case_output, output_dir);
                if write_result != 0 {
                    exit_code = write_result;
                }

                let deps: Vec<String> = case_output
                    .nodes
                    .iter()
                    .filter_map(|n| {
                        reg.get_by_name(&n.name)
                            .and_then(|e| e.path.to_str().map(String::from))
                    })
                    .collect();

                if let Some(hash) = file_hashes.get(case_path) {
                    build_cache.put(case_path, hash.clone(), deps);
                }

                if collect_html {
                    all_outputs.push(case_output);
                }
            }
            Err(code) => {
                exit_code = code;
            }
        }
    }

    if skipped > 0 {
        eprintln!("incremental: {skipped} case(s) unchanged, skipped");
    }

    (exit_code, all_outputs)
}

fn finalize_build_cache(
    build_cache: &mut build_cache::BuildCache,
    case_files: &[String],
    file_hashes: &std::collections::HashMap<String, String>,
) {
    for (path_str, hash) in file_hashes {
        if !case_files.contains(path_str) {
            build_cache.put(path_str, hash.clone(), vec![]);
        }
    }

    let all_files: std::collections::HashSet<String> = file_hashes.keys().cloned().collect();
    build_cache.prune(&all_files);

    if let Err(e) = build_cache.save() {
        eprintln!("warning: failed to save build cache: {e}");
    } else if !build_cache.is_empty() {
        eprintln!("build cache: {} entries saved", build_cache.len());
    }
}

fn write_case_output(
    path: &str,
    case_id: &str,
    case_output: &output::CaseOutput,
    output_dir: Option<&str>,
) -> i32 {
    match output_dir {
        Some(dir) => {
            let out_path = format!("{dir}/{case_id}.json");
            match serde_json::to_string_pretty(case_output) {
                Ok(json) => {
                    if let Err(e) = std::fs::write(&out_path, json) {
                        eprintln!("{out_path}: error writing file: {e}");
                        return 2;
                    }
                    eprintln!("{path} -> {out_path}");
                }
                Err(e) => {
                    eprintln!("{path}: JSON serialization error: {e}");
                    return 2;
                }
            }
        }
        None => match serde_json::to_string_pretty(case_output) {
            Ok(json) => println!("{json}"),
            Err(e) => {
                eprintln!("{path}: JSON serialization error: {e}");
                return 2;
            }
        },
    }

    0
}

#[cfg(test)]
mod tests {
    use super::*;
    use weave_content::parse_full;

    const FULL_CASE: &str = r"---
id: 01JABC000000000000000000AA
sources:
  - https://www.theguardian.com/football/2025/feb/03/bonnick
  - https://novaramedia.com/2025/02/04/bonnick
---

# Bonnick v Arsenal FC

Kit manager dismissed over social media posts about Israel-Gaza.

## Events

### Bonnick dismissal
- occurred_at: 2024-12-24
- event_type: dismissal
- description: Arsenal dismisses Bonnick over social media posts
  regarding Israel-Gaza conflict.

### FA investigation finding
- occurred_at: 2024
- event_type: investigation_closed
- description: FA investigates and finds the posts did not breach
  FA rules. Matter closed by FA.

### Employment tribunal filing
- occurred_at: 2025-02-03
- event_type: custom:Employment Tribunal
- description: Bonnick files employment tribunal claim against Arsenal.

## Relationships

- Bonnick dismissal -> FA investigation finding: preceded_by
- FA investigation finding -> Employment tribunal filing: preceded_by
- Bonnick dismissal -> Employment tribunal filing: references
  source: https://novaramedia.com/2025/02/04/bonnick

## Timeline

- Bonnick dismissal -> FA investigation finding
- FA investigation finding -> Employment tribunal filing
";

    #[test]
    fn parse_full_case_file() {
        let (case, entities, rels) = parse_full(FULL_CASE, None).unwrap();

        assert_eq!(case.id.as_deref(), Some("01JABC000000000000000000AA"));
        assert_eq!(case.title, "Bonnick v Arsenal FC");
        assert!(case.summary.contains("Kit manager dismissed"));
        assert_eq!(case.sources.len(), 2);

        // 3 events (Event entities)
        assert_eq!(entities.len(), 3);
        assert!(entities.iter().all(|e| e.label == entity::Label::Event));

        let dismissal = entities
            .iter()
            .find(|e| e.name == "Bonnick dismissal")
            .unwrap();
        assert_eq!(dismissal.label, entity::Label::Event);

        // 3 explicit rels + 2 preceded_by from timeline = 5
        assert_eq!(rels.len(), 5);

        // Check preceded_by relationships from timeline (no source URLs)
        let timeline_rels: Vec<_> = rels
            .iter()
            .filter(|r| r.rel_type == "preceded_by" && r.source_urls.is_empty())
            .collect();
        assert_eq!(timeline_rels.len(), 2);
        assert_eq!(timeline_rels[0].source_name, "Bonnick dismissal");
        assert_eq!(timeline_rels[0].target_name, "FA investigation finding");
        assert_eq!(timeline_rels[1].source_name, "FA investigation finding");
        assert_eq!(timeline_rels[1].target_name, "Employment tribunal filing");
    }

    #[test]
    fn parse_full_minimal_case() {
        let input = r"---
sources:
  - https://example.com/source
---

# Minimal Test Case

A simple test.

## Events

### Something happened
- occurred_at: 2025-01-01
- event_type: conviction
";
        let (case, entities, rels) = parse_full(input, None).unwrap();
        assert!(case.id.is_none());
        assert_eq!(case.title, "Minimal Test Case");
        assert_eq!(entities.len(), 1);
        assert_eq!(entities[0].name, "Something happened");
        assert!(rels.is_empty());
    }

    #[test]
    fn json_snapshot_full_case() {
        let (case, entities, rels) = parse_full(FULL_CASE, None).unwrap();
        let build_result = output::build_output(
            "bonnick-v-arsenal",
            "01TEST00000000000000000000",
            &case.title,
            &case.summary,
            &case.tags,
            None,
            case.case_type.as_deref(),
            case.status.as_deref(),
            case.amounts.as_deref(),
            case.tagline.as_deref(),
            &case.sources,
            &case.related_cases,
            &std::collections::HashMap::new(),
            &entities,
            &rels,
            &[],
            &case.involved,
        )
        .unwrap();

        let json = serde_json::to_string_pretty(&build_result.output).unwrap();

        // Verify structure
        assert!(json.contains("\"case_id\": \"bonnick-v-arsenal\""));
        assert!(json.contains("\"title\": \"Bonnick v Arsenal FC\""));
        assert!(json.contains("\"label\": \"event\""));
        assert!(json.contains("\"name\": \"Bonnick dismissal\""));
        assert!(json.contains("\"name\": \"FA investigation finding\""));
        assert!(json.contains("\"event_type\": \"dismissal\""));
        assert!(json.contains("\"event_type\": \"investigation_closed\""));
        assert!(json.contains("\"type\": \"preceded_by\""));
        assert!(json.contains("\"type\": \"references\""));

        // Verify NULIDs
        let output: serde_json::Value = serde_json::from_str(&json).unwrap();
        let nodes = output["nodes"].as_array().unwrap();
        let rels_arr = output["relationships"].as_array().unwrap();

        for node in nodes {
            let id = node["id"].as_str().unwrap();
            assert!(!id.is_empty());
            assert!(id.len() >= 20);
        }
        for rel in rels_arr {
            let id = rel["id"].as_str().unwrap();
            assert!(!id.is_empty());
        }

        // source_id/target_id should reference existing node IDs
        let node_ids: Vec<&str> = nodes.iter().map(|n| n["id"].as_str().unwrap()).collect();
        for rel in rels_arr {
            let source_id = rel["source_id"].as_str().unwrap();
            let target_id = rel["target_id"].as_str().unwrap();
            assert!(
                node_ids.contains(&source_id),
                "source_id {source_id} not found in nodes"
            );
            assert!(
                node_ids.contains(&target_id),
                "target_id {target_id} not found in nodes"
            );
        }
    }

    #[test]
    fn json_snapshot_omits_empty_fields() {
        let input = r"---
sources:
  - https://example.com/src
---

# Sparse Case

Summary.

## Events

### Something
- occurred_at: 2025-01-01
";
        let (case, entities, rels) = parse_full(input, None).unwrap();
        let build_result = output::build_output(
            "sparse",
            "01TEST00000000000000000000",
            &case.title,
            &case.summary,
            &case.tags,
            None,
            case.case_type.as_deref(),
            case.status.as_deref(),
            case.amounts.as_deref(),
            case.tagline.as_deref(),
            &case.sources,
            &case.related_cases,
            &std::collections::HashMap::new(),
            &entities,
            &rels,
            &[],
            &case.involved,
        )
        .unwrap();

        let json = serde_json::to_string_pretty(&build_result.output).unwrap();

        // These should be omitted from the event node (not present at all)
        assert!(!json.contains("\"qualifier\""));
        // Note: "description" may appear on the Case node (from summary)
        assert!(!json.contains("\"thumbnail\""));
        assert!(!json.contains("\"aliases\""));
        assert!(!json.contains("\"urls\""));

        // These should be present
        assert!(json.contains("\"occurred_at\": \"2025-01-01\""));
    }

    #[test]
    fn cross_file_resolution_with_registry() {
        use std::path::PathBuf;
        use weave_content::entity::Entity;

        // Create a registry with an actor
        let entries = vec![registry::RegistryEntry {
            entity: Entity {
                name: "Mark Bonnick".to_string(),
                label: entity::Label::Person,
                fields: vec![(
                    "nationality".to_string(),
                    entity::FieldValue::Single("British".to_string()),
                )],
                id: Some("01JXYZ123456789ABCDEFGHIJK".to_string()),
                line: 1,
                tags: Vec::new(),
                slug: None,
            },
            path: PathBuf::from("people/mark-bonnick.md"),
            tags: Vec::new(),
        }];
        let reg = registry::EntityRegistry::from_entries(entries).unwrap();

        // Case file references "Mark Bonnick" in relationships
        let input = r"---
sources:
  - https://example.com/src
---

# Cross Reference Test

Summary.

## Events

### Dismissal
- occurred_at: 2024-12-24
- event_type: dismissal

## Relationships

- Mark Bonnick -> Dismissal: associate_of
";
        // Without registry: should fail (Mark Bonnick not found)
        let err = parse_full(input, None).unwrap_err();
        assert!(err.iter().any(|e| e.message.contains("Mark Bonnick")));

        // With registry: should succeed
        let (case, entities, rels) = parse_full(input, Some(&reg)).unwrap();
        assert!(case.id.is_none());
        assert_eq!(entities.len(), 1); // only inline event
        assert_eq!(rels.len(), 1);
        assert_eq!(rels[0].source_name, "Mark Bonnick");
        assert_eq!(rels[0].target_name, "Dismissal");
    }
}