quarto-dev / quarto-cli

Open-source scientific and technical publishing system built on Pandoc.
https://quarto.org
Other
3.97k stars 327 forks source link

Code annotations broken with Rust #8206

Closed sasja-san closed 10 months ago

sasja-san commented 10 months ago

Bug description

  1. Render this code both as HTML and PDF.
  2. Note how PDF renders in three different ways. HTML only two...
  3. Remove the C code, render again.
  4. HTML renders fine, but PDF breaks due to some include.
```c
int main()
{
  printf("Hello, World!\n"); /* <1> */
  return 0;                  /* <2> */
}
  1. Say hello.
  2. Leave.
fn main() {
    println!("Hello World!"); /* <1> */  
    return 0;                 /* <2> */
}
  1. Say hello.
  2. Leave.
fn main() {
    println!("Hello World!"); // <1>
    return 0;                 // <2>
}
  1. Say hello.
  2. Leave.

![quoarto-rs-c-bug](https://github.com/quarto-dev/quarto-cli/assets/712688/b93af192-a322-44e4-84a1-bd80e5c769ef)

### Quarto check output

Just did a pull from `main` branch.

$ quarto check Quarto 99.9.9 [✓] Checking versions of quarto binary dependencies... Pandoc version 3.1.11: OK Dart Sass version 1.69.5: OK Deno version 1.37.2: OK [✓] Checking versions of quarto dependencies......OK [✓] Checking Quarto installation......OK Version: 99.9.9 Path: /home/sasja/.quarto/quarto-cli/package/dist/bin

[✓] Checking tools....................OK TinyTeX: v2024.01 Chromium: 869685

[✓] Checking LaTeX....................OK Using: TinyTex Path: /home/sasja/.TinyTeX/bin/x86_64-linux Version: 2023

[✓] Checking basic markdown render....OK

[✓] Checking Python 3 installation....OK Version: 3.11.6 Path: /usr/bin/python3 Jupyter: 5.5.1 Kernels: python3

[✓] Checking Jupyter engine render....OK

[✓] Checking R installation...........OK Version: 4.3.2 Path: /usr/lib64/R LibPaths:

[✓] Checking Knitr engine render......OK

sasja-san commented 10 months ago

I was making a work-around and found out that the spacing to the annotation number marker matters. Maybe this is intended?

I have yet to find a way to make the actual annotation (numbered list) to be able to go to the margin while still having the circled numbers.

---
format:
    pdf:
      documentclass: scrartcl
      papersize: a4
      geometry:
        - showframe # Debugging purposes 
        - left=15.0mm
        - textwidth=102.0mm
        - marginparsep=7.7mm
        - marginparwidth=70.3mm

      code-annotations: true
      code-block-bg: "#FFFFE8"
      code-block-border-left: "#DDDDC8"
---

# Rust acting up *(as usual)*

lol might just do what (6) does, for all my numbers

::: {.column-margin}
1. The hardware 'dispatcher' whose interrupt will launch this function.
2. Global variable (a peripheral), only accessed from here.
3. Global variable, also accessed by others (thus "shared").
4. `cx` type is specific to this function. It has been generated through the RTIC macros.
5. Only way to access shared variable. Had this been many shared that needed simultaneous access then there would have been more arguments together with `tref`.
6. Launch the `main_task` isr, through software.
:::
```.rs
#[task(
    binds  = TIM4,                                                      // <1>
    local  = [ ms_timer ],                                // <2>
    shared = [ current_tick ])]    // <3>
fn milli_isr(                         // <4>
    mut cx: milli_isr::Context) {     // <4>

    let u = TimerInterrupt::Update
    cx.local.ms_timer.clear_interrupt(u);

    cx.shared.current_tick.lock(             // <5>
        |tref| { *tref += 1; });             // <5>

    let ev = Event::TickMillesec;
    main_task::spawn(ev).ok();                   // <6>
}

Can't get too close, though:

