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
#![doc = include_str!("readme.md")]

#[cfg(feature = "url")]
pub use url::Url;
#[cfg(feature = "image")]
mod for_image;
#[cfg(feature = "mathml-core")]
mod for_mathml;

mod execute;
mod inspects;

pub use self::{
    execute::{JupyterContext, JupyterTheme},
    inspects::{InspectModule, InspectVariable, InspectVariableRequest},
};
use crate::{Executed, JupyterError};
#[cfg(feature = "mathml-core")]
pub use mathml_core::MathML;
use serde_json::Value;
#[cfg(feature = "svg")]
use svg::Document;

/// A latex text that can render by [MathJax](https://www.mathjax.org/).
#[derive(Clone, Debug)]
pub struct LatexText {
    text: String,
}
impl LatexText {
    /// Create a new latex text.
    pub fn new<S: ToString>(text: S) -> Self {
        LatexText { text: text.to_string() }
    }
}

/// A raw html text.
#[derive(Clone, Debug)]
pub struct HtmlText {
    text: String,
}

impl HtmlText {
    /// Create a new html text.
    pub fn new<S: ToString>(text: S) -> Self {
        HtmlText { text: text.to_string() }
    }
}
impl Executed for HtmlText {
    fn mime_type(&self) -> String {
        "text/html".to_string()
    }

    fn as_json(&self, _: &JupyterContext) -> Value {
        self.text.clone().into()
    }
}

impl Executed for JupyterError {
    fn mime_type(&self) -> String {
        "application/vnd.jupyter.stderr".to_string()
    }

    fn as_json(&self, _: &JupyterContext) -> Value {
        Value::String(self.to_string())
    }
}

impl Executed for bool {
    fn mime_type(&self) -> String {
        "text/plain".to_string()
    }

    fn as_json(&self, _: &JupyterContext) -> Value {
        // bool not support in Jupyter
        Value::String(self.to_string())
    }
}

impl Executed for String {
    fn mime_type(&self) -> String {
        "text/plain".to_string()
    }

    fn as_json(&self, _: &JupyterContext) -> Value {
        self.clone().into()
    }
}

impl Executed for char {
    fn mime_type(&self) -> String {
        "text/plain".to_string()
    }
    fn as_json(&self, _: &JupyterContext) -> Value {
        Value::String(self.to_string())
    }
}

impl<'a> Executed for &'a str {
    fn mime_type(&self) -> String {
        "text/plain".to_string()
    }
    fn as_json(&self, _: &JupyterContext) -> Value {
        self.to_string().into()
    }
}

impl Executed for Value {
    fn mime_type(&self) -> String {
        "application/json".to_string()
    }

    fn as_json(&self, _: &JupyterContext) -> Value {
        self.clone()
    }
}

#[cfg(feature = "url")]
impl Executed for Url {
    fn mime_type(&self) -> String {
        "text/html".to_string()
    }

    fn as_json(&self, _: &JupyterContext) -> Value {
        Value::String(format!(r#"<a href="{}">{}</a>"#, self, self))
    }
}

impl Executed for i32 {
    fn mime_type(&self) -> String {
        "text/plain".to_string()
    }

    fn as_json(&self, _: &JupyterContext) -> Value {
        // number not support in Jupyter
        Value::String(self.to_string())
    }
}

impl Executed for f64 {
    fn mime_type(&self) -> String {
        "text/plain".to_string()
    }

    fn as_json(&self, _: &JupyterContext) -> Value {
        Value::String(self.to_string())
    }
}

impl Executed for LatexText {
    fn mime_type(&self) -> String {
        "text/latex".to_string()
    }

    fn as_json(&self, _: &JupyterContext) -> Value {
        Value::String(self.text.clone())
    }
}

#[cfg(feature = "mathml-core")]
impl Executed for MathML {
    fn mime_type(&self) -> String {
        // has been banned, https://github.com/gnestor/notebook/blob/master/notebook/static/notebook/js/outputarea.js#L260
        // "application/mathml+xml".to_string();
        "text/html".to_string()
    }

    fn as_json(&self, _: &JupyterContext) -> Value {
        Value::String(self.to_string())
    }
}

#[cfg(feature = "svg")]
impl Executed for Document {
    fn mime_type(&self) -> String {
        "image/svg+xml".to_string()
    }

    fn as_json(&self, _: &JupyterContext) -> Value {
        Value::String(self.to_string())
    }
}