Mc-Zen / tidy

A documentation generator for Typst in Typst.
https://typst.app/universe/package/tidy
MIT License
49 stars 2 forks source link

#example() fails for code that results in content with relative width #14

Closed SillyFreak closed 6 months ago

SillyFreak commented 6 months ago

Here's a minimal example with three modules given as inline code:

#import "@preview/tidy:0.2.0"

#tidy.show-module(
  tidy.parse-module(````
/// A function
///
/// #example(mode: "markup", ```
/// #rect(width: 2em)
/// ```)
#let func-a() = {}
````.text),
  style: tidy.styles.minimal
)

// #tidy.show-module(
//   tidy.parse-module(````
// /// A function
// ///
// /// #example(mode: "markup", ```
// /// #rect(width: 100%)
// /// ```)
// #let func-b() = {}
// ````.text),
//   style: tidy.styles.minimal
// )

#tidy.show-module(
  tidy.parse-module(````
/// A function
///
/// #example(mode: "markup", scale-preview: 100%, ```
/// #rect(width: 100%)
/// ```)
#let func-c() = {}
````.text),
  style: tidy.styles.minimal
)

The first module contains a func-a, and its documentation has an example. The width is 2em (plus some border) - no problem.

However, the second module (commented out) has a rectangle with width: 100%. This leads to a division by zero, it seems:

error: cannot divide by zero
   ┌─ @preview/tidy:0.2.0/src/show-example.typ:59:16
   │
59 │     calc.min(1, available-preview-width / preview-size.width) * 100%
   │                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The third module circumvents this: the division only happens if scale-preview == auto, so I set scale-preview: 100%. However, this seems to give the example content a "viewport" width of zero: width: preview-size.width * (scale-preview / 100%).

So in total: examples can't have a width that is a ratio, as measuring that leads to either dividing by or multiplying with zero.

Mc-Zen commented 6 months ago

Hi @SillyFreak ,

Yes this is kind of the intended behaviour. The example should not have a size that is dependent on the parent container because how would scaling then work? Do you have a use-case where you would want to have an example with a width that is a ratio in this scenario?

SillyFreak commented 6 months ago

I basically stumbled upon it because I had a function returning a 100% width table and wanted to add an example; I wound up using #show: block.with(width: 15cm) to give it a fixed width, but that code is for now also part of the example. I'd say failing on scale-preview == auto is (apart from the error message) correct, but if the scale is given, shouldn't this be able to work? Maybe I'm missing a detail.

Mc-Zen commented 6 months ago

I see, mmh.

Well even for a fixed scale factor, a width of 100% seems conceptionally a bit problematic.

No, I think we can make it work.

Mc-Zen commented 6 months ago

Fixed via beca07c