]> OzVa Git service - gn-editor/commitdiff
Added HTML template. HeaderBar buttons.
authorNil Gradisnik <nil@layer.com>
Wed, 6 Dec 2017 03:14:37 +0000 (19:14 -0800)
committerNil Gradisnik <nil@layer.com>
Wed, 6 Dec 2017 03:14:37 +0000 (19:14 -0800)
Cargo.lock
Cargo.toml
src/gtk-ui.glade
src/main.rs
src/markdown.rs [deleted file]
src/preview.rs [new file with mode: 0644]

index 72c34e2c4da9e13367cbb6dc8671cc463aea485a..b6d900f2cc117e51cc3378966c0337b0de841290 100644 (file)
@@ -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"
index 70cee8e953b95fb1f7b7399873cc56d48e9d24fc..e227b6e7a4716a2d0faa30e176686384cc2dc501 100644 (file)
@@ -11,6 +11,7 @@ license = "MIT"
 gio = "0.3"
 sourceview = "0.3"
 comrak = "0.2"
+horrorshow = "0.6"
 
 [dependencies.gtk]
 version = "0.3"
index aea537bdc672e38ceabdf3607ae81caf4c869d8d..7bfa1e103d0e698a99c249922b32f94bc6fe6084 100644 (file)
@@ -53,85 +53,6 @@ Author: Nil Gradisnik
         <property name="visible">True</property>
         <property name="can_focus">False</property>
         <property name="orientation">vertical</property>
-        <child>
-          <object class="GtkToolbar" id="toolbar">
-            <property name="visible">True</property>
-            <property name="can_focus">False</property>
-            <child>
-              <object class="GtkToolButton" id="open_button">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="tooltip_text" translatable="yes">Open</property>
-                <property name="is_important">True</property>
-                <property name="label" translatable="yes">Open</property>
-                <property name="use_underline">True</property>
-                <property name="icon_name">document-open</property>
-              </object>
-              <packing>
-                <property name="expand">False</property>
-                <property name="homogeneous">True</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkToolButton" id="render_button">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="tooltip_text" translatable="yes">Render Markdown</property>
-                <property name="is_important">True</property>
-                <property name="label" translatable="yes">Render</property>
-                <property name="use_underline">True</property>
-                <property name="icon_name">view-refresh</property>
-              </object>
-              <packing>
-                <property name="expand">False</property>
-                <property name="homogeneous">True</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkToggleToolButton" id="live_button">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="tooltip_text" translatable="yes">Live Markdown</property>
-                <property name="label" translatable="yes">Live</property>
-                <property name="use_underline">True</property>
-                <property name="icon_name">media-playback-start</property>
-              </object>
-              <packing>
-                <property name="expand">False</property>
-                <property name="homogeneous">True</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkSeparatorToolItem" id="spacer">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="visible_vertical">False</property>
-                <property name="draw">False</property>
-              </object>
-              <packing>
-                <property name="expand">True</property>
-                <property name="homogeneous">True</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkToolButton" id="about_button">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="use_underline">True</property>
-                <property name="stock_id">gtk-about</property>
-              </object>
-              <packing>
-                <property name="expand">False</property>
-                <property name="homogeneous">False</property>
-              </packing>
-            </child>
-          </object>
-          <packing>
-            <property name="expand">False</property>
-            <property name="fill">True</property>
-            <property name="position">0</property>
-          </packing>
-        </child>
         <child>
           <object class="GtkPaned">
             <property name="visible">True</property>
@@ -203,7 +124,62 @@ Author: Nil Gradisnik
       </object>
     </child>
     <child type="titlebar">
-      <placeholder/>
+      <object class="GtkHeaderBar" id="header_bar">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="show_close_button">True</property>
+        <child>
+          <object class="GtkToolButton" id="open_button">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="tooltip_text" translatable="yes">Open</property>
+            <property name="is_important">True</property>
+            <property name="label" translatable="yes">Open</property>
+            <property name="use_underline">True</property>
+            <property name="icon_name">document-open</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkToolButton" id="render_button">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="tooltip_text" translatable="yes">Render</property>
+            <property name="is_important">True</property>
+            <property name="label" translatable="yes">Render</property>
+            <property name="use_underline">True</property>
+            <property name="icon_name">view-refresh</property>
+          </object>
+          <packing>
+            <property name="position">1</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkToggleToolButton" id="live_button">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="tooltip_text" translatable="yes">Live</property>
+            <property name="label" translatable="yes">Live</property>
+            <property name="use_underline">True</property>
+            <property name="icon_name">media-playback-start</property>
+          </object>
+          <packing>
+            <property name="position">2</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkToolButton" id="about_button">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="tooltip_text" translatable="yes">About</property>
+            <property name="use_underline">True</property>
+            <property name="stock_id">gtk-about</property>
+          </object>
+          <packing>
+            <property name="pack_type">end</property>
+            <property name="position">3</property>
+          </packing>
+        </child>
+      </object>
     </child>
   </object>
   <object class="GtkAboutDialog" id="about_dialog">
index da9a694127845cab060d181922c3172c7c11298b..6e5e7fd0a70186a8cf2bf7ff719a2c0d36d518f6 100644 (file)
@@ -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: &gtk::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: &gtk::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: &gtk::Application) {
     window.show_all();
 }
 
+fn buffer_to_string(buffer: Option<TextBuffer>) -> 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 (file)
index 80d6144..0000000
+++ /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 (file)
index 0000000..d0fba92
--- /dev/null
@@ -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));
+                }
+            }
+        )
+    )
+}