Skip to content

Add raw.line - #2341

Merged
laurmaedje merged 9 commits into
typst:mainfrom
Dherse:raw-line
Oct 10, 2023
Merged

laurmaedje merged 9 commits into
typst:mainfrom
Dherse:raw-line

Conversation

@Dherse

@Dherse Dherse commented Oct 9, 2023 •

Copy link
Copy Markdown
Contributor

Adds a new sub-element called raw.line that allows access to individual lines of stylized code.

The raw.line contains the following properties:

  • the line number (int, starting at 1)
  • the raw text (string)
  • the highlighted text (content)
  • whether it's the first line (bool)
  • whether it's the last line (bool)

Line of raw code can be either individually show-ruled or accessed on a synthesized field called lines on raw.

It allows the following:

#show raw: it => stack(dir: ttb, ..it.lines)
#show raw.line: it => {
  box(
    width: 100%,
    height: 1.75em,
    inset: 0.25em,
    fill: if calc.rem(it.number, 2) == 0 { luma(90%) } else { white },
    align(horizon, stack(
      dir: ltr,
      box(width: 15pt)[#it.number],
      it.body,
    ))
  )
}

```typ
#show raw.line: block.with(
  fill: luma(60%)
);

Hello, world!

= A heading for good measure
```

image

A big thank you to @laurmaedje and @edhebi for the inspiration and the help 🎉

@sitandr

sitandr commented Oct 9, 2023

Copy link
Copy Markdown
Contributor

Maybe we could rename raw.line.line to raw.line.number, like enum.item.number? line.line seems extremely ambiguous to me and a bit inconsistent.

@Dherse

Dherse commented Oct 9, 2023

Copy link
Copy Markdown
Contributor Author

Maybe we could rename raw.line.line to raw.line.number, like enum.item.number? line.line seems extremely ambiguous to me and a bit inconsistent.

I didn't think of that, I will rename it ;)

@laurmaedje

Copy link
Copy Markdown
Member

What about about number/total instead of number/first/last? seems more flexible.

Comment thread crates/typst-library/src/text/raw.rs Resolved
Comment thread crates/typst-library/src/text/raw.rs Resolved
Comment thread crates/typst-library/src/text/raw.rs Resolved
Comment thread tests/typ/text/raw-line.typ Resolved
@Dherse

Dherse commented Oct 10, 2023

Copy link
Copy Markdown
Contributor Author

What about about number/total instead of number/first/last? seems more flexible.

The main reason why I didn't do it was that (initially) I had a hard time doing it for typst code because I didn't want to manually split/iterate over the code to count the number of lines, but I think there is a nicer way, I'll give it a try.

@laurmaedje

Copy link
Copy Markdown
Member

What is the nicer way? I think iterating the code once to count the lines should be pretty cheap.

@Dherse

Dherse commented Oct 10, 2023

Copy link
Copy Markdown
Contributor Author

What is the nicer way? I think iterating the code once to count the lines should be pretty cheap.

The nicer way would be doing it as you're iterating over the lines, but it turns out to not really work since I would need a second pass to update all of them anyway. So I added a count value with the max number of lines.

@laurmaedje
laurmaedje merged commit 0dd79bb into typst:main Oct 10, 2023
@laurmaedje

Copy link
Copy Markdown
Member

Thanks once more!

@Dherse
Dherse deleted the raw-line branch October 10, 2023 12:49
@CGMossa CGMossa mentioned this pull request Oct 16, 2023
Dikluwe pushed a commit to Dikluwe/typst-crystalline that referenced this pull request Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants