|
| 1 | +// Write Markdown to the terminal |
| 2 | + |
| 3 | +use crate::term2::{color, Attr, Terminal}; |
| 4 | +use markdown::tokenize; |
| 5 | +use markdown::{Block, ListItem, Span}; |
| 6 | +use std::io; |
| 7 | + |
| 8 | +// Handles the wrapping of text written to the console |
| 9 | +struct LineWrapper<'a, T: Terminal> { |
| 10 | + indent: u32, |
| 11 | + margin: u32, |
| 12 | + pos: u32, |
| 13 | + pub w: &'a mut T, |
| 14 | +} |
| 15 | + |
| 16 | +impl<'a, T: Terminal + 'a> LineWrapper<'a, T> { |
| 17 | + // Just write a newline |
| 18 | + fn write_line(&mut self) { |
| 19 | + let _ = writeln!(self.w); |
| 20 | + // Reset column position to start of line |
| 21 | + self.pos = 0; |
| 22 | + } |
| 23 | + // Called before writing text to ensure indent is applied |
| 24 | + fn write_indent(&mut self) { |
| 25 | + if self.pos == 0 { |
| 26 | + // Write a space for each level of indent |
| 27 | + for _ in 0..self.indent { |
| 28 | + let _ = write!(self.w, " "); |
| 29 | + } |
| 30 | + self.pos = self.indent; |
| 31 | + } |
| 32 | + } |
| 33 | + // Write a non-breaking word |
| 34 | + fn write_word(&mut self, word: &str) { |
| 35 | + // Ensure correct indentation |
| 36 | + self.write_indent(); |
| 37 | + let word_len = word.len() as u32; |
| 38 | + |
| 39 | + // If this word goes past the margin |
| 40 | + if self.pos + word_len > self.margin { |
| 41 | + // And adding a newline would give us more space |
| 42 | + if self.pos > self.indent { |
| 43 | + // Then add a newline! |
| 44 | + self.write_line(); |
| 45 | + self.write_indent(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Write the word |
| 50 | + let _ = write!(self.w, "{}", word); |
| 51 | + self.pos += word_len; |
| 52 | + } |
| 53 | + fn write_space(&mut self) { |
| 54 | + if self.pos > self.indent { |
| 55 | + if self.pos < self.margin { |
| 56 | + self.write_word(" "); |
| 57 | + } else { |
| 58 | + self.write_line(); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + // Writes a span of text which wraps at the margin |
| 63 | + fn write_span(&mut self, text: &str) { |
| 64 | + // Allow words to wrap on whitespace |
| 65 | + let mut is_first = true; |
| 66 | + for word in text.split(char::is_whitespace) { |
| 67 | + if is_first { |
| 68 | + is_first = false; |
| 69 | + } else { |
| 70 | + self.write_space(); |
| 71 | + } |
| 72 | + self.write_word(word); |
| 73 | + } |
| 74 | + } |
| 75 | + // Constructor |
| 76 | + fn new(w: &'a mut T, indent: u32, margin: u32) -> Self { |
| 77 | + LineWrapper { |
| 78 | + indent, |
| 79 | + margin, |
| 80 | + pos: indent, |
| 81 | + w, |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// Handles the formatting of text |
| 87 | +struct LineFormatter<'a, T: Terminal + io::Write> { |
| 88 | + wrapper: LineWrapper<'a, T>, |
| 89 | + attrs: Vec<Attr>, |
| 90 | +} |
| 91 | + |
| 92 | +impl<'a, T: Terminal + io::Write + 'a> LineFormatter<'a, T> { |
| 93 | + fn new(w: &'a mut T, indent: u32, margin: u32) -> Self { |
| 94 | + LineFormatter { |
| 95 | + wrapper: LineWrapper::new(w, indent, margin), |
| 96 | + attrs: Vec::new(), |
| 97 | + } |
| 98 | + } |
| 99 | + fn push_attr(&mut self, attr: Attr) { |
| 100 | + self.attrs.push(attr); |
| 101 | + let _ = self.wrapper.w.attr(attr); |
| 102 | + } |
| 103 | + fn pop_attr(&mut self) { |
| 104 | + self.attrs.pop(); |
| 105 | + let _ = self.wrapper.w.reset(); |
| 106 | + for attr in &self.attrs { |
| 107 | + let _ = self.wrapper.w.attr(*attr); |
| 108 | + } |
| 109 | + } |
| 110 | + fn do_spans(&mut self, spans: Vec<Span>) { |
| 111 | + for span in spans { |
| 112 | + match span { |
| 113 | + Span::Break => {} |
| 114 | + Span::Text(text) => { |
| 115 | + self.wrapper.write_span(&text); |
| 116 | + } |
| 117 | + Span::Code(code) => { |
| 118 | + self.push_attr(Attr::Bold); |
| 119 | + self.wrapper.write_word(&code); |
| 120 | + self.pop_attr(); |
| 121 | + } |
| 122 | + Span::Emphasis(spans) => { |
| 123 | + self.push_attr(Attr::ForegroundColor(color::BRIGHT_RED)); |
| 124 | + self.do_spans(spans); |
| 125 | + self.pop_attr(); |
| 126 | + } |
| 127 | + _ => {} |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + fn do_block(&mut self, b: Block) { |
| 132 | + match b { |
| 133 | + Block::Header(spans, _) => { |
| 134 | + self.push_attr(Attr::Bold); |
| 135 | + self.wrapper.write_line(); |
| 136 | + self.do_spans(spans); |
| 137 | + self.wrapper.write_line(); |
| 138 | + self.pop_attr(); |
| 139 | + } |
| 140 | + Block::CodeBlock(code) => { |
| 141 | + self.wrapper.write_line(); |
| 142 | + self.wrapper.indent += 2; |
| 143 | + for line in code.lines() { |
| 144 | + // Don't word-wrap code lines |
| 145 | + self.wrapper.write_word(line); |
| 146 | + self.wrapper.write_line(); |
| 147 | + } |
| 148 | + self.wrapper.indent -= 2; |
| 149 | + } |
| 150 | + Block::Paragraph(spans) => { |
| 151 | + self.wrapper.write_line(); |
| 152 | + self.do_spans(spans); |
| 153 | + self.wrapper.write_line(); |
| 154 | + } |
| 155 | + Block::UnorderedList(items) => { |
| 156 | + self.wrapper.write_line(); |
| 157 | + for item in items { |
| 158 | + self.wrapper.indent += 2; |
| 159 | + match item { |
| 160 | + ListItem::Simple(spans) => { |
| 161 | + self.do_spans(spans); |
| 162 | + } |
| 163 | + ListItem::Paragraph(blocks) => { |
| 164 | + for block in blocks { |
| 165 | + self.do_block(block); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + self.wrapper.write_line(); |
| 170 | + self.wrapper.indent -= 2; |
| 171 | + } |
| 172 | + } |
| 173 | + _ => {} |
| 174 | + } |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +pub fn md<'a, S: AsRef<str>, T: Terminal + io::Write + 'a>(t: &'a mut T, content: S) { |
| 179 | + let mut f = LineFormatter::new(t, 0, 79); |
| 180 | + let blocks = tokenize(content.as_ref()); |
| 181 | + for b in blocks { |
| 182 | + f.do_block(b); |
| 183 | + } |
| 184 | +} |
0 commit comments