From 1923b9901d1c18283f6778eb6945e3d9591af3f2 Mon Sep 17 00:00:00 2001 From: Yorhel Date: Mon, 16 Jan 2017 09:52:32 +0100 Subject: [PATCH] Support bold+italic in HTML conversion --- web/src/lib.rs | 59 ++++++++++++++++++++++++++++++++++++-------------- www/man.css | 3 ++- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/web/src/lib.rs b/web/src/lib.rs index 5f1fa97..c9a02b2 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -12,6 +12,37 @@ enum FmtChar { Regular, Italic, Bold, + Both, +} + +impl FmtChar { + fn add(self, b: Self) -> Self { + match (self, b) { + (FmtChar::Regular, x) | + (x, FmtChar::Regular) => x, + (FmtChar::Italic, FmtChar::Bold) | + (FmtChar::Bold, FmtChar::Italic) => FmtChar::Both, + _ => self + } + } + + fn open(self) -> &'static str { + match self { + FmtChar::Regular => "", + FmtChar::Italic => "", + FmtChar::Bold => "", + FmtChar::Both => "", + } + } + + fn close(self) -> &'static str { + match self { + FmtChar::Regular => "", + FmtChar::Italic => "", + FmtChar::Bold => "", + FmtChar::Both => "", + } + } } @@ -57,9 +88,14 @@ impl CharParse { Some((c, f)) }, - CharParse::Escape(c, _) => { - // TODO: Handle combination of bold & italic - *self = CharParse::Token(chr, if c == '_' { FmtChar::Italic } else { FmtChar::Bold }); + CharParse::Escape(c, f) => { + *self = if c == '_' { + CharParse::Token(chr, f.add(FmtChar::Italic)) + } else if chr == '_' { + CharParse::Token(c, f.add(FmtChar::Italic)) + } else { + CharParse::Token(chr, f.add(FmtChar::Bold)) + }; None }, } @@ -68,11 +104,9 @@ impl CharParse { fn pushfmt(out: &mut String, old: FmtChar, new: FmtChar) { - if new != old && old != FmtChar::Regular { - out.push_str(if old == FmtChar::Italic { "" } else { "" }); - } - if new != old && new != FmtChar::Regular { - out.push_str(if new == FmtChar::Italic { "" } else { "" }); + if new != old { + out.push_str(old.close()); + out.push_str(new.open()); } } @@ -133,7 +167,7 @@ impl FmtBuf { st.idx = chunk; lastfmt = fmt; } - pushfmt(st.out, lastfmt, FmtChar::Regular); + st.out.push_str(lastfmt.close()); } // Consume the input buffer until 'end' without generating output @@ -276,13 +310,6 @@ pub fn grotty2html(input: &str) -> String { for chr in input.chars() { if let Some((chr, fmt)) = state.update(chr) { buf.push(chr, fmt); - // Line-based flushing is also possible, but not as fast. - //if chr == '\n' { - // buf.flush(&mut out); - // buf.buf.clear(); - // buf.fmt.clear(); - // buf.lastfmt = FmtChar::Regular; - //} } } if let CharParse::Token(chr, fmt) = state { diff --git a/www/man.css b/www/man.css index 8753e60..95d0aaf 100644 --- a/www/man.css +++ b/www/man.css @@ -120,7 +120,8 @@ i.grayedout { color: #aaa; font-size: 13px; } #footer a:hover { background: none; } pre, pre * { font-family: "Lucida Console", Monospace; font-size: 15px } -pre b, pre a { color: #369; font-weight: normal; text-decoration: none } +pre b, pre em, pre a { color: #369; font-weight: normal; text-decoration: none } +pre em { font-style: italic } #ds_box { position: absolute; top: 0; border: 1px solid $border$; border-top: none; background: #f0f8ff; cursor: pointer; z-index: 2 } #ds_box b { padding: 2px 0 0 10px; font-size: 12px; }