mirror of
https://github.com/rust-lang/book.git
synced 2026-05-17 21:21:55 -04:00
Merge main into only-new-async
This commit is contained in:
61
ferris.js
61
ferris.js
@@ -1,4 +1,11 @@
|
||||
var ferrisTypes = [
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* @typedef {{ attr: string, title: string }} FerrisType
|
||||
*/
|
||||
|
||||
/** @type {Array<FerrisType>} */
|
||||
const FERRIS_TYPES = [
|
||||
{
|
||||
attr: 'does_not_compile',
|
||||
title: 'This code does not compile!'
|
||||
@@ -14,46 +21,74 @@ var ferrisTypes = [
|
||||
]
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
for (var ferrisType of ferrisTypes) {
|
||||
for (let ferrisType of FERRIS_TYPES) {
|
||||
attachFerrises(ferrisType)
|
||||
}
|
||||
})
|
||||
|
||||
/**
|
||||
* @param {FerrisType} type
|
||||
*/
|
||||
function attachFerrises(type) {
|
||||
var elements = document.getElementsByClassName(type.attr)
|
||||
let elements = document.getElementsByClassName(type.attr)
|
||||
|
||||
for (var codeBlock of elements) {
|
||||
var lines = codeBlock.innerText.replace(/\n$/, '').split(/\n/).length
|
||||
var size = 'large'
|
||||
if (lines < 4) {
|
||||
size = 'small'
|
||||
for (let codeBlock of elements) {
|
||||
// Skip SVG etc.: in principle, these should never be attached to those, but
|
||||
// this means if someone happens to have a browser extension which *is*
|
||||
// attaching them, it will not break the code.
|
||||
if (!(codeBlock instanceof HTMLElement)) {
|
||||
continue
|
||||
}
|
||||
|
||||
let lines = codeBlock.innerText.replace(/\n$/, '').split(/\n/).length
|
||||
|
||||
/** @type {'small' | 'large'} */
|
||||
let size = lines < 4 ? 'small' : 'large'
|
||||
|
||||
let container = prepareFerrisContainer(codeBlock, size == 'small')
|
||||
if (!container) {
|
||||
continue
|
||||
}
|
||||
|
||||
var container = prepareFerrisContainer(codeBlock, size == 'small')
|
||||
container.appendChild(createFerris(type, size))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} element - Code block element to attach a Ferris to.
|
||||
* @param {boolean} useButtons - Whether to attach to existing buttons.
|
||||
* @returns {Element | null} - The container element to use.
|
||||
*/
|
||||
function prepareFerrisContainer(element, useButtons) {
|
||||
var foundButtons = element.parentElement.querySelector('.buttons')
|
||||
let foundButtons = element.parentElement?.querySelector('.buttons')
|
||||
if (useButtons && foundButtons) {
|
||||
return foundButtons
|
||||
}
|
||||
|
||||
var div = document.createElement('div')
|
||||
let div = document.createElement('div')
|
||||
div.classList.add('ferris-container')
|
||||
|
||||
if (!element.parentElement) {
|
||||
console.error(`Could not install Ferris on ${element}, which is missing a parent`);
|
||||
return null;
|
||||
}
|
||||
|
||||
element.parentElement.insertBefore(div, element)
|
||||
|
||||
return div
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {FerrisType} type
|
||||
* @param {'small' | 'large'} size
|
||||
* @returns {HTMLAnchorElement} - The generated anchor element.
|
||||
*/
|
||||
function createFerris(type, size) {
|
||||
var a = document.createElement('a')
|
||||
let a = document.createElement('a')
|
||||
a.setAttribute('href', 'ch00-00-introduction.html#ferris')
|
||||
a.setAttribute('target', '_blank')
|
||||
|
||||
var img = document.createElement('img')
|
||||
let img = document.createElement('img')
|
||||
img.setAttribute('src', 'img/ferris/' + type.attr + '.svg')
|
||||
img.setAttribute('title', type.title)
|
||||
img.classList.add('ferris')
|
||||
|
||||
1170
packages/mdbook-trpl-listing/Cargo.lock
generated
Normal file
1170
packages/mdbook-trpl-listing/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -2,9 +2,10 @@ use mdbook::{
|
||||
book::Book,
|
||||
errors::Result,
|
||||
preprocess::{Preprocessor, PreprocessorContext},
|
||||
utils::new_cmark_parser,
|
||||
BookItem,
|
||||
};
|
||||
use pulldown_cmark::{html, Event, Parser};
|
||||
use pulldown_cmark::{html, Event};
|
||||
use pulldown_cmark_to_cmark::cmark;
|
||||
use xmlparser::{Token, Tokenizer};
|
||||
|
||||
@@ -138,7 +139,7 @@ impl TryFrom<&str> for Mode {
|
||||
}
|
||||
|
||||
fn rewrite_listing(src: &str, mode: Mode) -> Result<String, String> {
|
||||
let parser = Parser::new(src);
|
||||
let parser = new_cmark_parser(src, true);
|
||||
|
||||
struct State<'e> {
|
||||
current_listing: Option<Listing>,
|
||||
@@ -339,7 +340,7 @@ impl<'a> ListingBuilder<'a> {
|
||||
let caption = self
|
||||
.caption
|
||||
.map(|caption_source| {
|
||||
let events = Parser::new(caption_source);
|
||||
let events = new_cmark_parser(caption_source, true);
|
||||
let mut buf = String::with_capacity(caption_source.len() * 2);
|
||||
html::push_html(&mut buf, events);
|
||||
|
||||
|
||||
1087
packages/mdbook-trpl-note/Cargo.lock
generated
Normal file
1087
packages/mdbook-trpl-note/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -2,11 +2,12 @@ use mdbook::{
|
||||
book::Book,
|
||||
errors::Result,
|
||||
preprocess::{Preprocessor, PreprocessorContext},
|
||||
utils::new_cmark_parser,
|
||||
BookItem,
|
||||
};
|
||||
use pulldown_cmark::{
|
||||
Event::{self, *},
|
||||
Parser, Tag, TagEnd,
|
||||
Tag, TagEnd,
|
||||
};
|
||||
use pulldown_cmark_to_cmark::cmark;
|
||||
|
||||
@@ -53,7 +54,7 @@ impl Preprocessor for TrplNote {
|
||||
}
|
||||
|
||||
pub fn rewrite(text: &str) -> String {
|
||||
let parser = Parser::new(text);
|
||||
let parser = new_cmark_parser(text, true);
|
||||
|
||||
let mut events = Vec::new();
|
||||
let mut state = Default;
|
||||
@@ -301,8 +302,43 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normal_table() {
|
||||
let text = "| Header 1 | Header 2 |\n| -------- | -------- |\n| Text 123 | More 456 |";
|
||||
let processed = rewrite(text);
|
||||
|
||||
assert_eq!(
|
||||
processed,
|
||||
"|Header 1|Header 2|\n|--------|--------|\n|Text 123|More 456|",
|
||||
"It strips some whitespace but otherwise leaves the table intact."
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_in_note() {
|
||||
let text = "> Note: table stuff.\n\n| Header 1 | Header 2 |\n| -------- | -------- |\n| Text 123 | More 456 |";
|
||||
let processed = rewrite(text);
|
||||
|
||||
assert_eq!(
|
||||
processed,
|
||||
"\n\n<section class=\"note\" aria-role=\"note\">\n\nNote: table stuff.\n\n</section>\n\n|Header 1|Header 2|\n|--------|--------|\n|Text 123|More 456|",
|
||||
"It adds the note markup but leaves the table untouched, to be rendered as Markdown."
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_in_quote() {
|
||||
let text = "> A table.\n\n| Header 1 | Header 2 |\n| -------- | -------- |\n| Text 123 | More 456 |";
|
||||
let processed = rewrite(text);
|
||||
assert_eq!(
|
||||
render_markdown(&processed),
|
||||
"<blockquote>\n<p>A table.</p>\n</blockquote>\n<table><thead><tr><th>Header 1</th><th>Header 2</th></tr></thead><tbody>\n<tr><td>Text 123</td><td>More 456</td></tr>\n</tbody></table>\n",
|
||||
"It renders blockquotes with nested tables as expected."
|
||||
);
|
||||
}
|
||||
|
||||
fn render_markdown(text: &str) -> String {
|
||||
let parser = Parser::new(text);
|
||||
let parser = new_cmark_parser(text, true);
|
||||
let mut buf = String::new();
|
||||
pulldown_cmark::html::push_html(&mut buf, parser);
|
||||
buf
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
figure.listing {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.listing figcaption {
|
||||
font-size: .8em;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user