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
//! [aurelius](https://github.com/euclio/aurelius) is a complete solution for rendering and
//! previewing markdown.
//!
//! This crate provides a server that can render and update an HTML preview of markdown without a
//! client-side refresh. The server listens for both WebSocket and HTTP connections on arbitrary
//! ports. Upon receiving an HTTP request, the server renders a page containing a markdown preview.
//! Client-side JavaScript then initiates a WebSocket connection which allows the server to push
//! changes to the client.
//!
//! This crate was designed to power [vim-markdown-composer], a markdown preview plugin for
//! [Neovim](http://neovim.io), but it may be used to implement similar plugins for any editor.
//! See [vim-markdown-composer] for a usage example.
//!
//! aurelius follows stable Rust. However, the API currently unstable and may change without
//! warning.
//!
//! # Acknowledgments
//! This crate is inspired by suan's
//! [instant-markdown-d](https://github.com/suan/instant-markdown-d).
//!
//! # Why the name?
//! "Aurelius" is a Roman *gens* (family name) shared by many famous Romans, including emperor
//! Marcus Aurelius, one of the "Five Good Emperors." The gens itself originates from the Latin
//! *aureus* meaning "golden." Also, tell me that "Markdown Aurelius" isn't a great pun.
//!
//! <cite>[Aurelia (gens) on Wikipedia](https://en.wikipedia.org/wiki/Aurelia_(gens))</cite>.
//!
//! [vim-markdown-composer]: https://github.com/euclio/vim-markdown-composer

#![deny(missing_docs)]

extern crate chan;
extern crate hoedown;
extern crate porthole;
extern crate url;
extern crate websocket as websockets;

#[macro_use]
extern crate log;
#[macro_use]
extern crate nickel;

pub mod browser;
pub mod markdown;

mod http;
mod websocket;

use std::env;
use std::net::SocketAddr;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::mpsc;
use std::thread;

use http::Server as HttpServer;
use websocket::Server as WebSocketServer;

/// The `Server` type constructs a new markdown preview server.
///
/// The server will listen for HTTP and WebSocket connections on arbitrary ports.
pub struct Server {
    config: Config,
}

/// A server that is listening for HTTP requests on a given port, and broadcasting rendered
/// markdown over a websocket on another port.
pub struct Handle {
    http_server: HttpServer,
    websocket_server: WebSocketServer,
    markdown_sender: mpsc::Sender<String>,
}

/// Configuration for the markdown server.
#[derive(Debug, Clone)]
pub struct Config {
    /// The initial markdown to render when starting the server.
    pub initial_markdown: String,

    /// The syntax highlighting theme to use.
    ///
    /// Defaults to the github syntax highlighting theme.
    pub highlight_theme: String,

    /// The directory that static files should be served out of.
    ///
    /// Defaults to the current working directory.
    pub working_directory: PathBuf,

    /// Custom CSS that should be used to style the markdown.
    ///
    /// Defaults to the github styles.
    pub custom_css: String,
}

impl Default for Config {
    fn default() -> Self {
        Config {
            working_directory: env::current_dir().unwrap().to_owned(),
            initial_markdown: "".to_owned(),
            highlight_theme: "github".to_owned(),
            custom_css: "/vendor/github-markdown-css/github-markdown.css".to_owned(),
        }
    }
}

impl Server {
    /// Creates a new markdown preview server.
    pub fn new() -> Server {
        Self::new_with_config(Config { ..Default::default() })
    }

    /// Creates a new configuration with the config struct.
    ///
    /// # Example
    /// ```
    /// use std::default::Default;
    /// use aurelius::{Config, Server};
    ///
    /// let server = Server::new_with_config(Config {
    ///     highlight_theme: "github".to_owned(), .. Default::default()
    /// });
    /// ```
    pub fn new_with_config(config: Config) -> Server {
        Server { config: config }
    }

    /// Starts the server.
    ///
    /// Returns a channel that can be used to send markdown to the server. The markdown will be
    /// sent as HTML to all clients of the websocket server.
    pub fn start(&mut self) -> Handle {
        let http_server = HttpServer::new(("localhost", 0), self.config.working_directory.clone());
        let mut websocket_server = WebSocketServer::new(("localhost", 0));

        let (markdown_sender, markdown_receiver) = mpsc::channel::<String>();
        let websocket_sender = websocket_server.start();

        thread::spawn(move || {
            for markdown in markdown_receiver.iter() {
                let html: String = markdown::to_html(&markdown);
                websocket_sender.send(html);
            }
        });

        let websocket_port = websocket_server.local_addr().unwrap().port();

        debug!("Starting http_server");
        http_server.start(websocket_port, &self.config);

        Handle {
            http_server: http_server,
            websocket_server: websocket_server,
            markdown_sender: markdown_sender,
        }
    }
}

impl Handle {
    /// Returns the socket address that the websocket server is listening on.
    pub fn websocket_addr(&self) -> io::Result<SocketAddr> {
        self.websocket_server.local_addr()
    }

    /// Returns the socket address that the HTTP server is listening on.
    pub fn http_addr(&self) -> io::Result<SocketAddr> {
        self.http_server.local_addr()
    }

    /// Changes the "current working directory" of the HTTP server. The HTTP server will serve
    /// static file requests out of the new directory.
    pub fn change_working_directory<P>(&mut self, dir: P)
        where P: AsRef<Path>
    {
        self.http_server.change_working_directory(dir);
    }

    /// Publish new markdown to be rendered by the server.
    pub fn send<S>(&self, data: S)
        where S: Into<String>
    {
        self.markdown_sender.send(data.into()).unwrap()
    }
}

#[cfg(test)]
mod tests {
    use super::Server;

    #[test]
    fn sanity() {
        let mut server = Server::new();
        server.start();
    }
}