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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#![deny(missing_docs)]
#![recursion_limit = "1024"]
extern crate chan;
extern crate handlebars_iron;
extern crate iron;
extern crate mount;
extern crate pulldown_cmark;
extern crate serde;
extern crate shlex;
extern crate staticfile;
extern crate url;
extern crate websocket as websockets;
#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
#[macro_use]
extern crate serde_json;
pub mod browser;
pub mod errors;
pub mod markdown;
mod http;
mod websocket;
use std::env;
use std::io;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::process::Command;
use shlex::Shlex;
use errors::*;
const DEFAULT_HIGHLIGHT_THEME: &'static str = "github";
const DEFAULT_CSS: &'static str = "/_static/vendor/github-markdown-css/github-markdown.css";
#[derive(Debug, Clone)]
pub struct Server {
initial_markdown: String,
working_directory: PathBuf,
highlight_theme: String,
css: String,
websocket_port: u16,
http_port: u16,
external_renderer: Option<String>,
}
impl Server {
pub fn new() -> Self {
Self::default()
}
pub fn initial_markdown<S: Into<String>>(&mut self, markdown: S) -> &Self {
self.initial_markdown = markdown.into();
self
}
pub fn working_directory<D: Into<PathBuf>>(&mut self, directory: D) -> &Self {
self.working_directory = directory.into();
self
}
pub fn highlight_theme<T: Into<String>>(&mut self, theme: T) -> &Self {
self.highlight_theme = theme.into();
self
}
pub fn css<C: Into<String>>(&mut self, css: C) -> &Self {
self.css = css.into();
self
}
pub fn websocket_port(&mut self, port: u16) -> &Self {
self.websocket_port = port;
self
}
pub fn http_port(&mut self, port: u16) -> &Self {
self.http_port = port;
self
}
pub fn external_renderer<C: Into<String>>(&mut self, command: C) -> &Self {
self.external_renderer = Some(command.into());
self
}
pub fn start(&self) -> Result<Listening> {
debug!("starting websocket server");
let websocket_listening = websocket::Server::new()
.listen(("localhost", self.websocket_port))?;
debug!("websockets listening on {}", websocket_listening.local_addr()?);
let initial_html = markdown_to_html(&self.initial_markdown, &self.external_renderer)?;
debug!("starting http_server");
let assigned_websocket_port = websocket_listening.local_addr()?.port();
let http_listening = http::Server::new(
self.working_directory.clone(),
assigned_websocket_port,
http::StyleConfig {
css: self.css.clone(),
highlight_theme: self.highlight_theme.clone(),
}).listen(("localhost", self.http_port), &initial_html)?;
debug!("http listening on {}", http_listening.local_addr()?);
let listening = Listening {
http_listening: http_listening,
websocket_listening: websocket_listening,
external_renderer: self.external_renderer.clone(),
};
Ok(listening)
}
}
impl Default for Server {
fn default() -> Self {
Server {
working_directory: env::current_dir().unwrap().to_owned(),
initial_markdown: String::default(),
highlight_theme: DEFAULT_HIGHLIGHT_THEME.to_string(),
css: DEFAULT_CSS.to_string(),
websocket_port: 0,
http_port: 0,
external_renderer: None,
}
}
}
pub struct Listening {
http_listening: http::Listening,
websocket_listening: websocket::Listening,
external_renderer: Option<String>,
}
impl Listening {
pub fn websocket_addr(&self) -> io::Result<SocketAddr> {
self.websocket_listening.local_addr()
}
pub fn http_addr(&self) -> io::Result<SocketAddr> {
self.http_listening.local_addr()
}
pub fn change_working_directory<P>(&mut self, dir: P)
where P: AsRef<Path>
{
self.http_listening.change_working_directory(dir);
}
pub fn send(&self, markdown: &str) -> Result<()> {
let html_sender = self.websocket_listening.html_sender();
html_sender.send(markdown_to_html(&markdown, &self.external_renderer)?);
Ok(())
}
}
fn markdown_to_html(markdown: &str, external_command: &Option<String>) -> Result<String> {
let html = if let Some(ref command) = *external_command {
let mut shlex = Shlex::new(command);
let renderer = shlex.next().ok_or_else(|| "no external renderer specified")?;
let mut command = Command::new(renderer);
command.args(shlex);
markdown::to_html_external(command, markdown)?
} else {
markdown::to_html_cmark(markdown)
};
Ok(html)
}
#[cfg(test)]
mod tests {
use super::Server;
#[test]
fn sanity() {
let server = Server::new();
server.start().unwrap();
}
}