Skip to content

Commit 082dba8

Browse files
committed
Unescape brackets in bit description tables
The text is only processed as HTML, so mdBook helpfully escaping special characters is actually unhelpful.
1 parent c673c12 commit 082dba8

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

preproc/src/preproc.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,9 @@ impl Pandocs {
368368
while pos < attrs.width {
369369
let (len, is_unused, name) = match fields.peek() {
370370
// If we are at the edge of a "used" field, use it
371-
Some(field) if field.start == pos => (field.len, false, field.name),
371+
Some(field) if field.start == pos => {
372+
(field.len, false, field.name.as_ref())
373+
}
372374
// If in an unused field, end at the next field, or the width if none such
373375
res => (res.map_or(attrs.width, |field| field.start) - pos, true, ""),
374376
};
@@ -434,7 +436,7 @@ fn find_bit_descrs(
434436
#[derive(Debug)]
435437
struct BitDescrAttrs<'input> {
436438
width: usize,
437-
rows: Vec<(&'input str, Vec<BitDescrField<'input>>)>,
439+
rows: Vec<(Cow<'input, str>, Vec<BitDescrField<'input>>)>,
438440
increasing: bool,
439441
}
440442

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

488+
fn undo_escapes(escaped: &str) -> Cow<'_, str> {
489+
lazy_static! {
490+
static ref RE: Regex = Regex::new(r#"\\(\[|\])"#).unwrap();
491+
}
492+
RE.replace_all(escaped, "$1")
493+
}
494+
486495
fn parse_name(row_str: &str) -> Option<usize> {
487496
if !row_str.starts_with('"') {
488497
return None;
@@ -496,7 +505,7 @@ impl<'input> BitDescrAttrs<'input> {
496505
"Expected row to begin by its name (did you forget to put quotes around it?)"
497506
);
498507
};
499-
let name = &row_str[1..(name_len + 1)];
508+
let name = undo_escapes(&row_str[1..(name_len + 1)]);
500509
let mut row_str = row_str[(name_len + 2)..].trim_start(); // The end is already trimmed.
501510

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

520529
// Perform some sanity checks.
521530
let Some((mut start, len)) = if increasing {
@@ -584,7 +593,7 @@ impl<'input> BitDescrAttrs<'input> {
584593
struct BitDescrField<'a> {
585594
start: usize,
586595
len: usize,
587-
name: &'a str,
596+
name: Cow<'a, str>,
588597
}
589598

590599
impl BitDescrField<'_> {

0 commit comments

Comments
 (0)