coursemap 0.1.5

A tool to visualize course dependencies from Quarto/Markdown documents
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
//! Document parsing functionality for extracting course metadata

use anyhow::{Context, Result};
use gray_matter::{engine::YAML, Matter};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

use crate::config::Config;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Document {
    pub id: String,
    pub title: String,
    pub file_path: PathBuf,
    pub phase: String,
    pub prerequisites: Vec<String>,
    pub metadata: HashMap<String, serde_yaml::Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct CourseMapMetadata {
    pub id: String,
    pub phase: Option<String>,
    pub prerequisites: Option<Vec<String>>,
}

impl Document {
    /// Create a new document with the given metadata
    pub fn new(
        id: String,
        title: String,
        file_path: PathBuf,
        phase: String,
        prerequisites: Vec<String>,
        metadata: HashMap<String, serde_yaml::Value>,
    ) -> Self {
        Self {
            id,
            title,
            file_path,
            phase,
            prerequisites,
            metadata,
        }
    }

    /// Get the display name for this document
    pub fn display_name(&self) -> String {
        if self.title.is_empty() {
            self.id.clone()
        } else {
            format!("{}\n({})", self.title, self.id)
        }
    }
}

/// Parse all documents in a directory
pub fn parse_directory(dir_path: &str, config: &Config) -> Result<Vec<Document>> {
    let mut documents = Vec::new();
    let dir = Path::new(dir_path);

    if !dir.exists() {
        return Err(anyhow::anyhow!("Directory does not exist: {}", dir_path));
    }

    for entry in WalkDir::new(dir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_file())
    {
        let path = entry.path();

        // Check if file should be ignored
        if let Some(path_str) = path.to_str() {
            if config.should_ignore(path_str) {
                continue;
            }
        }

        // Check if file has a supported extension
        if let Some(extension) = path.extension() {
            let ext = extension.to_string_lossy().to_lowercase();
            if matches!(ext.as_str(), "qmd" | "md" | "rmd") {
                if let Ok(doc) = parse_document(path, config) {
                    documents.push(doc);
                }
            }
        }
    }

    Ok(documents)
}

/// Parse a single document file
pub fn parse_document(file_path: &Path, config: &Config) -> Result<Document> {
    let content = fs::read_to_string(file_path)
        .with_context(|| format!("Failed to read file: {}", file_path.display()))?;

    let matter = Matter::<YAML>::new();
    let result = matter.parse(&content);

    // Extract basic metadata
    let mut metadata: HashMap<String, serde_yaml::Value> = HashMap::new();
    let mut title = String::new();
    let mut course_map_data: Option<CourseMapMetadata> = None;

    if let Some(_front_matter) = result.data {
        // Parse the front matter as YAML directly from the original content
        // Extract the YAML front matter section manually
        let lines: Vec<&str> = content.lines().collect();
        let mut yaml_content = String::new();
        let mut in_frontmatter = false;

        for line in lines {
            if line.trim() == "---" {
                if !in_frontmatter {
                    in_frontmatter = true;
                    continue;
                } else {
                    break;
                }
            }

            if in_frontmatter {
                yaml_content.push_str(line);
                yaml_content.push('\n');
            }
        }

        if !yaml_content.is_empty() {
            if let Ok(serde_yaml::Value::Mapping(map)) =
                serde_yaml::from_str::<serde_yaml::Value>(&yaml_content)
            {
                for (key, value) in map {
                    if let serde_yaml::Value::String(key_str) = key {
                        metadata.insert(key_str.clone(), value.clone());

                        // Extract title
                        if key_str == "title" {
                            if let serde_yaml::Value::String(ref title_str) = value {
                                title = title_str.clone();
                            }
                        }

                        // Extract course-map metadata
                        if key_str == config.root_key {
                            if let Ok(cm_data) = serde_yaml::from_value::<CourseMapMetadata>(value)
                            {
                                course_map_data = Some(cm_data);
                            }
                        }
                    }
                }
            }
        }
    }

    // Extract course map information
    let (id, phase, prerequisites) = if let Some(cm_data) = course_map_data {
        let phase = cm_data.phase.unwrap_or_else(|| "Unknown".to_string());
        let prerequisites = cm_data.prerequisites.unwrap_or_default();
        (cm_data.id, phase, prerequisites)
    } else {
        // Fallback: use filename as ID
        let filename = file_path
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("unknown")
            .to_string();
        (filename, "Unknown".to_string(), Vec::new())
    };

    Ok(Document::new(
        id,
        title,
        file_path.to_path_buf(),
        phase,
        prerequisites,
        metadata,
    ))
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::NamedTempFile;

    #[test]
    fn test_parse_document_with_frontmatter() -> Result<()> {
        let temp_file = NamedTempFile::with_suffix(".qmd")?;

        std::fs::write(
            temp_file.path(),
            r#"---
title: "Test Course"
course-map:
  id: test-course
  phase: Pre
  prerequisites: ["intro"]
---

# Test Course Content
"#,
        )?;

        let config = Config::default();
        let doc = parse_document(temp_file.path(), &config)?;

        assert_eq!(doc.id, "test-course");
        assert_eq!(doc.title, "Test Course");
        assert_eq!(doc.phase, "Pre");
        assert_eq!(doc.prerequisites, vec!["intro"]);

        Ok(())
    }

    #[test]
    fn test_parse_document_without_frontmatter() -> Result<()> {
        let temp_file = NamedTempFile::with_suffix(".md")?;

        std::fs::write(temp_file.path(), "# Just a regular markdown file")?;

        let config = Config::default();
        let doc = parse_document(temp_file.path(), &config)?;

        assert!(!doc.id.is_empty());
        assert_eq!(doc.phase, "Unknown");
        assert!(doc.prerequisites.is_empty());

        Ok(())
    }

    #[test]
    fn test_document_display_name() {
        let doc = Document::new(
            "test-id".to_string(),
            "Test Title".to_string(),
            PathBuf::from("test.qmd"),
            "Pre".to_string(),
            vec![],
            HashMap::new(),
        );

        assert_eq!(doc.display_name(), "Test Title\n(test-id)");

        let doc_no_title = Document::new(
            "test-id".to_string(),
            "".to_string(),
            PathBuf::from("test.qmd"),
            "Pre".to_string(),
            vec![],
            HashMap::new(),
        );

        assert_eq!(doc_no_title.display_name(), "test-id");
    }

    #[test]
    fn test_parse_directory() -> Result<()> {
        let temp_dir = tempfile::tempdir()?;
        let dir_path = temp_dir.path();

        // Create test files
        std::fs::write(
            dir_path.join("course1.qmd"),
            r#"---
title: "Course 1"
course-map:
  id: course1
  phase: Pre
  prerequisites: []
---
# Course 1 Content
"#,
        )?;

        std::fs::write(
            dir_path.join("course2.md"),
            r#"---
title: "Course 2"
course-map:
  id: course2
  phase: InClass
  prerequisites: ["course1"]
---
# Course 2 Content
"#,
        )?;

        // Create a file that should be ignored
        std::fs::write(dir_path.join("index.qmd"), "# Index file")?;

        // Create a non-course file
        std::fs::write(dir_path.join("readme.txt"), "Not a course file")?;

        let config = Config::default();
        let documents = parse_directory(dir_path.to_str().unwrap(), &config)?;

        assert_eq!(documents.len(), 2);

        let course1 = documents.iter().find(|d| d.id == "course1").unwrap();
        assert_eq!(course1.title, "Course 1");
        assert_eq!(course1.phase, "Pre");
        assert!(course1.prerequisites.is_empty());

        let course2 = documents.iter().find(|d| d.id == "course2").unwrap();
        assert_eq!(course2.title, "Course 2");
        assert_eq!(course2.phase, "InClass");
        assert_eq!(course2.prerequisites, vec!["course1"]);

        Ok(())
    }

    #[test]
    fn test_parse_directory_nonexistent() {
        let config = Config::default();
        let result = parse_directory("/nonexistent/path", &config);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Directory does not exist"));
    }

    #[test]
    fn test_parse_document_with_custom_root_key() -> Result<()> {
        let temp_file = NamedTempFile::with_suffix(".qmd")?;

        std::fs::write(
            temp_file.path(),
            r#"---
title: "Custom Course"
my-custom-key:
  id: custom-course
  phase: Post
  prerequisites: ["req1", "req2"]
---
# Custom Course Content
"#,
        )?;

        let mut config = Config::default();
        config.root_key = "my-custom-key".to_string();

        let doc = parse_document(temp_file.path(), &config)?;

        assert_eq!(doc.id, "custom-course");
        assert_eq!(doc.title, "Custom Course");
        assert_eq!(doc.phase, "Post");
        assert_eq!(doc.prerequisites, vec!["req1", "req2"]);

        Ok(())
    }

    #[test]
    fn test_parse_document_partial_metadata() -> Result<()> {
        let temp_file = NamedTempFile::with_suffix(".qmd")?;

        std::fs::write(
            temp_file.path(),
            r#"---
title: "Partial Course"
course-map:
  id: partial-course
  # phase and prerequisites are optional
---
# Partial Course Content
"#,
        )?;

        let config = Config::default();
        let doc = parse_document(temp_file.path(), &config)?;

        assert_eq!(doc.id, "partial-course");
        assert_eq!(doc.title, "Partial Course");
        assert_eq!(doc.phase, "Unknown"); // Default when not specified
        assert!(doc.prerequisites.is_empty()); // Default when not specified

        Ok(())
    }

    #[test]
    fn test_supported_file_extensions() -> Result<()> {
        let temp_dir = tempfile::tempdir()?;
        let dir_path = temp_dir.path();

        // Create files with different extensions
        std::fs::write(
            dir_path.join("test.qmd"),
            "---\ntitle: QMD\ncourse-map:\n  id: qmd\n---\n",
        )?;
        std::fs::write(
            dir_path.join("test.md"),
            "---\ntitle: MD\ncourse-map:\n  id: md\n---\n",
        )?;
        std::fs::write(
            dir_path.join("test.rmd"),
            "---\ntitle: RMD\ncourse-map:\n  id: rmd\n---\n",
        )?;
        std::fs::write(
            dir_path.join("test.txt"),
            "---\ntitle: TXT\ncourse-map:\n  id: txt\n---\n",
        )?; // Should be ignored

        let config = Config::default();
        let documents = parse_directory(dir_path.to_str().unwrap(), &config)?;

        assert_eq!(documents.len(), 3); // Only .qmd, .md, .rmd files

        let ids: Vec<&String> = documents.iter().map(|d| &d.id).collect();
        assert!(ids.contains(&&"qmd".to_string()));
        assert!(ids.contains(&&"md".to_string()));
        assert!(ids.contains(&&"rmd".to_string()));

        Ok(())
    }
}