From 9375939d1adfdd0af676eb2e0f1b432e110b3ade Mon Sep 17 00:00:00 2001 From: Nil Gradisnik Date: Sat, 2 Dec 2017 15:04:53 -0800 Subject: [PATCH] Using sourceview. Live render added. --- Cargo.lock | 43 +++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/gtk-ui.glade | 53 ++++++++++++++++++++++++++++++++---------------- src/main.rs | 28 ++++++++++++++++--------- src/markdown.rs | 14 +++++++++++-- 5 files changed, 110 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 809148c..5853701 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -299,6 +299,23 @@ dependencies = [ "pango 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "gtk-source-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cairo-sys-rs 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gdk-pixbuf-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gdk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gio-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gtk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "gtk-sys" version = "0.5.0" @@ -372,6 +389,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)", + "sourceview 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -524,6 +542,29 @@ dependencies = [ "serde 1.0.23 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "sourceview" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cairo-rs 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gdk 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gdk-pixbuf 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gdk-pixbuf-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gdk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gio 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gio-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "glib 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gtk 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gtk-source-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gtk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", + "pango 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "strsim" version = "0.6.0" @@ -695,6 +736,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9693049613ff52b93013cc3d2590366d8e530366d288438724b73f6c7dc4be8" "checksum gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "60d507c87a71b1143c66ed21a969be9b99a76df234b342d733e787e6c9c7d7c2" "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 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" @@ -723,6 +765,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum serde_derive 1.0.23 (registry+https://github.com/rust-lang/crates.io-index)" = "0672de7300b02bac3f3689f8faea813c4a1ea9fe0cb49e80f714231d267518a2" "checksum serde_derive_internals 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = "32f1926285523b2db55df263d2aa4eb69ddcfa7a7eade6430323637866b513ab" "checksum serde_json 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "ea28ea0cca944668919bec6af209864a8dfe769fd2b0b723f36b22e20c1bf69f" +"checksum sourceview 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "673844d773083b34c3cb3dab036c75eebb7b223343724f167915980a4586912e" "checksum strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d15c810519a91cf877e7e36e63fe068815c678181439f2f29e2562147c3694" "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" diff --git a/Cargo.toml b/Cargo.toml index 59b5b35..a726562 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Nil Gradisnik "] [dependencies] gio = "0.3" +sourceview = "0.3" comrak = "0.2" [dependencies.gtk] diff --git a/src/gtk-ui.glade b/src/gtk-ui.glade index 34c6a10..48a004e 100644 --- a/src/gtk-ui.glade +++ b/src/gtk-ui.glade @@ -28,6 +28,7 @@ Author: Nil Gradisnik --> + @@ -67,10 +68,24 @@ Author: Nil Gradisnik True False - Open + Render Markdown True Render True + view-refresh + + + False + True + + + + + True + False + Live Markdown + Live + True media-playback-start @@ -86,34 +101,38 @@ Author: Nil Gradisnik - + True - False - True + True True True in + 200 - + True True word - 4 - 4 - 4 - 4 - 2 + 2 + 2 + 2 + 2 GTK_INPUT_HINT_SPELLCHECK | GTK_INPUT_HINT_WORD_COMPLETION | GTK_INPUT_HINT_NONE True + True + 4 + True + True + True + grid - False - True - 0 + True + False @@ -121,6 +140,7 @@ Author: Nil Gradisnik True True in + 200 True @@ -137,9 +157,8 @@ Author: Nil Gradisnik - False - True - 1 + True + False @@ -151,7 +170,7 @@ Author: Nil Gradisnik - + diff --git a/src/main.rs b/src/main.rs index 92d2019..e397327 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ extern crate gio; extern crate gtk; +extern crate sourceview; extern crate comrak; mod markdown; @@ -15,6 +16,8 @@ use gio::prelude::*; use gtk::prelude::*; use gtk::Builder; +use markdown::{string_to_html, buffer_to_html}; + // http://gtk-rs.org/tuto/closures macro_rules! clone { (@param _) => ( _ ); @@ -43,11 +46,12 @@ fn build_ui(application: >k::Application) { let open_button: gtk::ToolButton = builder.get_object("open_button").expect("Couldn't get builder"); let render_button: gtk::ToolButton = builder.get_object("render_button").expect("Couldn't get builder"); + let live_button: gtk::ToggleToolButton = builder.get_object("live_button").expect("Couldn't get builder"); - let text_view: gtk::TextView = builder.get_object("text_view").expect("Couldn't get text_view"); + let text_view: sourceview::View = builder.get_object("text_view").expect("Couldn't get text_view"); let markdown_view: gtk::TextView = builder.get_object("markdown_view").expect("Couldn't get text_view"); - open_button.connect_clicked(clone!(window, text_view => move |_| { + open_button.connect_clicked(clone!(window, text_view, markdown_view => move |_| { let file_chooser = gtk::FileChooserDialog::new(Some("Open File"), Some(&window), gtk::FileChooserAction::Open); file_chooser.add_buttons(&[ ("Open", gtk::ResponseType::Ok.into()), @@ -61,19 +65,24 @@ fn build_ui(application: >k::Application) { let mut contents = String::new(); let _ = reader.read_to_string(&mut contents); - text_view.get_buffer().expect("Couldn't get window").set_text(&contents); + text_view.get_buffer().unwrap().set_text(&contents); + markdown_view.get_buffer().unwrap().set_text(&string_to_html(contents)); } file_chooser.destroy(); })); - render_button.connect_clicked(clone!(text_view => move |_| { - let buffer = text_view.get_buffer().expect("Error getting text"); - let (start, end) = buffer.get_bounds(); - let text = buffer.get_text(&start, &end, false); + 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)); + } + Inhibit(true) + })); - let html = markdown::to_html(text); - markdown_view.get_buffer().expect("Couldn't get window").set_text(&html); + 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)); })); window.connect_delete_event(clone!(window => move |_, _| { @@ -84,7 +93,6 @@ fn build_ui(application: >k::Application) { window.show_all(); } - 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 index 87a7bea..2b18874 100644 --- a/src/markdown.rs +++ b/src/markdown.rs @@ -1,8 +1,11 @@ // https://github.com/kivikakk/comrak +use gtk::prelude::*; +use gtk::TextBuffer; + use comrak::{markdown_to_html, ComrakOptions}; -pub fn to_html(text: Option) -> String { +pub fn string_to_html(text: String) -> String { let options = ComrakOptions { hardbreaks: true, ext_table: true, @@ -10,5 +13,12 @@ pub fn to_html(text: Option) -> String { ..ComrakOptions::default() }; - markdown_to_html(&text.unwrap(), &options) + 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()) } -- 2.39.2