noosphere_into/transform/
file.rs

1use async_stream::stream;
2use futures::Stream;
3use noosphere_core::context::SphereFile;
4use noosphere_core::data::ContentType;
5use tokio::io::AsyncRead;
6
7use crate::{subtext_to_html_document_stream, subtext_to_html_fragment_stream, Transform};
8
9/// Used to configure the output format of the [file_to_html_stream] transform
10pub enum HtmlOutput {
11    /// Output as a full HTML document
12    Document,
13    /// Output as just a body content fragment
14    Fragment,
15}
16
17/// Given a [Transform], a [SphereFile] and an [HtmlOutput], perform a streamed
18/// transformation of the [SphereFile] into HTML. The transformation that is
19/// performed may vary depending on content type. At this time, only Subtext
20/// is supported.
21pub fn file_to_html_stream<T, R>(
22    file: SphereFile<R>,
23    output: HtmlOutput,
24    transform: T,
25) -> impl Stream<Item = String>
26where
27    T: Transform,
28    R: AsyncRead + Unpin,
29{
30    stream! {
31        match file.memo.content_type() {
32            Some(ContentType::Subtext) => {
33                match output {
34                    HtmlOutput::Document => {
35                        let stream = subtext_to_html_document_stream(transform, file);
36                        for await part in stream {
37                            yield part;
38                        }
39                    },
40                    HtmlOutput::Fragment => {
41                        let stream = subtext_to_html_fragment_stream(file, transform);
42                        for await part in stream {
43                            yield part;
44                        }
45                    }
46                };
47            }
48            _ => {
49                yield "<article><p>Format cannot be rendered as HTML</p></article>".into();
50            }
51        };
52    }
53}