Skip to content

Commit c26b66e

Browse files
committed
rustdoc: Add a custom callback for codespan to collapse whitespace
Because the current style for `code` in rustdoc is to prewrap whitespace, code spans that are hard wrapped in the source documentation are prematurely wrapped when rendered in HTML. CommonMark 0.18 [[1]] specifies "interior spaces and line endings are collapsed into single spaces" for code spans, which would actually prevent this issue, but hoedown does not currently conform to the CommonMark spec. The added span-level callback attempts to adhere to how whitespace is handled as described by CommonMark, fixing the issue of early, unintentional wrapping of code spans in rendered HTML. [1]: http://spec.commonmark.org/0.18/
1 parent 9f37ba6 commit c26b66e

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ type blockcodefn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
7272
type headerfn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
7373
libc::c_int, *mut libc::c_void);
7474

75+
type codespanfn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
76+
*mut libc::c_void);
77+
7578
type linkfn = extern "C" fn (*mut hoedown_buffer, *const hoedown_buffer,
7679
*const hoedown_buffer, *const hoedown_buffer,
7780
*mut libc::c_void) -> libc::c_int;
@@ -89,11 +92,12 @@ struct hoedown_renderer {
8992
blockhtml: Option<extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
9093
*mut libc::c_void)>,
9194
header: Option<headerfn>,
92-
9395
other_block_level_callbacks: [libc::size_t; 9],
9496

9597
/* span level callbacks - NULL or return 0 prints the span verbatim */
96-
other_span_level_callbacks_1: [libc::size_t; 9],
98+
autolink: libc::size_t, // unused
99+
codespan: Option<codespanfn>,
100+
other_span_level_callbacks_1: [libc::size_t; 7],
97101
link: Option<linkfn>,
98102
other_span_level_callbacks_2: [libc::size_t; 5],
99103
// hoedown will add `math` callback here, but we use an old version of it.
@@ -185,6 +189,30 @@ fn stripped_filtered_line<'a>(s: &'a str) -> Option<&'a str> {
185189
}
186190
}
187191

192+
/// Returns a new string with all consecutive whitespace collapsed into
193+
/// single spaces.
194+
///
195+
/// The input is assumed to be already trimmed.
196+
fn collapse_whitespace(s: &str) -> String {
197+
let mut buffer = String::with_capacity(s.len());
198+
let mut previous_char_is_whitespace = false;
199+
200+
for c in s.chars() {
201+
if c.is_whitespace() {
202+
if !previous_char_is_whitespace {
203+
buffer.push(' ');
204+
}
205+
206+
previous_char_is_whitespace = true;
207+
} else {
208+
buffer.push(c);
209+
previous_char_is_whitespace = false;
210+
}
211+
}
212+
213+
buffer
214+
}
215+
188216
thread_local!(static USED_HEADER_MAP: RefCell<HashMap<String, usize>> = {
189217
RefCell::new(HashMap::new())
190218
});
@@ -299,6 +327,20 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
299327

300328
reset_headers();
301329

330+
extern fn codespan(ob: *mut hoedown_buffer, text: *const hoedown_buffer, _: *mut libc::c_void) {
331+
let content = if text.is_null() {
332+
"".to_string()
333+
} else {
334+
let bytes = unsafe { (*text).as_bytes() };
335+
let s = str::from_utf8(bytes).unwrap();
336+
collapse_whitespace(s)
337+
};
338+
339+
let content = format!("<code>{}</code>", Escape(&content));
340+
let element = CString::new(content).unwrap();
341+
unsafe { hoedown_buffer_puts(ob, element.as_ptr()); }
342+
}
343+
302344
unsafe {
303345
let ob = hoedown_buffer_new(DEF_OUNIT);
304346
let renderer = hoedown_html_renderer_new(0, 0);
@@ -310,6 +352,7 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
310352
= &mut opaque as *mut _ as *mut libc::c_void;
311353
(*renderer).blockcode = Some(block);
312354
(*renderer).header = Some(header);
355+
(*renderer).codespan = Some(codespan);
313356

314357
let document = hoedown_document_new(renderer, HOEDOWN_EXTENSIONS, 16);
315358
hoedown_document_render(document, ob, s.as_ptr(),
@@ -571,4 +614,17 @@ mod tests {
571614
t("# top header", "top header");
572615
t("## header", "header");
573616
}
617+
618+
#[test]
619+
fn test_collapse_whitespace() {
620+
fn t(input: &str, expected: &str) {
621+
let actual = collapse_whitespace(input);
622+
assert_eq!(actual, expected);
623+
}
624+
625+
t("foo", "foo");
626+
t("foo bar", "foo bar");
627+
t("foo bar\nbaz", "foo bar baz");
628+
t("foo bar \n baz\t\tqux", "foo bar baz qux");
629+
}
574630
}

0 commit comments

Comments
 (0)