1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 05:40:03 -04:00

Replace markdown editor with new 'EasyMark' markup language

This commit is contained in:
Emil Ernerfeldt
2021-01-28 23:50:23 +01:00
parent b647592a5a
commit 16d66bd22d
11 changed files with 593 additions and 281 deletions

View File

@@ -16,7 +16,6 @@ impl Default for Demos {
Box::new(super::dancing_strings::DancingStrings::default()),
Box::new(super::drag_and_drop::DragAndDropDemo::default()),
Box::new(super::font_book::FontBook::default()),
Box::new(super::markdown_editor::MarkdownEditor::default()),
Box::new(super::painting::Painting::default()),
Box::new(super::scrolling::Scrolling::default()),
Box::new(super::sliders::Sliders::default()),

View File

@@ -1,66 +0,0 @@
use egui::*;
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
#[derive(PartialEq)]
pub struct MarkdownEditor {
markdown: String,
}
impl Default for MarkdownEditor {
fn default() -> Self {
Self {
markdown: r#"
# Markdown editor
Markdown support in egui is experimental, and *very* limited. There are:
* bullet points
* with sub-points
* `inline code`
* *emphasis*
* [hyperlinks](https://github.com/emilk/egui)
---
Also the separator
"#
.trim_start()
.to_owned(),
}
}
}
impl super::Demo for MarkdownEditor {
fn name(&self) -> &str {
"🖹 Markdown Editor"
}
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
egui::Window::new(self.name()).open(open).show(ctx, |ui| {
use super::View;
self.ui(ui);
});
}
}
impl super::View for MarkdownEditor {
fn ui(&mut self, ui: &mut egui::Ui) {
ui.vertical_centered(|ui| {
egui::reset_button(ui, self);
ui.add(crate::__egui_github_link_file!());
});
ui.separator();
ui.columns(2, |columns| {
ScrollArea::auto_sized()
.id_source("source")
.show(&mut columns[0], |ui| {
ui.text_edit_multiline(&mut self.markdown);
});
ScrollArea::auto_sized()
.id_source("rendered")
.show(&mut columns[1], |ui| {
egui::experimental::markdown(ui, &self.markdown);
});
});
}
}

View File

@@ -14,7 +14,6 @@ pub mod font_contents_emoji;
pub mod font_contents_ubuntu;
pub mod input_test;
pub mod layout_test;
pub mod markdown_editor;
pub mod painting;
pub mod scrolling;
pub mod sliders;

View File

@@ -0,0 +1,117 @@
use egui::*;
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
#[derive(PartialEq)]
pub struct EasyMarkEditor {
code: String,
}
impl Default for EasyMarkEditor {
fn default() -> Self {
Self {
code: DEFAULT_CODE.trim().to_owned(),
}
}
}
impl epi::App for EasyMarkEditor {
fn name(&self) -> &str {
"🖹 EasyMark editor"
}
fn update(&mut self, ctx: &egui::CtxRef, _frame: &mut epi::Frame<'_>) {
egui::CentralPanel::default().show(ctx, |ui| {
self.ui(ui);
});
}
}
impl EasyMarkEditor {
fn ui(&mut self, ui: &mut egui::Ui) {
ui.vertical_centered(|ui| {
egui::reset_button(ui, self);
ui.add(crate::__egui_github_link_file!());
});
ui.separator();
ui.columns(2, |columns| {
ScrollArea::auto_sized()
.id_source("source")
.show(&mut columns[0], |ui| {
// ui.text_edit_multiline(&mut self.code);
ui.add(TextEdit::multiline(&mut self.code).text_style(TextStyle::Monospace));
});
ScrollArea::auto_sized()
.id_source("rendered")
.show(&mut columns[1], |ui| {
egui::experimental::easy_mark(ui, &self.code);
});
});
}
}
const DEFAULT_CODE: &str = r#"
# EasyMark
EasyMark is a markup language, designed for extreme simplicity.
```
WARNING: EasyMark is still an evolving specification,
and is also missing some features.
```
----------------
# At a glance
- inline text:
- normal, `code`, *strong*, ~strikethrough~, _underline_, /italics/
- `\` escapes the next character
- [hyperlink](https://github.com/emilk/egui)
- Embedded URL: <https://github.com/emilk/egui>
- `# ` header
- `---` separator (horizontal line)
- `> ` quote
- `- ` bullet list
- `1. ` numbered list
- \`\`\` code fence
# Design
> /"Why do what everyone else is doing, when everyone else is already doing it?"
> \- Emil
Goals:
1. easy to parse
2. easy to learn
3. similar to markdown
[The reference parser](https://github.com/emilk/egui/blob/master/egui/src/experimental/easy_mark_parser.rs) is \~250 lines of code, using only the Rust standard library. The parser uses no look-ahead or recursion.
There is never more than one way to accomplish the same thing, and each special character is only used for one thing. For instance `*` is used for *strong* and `-` is used for bullet lists. There is no alternative way to specify the *strong* style or getting a bullet list.
Similarity to markdown is kept when possible, but with much less ambiguity and some improvements (like _underlining_).
# Details
All style changes are single characters, so it is `*strong*`, NOT `**strong**`. Style is reset by a matching character, or at the end of the line.
Style change characters and escapes (`\`) work everywhere except for in inline code, code blocks and in URLs.
You can mix styles. For instance: /italics _underline_/ and *strong `code`*.
You can use styles on URLs: ~my webpage is at <http://www.example.com>~.
Newlines are preserved. If you want to continue text on the same line, just do so. Alternatively, escape the newline by ending the line with a backslash (`\`). \
Escaping the newline effectively ignores it.
The style characters are chosen to be similar to what they are representing:
`_` = _underline_
`~` = ~strikethrough~ (`-` is too common in normal text)
`/` = /italics/
`*` = *strong*
# TODO
- Sub-headers (`## h2`, `### h3` etc)
- Images
- we want to be able to optionally specify size (width and\/or height)
- centering of images is very desirable
- captioning (image with a text underneath it)
- `![caption=My image][width=200][center](url)` ?
- Tables
"#;

View File

@@ -1,11 +1,13 @@
mod color_test;
mod demo;
mod easy_mark_editor;
mod fractal_clock;
#[cfg(feature = "http")]
mod http_app;
pub use color_test::ColorTest;
pub use demo::DemoApp;
pub use easy_mark_editor::EasyMarkEditor;
pub use fractal_clock::FractalClock;
#[cfg(feature = "http")]
pub use http_app::HttpApp;

View File

@@ -4,6 +4,7 @@
#[cfg_attr(feature = "persistence", serde(default))]
pub struct Apps {
demo: crate::apps::DemoApp,
easy_mark_editor: crate::apps::EasyMarkEditor,
#[cfg(feature = "http")]
http: crate::apps::HttpApp,
clock: crate::apps::FractalClock,
@@ -14,6 +15,7 @@ impl Apps {
fn iter_mut(&mut self) -> impl Iterator<Item = (&str, &mut dyn epi::App)> {
vec![
("demo", &mut self.demo as &mut dyn epi::App),
("easymark", &mut self.easy_mark_editor as &mut dyn epi::App),
#[cfg(feature = "http")]
("http", &mut self.http as &mut dyn epi::App),
("clock", &mut self.clock as &mut dyn epi::App),