Skip to content

Commit 09353f1

Browse files
committed
fix: random touch ups
1 parent d0765ed commit 09353f1

File tree

4 files changed

+183
-398
lines changed

4 files changed

+183
-398
lines changed

tui-markdown/src/lib.rs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ struct TextWriter<'a, I> {
5656
/// Stack of line styles.
5757
line_styles: Vec<Style>,
5858

59-
#[cfg(feature = "highlight-code")]
6059
/// Used to highlight code blocks, set when a codeblock is encountered
61-
current_code_highlighter: Option<HighlightLines<'a>>,
60+
#[cfg(feature = "highlight-code")]
61+
code_highlighter: Option<HighlightLines<'a>>,
6262

6363
/// Current list index as a stack of indices.
6464
list_indices: Vec<Option<u64>>,
@@ -67,10 +67,9 @@ struct TextWriter<'a, I> {
6767
}
6868

6969
#[cfg(feature = "highlight-code")]
70-
static SYNTAX_SET: LazyLock<SyntaxSet> =
71-
std::sync::LazyLock::new(SyntaxSet::load_defaults_newlines);
70+
static SYNTAX_SET: LazyLock<SyntaxSet> = LazyLock::new(SyntaxSet::load_defaults_newlines);
7271
#[cfg(feature = "highlight-code")]
73-
static THEME_SET: LazyLock<ThemeSet> = std::sync::LazyLock::new(ThemeSet::load_defaults);
72+
static THEME_SET: LazyLock<ThemeSet> = LazyLock::new(ThemeSet::load_defaults);
7473

7574
impl<'a, I> TextWriter<'a, I>
7675
where
@@ -86,7 +85,7 @@ where
8685
list_indices: vec![],
8786
needs_newline: false,
8887
#[cfg(feature = "highlight-code")]
89-
current_code_highlighter: None,
88+
code_highlighter: None,
9089
}
9190
}
9291

@@ -219,9 +218,9 @@ where
219218

220219
fn text(&mut self, text: CowStr<'a>) {
221220
#[cfg(feature = "highlight-code")]
222-
if let Some(h) = &mut self.current_code_highlighter {
221+
if let Some(highlighter) = &mut self.code_highlighter {
223222
let text: Text = LinesWithEndings::from(&text)
224-
.filter_map(|line| h.highlight_line(line, &SYNTAX_SET).ok())
223+
.filter_map(|line| highlighter.highlight_line(line, &SYNTAX_SET).ok())
225224
.filter_map(|part| as_24_bit_terminal_escaped(&part, false).into_text().ok())
226225
.flatten()
227226
.collect();
@@ -300,27 +299,47 @@ where
300299
CodeBlockKind::Fenced(ref lang) => lang.as_ref(),
301300
CodeBlockKind::Indented => "",
302301
};
303-
if !cfg!(feature = "highlight-code") {
304-
self.line_styles.push(styles::CODE);
305-
self.push_line(Line::default());
306-
self.needs_newline = true;
307-
}
308302

309-
let span = Span::from(format!("```{}", lang));
310-
self.push_line(span.into());
303+
#[cfg(not(feature = "highlight-code"))]
304+
self.line_styles.push(styles::CODE);
311305

312306
#[cfg(feature = "highlight-code")]
313-
self.set_current_code_highlighter(lang);
307+
self.set_code_highlighter(lang);
308+
309+
let span = Span::from(format!("```{lang}"));
310+
self.push_line(span.into());
311+
self.needs_newline = true;
314312
}
315313

316314
fn end_codeblock(&mut self) {
317315
let span = Span::from("```");
318316
self.push_line(span.into());
319-
self.line_styles.pop();
320317
self.needs_newline = true;
321318

319+
#[cfg(not(feature = "highlight-code"))]
320+
self.line_styles.pop();
321+
322322
#[cfg(feature = "highlight-code")]
323-
self.clear_current_code_highlighter();
323+
self.clear_code_highlighter();
324+
}
325+
326+
#[cfg(feature = "highlight-code")]
327+
#[instrument(level = "trace", skip(self))]
328+
fn set_code_highlighter(&mut self, lang: &str) {
329+
if let Some(syntax) = SYNTAX_SET.find_syntax_by_token(lang) {
330+
debug!("Starting code block with syntax: {:?}", lang);
331+
let theme = &THEME_SET.themes["base16-ocean.dark"];
332+
let highlighter = HighlightLines::new(syntax, theme);
333+
self.code_highlighter = Some(highlighter);
334+
} else {
335+
warn!("Could not find syntax for code block: {:?}", lang);
336+
}
337+
}
338+
339+
#[cfg(feature = "highlight-code")]
340+
#[instrument(level = "trace", skip(self))]
341+
fn clear_code_highlighter(&mut self) {
342+
self.code_highlighter = None;
324343
}
325344

326345
#[instrument(level = "trace", skip(self))]
@@ -362,25 +381,6 @@ where
362381
self.push_line(Line::from(vec![span]));
363382
}
364383
}
365-
366-
#[cfg(feature = "highlight-code")]
367-
#[instrument(level = "trace", skip(self))]
368-
fn set_current_code_highlighter(&mut self, lang: &str) {
369-
let syntax = SYNTAX_SET.find_syntax_by_token(lang);
370-
if let Some(syntax) = syntax {
371-
debug!("Starting code block with syntax: {:?}", lang);
372-
let mut h = HighlightLines::new(syntax, &THEME_SET.themes["base16-ocean.dark"]);
373-
self.current_code_highlighter = Some(h);
374-
} else {
375-
warn!("Could not find syntax for code block: {:?}", lang);
376-
}
377-
}
378-
379-
#[cfg(feature = "highlight-code")]
380-
#[instrument(level = "trace", skip(self))]
381-
fn clear_current_code_highlighter(&mut self) {
382-
self.current_code_highlighter = None;
383-
}
384384
}
385385

386386
mod styles {

0 commit comments

Comments
 (0)