Skip to content

Commit

Permalink
Implement placeholder escaping, renderer cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
acuteenvy committed Nov 29, 2023
1 parent 2dc7a81 commit 9982bc2
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ fn highlight(start: &str, end: &str, s: &str, style_normal: Style, style_hl: Sty
// 3: "dd" (highlighted)
// 4: " ee"
if i % 2 == 0 {
buf.push_str(&style_normal.paint(spl).to_string());
buf += &style_normal.paint(spl).to_string();
} else {
buf.push_str(&style_hl.paint(spl).to_string());
buf += &style_hl.paint(spl).to_string();
}
} else if spl.contains(end) {
// The first part of the second split contains the part to be highlighted.
Expand All @@ -53,14 +53,14 @@ fn highlight(start: &str, end: &str, s: &str, style_normal: Style, style_hl: Sty
// "<http" is used to detect documentation URLs and it is removed during split(),
// we have to add it back again.
if end == ">" {
buf.push_str(&style_hl.paint("http").to_string());
buf += &style_hl.paint("http").to_string();
}

buf.push_str(&style_hl.paint(spl2.next().unwrap()).to_string());
buf.push_str(&style_normal.paint(spl2.next().unwrap()).to_string());
buf += &style_hl.paint(spl2.next().unwrap()).to_string();
buf += &style_normal.paint(spl2.next().unwrap()).to_string();
} else {
// Highlight ending not found.
buf.push_str(&style_normal.paint(spl).to_string());
buf += &style_normal.paint(spl).to_string();
}
}

Expand Down Expand Up @@ -184,17 +184,19 @@ impl<'a> PageRenderer<'a> {
Cow::Borrowed(line)
};

Ok(write!(
write!(
self.stdout,
"{}{}",
" ".repeat(self.cfg.indent.title),
self.style.title.paint(title)
)?)
)?;

Ok(())
}

/// Write the current line to the page buffer as a description.
fn add_desc(&mut self) -> Result<()> {
Ok(write!(
write!(
self.stdout,
"{}{}",
" ".repeat(self.cfg.indent.description),
Expand All @@ -211,7 +213,9 @@ impl<'a> PageRenderer<'a> {
self.style.desc,
self.style.inline_code,
)
)?)
)?;

Ok(())
}

/// Write the current line to the page buffer as a bullet point.
Expand All @@ -224,7 +228,7 @@ impl<'a> PageRenderer<'a> {
self.current_line.strip_prefix(BULLET).unwrap()
};

Ok(write!(
write!(
self.stdout,
"{}{}",
" ".repeat(self.cfg.indent.bullet),
Expand All @@ -235,12 +239,21 @@ impl<'a> PageRenderer<'a> {
self.style.bullet,
self.style.inline_code,
)
)?)
)?;

Ok(())
}

/// Write the current line to the page buffer as an example.
fn add_example(&mut self) -> Result<()> {
Ok(writeln!(
// Add spaces around escaped curly braces in order not to
// interpret them as a placeholder (e.g. in "\{\{{{ }}\}\}").
self.current_line = self
.current_line
.replace("\\{\\{", " \\{\\{ ")
.replace("\\}\\}", " \\}\\} ");

writeln!(
self.stdout,
"{}{}",
" ".repeat(self.cfg.indent.example),
Expand All @@ -259,14 +272,20 @@ impl<'a> PageRenderer<'a> {
self.style.example,
self.style.placeholder,
)
)?)
// Remove the extra spaces and backslashes.
.replace(" \\{\\{ ", "{{")
.replace(" \\}\\} ", "}}")
)?;

Ok(())
}

/// Write a newline to the page buffer if compact mode is not turned on.
fn add_newline(&mut self) -> Result<()> {
if !self.cfg.output.compact {
writeln!(self.stdout)?;
}

Ok(())
}

Expand All @@ -291,6 +310,7 @@ impl<'a> PageRenderer<'a> {
);
}
}

self.add_newline()?;
Ok(self.stdout.flush()?)
}
Expand Down

0 comments on commit 9982bc2

Please sign in to comment.