From 41e2df2fda1a7351bcf85240eac179627e6a7eb4 Mon Sep 17 00:00:00 2001 From: Nil Gradisnik Date: Tue, 5 Dec 2017 19:14:37 -0800 Subject: [PATCH] Added HTML template. HeaderBar buttons. --- Cargo.lock | 7 +++ Cargo.toml | 1 + src/gtk-ui.glade | 136 +++++++++++++++++++---------------------------- src/main.rs | 25 ++++++--- src/markdown.rs | 24 --------- src/preview.rs | 44 +++++++++++++++ 6 files changed, 125 insertions(+), 112 deletions(-) delete mode 100644 src/markdown.rs create mode 100644 src/preview.rs diff --git a/Cargo.lock b/Cargo.lock index 72c34e2..b6d900f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -334,6 +334,11 @@ dependencies = [ "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "horrorshow" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "idna" version = "0.1.4" @@ -389,6 +394,7 @@ dependencies = [ "comrak 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "gio 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "gtk 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "horrorshow 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "sourceview 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -738,6 +744,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum gtk 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0847c507e52c1feaede13ef56fb4847742438602655449d5f1f782e8633f146f" "checksum gtk-source-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2b893d266bbc13de37ecd4ab7125761ce4cb924b1cf4603e73f14d13ca7d3a9c" "checksum gtk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "905fcfbaaad1b44ec0b4bba9e4d527d728284c62bc2ba41fccedace2b096766f" +"checksum horrorshow 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d587765115bf4e613f0c88e3fcad077dc9b1a339de89912f6a75f21049938bce" "checksum idna 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "014b298351066f1512874135335d62a789ffe78a9974f94b43ed5621951eaf7d" "checksum if_chain 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "61bb90bdd39e3af69b0172dfc6130f6cd6332bf040fbb9bdd4401d37adbd48b8" "checksum itertools 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d3f2be4da1690a039e9ae5fd575f706a63ad5a2120f161b1d653c9da3930dd21" diff --git a/Cargo.toml b/Cargo.toml index 70cee8e..e227b6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ license = "MIT" gio = "0.3" sourceview = "0.3" comrak = "0.2" +horrorshow = "0.6" [dependencies.gtk] version = "0.3" diff --git a/src/gtk-ui.glade b/src/gtk-ui.glade index aea537b..7bfa1e1 100644 --- a/src/gtk-ui.glade +++ b/src/gtk-ui.glade @@ -53,85 +53,6 @@ Author: Nil Gradisnik True False vertical - - - True - False - - - True - False - Open - True - Open - True - document-open - - - False - True - - - - - True - False - Render Markdown - True - Render - True - view-refresh - - - False - True - - - - - True - False - Live Markdown - Live - True - media-playback-start - - - False - True - - - - - True - False - False - False - - - True - True - - - - - True - False - True - gtk-about - - - False - False - - - - - False - True - 0 - - True @@ -203,7 +124,62 @@ Author: Nil Gradisnik - + + True + False + True + + + True + False + Open + True + Open + True + document-open + + + + + True + False + Render + True + Render + True + view-refresh + + + 1 + + + + + True + False + Live + Live + True + media-playback-start + + + 2 + + + + + True + False + About + True + gtk-about + + + end + 3 + + + diff --git a/src/main.rs b/src/main.rs index da9a694..6e5e7fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,8 +4,10 @@ extern crate gio; extern crate gtk; extern crate sourceview; extern crate comrak; +#[macro_use] +extern crate horrorshow; -mod markdown; +mod preview; use std::env::args; use std::fs::File; @@ -15,8 +17,7 @@ use std::io::BufReader; use gio::prelude::*; use gtk::prelude::*; use gtk::Builder; - -use markdown::{string_to_html, buffer_to_html}; +use gtk::TextBuffer; const NAME: &str = env!("CARGO_PKG_NAME"); const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -81,7 +82,7 @@ fn build_ui(application: >k::Application) { let _ = reader.read_to_string(&mut contents); text_view.get_buffer().unwrap().set_text(&contents); - markdown_view.get_buffer().unwrap().set_text(&string_to_html(&contents)); + markdown_view.get_buffer().unwrap().set_text(&preview::render(&contents)); } file_chooser.hide(); @@ -89,15 +90,15 @@ fn build_ui(application: >k::Application) { text_view.connect_key_release_event(clone!(text_view, markdown_view, live_button => move |_, _| { if live_button.get_active() { - let buffer = text_view.get_buffer().unwrap(); - markdown_view.get_buffer().unwrap().set_text(&buffer_to_html(&buffer)); + let markdown = buffer_to_string(text_view.get_buffer()); + markdown_view.get_buffer().unwrap().set_text(&preview::render(&markdown)); } Inhibit(true) })); render_button.connect_clicked(clone!(text_view, markdown_view => move |_| { - let buffer = text_view.get_buffer().unwrap(); - markdown_view.get_buffer().unwrap().set_text(&buffer_to_html(&buffer)); + let markdown = buffer_to_string(text_view.get_buffer()); + markdown_view.get_buffer().unwrap().set_text(&preview::render(&markdown)); })); about_button.connect_clicked(clone!(about_dialog => move |_| { @@ -117,6 +118,14 @@ fn build_ui(application: >k::Application) { window.show_all(); } +fn buffer_to_string(buffer: Option) -> String { + let buffer = buffer.unwrap(); + let (start, end) = buffer.get_bounds(); + let text = buffer.get_text(&start, &end, false); + + text.unwrap() +} + fn main() { let application = gtk::Application::new("com.github.markdown-rs", gio::ApplicationFlags::empty()) .expect("Initialization failed..."); diff --git a/src/markdown.rs b/src/markdown.rs deleted file mode 100644 index 80d6144..0000000 --- a/src/markdown.rs +++ /dev/null @@ -1,24 +0,0 @@ -// https://github.com/kivikakk/comrak - -use gtk::prelude::*; -use gtk::TextBuffer; - -use comrak::{markdown_to_html, ComrakOptions}; - -pub fn string_to_html(text: &str) -> String { - let options = ComrakOptions { - hardbreaks: true, - ext_table: true, - ext_strikethrough: true, - ..ComrakOptions::default() - }; - - markdown_to_html(text, &options) -} - -pub fn buffer_to_html(buffer: &TextBuffer) -> String { - let (start, end) = buffer.get_bounds(); - let text = buffer.get_text(&start, &end, false); - - string_to_html(&text.unwrap()) -} diff --git a/src/preview.rs b/src/preview.rs new file mode 100644 index 0000000..d0fba92 --- /dev/null +++ b/src/preview.rs @@ -0,0 +1,44 @@ + +// https://github.com/Stebalien/horrorshow-rs +use horrorshow::Raw; +use horrorshow::helper::doctype; + +// https://github.com/kivikakk/comrak +use comrak::{markdown_to_html, ComrakOptions}; + +pub fn string_to_html(markdown: &str) -> String { + let options = ComrakOptions { + hardbreaks: true, + ext_table: true, + ext_strikethrough: true, + ..ComrakOptions::default() + }; + + markdown_to_html(markdown, &options) +} + +pub fn render(markdown: &str) -> String { + format!( + "{}", + html!( + : doctype::HTML; + html { + head { + link(rel="stylesheet", href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css") {} + script(src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js") {} + script(src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/languages/rust.min.js") {} + script { + : Raw("hljs.initHighlightingOnLoad()") + } + style { + : "body { width: 80%; margin: 0 auto }"; + : "img { max-width: 80% }" + } + } + body { + : Raw(&string_to_html(markdown)); + } + } + ) + ) +} -- 2.39.2