@@ 52,7 52,7 @@ dependencies = [
[[package]]
name = "ogmarkup"
version = "0.1.0"
-source = "git+https://git.sr.ht/~lthms/ogmarkup?rev=20346a1b3f0e243ff93dc1894f57084346c496ba#20346a1b3f0e243ff93dc1894f57084346c496ba"
+source = "git+https://git.sr.ht/~lthms/ogmarkup?rev=feaf8d0cbc7aa34ac521020914df91489ef24595#feaf8d0cbc7aa34ac521020914df91489ef24595"
dependencies = [
"nom 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ 118,7 118,7 @@ dependencies = [
name = "varuna"
version = "0.1.0"
dependencies = [
- "ogmarkup 0.1.0 (git+https://git.sr.ht/~lthms/ogmarkup?rev=20346a1b3f0e243ff93dc1894f57084346c496ba)",
+ "ogmarkup 0.1.0 (git+https://git.sr.ht/~lthms/ogmarkup?rev=feaf8d0cbc7aa34ac521020914df91489ef24595)",
"wasm-bindgen 0.2.46 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ 188,7 188,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6"
"checksum memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2efc7bc57c883d4a4d6e3246905283d8dae951bb3bd32f49d6ef297f546e1c39"
"checksum nom 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e9761d859320e381010a4f7f8ed425f2c924de33ad121ace447367c713ad561b"
-"checksum ogmarkup 0.1.0 (git+https://git.sr.ht/~lthms/ogmarkup?rev=20346a1b3f0e243ff93dc1894f57084346c496ba)" = "<none>"
+"checksum ogmarkup 0.1.0 (git+https://git.sr.ht/~lthms/ogmarkup?rev=feaf8d0cbc7aa34ac521020914df91489ef24595)" = "<none>"
"checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
"checksum quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "faf4799c5d274f3868a4aae320a0a182cbd2baee377b378f080e16a23e9d80db"
"checksum ryu 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "b96a9549dc8d48f2c283938303c4b5a77aa29bfbc5b54b084fb1630408899a8f"
@@ 1,93 1,149 @@
use ogmarkup::typography::Space;
-use ogmarkup::generator::Renderer;
+use ogmarkup::generator::Output;
-pub struct Html;
+pub struct Html(String);
-impl<'input> Renderer<'input, String> for Html {
- fn append(&self, before: String, after: String) -> String {
- let mut before = before;
- before.push_str(after.as_ref());
-
- before
- }
-
- fn empty(&self) -> String {
- String::from("")
- }
-
- fn render_space(&self, space: Space) -> String {
- match space {
- Space::Normal => String::from(" "),
- Space::Nbsp => String::from(" "),
- Space::None => String::from(""),
- }
- }
-
- fn render_word(&self, word: &'input str) -> String {
- String::from(word)
- }
-
- fn render_mark(&self, mark: &'input str) -> String {
- String::from(mark)
- }
-
- fn render_illformed(&self, err: &'input str) -> String {
- String::from(err)
- }
-
- fn emph_template(&self, format: String) -> String {
- format!("<em>{}</em>", format)
- }
-
- fn strong_emph_template(&self, format: String) -> String {
- format!("<strong>{}</strong>", format)
+impl Html {
+ fn push_str(&mut self, s: &str) -> () {
+ self.0.push_str(s);
}
- fn reply_template(&self, reply: String, _author: &Option<&'input str>) -> String {
- format!("<span class=\"reply\">{}</span>", reply)
- }
-
- fn thought_template(&self, reply: String, author: &Option<&'input str>) -> String {
- format!(
- "<span class=\"thought{}\">{}</span>",
- author
- .map(|x| format!(" by-{}", x))
- .unwrap_or(String::from("")),
- reply,
- )
- }
-
- fn dialogue_template(&self, reply: String, author: &Option<&'input str>) -> String {
- format!(
- "<span class=\"dialogue{}\">{}</span>",
- author
- .map(|x| format!(" by-{}", x))
- .unwrap_or(String::from("")),
- reply,
- )
+ pub fn to_string(self) -> String {
+ self.0
}
+}
- fn between_dialogue(&self) -> String {
- String::from("</p><p>")
+impl Output for Html {
+ fn empty(input_size: usize) -> Html {
+ Html(String::with_capacity((15 * input_size) / 10))
}
- fn illformed_inline_template(&self, err: String) -> String {
- format!("<span class=\"illformed_inline\">{}</span>", err)
+ fn render_space(&mut self, space: Space) -> () {
+ self.push_str(match space {
+ Space::Normal => " ",
+ Space::Nbsp => " ",
+ Space::None => "",
+ })
}
- fn paragraph_template(&self, para: String) -> String {
- format!("<p>{}</p>", para)
+ fn render_word(&mut self, word: & str) -> () {
+ self.push_str(word)
}
- fn illformed_block_template(&self, err: String) -> String {
- format!("<div class=\"illformed_block\">{}</div>", err)
+ fn render_mark(&mut self, mark: &str) -> () {
+ self.push_str(mark)
}
- fn story_template(&self, story: String) -> String {
- format!("<div class=\"story\">{}</div>", story)
+ fn render_illformed(&mut self, err: &str) -> () {
+ self.push_str(err)
}
- fn aside_template(&self, cls: &Option<&'input str>, aside: String) -> String {
- format!("<div class=\"aside {}\">{}</div>", cls.unwrap_or(""), aside)
+ fn emph_template<F>(&mut self, format: F) -> ()
+ where
+ F : FnOnce(&mut Html) -> ()
+ {
+ self.push_str("<em>");
+ format(self);
+ self.push_str("</em>");
+ }
+
+ fn strong_emph_template<F>(&mut self, format: F) -> ()
+ where
+ F : FnOnce(&mut Html) -> ()
+ {
+ self.push_str("<strong>");
+ format(self);
+ self.push_str("</strong>");
+ }
+
+ fn reply_template<F>(&mut self, reply: F, _author: &Option<&str>) -> ()
+ where
+ F : FnOnce(&mut Html) -> ()
+ {
+ self.push_str("<span class=\"reply\">");
+ reply(self);
+ self.push_str("</span>");
+ }
+
+ fn thought_template<F>(&mut self, reply: F, author: &Option<&str>) -> ()
+ where
+ F : FnOnce(&mut Html) -> ()
+ {
+ self.push_str("<span class=\"thought");
+ author.map(|a| {
+ self.push_str(" by-");
+ self.push_str(a);
+ });
+ self.push_str("\">");
+ reply(self);
+ self.push_str("</span>");
+ }
+
+ fn dialogue_template<F>(&mut self, reply: F, author: &Option<&str>) -> ()
+ where
+ F : FnOnce(&mut Html) -> ()
+ {
+ self.push_str("<span class=\"dialogue");
+ author.map(|a| {
+ self.push_str(" by-");
+ self.push_str(a);
+ });
+ self.push_str("\">");
+ reply(self);
+ self.push_str("</span>");
+ }
+
+ fn between_dialogue(&mut self) -> () {
+ self.push_str("</p><p>");
+ }
+
+ fn illformed_inline_template<F>(&mut self, err: F) -> ()
+ where
+ F: FnOnce(&mut Html) -> ()
+ {
+ self.push_str("<span class=\"illformed_inline\">");
+ err(self);
+ self.push_str("</span>");
+ }
+
+ fn paragraph_template<F>(&mut self, para: F) -> ()
+ where
+ F : FnOnce(&mut Html) -> ()
+ {
+ self.push_str("<p>");
+ para(self);
+ self.push_str("</p>");
+ }
+
+ fn illformed_block_template<F>(&mut self, err: F) -> ()
+ where
+ F : FnOnce(&mut Html) -> ()
+ {
+ self.push_str("<div class=\"illformed_block\">");
+ err(self);
+ self.push_str("</div>");
+ }
+
+ fn story_template<F>(&mut self, story: F) -> ()
+ where
+ F : FnOnce(&mut Html) -> ()
+ {
+ self.push_str("<div class=\"story\">");
+ story(self);
+ self.push_str("</div>");
+ }
+
+ fn aside_template<F>(&mut self, cls: &Option<&str>, aside: F) -> ()
+ where
+ F : FnOnce(&mut Html) -> ()
+ {
+ self.push_str("<div class=\"aside");
+ cls.map(|c| {
+ self.push_str(" ");
+ self.push_str(c);
+ });
+ self.push_str("\">");
+ aside(self);
+ self.push_str("</div>");
}
}