book icon indicating copy to clipboard operation
book copied to clipboard

Convert all code listings to use new `<Listing>` preprocessor

Open chriskrycho opened this issue 1 year ago • 29 comments

Background

As of #3918, we have a preprocessor that allows us to author with a custom HTML tag, Listing, roughly as if it were a component in a templating language. This input:

<Listing number="1-1" file-name="src/lib.rs" caption="A listing showing how to use a `Listing`">

```rust
fn main() {
    println!("Hello, listing!");
}
```

</Listing>

…will generate this output in the regular book (and strip all the tags in the NoStarch book):

<figure class="listing">
<span class="file-name">Filename: src/lib.rs</span>
<pre><pre class="playground"><code class="language-rust">fn main() {
    println!("Hello, listing!");
}</code></pre></pre>
<figcaption>Listing 1-2: A listing showing how to use a <code>Listing</code></figcaption>
</figure>

As described in the PR adding support for this, the result is more accessible HTML, which will also give us a nice way to hook in for styling things better if we so choose.

Contributing

When converting to a <Listing>, you can drop the leading Listing <number>: from the caption arg, since it handles that automatically with the number arg.

Do add a <Listing> for all code blocks (including rust, text, console, etc.) which have either or both of:

  • an explicit listing number, in the form <span class="caption">Listing XX-YY: …</span>
  • an explicit file name, in the form <span class="filename">Filename: src/main.rs</span>

Do not add a <Listing> for code blocks which do not have at least one or the other of those.

