|
| 1 | +import gleam/dict |
| 2 | +import gleam/list |
| 3 | +import gleam/option |
| 4 | +import lustre/ssg |
| 5 | +import simplifile |
| 6 | + |
| 7 | +pub fn add_tree( |
| 8 | + config: ssg.Config(a, b, c), |
| 9 | + path: String, |
| 10 | + base_url_path: String, |
| 11 | + file_action: fn(ssg.Config(a, b, c), String, String) -> |
| 12 | + #(ssg.Config(a, b, c), option.Option(d)), |
| 13 | + dir_action: fn(ssg.Config(a, b, c), dict.Dict(String, d), String, String) -> |
| 14 | + #(ssg.Config(a, b, c), option.Option(d)), |
| 15 | +) -> #(ssg.Config(a, b, c), option.Option(d)) { |
| 16 | + case simplifile.is_file(path), simplifile.is_directory(path) { |
| 17 | + // file |
| 18 | + Ok(True), _ -> file_action(config, path, base_url_path) |
| 19 | + // directory |
| 20 | + _, Ok(True) -> { |
| 21 | + case simplifile.read_directory(path) { |
| 22 | + Ok(files) -> { |
| 23 | + let sub_tree = |
| 24 | + files |
| 25 | + |> list.fold(#(config, dict.new()), fn(acc, p) { |
| 26 | + let config_data = |
| 27 | + add_tree( |
| 28 | + acc.0, |
| 29 | + path <> "/" <> p, |
| 30 | + base_url_path <> "/" <> p, |
| 31 | + file_action, |
| 32 | + dir_action, |
| 33 | + ) |
| 34 | + |
| 35 | + let new_dict = case config_data.1 { |
| 36 | + option.Some(data) -> dict.insert(acc.1, p, data) |
| 37 | + option.None -> acc.1 |
| 38 | + } |
| 39 | + |
| 40 | + #(config_data.0, new_dict) |
| 41 | + }) |
| 42 | + |
| 43 | + dir_action(sub_tree.0, sub_tree.1, path, base_url_path) |
| 44 | + } |
| 45 | + Error(_) -> #(config, option.None) |
| 46 | + } |
| 47 | + } |
| 48 | + // what? |
| 49 | + _, _ -> #(config, option.None) |
| 50 | + } |
| 51 | +} |
0 commit comments