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
  use std::path::Path;
  use std::path::PathBuf;

  use std::str::Lines;

  use std::fs;
  use std::io;
  use std::env;
  #[derive(Debug)]
  struct TangleError {
      report: String,
  }

  impl TangleError {
      fn new (report: &str) -> Self {
          TangleError {
              report: report.to_string (),
          }
      }
  }
  const DESTINATION_PREFIX: &'static str = "#+property: tangle ";

  fn destination_line_p (line: &str) -> bool {
      line .trim_start () .starts_with (DESTINATION_PREFIX)
  }

  fn find_destination (string: &str) -> Option <String> {
      for line in string.lines () {
          if destination_line_p (line) {
              let destination = &line [DESTINATION_PREFIX.len () ..];
              let destination = destination.trim ();
              return Some (destination.to_string ());
          }
      }
      None
  }

  #[test]
  fn test_find_destination () {
      let example = "#+property: tangle core.rs";
      let destination = find_destination (example) .unwrap ();
      assert_eq! (destination, "core.rs");
  }
    const BLOCK_BEGIN: &'static str = "#+begin_src ";
    const BLOCK_END: &'static str = "#+end_src";

    fn block_begin_line_p (line: &str) -> bool {
        line .trim_start () .starts_with (BLOCK_BEGIN)
    }

    fn block_end_line_p (line: &str) -> bool {
        line .trim_start () .starts_with (BLOCK_END)
    }
    fn tangle_collect (
        result: &mut String,
        lines: &mut Lines,
    ) -> Result <(), TangleError> {
        for line in lines {
            if block_end_line_p (line) {
                return Ok (());
            } else {
                result.push_str (line);
                result.push ('\n');
            }
        }
        let error = TangleError::new ("block_end mismatch");
        Err (error)
    }
    fn tangle (string: &str) -> Result <String, TangleError> {
        let mut result = String::new ();
        let mut lines = string.lines ();
        while let Some (line) = lines.next () {
            if block_begin_line_p (line) {
                tangle_collect (&mut result, &mut lines)?;
            }
        }
        Ok (result)
    }
    #[test]
    fn test_tangle () {
        let example = format! (
            "{}\n{}\n{}\n{}\n",
            "#+begin_src rust",
            "hi",
            "hi",
            "#+end_src",
        );
        let expect = format! (
            "{}\n{}\n",
            "hi",
            "hi",
        );
        let result = tangle (&example) .unwrap ();
        assert_eq! (expect, result);
        let example = format! (
            "{}\n{}\n{}\n{}\n",
            "    #+begin_src rust",
            "    hi",
            "    hi",
            "    #+end_src",
        );
        let expect = format! (
            "{}\n{}\n",
            "    hi",
            "    hi",
        );
        let result = tangle (&example) .unwrap ();
        assert_eq! (expect, result);
    }
    fn good_path_p (path: &Path) -> bool {
        for component in path.iter () {
            if let Some (string) = component.to_str () {
                if string.starts_with ('.') {
                    if ! string .chars () .all (|x| x == '.') {
                        return false;
                    }
                }
            } else {
                return false;
            }
        }
        true
    }
    pub fn org_file_p (file: &Path) -> bool {
        if let Some (os_string) = file.extension () {
            if let Some (string) = os_string.to_str () {
                string == "org"
            } else {
                false
            }
        } else {
            false
        }
    }
    pub fn file_tangle (file: &Path) -> io::Result <()> {
        if ! org_file_p (file) {
            return Ok (());
        }
        println! ("- tangle : {:?}", file);
        let string = fs::read_to_string (file)?;
        if let Some (destination) = find_destination (&string) {
            let result = tangle (&string) .unwrap ();
            let mut destination_path = PathBuf::new ();
            destination_path.push (file);
            destination_path.pop ();
            destination_path.push (destination);
            fs::write (&destination_path, result)
        } else {
            Ok (())
        }
    }
    pub fn dir_tangle (dir: &Path) -> io::Result <()> {
        for entry in dir.read_dir ()? {
            if let Ok (entry) = entry {
                if good_path_p (&entry.path ()) {
                    if entry.file_type ()? .is_file () {
                        file_tangle (&entry.path ())?
                    }
                }
            }
        }
        Ok (())
    }
    pub fn dir_tangle_rec (dir: &Path) -> io::Result <()> {
        for entry in dir.read_dir ()? {
            if let Ok (entry) = entry {
                if good_path_p (&entry.path ()) {
                    if entry.file_type ()? .is_file () {
                        file_tangle (&entry.path ())?
                    } else if entry.file_type ()? .is_dir () {
                        dir_tangle_rec (&entry.path ())?
                    }
                }
            }
        }
        Ok (())
    }
    pub fn absolute_lize (path: &Path) -> PathBuf {
        if path.is_relative () {
            let mut absolute_path = env::current_dir () .unwrap ();
            absolute_path.push (path);
            absolute_path
        } else {
            path.to_path_buf ()
        }
    }
    pub fn tangle_all_before_build () -> io::Result <()> {
        let path = Path::new (".");
        let current_dir = env::current_dir () .unwrap ();
        println! ("- org_tangle_engine");
        println! ("  tangle_all_before_build");
        println! ("  current_dir : {:?}", current_dir);
        let path = absolute_lize (&path);
        dir_tangle_rec (&path)
    }