#Source Code Listings
117 messages · Page 1 of 1 (latest)
so you said i'll have to read and then but it in the raw format?
but isnt the read already what i want?
Read gets the entire file. You then extract the correct lines before calling raw
raw will highlight the code
ah i see
which means i would
read the whole file -> gets me the code above
slice the get time function with lines 1 to 4 for example
pipe the code into raw to get highlighting
translate it back into sth to actually be able to see it?
Because raw is what does the highlighting
i dont get how to call the raw then?
try this: #raw(read(file-name))
ah i see
but the code isnt highlighted. maybe because it doesnt support python though
the raw blocks remove some of the code parts
you may want to see the official doc about raw. to let typst know the language of raw block, an extra argument named lang is needed
you should also define that it should be a block:
which i then can slice again, right?
#raw(block: true, lang: "python", python-code)
you can slice the code, yes
the raw block then?
python-code.split("\n").slice(0, 4).join("\n")
You slice it before putting it in raw
Because you're looking to show specific lines?
right
#let python-code = read("../example.py").split("\n").slice(1, 5).join("\n")
#let raw-code = raw(block: true, python-code, lang: "python")
#raw-code
thats super little code, i thought that's gonna be so much more though
i could place that one into a block and show the line numbers next to it, right? that for sure works somehow
here's an example on how you could do line numbers without a package: #discussions message
at this point you might want to use https://typst.app/universe/package/codelst though
but the code doesnt read from files, i mean i could add it
if you write a show rule on raw, it would work on any code blocks, be it read from a different file, or not
If you're using a package, you would use the specific package function, instead of raw (or just raw would work if the package just stylises raw)
styling has nothing to do with where the code is coming from
well but then its not needed since the codelst package does that for me
if you use codelst, you can just pass the raw block into sourcecode
as #sourcecode(raw-code)
why isnt the code on the left as i told the figure to be?
#let code-block = block.with(
stroke: 1pt,
inset: 0.65em,
radius: 4pt
)
#let code-path = "../example.py"
#let raw-code = raw(code-path, block: true, lang: "python")
#let listcode(file, from, to, language: "python") = {
let read-code = read(file).split("\n").slice(from, to).join("\n")
let raw-block = raw(block: true, read-code, lang: language)
raw-block
}
#align(left)[
#figure(
code-block(listcode("../example.py", 1, 5)),
caption: [Hello World]
) <code-listing>
]
and is there any way to change the default caption to a Code or SourceCode?
Did you look at the documentation for https://typst.app/docs/reference/model/figure/ ?
there is a supplement parameter
right that explains the listing but not the alignment which is stil off
you can fix it by wrapping it in a block, but that shouldn't be necessary
?render ```
#set page(width: 20cm)
#show figure: it => align(left,block(it))
#figure(
raw("test"),
caption: [Hello World]
)```
Likely a bug
top and bottom work but not left and right
and that one might be another weird one:
#let test(file: none, code: none, range: (), lang: "plain") = {
if code != none {
let raw-block = raw(block: true, code, lang: lang)
}else if file != none {
let read-code = read(file).split("\n").slice(range.first(), range.last()).join("\n")
let raw-block = raw(block: true, read-code, lang: lang)
raw-block
} else {
panic("No code or file given")
}
}
error on raw(block: true, code, lang: lang)
but the code is a string
@quasi canyon this is the support thread
Right
So
Someone had asked me for something similar
It can be adapted to have multiple langs
Or whatever
#let partial-raw(code, lines: ()) = {
let text
let lang
let is-block
if type(code) == content and code.func() == raw {
text = code.text
is-block = code.block
lang = code.at("lang", default: none)
} else {
panic("First argument to 'partial-raw' must be a raw block.")
}
let code-lines = text.split("\n")
let raw-blocks = ()
let highlighting = none
for (i, line) in code-lines.enumerate() {
let should-highlight = i in lines
if highlighting != should-highlight {
highlighting = should-highlight
raw-blocks.push((lang: if should-highlight { lang } else { none }, block: is-block, text: ""))
}
raw-blocks.last().text += line + "\n"
}
show raw.where(block: true): set block(spacing: 0.65em)
raw-blocks.map(args => raw(args.remove("text"), ..args)).join()
}
#partial-raw(
[three backticks here]rs
pub fn main() {
let x = 5;
println!("Hello world!");
let y = 5;
println!("Bye world!");
}
[three backticks here],
lines: (0, 1, 4)
)
i mean i kinda got it though
i just want it to support code from files too
and it kinda works as you can see above
but i legit dont know what the compiler wants here lmfao
it legit tells me that the type is wrong
but it for sure isnt
What's your function call looking like?
oh dang the type indeed is content
#type("python")
#type(code-part)
the code part starts with 3 backticks
Then you're providing a raw element
To a new raw element
Not a string
You can use .text to extract the string inside it
As I do here
is there a way to provide only a single element in an array for a range?
something like
(1, ) // goes up to the end ```
what does that mean?
uh 1sec
let read-code = read(file).split("\n").slice(range.first(), range.last()).join("\n")
that's what i'm doing right now to get the two "points"
#code(file: code-path, range: (1, 8), lang: "python")
but i essentially want something like: range: (1, ) which then takes the whole code
slice(..range)
what does the .. do here?
Well that's very smart lol
Passes as arguments
ahh
.slice(1) vs slice(1, 8)
right and for line numbers i could just enumerate and print all of them to the left
sounds a bit tricky, will get it i guess haha
?render ```
#let test = raw("
Some lines
with indentation
all over the place
").text
#let offset = 5
#show raw.line: it => {
str(it.number + offset) + it.body
}
#raw(test)
proof of concept
ignore the leading empty lines, just discord not liking backticks
or just (for instance) https://typst.app/universe/package/codly if you would like something plug and play
is there a way to get the "offset" from the range i have?
something like total - range
well, it would be the first number in your range minus one
there is no fixed types in typst yet, right?
because when i try to type it it's missing argument "self"
ah i got it
#let file-path = "../example.py"
#let code-example = ```python
def test(name: str) -> str:
return f"Hello {name}!"
#let code(code: none, file: none, range: (), lang: "python", use-block: true) = {
if code != none {
let raw-code = raw(block: true, code-example.text, lang: "python")
code-block(raw-code)
} else if file != none {
let read-code = read(file).split("\n").slice(..range).join("\n")
let raw-block = raw(block: use-block, read-code, lang: lang)
show raw.line: it => {
str(it.number + range.first() - 1) + h(1em) + it.text
}
code-block(raw-block)
} else {
panic("No code or file provided")
}
}
#code(file: file-path, range: (1, ))
#parbreak()
#code(code: code-example.text)```
i dont get why i cant add numbers and get it colored
bump
replace it.text with it.body
it.text is the raw content of the line