If you’d like to help, please leave a comment below noting which chapter you’d like to pick up so folks don’t do duplicate work! If it already has a user handle by it, please don’t work on that chapter.

  • [x] Chapter 1 (@davidkurilla - #3924)
  • [x] Chapter 2 (@davidkurilla – #3926)
  • [x] Chapter 3 (@davidkurilla – #3926)
  • [x] Chapter 4 (@jpmelos – #4043)
  • [ ] Chapter 5 (@jpmelos – #4051)
  • [ ] Chapter 6 (@SpectralPixel)
  • [ ] Chapter 7 (@SpectralPixel)
  • [ ] Chapter 8 (@SpectralPixel)
  • [ ] Chapter 9 (@SpectralPixel)
  • [ ] Chapter 10 (@SpectralPixel)
  • [x] Chapter 11 (@bzierk – #3955)
  • [x] Chapter 12 (@bzierk – #3959)
  • [x] Chapter 13 (@bzierk – #3959)
  • [x] Chapter 14 (@bzierk – #3959)
  • [ ] Chapter 15 (@bzierk)
  • [ ] Chapter 16 (@LifeAdventurer – #4066)
  • [x] Chapter 17 (@chriskrycho did this from the start while writing the new chapter!)
  • [x] Chapter 18 (@chriskrycho – #4060)
  • [x] Chapter 19 (@chriskrycho – #4060)
  • [x] Chapter 20 (@chriskrycho – #4060)
  • [x] Chapter 21 (@chriskrycho – #4060)

chriskrycho avatar May 14 '24 17:05 chriskrycho

I'd like to help with chapter 1.

davidkurilla avatar May 16 '24 00:05 davidkurilla

Thanks for the feedback for my pervious contribution! I plan to work on converting chapters 2 and 3

davidkurilla avatar May 16 '24 16:05 davidkurilla

Very good! Thank you! 💙

chriskrycho avatar May 16 '24 16:05 chriskrycho

I'll do chapters 6-10. Already working on it.

SpectralPixel avatar May 26 '24 11:05 SpectralPixel

I am working on converting chapter 11.

bzierk avatar Jun 12 '24 14:06 bzierk

I'm in the process of converting 12-15. It is mostly working but all captions which have angle brackets (eg Box<T>) are breaking the xml parser. Will work on handling the necessary escape characters.

bzierk avatar Jun 12 '24 22:06 bzierk

Interesting – I would not have expected that! 🤔 Let me know what you come up with.

chriskrycho avatar Jun 13 '24 16:06 chriskrycho

The issue appears to be that < is not a valid character in the body of an attribute in XML. These characters have to be escaped but with the ListingBuilder taking things by reference its a little trickier.

https://github.com/RazrFalcon/xmlparser/blob/e54c2768f20814140c11e6c92f94ee74bfd54808/src/lib.rs#L1037

bzierk avatar Jun 14 '24 03:06 bzierk

I've been quite busy over here, I hope to get back to work soon and finish the chapters.

SpectralPixel avatar Jun 14 '24 09:06 SpectralPixel

How do I build the mdbook with the Listing pre-processor? If I recall correctly, <Listing> support hasn't been upstreamed yet, so how do I test the book? Or is that responsibility offloaded to code reviewers? Thanks :)

SpectralPixel avatar Jun 26 '24 10:06 SpectralPixel

Also, what about code that is preceded by a filename, yet doesn't have a listing number? Are these also turned into <Listing>s that don't have a number or caption? (Edit: I'll do these as well for now, feel free to revert them later.)

SpectralPixel avatar Jun 26 '24 14:06 SpectralPixel

@SpectralPixel sorry, just saw these questions.

  1. For the listing preprocessor, it worked just fine in this repo anyway, and it has been fixed in the rust repo, so nothing to worry about there.
  2. Mmmm, those are interesting – we probably need to think about how <Listing> should handle those. I will poke at how many of those there are and figure out what we ought to do with them!

chriskrycho avatar Jul 10 '24 17:07 chriskrycho

Hmmm, I already converted those special <Listing>s, so I'll add them in my PR. Feel free to revert those commits if necessary though. I'll try to finish it all up this afternoon.

SpectralPixel avatar Jul 12 '24 14:07 SpectralPixel

Weird, mdbook seems not to work on my machine when I install it the way that is recommended for this repo... I guess I'll just send the PR so you can check if it works fine, it's probably just me doing something wrong with mdbook...

SpectralPixel avatar Jul 12 '24 16:07 SpectralPixel

@SpectralPixel if you consistently have issues with mdbook not working, would you open an issue? That way we can track fixing that—it’s important to us to make sure folks can contribute easily!

chriskrycho avatar Jul 13 '24 17:07 chriskrycho

Well, I'm not sure if I have it set up properly...

SpectralPixel avatar Jul 13 '24 19:07 SpectralPixel

@SpectralPixel @bzierk I believe #3975 should address the issues each of you were separately hitting in terms of what the <Listing> preprocessor supports!

chriskrycho avatar Jul 15 '24 23:07 chriskrycho

I did chapter 4 here: https://github.com/rust-lang/book/pull/4043.

I think some explanation about which listings need to be converted would be helpful:

  • Just the ones with listing number, caption, or file name? Or also the ones without any of those things?
  • Only Rust listings, or all of them (even the console ones, for example)?

jpmelos avatar Sep 26 '24 21:09 jpmelos

Thanks for the note, @jpmelos – I’ll update the issue description here. Basically, it should only be converted if it has an explicit listing number (<span class="caption">Listing XX-YY: …</span>), an explicit file name (<span class="filename">Filename: …</span>), or both. Otherwise, it isn’t a “listing” in the way we approach it, so should not be converted!

chriskrycho avatar Sep 27 '24 13:09 chriskrycho

I did chapter 05 here: https://github.com/rust-lang/book/pull/4051.

jpmelos avatar Oct 01 '24 19:10 jpmelos

@SpectralPixel would you like me to fix up those PRs you opened and get them across the line? (No worries; I have very often been the guy who started something and then life got busy!)

chriskrycho avatar Oct 09 '24 20:10 chriskrycho

Oh, I must've forgotten to revert those few commits. Should be only one per PR, otherwise they're all ready to go.

SpectralPixel avatar Oct 09 '24 20:10 SpectralPixel

I'd like to help with chapter 16.

LifeAdventurer avatar Oct 10 '24 15:10 LifeAdventurer

@LifeAdventurer go for it – I’ll mark you down on the list! Thanks!

chriskrycho avatar Oct 10 '24 18:10 chriskrycho

@SpectralPixel see the PRs—they’re all currently failing; I think it’s primarily a function of line-wrapping (see my comment on #3977 for more).

chriskrycho avatar Oct 10 '24 18:10 chriskrycho

I did all the line wrapping in at most one commit per PR, could you revert these? I'm quite busy at the moment. Other than that, the PRs should be ready to go. :)

SpectralPixel avatar Oct 10 '24 19:10 SpectralPixel

Ah, got it—that’ll work!

chriskrycho avatar Oct 10 '24 20:10 chriskrycho

@LifeAdventurer go for it – I’ll mark you down on the list! Thanks!

Thanks! I'll get started on it now.

LifeAdventurer avatar Oct 11 '24 14:10 LifeAdventurer

@chriskrycho While working on this, I noticed the changes in your PR #4060 for a different chapter and saw that a <span> tag might have been unintentionally left in the listing: https://github.com/rust-lang/book/blob/2399b906ca173c0fc23c71b0031cc9c75f1cd247/src/ch18-01-what-is-oo.md?plain=1#L61-L69

Also, both the text Listing 18-1: and Listing 18-2: still remain in the caption:

https://github.com/rust-lang/book/blob/2399b906ca173c0fc23c71b0031cc9c75f1cd247/src/ch18-01-what-is-oo.md?plain=1#L47 https://github.com/rust-lang/book/blob/2399b906ca173c0fc23c71b0031cc9c75f1cd247/src/ch18-01-what-is-oo.md?plain=1#L61

Could you check if they need to be removed? If needed, I can open another pull request to fix it.

LifeAdventurer avatar Oct 11 '24 14:10 LifeAdventurer

Thanks for noting that, @LifeAdventurer – I went ahead and fixed it this morning, and am reviewing your PR for ch. 16 now!

chriskrycho avatar Oct 15 '24 12:10 chriskrycho