Skip to content

Commit

Permalink
Display note as markup
Browse files Browse the repository at this point in the history
  • Loading branch information
sanpii committed Jan 5, 2018
1 parent 5f3a232 commit 492f8d9
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ gdk = "^0.7"
gdk-sys = "^0.5"
glib = "^0.4"
log = "^0.4"
pulldown-cmark = "^0.1"
rand = "^0.4"
regex = "^0.2"
relm = "^0.11"
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern crate gdk;
extern crate gdk_sys;
extern crate glib;
extern crate gtk;
extern crate pulldown_cmark;
extern crate rand;
extern crate regex;
#[macro_use]
Expand Down
62 changes: 62 additions & 0 deletions src/tasks/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,68 @@ impl Note
}
}

pub fn markup(&self) -> Option<String>
{
let content = match self.content() {
Some(content) => content,
None => return None,
};

let mut parser = ::pulldown_cmark::Parser::new(&content);

let mut markup = String::from("<markup>");

let headers = vec![
"xx-large",
"x-large",
"large",
"medium",
"small",
"x-small",
"xx-small",
];

while let Some(event) = parser.next() {
use ::pulldown_cmark::Event;
use ::pulldown_cmark::Tag;

match event {
Event::Start(Tag::Header(level)) => markup.push_str(&format!("<span font_size='{}'><u>", headers[level as usize])),
Event::End(Tag::Header(_)) => markup.push_str("</u></span>\n\n"),

Event::Start(Tag::Paragraph) => markup.push_str("<span>"),
Event::End(Tag::Paragraph) => markup.push_str("</span>\n"),

Event::Start(Tag::Code) => markup.push_str("<tt>"),
Event::End(Tag::Code) => markup.push_str("</tt>"),

Event::Start(Tag::Emphasis) => markup.push_str("<i>"),
Event::End(Tag::Emphasis) => markup.push_str("</i>"),

Event::Start(Tag::Strong) => markup.push_str("<b>"),
Event::End(Tag::Strong) => markup.push_str("</b>"),

Event::Start(Tag::Item) => markup.push_str("· "),
Event::End(Tag::Item) => markup.push_str("\n"),

Event::Start(Tag::Link(link, title)) => markup.push_str(&format!("<a href='{}' title='{}'>", link, title)),
Event::End(Tag::Link(_, _)) => markup.push_str("</a>"),

Event::Start(Tag::CodeBlock(_)) => markup.push_str("<tt>"),
Event::End(Tag::CodeBlock(_)) => markup.push_str("</tt>"),

Event::Text(t) => markup.push_str(&t),

Event::SoftBreak => markup.push_str("\n"),

_ => (),
}
}
markup.push_str("</markup>");

Some(markup)
}

pub fn write(&self) -> Result<Self, String>
{
let mut note = self.clone();
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ impl ::relm::Widget for Task
let note_label = ::gtk::Label::new(None);
note_label.show();

if let Some(ref note) = task.note.content() {
note_label.set_text(note);
if let Some(ref note) = task.note.markup() {
note_label.set_markup(note);
}

let note = ::gtk::Popover::new(None::<&::gtk::Button>);
Expand Down

0 comments on commit 492f8d9

Please sign in to comment.