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
use std::{fmt, path::Path, pin::Pin, process::Stdio};

use async_trait::async_trait;
use futures_util::{stream, Stream};
use tokio::{
    fs::File,
    io::{AsyncRead, AsyncWriteExt, BufReader},
    process::Command,
};

use crate::{
    document_loaders::{process_doc_stream, Loader, LoaderError},
    schemas::Document,
    text_splitter::TextSplitter,
};

#[derive(Debug)]
pub enum InputFormat {
    Docx,
    Epub,
    Html,
    JuypterNotebook,
    Markdown,
    MediaWiki,
    RichTextFormat,
    Typst,
    VimWiki,
}

impl ToString for InputFormat {
    fn to_string(&self) -> String {
        match self {
            InputFormat::Docx => "docx".into(),
            InputFormat::Epub => "epub".into(),
            InputFormat::Html => "html".into(),
            InputFormat::JuypterNotebook => "ipynb".into(),
            InputFormat::MediaWiki => "mediawiki".into(),
            InputFormat::Markdown => "markdown".into(),
            InputFormat::RichTextFormat => "rtf".into(),
            InputFormat::Typst => "typst".into(),
            InputFormat::VimWiki => "vimwiki".into(),
        }
    }
}

pub struct PandocLoader<R> {
    pandoc_path: String,
    input_format: String,
    input: R,
}

impl<R> fmt::Debug for PandocLoader<R> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PandocLoader")
            .field("pandoc_path", &self.pandoc_path)
            .field("input_format", &self.input_format)
            .finish()
    }
}

impl<R: AsyncRead + Send + Sync + Unpin + 'static> PandocLoader<R> {
    pub fn new<S: Into<String>>(pandoc_path: S, input_format: S, input: R) -> Self {
        PandocLoader {
            pandoc_path: pandoc_path.into(),
            input_format: input_format.into(),
            input,
        }
    }

    pub fn new_from_reader<S: Into<String>>(input_format: S, input: R) -> Self {
        PandocLoader::new("pandoc".into(), input_format.into(), input.into())
    }

    pub fn with_pandoc_path<S: Into<String>>(mut self, pandoc_path: S) -> Self {
        self.pandoc_path = pandoc_path.into();
        self
    }
}

impl PandocLoader<BufReader<File>> {
    pub async fn from_path<P: AsRef<Path>, S: Into<String>>(
        input_format: S,
        path: P,
    ) -> Result<Self, LoaderError> {
        let file = File::open(path).await?;
        let reader = BufReader::new(file);

        Ok(Self::new("pandoc".into(), input_format.into(), reader))
    }
}

#[async_trait]
impl<R: AsyncRead + Send + Sync + Unpin + 'static> Loader for PandocLoader<R> {
    async fn load(
        mut self,
    ) -> Result<
        Pin<Box<dyn Stream<Item = Result<Document, LoaderError>> + Send + 'static>>,
        LoaderError,
    > {
        // echo "# Heading1 \n ## Heading 2 \n this is a markdown" | pandoc -f markdown -t plain
        // cat test.md | pandoc -f markdown -t plain

        let mut process = Command::new(self.pandoc_path)
            .arg("-f")
            .arg(self.input_format)
            .arg("-t")
            .arg("plain")
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .spawn()?;

        // safe to unwrap since stdout/stdout has been configured.
        let mut stdin = process.stdin.take().unwrap();
        let mut stdout = process.stdout.take().unwrap();

        tokio::spawn(async move {
            match tokio::io::copy(&mut self.input, &mut stdin).await {
                Ok(_) => {}
                Err(e) => {
                    log::error!("pandoc stdin error: {}", e.to_string());
                }
            }
            stdin.flush().await.unwrap();
            stdin.shutdown().await.unwrap();
        });

        let stdout_task = tokio::spawn(async move {
            let mut buffer = Vec::new();
            match tokio::io::copy(&mut stdout, &mut buffer).await {
                Ok(_) => Ok(buffer),
                Err(e) => Err(e),
            }
        });

        let _exit_status = process.wait().await?;
        let stdout_result = stdout_task.await?.unwrap();
        let stdout_string = String::from_utf8(stdout_result).map_err(|e| {
            LoaderError::OtherError(format!(
                "Failed to convert to utf8 string: {}",
                e.to_string()
            ))
        })?;

        let doc = Document::new(stdout_string);
        let stream = stream::iter(vec![Ok(doc)]);
        Ok(Box::pin(stream))
    }

    async fn load_and_split<TS: TextSplitter + 'static>(
        mut self,
        splitter: TS,
    ) -> Result<
        Pin<Box<dyn Stream<Item = Result<Document, LoaderError>> + Send + 'static>>,
        LoaderError,
    > {
        let doc_stream = self.load().await?;
        let stream = process_doc_stream(doc_stream, splitter).await;
        Ok(Box::pin(stream))
    }
}

#[cfg(test)]
mod tests {
    use futures_util::StreamExt;

    use super::*;

    #[tokio::test]
    async fn test_pandoc_loader() {
        let path = "./src/document_loaders/test_data/sample.docx";

        let loader = PandocLoader::from_path(InputFormat::Docx.to_string(), path)
            .await
            .expect("Failed to create PandocLoader");

        let docs = loader
            .load()
            .await
            .unwrap()
            .map(|d| d.unwrap())
            .collect::<Vec<_>>()
            .await;

        // only pick the first 27 characters for now
        assert_eq!(&docs[0].page_content[..27], "Lorem ipsum dolor sit amet,");
        assert_eq!(docs.len(), 1);
    }
}