Skip to content

Commit

Permalink
Unescape brackets in bit description tables
Browse files Browse the repository at this point in the history
The text is only processed as HTML, so mdBook helpfully
escaping special characters is actually unhelpful.
  • Loading branch information
ISSOtm committed Nov 24, 2023
1 parent c673c12 commit 082dba8
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions preproc/src/preproc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,9 @@ impl Pandocs {
while pos < attrs.width {
let (len, is_unused, name) = match fields.peek() {
// If we are at the edge of a "used" field, use it
Some(field) if field.start == pos => (field.len, false, field.name),
Some(field) if field.start == pos => {
(field.len, false, field.name.as_ref())
}
// If in an unused field, end at the next field, or the width if none such
res => (res.map_or(attrs.width, |field| field.start) - pos, true, ""),
};
Expand Down Expand Up @@ -434,7 +436,7 @@ fn find_bit_descrs(
#[derive(Debug)]
struct BitDescrAttrs<'input> {
width: usize,
rows: Vec<(&'input str, Vec<BitDescrField<'input>>)>,
rows: Vec<(Cow<'input, str>, Vec<BitDescrField<'input>>)>,
increasing: bool,
}

Expand Down Expand Up @@ -483,6 +485,13 @@ impl<'input> BitDescrAttrs<'input> {
for row_str in s.split_terminator(';') {
let row_str = row_str.trim();

fn undo_escapes(escaped: &str) -> Cow<'_, str> {
lazy_static! {
static ref RE: Regex = Regex::new(r#"\\(\[|\])"#).unwrap();
}
RE.replace_all(escaped, "$1")
}

fn parse_name(row_str: &str) -> Option<usize> {
if !row_str.starts_with('"') {
return None;
Expand All @@ -496,7 +505,7 @@ impl<'input> BitDescrAttrs<'input> {
"Expected row to begin by its name (did you forget to put quotes around it?)"
);
};
let name = &row_str[1..(name_len + 1)];
let name = undo_escapes(&row_str[1..(name_len + 1)]);
let mut row_str = row_str[(name_len + 2)..].trim_start(); // The end is already trimmed.

// Then, the fields!
Expand All @@ -515,7 +524,7 @@ impl<'input> BitDescrAttrs<'input> {
let right = cap
.get(2)
.map_or(left, |end_match| end_match.as_str().parse().unwrap());
let name = &cap.get(3).unwrap().as_str();
let name = undo_escapes(&cap.get(3).unwrap().as_str());

// Perform some sanity checks.
let Some((mut start, len)) = if increasing {
Expand Down Expand Up @@ -584,7 +593,7 @@ impl<'input> BitDescrAttrs<'input> {
struct BitDescrField<'a> {
start: usize,
len: usize,
name: &'a str,
name: Cow<'a, str>,
}

impl BitDescrField<'_> {
Expand Down

0 comments on commit 082dba8

Please sign in to comment.