::: {.column-margin}

  1. The hardware 'dispatcher' whose interrupt will launch this function.
  2. Global variable (a peripheral), only accessed from here.
  3. Global variable, also accessed by others (thus "shared").
  4. cx type is specific to this function. It has been generated through the RTIC macros.
  5. Only way to access shared variable. Had this been many shared that needed simultaneous access then there would have been more arguments together with tref.
  6. Launch the main_task isr, through software. :::

    #[task(
    binds  = TIM4,               /* <1> */
    local  = [ ms_timer ],       /* <2> */
    shared = [ current_tick ])]  /* <3> */
    fn milli_irs(                    /* <4> */
    mut cx: milli_isr::Context) {/* <4> */
    
    let u = TimerInterrupt::Update
    cx.local.ms_timer.clear_interrupt(u);
    
    cx.shared.current_tick.lock( /* <5> */
        |tref| { *tref += 1; }); /* <5> */
    
    let ev = Event::TickMillesec;
    main_task::spawn(ev).ok();   /* <6> */
    }

\newpage

Mandatory C

int main()
{
  printf("Hello, World!\n"); /* <1> */
  return 0;                  /* <2> */
}
  1. Say hello.
  2. Leave.
dragonstyle commented 10 months ago

I've open a PR to fix the rust portion this issue, here's an example doc:

---
title: Hello World
code-annotations: true
format: pdf
---

```rust
fn main() {
    println!("Hello World!"); // <1>
    return 0;                 // <2>
}
  1. Say hello.
  2. Leave.


Code annotations aren't currently supported in the margins, though that is a great idea. Could you open a new issue to track adding support for that in a future release? We'll need to do some custom lifting to make that happen properly.
sasja-san commented 9 months ago

Did this really get fixed @dragonstyle? Try making a PDF out of this, remove the C code and then try again. At least I can't get it to work.


# C code

```c
int main();   /* <1> */

Rust code

#[task(local = [led], priority = 4)]                // <1>
fn blink(ctx: blink::Context)
{
    let p = match ctx.local.led.get_state()         // <2>
    {
        PinState::Low  => (PinState::High),
        PinState::High => (PinState::Low),
    }
    ctx.local.led.set_state(p);
    blink::spawn_after(1.secs()).ok();              // <3>
}

#[task(binds = EXTI15_10, local = [btn])]           // <4>
fn on_exti(ctx: on_exti::Context)
{
    defmt::println!("Button (PC13) was pressed!");
    ctx.local.btn.clear_interrupt_pending_bit();    // <5>
}
dragonstyle commented 9 months ago

Yes:


---
title: Hello World
format: pdf
---

# C code

```c
int main();   /* <1> */
  1. Hello

Rust code

#[task(local = [led], priority = 4)]                // <1>
fn blink(ctx: blink::Context)
{
    let p = match ctx.local.led.get_state()         // <2>
    {
        PinState::Low  => (PinState::High),
        PinState::High => (PinState::Low),
    }
    ctx.local.led.set_state(p);
    blink::spawn_after(1.secs()).ok();              // <3>
}

#[task(binds = EXTI15_10, local = [btn])]           // <4>
fn on_exti(ctx: on_exti::Context)
{
    defmt::println!("Button (PC13) was pressed!");
    ctx.local.btn.clear_interrupt_pending_bit();    // <5>
}
  1. Hello
  2. World
  3. This is the middle comment
  4. Comment
  5. This is the last comment
sasja-san commented 9 months ago

AHA! It has to be rust and cannot be rs.

So that I'm not as much of a burden going forward: How/where should I have gleamed this knowledge? Up until a minute ago (since I had a break and because I'm forgetfull) this was not a meaningful distinction.

dragonstyle commented 9 months ago

I don't know that we have the list of languages documented anywhere, but you can use:

quarto pandoc --list-highlight-languages

This is mentioned here: https://quarto.org/docs/output-formats/html-code.html#highlighting

But is definitely pretty subtle. I wonder if it would be cool to add an article regarding code highlighting, code annotation, and other commonly needed cross format code options... (could include a table of Languages and their corresponding names in markdown)

dragonstyle commented 9 months ago

(And you're not being a burden - I was just being sensitive because of course I fixed it [always true, except when it isn't ;-) ])

sasja-san commented 9 months ago

Haha, when annotation in the margin is added I'll do that write-up ;)

For now I'm happy with my cool h4xx to get around it. It definetly looks good enough.

quarto-code-annotation-h4xx

dragonstyle commented 9 months ago

I agree that looks amazing!