├── CHANGELOG.md ├── Justfile ├── LICENSE ├── README.md ├── docs ├── example.pdf ├── example.typ ├── manual.pdf └── manual.typ ├── src └── codelst.typ ├── tbump.toml ├── tests ├── .DS_Store ├── .gitkeep ├── gobble │ ├── .gitignore │ ├── ref │ │ └── 1.png │ └── test.typ ├── test-spaces.txt ├── test-tabs.txt └── test1 │ ├── ref │ └── 1.png │ └── test.typ └── typst.toml /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # [unreleased](https://github.com/author/my-package/releases/tags/) 2 | ## Added 3 | 4 | ## Removed 5 | 6 | ## Changed 7 | 8 | ## Migration Guide from v0.1.X 9 | 10 | --- 11 | 12 | # [v0.1.0](https://github.com/author/my-package/releases/tags/v0.1.0) 13 | Initial Release 14 | -------------------------------------------------------------------------------- /Justfile: -------------------------------------------------------------------------------- 1 | root := justfile_directory() 2 | 3 | export TYPST_ROOT := root 4 | 5 | [private] 6 | default: 7 | @just --list --unsorted 8 | 9 | # generate manual 10 | doc: 11 | typst compile docs/manual.typ docs/manual.pdf 12 | 13 | # run test suite 14 | test *args: 15 | typst-test run {{ args }} 16 | 17 | # update test cases 18 | update *args: 19 | typst-test update {{ args }} 20 | 21 | # run ci suite 22 | ci: test doc 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Jonas Neugebauer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # codelst (v2.0.2) 2 | 3 | **codelst** is a [Typst](https://github.com/typst/typst) package for rendering sourcecode with line numbers and some other additions. 4 | 5 | ## Usage 6 | 7 | Import the package from the typst preview repository: 8 | 9 | ```js 10 | #import "@preview/codelst:2.0.2": sourcecode 11 | ``` 12 | 13 | After importing the package, simply wrap any fenced code block in a call to `#sourcecode()`: 14 | 15 | ````js 16 | #import "@preview/codelst:2.0.2": sourcecode 17 | 18 | #sourcecode[```typ 19 | #show "ArtosFlow": name => box[ 20 | #box(image( 21 | "logo.svg", 22 | height: 0.7em, 23 | )) 24 | #name 25 | ] 26 | 27 | This report is embedded in the 28 | ArtosFlow project. ArtosFlow is a 29 | project of the Artos Institute. 30 | ```] 31 | ```` 32 | 33 | ## Further documentation 34 | 35 | See `manual.pdf` for a comprehensive manual of the package. 36 | 37 | See `example.typ` for some quick usage examples. 38 | 39 | ## Development 40 | 41 | The documentation is created using [Mantys](https://github.com/jneug/typst-mantys), a Typst template for creating package documentation. 42 | 43 | To compile the manual, Mantys needs to be available as a local package. Refer to Mantys' manual for instructions on how to do so. 44 | 45 | ## Changelog 46 | 47 | ### v2.0.1 48 | 49 | This version makes `codelst` compatible to Typst 0.11.0. Version 2.0.1 now requires Typst 0.11.0, since there are some breaking changes to the way counters work. 50 | 51 | Thanks to @kilpkonn for theses changes. 52 | 53 | ### v2.0.0 54 | 55 | Version 2 requires Typst 0.9.0 or newer. Rendering is now done using the new 56 | `raw.line` elements get consistent line numbers and syntax highlighting (even 57 | if `showrange` is used). Rendering is now done in a `#table`. 58 | 59 | - Added `theme` and `syntaxes` options to overwrite passed in `#raw` values. 60 | - Breaking: Renamed `tab-indend` to `tab-size`, to conform with the Typst option. 61 | - Breaking: Removed `continue-numbering` option for now. (The feature failed in combination with label parsing and line highlights.) 62 | - Breaking: Removed styling of line numbers via a `show`-rule. 63 | 64 | ### v1.0.0 65 | 66 | - Complete rewrite of code rendering. 67 | - New options for `#sourcecode()`: 68 | - `lang`: Overwrite code language setting. 69 | - `numbers-first`: First line number to show. 70 | - `numbers-step`: Only show every n-th number. 71 | - `frame`: Set a frame (replaces `` label.) 72 | - Merged `line-numbers` and `numbering` options. 73 | - Removed `#numbers-style()` function. 74 | - `numbers-style` option now gets passed `counter.display()`. 75 | - Removed `` label. 76 | - `codelst-style` only sets `breakable` for figures. 77 | - New `codelst` function to setup a catchall show rules for `raw` text. 78 | - `label-regex: none` disables labels parsing. 79 | - Code improvements and refactorings. 80 | 81 | ### v0.0.5 82 | 83 | - Fixed insets for line highlights. 84 | - Added `numbers-width` option to manually set width of line numbers column. 85 | - This allows line numbers on margins by setting `numbers-width` to `0pt` or a negative number like `-1em`. 86 | 87 | ### v0.0.4 88 | 89 | - Fixed issue with context unaware syntax highlighting. 90 | 91 | ### v0.0.3 92 | 93 | - Removed call to `#read()` from `#sourcefile()`. 94 | - Added `continue-numbering` argument to `#sourcecode()`. 95 | - Fixed problem with `showrange` having out of range line numbers. 96 | 97 | ### v0.0.2 98 | 99 | - Added a comprehensive manual. 100 | - Fixed crash for missing `lang` attribute in `raw` element. 101 | 102 | ### v0.0.1 103 | 104 | - Initial version submitted to typst/packages. 105 | -------------------------------------------------------------------------------- /docs/example.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jneug/typst-codelst/2374c6c6a8035e12d0f340ed7fe65858e9648019/docs/example.pdf -------------------------------------------------------------------------------- /docs/example.typ: -------------------------------------------------------------------------------- 1 | #import "../src/codelst.typ": sourcecode, sourcefile, lineref, code-frame 2 | 3 | #let codelst = text(fill: rgb(254, 48, 147), smallcaps("codelst")) 4 | #let cmd(name) = text(fill: rgb(99, 170, 234), raw(block: false, sym.hash + name.text + sym.paren.l + sym.paren.r)) 5 | 6 | #let code-block = block.with( 7 | stroke: 1pt, 8 | inset: 0.65em, 9 | radius: 4pt, 10 | ) 11 | 12 | #let code-example = ```typ 13 | /* 14 | * Example taken from 15 | * https://typst.app/docs/tutorial/formatting/ 16 | */ 17 | #show "ArtosFlow": name => box[ 18 | #box(image( 19 | "logo.svg", 20 | height: 0.7em, 21 | )) 22 | #name 23 | ] 24 | 25 | // Long line that breaks 26 | This report is embedded in the ArtosFlow project. ArtosFlow is a project of the Artos Institute. 27 | 28 | /* 29 | Very long line without linebreak 30 | with a preceding block comment 31 | */ 32 | This_report_is_embedded_in_the_ArtosFlow_project._ArtosFlow_is_a_project_of_the_Artos_Institute. 33 | 34 | // End example 35 | ``` 36 | 37 | Normal #cmd[raw] works as expected: 38 | 39 | #code-block(code-example) 40 | 41 | Using #cmd[sourcecode] will add line numbers and a frame. 42 | 43 | #code-block(sourcecode(code-example)) 44 | 45 | #pagebreak() 46 | #code-block( 47 | sourcecode( 48 | showrange: (15, 21), 49 | showlines: true, 50 | code-example, 51 | ), 52 | ) 53 | 54 | Sourcecode can be loaded from a file and passed to #cmd[sourcefile]. Any #codelst sourcecode can be wrapped inside #cmd[figure] as expected. 55 | 56 | #codelst line numbers can be formatted via a bunch of `numbers-` options: 57 | 58 | #code-block[ 59 | #let filename = "typst.toml" 60 | #let number-format(n) = text(fill: blue, emph(n)) 61 | 62 | #show figure.where(kind: raw): fig => grid( 63 | columns: (1fr, 2fr), 64 | gutter: .65em, 65 | [ 66 | #set align(left) 67 | #set par(justify: true) 68 | To the right in @lst-sourcefile you can see the #raw(filename) file of this package with some #number-format[fancy line numbers]. 69 | ], 70 | fig, 71 | ) 72 | 73 | #figure( 74 | caption: filename, 75 | sourcefile( 76 | numbers-side: right, 77 | numbers-style: number-format, 78 | file: filename, 79 | read("../" + filename), 80 | ), 81 | ) 82 | ] 83 | 84 | Since packages can't #cmd[read] files from outside their own directory, you can alias #cmd[sourcefile] for a more convenient command: 85 | 86 | ```typc 87 | let srcfile( filename, ..args ) = sourcefile(read(filename), file:filename, ..args) 88 | ``` 89 | #let srcfile(filename, ..args) = sourcefile(read(filename), file: filename, ..args) 90 | 91 | Formatting is controlled through options. To use a default style, create an alias for your command: 92 | 93 | ```typc 94 | let code = sourcecode.with( 95 | numbers-style: (lno) => text(black, lno), 96 | frame: none 97 | ) 98 | ``` 99 | 100 | #cmd[sourcecode] accepts a number of arguments to affect the output like _highlighting lines_, _restrict the line range_ or _place labels_ in specific lines to reference them later. 101 | #code-block[ 102 | #sourcecode( 103 | numbers-start: 9, 104 | highlighted: (14,), 105 | highlight-labels: true, 106 | highlight-color: rgb(250, 190, 144), 107 | gutter: 2em, 108 | label-regex: regex("<([a-z-]+)>"), 109 | frame: code => block(width: 100%, fill: rgb(254, 249, 222), inset: 5pt, code), 110 | )[```typ 111 | #"hello world!" \ 112 | #"\"hello\n world\"!" \ 113 | #"1 2 3".split() \ 114 | #"1,2;3".split(regex("[,;]")) \ 115 | #(regex("\d+") in "ten euros") \ 116 | #(regex("\d+") in "10 euros") 117 | ```] 118 | ] 119 | 120 | To reference a line use #cmd[lineref]: 121 | 122 | - See #lineref() for an example of the `split()` function. 123 | 124 | Long code breaks to new pages. To have listings in figures break, you need to allow it via a #cmd[show] rule: 125 | 126 | ```typ 127 | #show figure.where(kind: raw): set block(breakable: true) 128 | ``` 129 | #[ 130 | #show figure.where(kind: raw): set block(breakable: true) 131 | 132 | #show figure.where(kind: raw): fig => [ 133 | #v(1em) 134 | #set align(center) 135 | #strong([#fig.supplement #fig.counter.display()]): #emph(fig.caption.body) 136 | #fig.body 137 | ] 138 | 139 | #figure( 140 | srcfile("example.typ", highlighted: range(121, 136), numbers-step: 5, numbers-first: 5), 141 | caption: "Code of this example file.", 142 | ) 143 | ] 144 | 145 | #pagebreak() 146 | == More examples 147 | 148 | And last but not least, some weird examples of stuff you can do with this package (example code taken from #link("https://github.com/rust-lang/rust-by-example/blob/master/src/fn.md", raw("rust-lang/rust-by-example"))): 149 | 150 | #sourcecode(frame: none, numbering: none)[ 151 | ```rust 152 | // Unlike C/C++, there's no restriction on the order of function definitions 153 | fn main() { 154 | // We can use this function here, and define it somewhere later 155 | fizzbuzz_to(100); 156 | } 157 | ``` 158 | ] 159 | 160 | #sourcecode( 161 | numbering: "I", 162 | numbers-style: lno => align(right, [#text(eastern, emph(lno)) |]), 163 | gutter: 1em, 164 | tab-size: 8, 165 | gobble: 1, 166 | showlines: true, 167 | )[ 168 | ```rust 169 | 170 | 171 | // Function that returns a boolean value 172 | fn is_divisible_by(lhs: u32, rhs: u32) -> bool { 173 | // Corner case, early return 174 | if rhs == 0 { 175 | return false; 176 | } 177 | 178 | // This is an expression, the `return` keyword is not necessary here 179 | lhs % rhs == 0 180 | } 181 | 182 | 183 | ``` 184 | ] 185 | 186 | #block(width: 100%)[ 187 | #sourcecode( 188 | numbers-width: -6mm, 189 | frame: block.with(width: 75%, fill: rgb("#b7d4cf"), inset: 5mm), 190 | )[```rust 191 | // Functions that "don't" return a value, actually return the unit type `()` 192 | fn fizzbuzz(n: u32) -> () { 193 | if is_divisible_by(n, 15) { 194 | println!("fizzbuzz"); 195 | } else if is_divisible_by(n, 3) { 196 | println!("fizz"); 197 | } else if is_divisible_by(n, 5) { 198 | println!("buzz"); 199 | } else { 200 | println!("{}", n); 201 | } 202 | } 203 | ```] 204 | #place( 205 | top + right, 206 | block(width: 23%)[ 207 | #set par(justify: true) 208 | #lorem(40) 209 | ], 210 | ) 211 | ] 212 | 213 | #sourcecode( 214 | numbering: "(1)", 215 | numbers-side: right, 216 | numbers-style: lno => text(1.5em, rgb(143, 254, 9), [#sym.arrow.l #lno]), 217 | 218 | frame: code => { 219 | set text(luma(245)) 220 | code-frame( 221 | fill: luma(24), 222 | stroke: 4pt + rgb(143, 254, 9), 223 | radius: 0pt, 224 | inset: .65em, 225 | code, 226 | ) 227 | }, 228 | )[```rust 229 | // When a function returns `()`, the return type can be omitted from the 230 | // signature 231 | fn fizzbuzz_to(n: u32) { 232 | for n in 1..=n { 233 | fizzbuzz(n); 234 | } 235 | } 236 | ```] 237 | -------------------------------------------------------------------------------- /docs/manual.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jneug/typst-codelst/2374c6c6a8035e12d0f340ed7fe65858e9648019/docs/manual.pdf -------------------------------------------------------------------------------- /docs/manual.typ: -------------------------------------------------------------------------------- 1 | #import "@local/mantys:0.1.1": * 2 | 3 | #import "../src/codelst.typ" 4 | 5 | #show: mantys.with( 6 | name: "codelst", 7 | title: "The codelst Package", 8 | subtitle: [A *Typst* package to render source code], 9 | authors: "Jonas Neugebauer", 10 | url: "https://github.com/jneug/typst-codelst", 11 | version: "2.0.2", 12 | date: "2023-07-19", 13 | abstract: [ 14 | #package[codelst] is a *Typst* package inspired by LaTeX packages like #package[listings]. It adds functionality to render source code with line numbers, highlighted lines and more. 15 | ], 16 | 17 | examples-scope: ( 18 | codelst: codelst, 19 | sourcecode: codelst.sourcecode, 20 | sourcefile: codelst.sourcefile, 21 | code-frame: codelst.code-frame, 22 | lineref: codelst.lineref, 23 | ), 24 | ) 25 | 26 | #let footlink(url, label) = [#link(url, label)#footnote(link(url))] 27 | #let gitlink(repo) = footlink("https://github.com/" + repo, repo) 28 | 29 | // End preamble 30 | 31 | = About 32 | 33 | This package was created to render source code on my exercise sheets for my computer science classes. The exercises required source code to be set with line numbers that could be referenced from other parts of the document, to highlight certain lines and to load code from external files into my documents. 34 | 35 | Since I used LaTeX before, I got inspired by packages like #footlink("https://ctan.org/package/listings", package("listings")) and attempted to replicate some of its functionality. CODELST is the result of this effort. 36 | 37 | This document is a full description of all available commands and options. The first part provides examples of the major features. The second part is a command reference for CODELST. 38 | 39 | See `example.typ`/`example.pdf` for some quick examples how to use CODELST. 40 | 41 | = Usage 42 | 43 | == Use as a package (Typst 0.9.0 and later) 44 | 45 | For Typst 0.9.0 and later, CODELST can be imported from the preview repository: 46 | #sourcecode(numbering: none)[```typ 47 | #import "@preview/codelst:2.0.2": sourcecode 48 | ```] 49 | 50 | Alternatively, the package can be downloaded and saved into the system dependent local package repository. 51 | 52 | Either download the current release from GitHub#footnote[#link("https://github.com/jneug/typst-codelst/releases/latest")] and unpack the archive into your system dependent local repository folder#footnote[#link("https://github.com/typst/packages#local-packages")] or clone it directly: 53 | #codesnippet[ 54 | ```shell-unix-generic 55 | git clone https://github.com/jneug/typst-codelst.git codelst-2.0.2 56 | ```] 57 | 58 | In either case, make sure the files are placed in a folder with the correct version number: `codelst-2.0.2` 59 | 60 | After installing the package, just import it inside your `typ` file: 61 | #codesnippet[```typ 62 | #import "@local/codelst:2.0.2": sourcecode 63 | ```] 64 | 65 | == Use as a module 66 | 67 | To use CODELST as a module for one project, get the file `codelst.typ` from the repository and save it in your project folder. 68 | 69 | Import the module as usual: 70 | #codesnippet[```typ 71 | #import "codelst.typ": sourcecode 72 | ```] 73 | 74 | == Rendering source code 75 | 76 | CODELST adds the #cmd[sourcecode] command with various options to render code blocks. It wraps around any #cmd-[raw] block to adds some functionality and formatting options to it: 77 | 78 | #example[```` 79 | #sourcecode[```typ 80 | #show "ArtosFlow": name => box[ 81 | #box(image( 82 | "logo.svg", 83 | height: 0.7em, 84 | )) 85 | #name 86 | ] 87 | 88 | This report is embedded in the 89 | ArtosFlow project. ArtosFlow is a 90 | project of the Artos Institute. 91 | ```] 92 | ````] 93 | 94 | CODELST adds line numbers and some default formatting to the code. Line numbers can be configured with a variety of options and #arg[frame] sets a custom wrapper function for the code. Setting #arg(frame: none) disables the code frame. 95 | 96 | #example[```` 97 | #sourcecode( 98 | numbers-side: right, 99 | numbering: "I", 100 | numbers-start: 10, 101 | numbers-first: 11, 102 | numbers-step: 4, 103 | numbers-style: (i) => align(right, text(fill:blue, emph(i))), 104 | frame: none 105 | )[```typ 106 | #show "ArtosFlow": name => box[ 107 | #box(image( 108 | "logo.svg", 109 | height: 0.7em, 110 | )) 111 | #name 112 | ] 113 | 114 | This report is embedded in the 115 | ArtosFlow project. ArtosFlow is a 116 | project of the Artos Institute. 117 | ```] 118 | ````] 119 | 120 | Since it is common to highlight code blocks by putting them inside a #cmd-[block] element, CODELST does so with a light gray background and a border. 121 | 122 | The frame can be modified by setting #arg[frame] to a function with one argument. To do this globally, an alias for the #cmd-[sourcecode] command can be created: 123 | 124 | #example[```` 125 | #let codelst-sourcecode = sourcecode 126 | #let sourcecode = codelst-sourcecode.with( 127 | frame: block.with( 128 | fill: fuchsia.lighten(96%), 129 | stroke: 1pt + fuchsia, 130 | radius: 2pt, 131 | inset: (x: 10pt, y: 5pt) 132 | ) 133 | ) 134 | 135 | #sourcecode[```typ 136 | #show "ArtosFlow": name => box[ 137 | #box(image( 138 | "logo.svg", 139 | height: 0.7em, 140 | )) 141 | #name 142 | ] 143 | 144 | This report is embedded in the 145 | ArtosFlow project. ArtosFlow is a 146 | project of the Artos Institute. 147 | ```] 148 | ````] 149 | 150 | Line numbers can be formatted with the #opt[numbers-style] option: 151 | 152 | #example[```` 153 | #sourcecode( 154 | gutter:2em, 155 | numbers-style: (lno) => text(fill:luma(120), size:10pt, emph(lno) + sym.arrow.r) 156 | )[```typ 157 | #show "ArtosFlow": name => box[ 158 | #box(image( 159 | "logo.svg", 160 | height: 0.7em, 161 | )) 162 | #name 163 | ] 164 | 165 | This report is embedded in the 166 | ArtosFlow project. ArtosFlow is a 167 | project of the Artos Institute. 168 | ```] 169 | ````] 170 | 171 | CODELST handles whitespace in the code to save space and display the code as intended (and indented). Unnecessary blank lines at the beginning and end will be removed, alongside superfluous indentation: 172 | 173 | #example[```` 174 | #sourcecode[```java 175 | 176 | class HelloWorld { 177 | public static void main( String[] args ) { 178 | System.out.println("Hello World!"); 179 | } 180 | } 181 | 182 | 183 | ```] 184 | ````] 185 | 186 | This behavior can be disabled or modified: 187 | 188 | #example[```` 189 | #sourcecode(showlines:true, gobble:1, tab-size:4)[```java 190 | 191 | class HelloWorld { 192 | public static void main( String[] args ) { 193 | System.out.println("Hello World!"); 194 | } 195 | } 196 | 197 | 198 | ```] 199 | ````] 200 | 201 | To show code from a file, load it with #cmd[read] and pass the result to #cmd[sourcefile] alongside the filename: 202 | 203 | #example(raw("#sourcefile(read(\"typst.toml\"), file:\"typst.toml\")"))[ 204 | #codelst.sourcefile(read("../typst.toml"), file: "typst.toml") 205 | ] 206 | 207 | It is useful to define an alias for #cmd-[sourcefile]: 208 | #codesnippet[```typc 209 | let codelst-sourcefile = sourcefile 210 | let sourcefile( filename, ..args ) = codelst-sourcefile( 211 | read(filename), file:filename, ..args 212 | ) 213 | ```] 214 | 215 | #cmd-[sourcefile] takes the same arguments as #cmd-[sourcecode]. For example, to limit the output to a range of lines: 216 | 217 | #example[``` 218 | #sourcefile( 219 | showrange: (2, 4), 220 | read("typst.toml"), 221 | file:"typst.toml" 222 | ) 223 | ```][ 224 | #codelst.sourcefile( 225 | showrange: (2, 4), 226 | read("../typst.toml"), 227 | file: "typst.toml", 228 | ) 229 | ] 230 | 231 | Specific lines can be highlighted: 232 | 233 | #example[``` 234 | #sourcefile( 235 | highlighted: (2, 3, 4), 236 | read("typst.toml"), 237 | file:"typst.toml" 238 | ) 239 | ```][ 240 | #codelst.sourcefile( 241 | highlighted: (2, 3, 4, 8, 9), 242 | read("../typst.toml"), 243 | file: "typst.toml", 244 | ) 245 | ] 246 | 247 | To reference a line from other parts of the document, CODELST looks for labels in the source code and makes them available to Typst. The regex to look for labels can be modified to be compatible with different source syntaxes: 248 | 249 | #example[``` 250 | #sourcefile( 251 | label-regex: regex("\"(codelst.typ)\""), 252 | highlight-labels: true, 253 | highlight-color: lime, 254 | read("typst.toml"), 255 | file:"typst.toml" 256 | ) 257 | 258 | See #lineref() for the _entrypoint_. 259 | 260 | ```][ 261 | #codelst.sourcefile( 262 | label-regex: regex("\"(codelst.typ)\""), 263 | highlight-labels: true, 264 | highlight-color: lime, 265 | read("../typst.toml"), 266 | file: "typst.toml", 267 | ) 268 | 269 | See line 4 for the _entrypoint_. (Note how the label was removed from the sourcecode before highlighting.) 270 | ] 271 | 272 | == Formatting 273 | 274 | #cmd-[sourcecode] can be used inside #cmd[figure] and will show the correct supplement. It is recommended to allow page breaks for `raw` figures: 275 | #sourcecode[```typ 276 | #show figure.where(kind: raw): set block(breakable: true) 277 | ```] 278 | 279 | Instead of the build in styles, custom functions can be used: 280 | #example(```typ 281 | #sourcecode( 282 | numbers-style: (lno) => text( 283 | size: 2em, 284 | fill:rgb(220, 65, 241), 285 | font:("Comic Sans MS"), 286 | str(lno) 287 | ), 288 | frame: (code) => block( 289 | width:100%, 290 | inset:(x:10%, y:0pt), 291 | block(fill: green, width:100%, code) 292 | ), raw("*some* 293 | _source_ 294 | = code", lang:"typc")) 295 | ```) 296 | 297 | Using other packages like #package[showybox] is easy: 298 | #example[```` 299 | #import "@preview/showybox:2.0.1": showybox 300 | 301 | #let showycode = sourcecode.with( 302 | frame: (code) => showybox( 303 | frame: ( 304 | title-color: red.darken(40%), 305 | body-color: red.lighten(90%), 306 | border-color: black, 307 | thickness: 2pt 308 | ), 309 | title: "Source code", 310 | code 311 | ) 312 | ) 313 | 314 | #showycode[```typ 315 | *some* 316 | _source_ 317 | = code 318 | ```] 319 | ````] 320 | 321 | This is nice in combination with figures: 322 | 323 | #example[```` 324 | #import "@preview/showybox:2.0.1": showybox 325 | 326 | #show figure.where(kind: raw): (fig) => showybox( 327 | frame: ( 328 | title-color: red.darken(40%), 329 | body-color: red.lighten(90%), 330 | border-color: black, 331 | thickness: 2pt 332 | ), 333 | title: [#fig.caption.body #h(1fr) #fig.supplement #fig.counter.display()], 334 | fig.body 335 | ) 336 | 337 | #figure( 338 | sourcecode(frame: none)[```typ 339 | *some* 340 | _source_ 341 | = code 342 | ```], 343 | caption: "Some code" 344 | ) 345 | ````] 346 | 347 | === Using CODELST for all raw text 348 | 349 | #ibox[ 350 | Since Typst 0.9.0 using a #var-[show] rule should become possible, but not yet fully implemented in CODELST. 351 | ] 352 | 353 | Using a #var-[show] rule to set all #cmd-[raw] blocks inside #cmd-[sourcecode] is not possible, since the command internally creates a new #cmd-[raw] block and would cause Typst to crash with an overflow error. Using a custom #arg[lang] can work around this, though: 354 | 355 | #example[```` 356 | #show raw.where(lang: "clst-typ"): (code) => sourcecode(lang:"typ", code) 357 | 358 | ```clst-typ 359 | *some* 360 | _source_ 361 | = code 362 | ``` 363 | ````][ 364 | #show raw.where(lang: "clst-typ"): code => codelst.sourcecode(lang: "typ", code) 365 | 366 | ```clst-typ 367 | *some* 368 | _source_ 369 | = code 370 | ```] 371 | 372 | CODELST provides two ways to get around this issue, however. One is to set up a custom language that is directly followed by a colon and the true language tag: 373 | 374 | #sourcecode[```codelst :typ 375 | *some* 376 | _source_ 377 | = code 378 | ```] 379 | 380 | This is a robust way to send anything to CODELST. But since this might prevent proper syntax highlighting in IDEs, a reversed syntax is possible: 381 | 382 | #sourcecode[```typ :codelst 383 | *some* 384 | _source_ 385 | = code 386 | ```] 387 | 388 | This will look at the first line of every `raw` text and if it matches `:codelst`, it will remove the activation tag and send the code to #cmd-[sourcecode]. 389 | 390 | Setting up one of these catchall methods is easily done by using the #cmd[codelst] function in a #var-[show] rule. Any arguments will be passed on to #cmd-[sourcecode]: 391 | 392 | #sourcecode[```typ 393 | #show: codelst( ..sourcecode-args ) 394 | 395 | // or 396 | 397 | #show: codelst( reversed: true, ..sourcecode-args ) 398 | ```] 399 | 400 | == Command overview 401 | 402 | #command( 403 | "sourcecode", 404 | ..args( 405 | lang: auto, 406 | 407 | numbering: "1", 408 | numbers-start: auto, 409 | numbers-side: left, 410 | numbers-width: auto, 411 | numbers-style: "function", 412 | numbers-first: 1, 413 | numbers-step: 1, 414 | // continue-numbering: false, 415 | 416 | gutter: 10pt, 417 | 418 | tab-indent: 2, 419 | gobble: auto, 420 | 421 | highlighted: (), 422 | highlight-color: rgb(234, 234,189), 423 | label-regex: regex("// <([a-z-]{3,})>$"), 424 | highlight-labels: false, 425 | 426 | showrange: none, 427 | showlines: false, 428 | 429 | frame: "code-frame", 430 | [code]), 431 | )[ 432 | 433 | #argument("numbering", types: ("string", "function", none), default: "1")[ 434 | A #doc("meta/numbering", name:"numbering pattern") to use for line numbers. Set to #value(none) to disable line numbers. 435 | ] 436 | 437 | #argument("numbers-start", types: (1, auto), default: auto)[ 438 | The number of the first code line. If set to #value(auto), the first line will be set to the start of #arg[showrange] or #value(1) otherwise. 439 | ] 440 | 441 | #argument("numbers-side", default: choices(left, right, default: left), types: "alignment")[ 442 | On which side of the code the line numbers should appear. 443 | ] 444 | 445 | #argument("numbers-width", types: (auto, 1pt), default: auto)[ 446 | The width of the line numbers column. Setting this to #value(auto) will measure the maximum size of the line numbers and size the column accordingly. Giving a negative length will move the numbers out of the frame into the margin. 447 | ] 448 | 449 | #argument("numbers-first", default: 1)[ 450 | The first line number to show. Compared to #arg[numbers-start], this will not change the numbers but hide all numbers before the given number. 451 | ] 452 | 453 | #argument("numbers-step", default: 1)[ 454 | The step size for line numbers. 455 | For #arg[numbers-step]: $n$ only every $n$-th line number is shown. 456 | ] 457 | 458 | #argument("numbers-style", default: "(i) => i", types: "function")[ 459 | A function of one argument to format the line numbers. Should return #dtype[content]. 460 | ] 461 | 462 | // #argument("continue-numbering", default:false)[ 463 | // If set to #value(true), the line numbers will continue from the last call of #cmd-[sourcecode]. 464 | // ] 465 | // #side-by-side[```` 466 | // #sourcecode[``` 467 | // one 468 | // two 469 | // ```] 470 | // #lorem(10) 471 | // #sourcecode(continue-numbering: true)[``` 472 | // three 473 | // four 474 | // ```] 475 | // ````] 476 | 477 | #argument("gutter", default: 10pt)[ 478 | Gutter between line numbers and code lines. 479 | ] 480 | 481 | #argument("tab-indent", default: 2)[ 482 | Number of spaces to replace tabs at the start of each line with. 483 | ] 484 | 485 | #argument("gobble", default: auto, types: (auto, "integer", "boolean"))[ 486 | How many whitespace characters to remove from each line. By default, the number is automatically determined by finding the maximum number of whitespace all lines have in common. If #arg(gobble: false), no whitespace is removed. 487 | ] 488 | 489 | #argument("highlighted", default: ())[ 490 | Line numbers to highlight. 491 | 492 | Note that the numbers will respect #arg[numbers-start]. To highlight the second line with #arg(numbers-start: 15), pass #arg(highlighted: (17,)) 493 | ] 494 | 495 | #argument("highlight-color", default: rgb(234, 234, 189))[ 496 | Color for highlighting lines. 497 | ] 498 | 499 | #argument("label-regex", types: "regular expression")[ 500 | A #dtype("regular expression") for matching labels in the source code. The default value will match labels with at least three characters at the end of lines, separated with a line comment (`//`). For example: 501 | 502 | ```typ 503 | #strong[Some text] // 504 | ``` 505 | 506 | If this line matches on a line, the full match will be removed from the output and the content of the first capture group will be used as the label's name (`my-line-label` in the example above). 507 | 508 | Note that to be valid, the expression needs to have at least one capture group. 509 | 510 | To reference a line, #cmd[lineref] should be used. 511 | ] 512 | 513 | #argument("highlight-labels", default: false)[ 514 | If set to #value(true), lines matching #arg[label-regex] will be highlighted. 515 | ] 516 | 517 | #argument("showrange", default: none, types: (none, "array"))[ 518 | If set to an array with exactly two #dtype("integer")s, the code-lines will be sliced to show only the lines within that range. 519 | 520 | For example, #arg(showrange: (5, 10)) will only show the lines 5 to 10. 521 | 522 | If settings this and #arg(numbers-start: auto), the line numbers will start at the number indicated by the first number in #arg[showrange]. Otherwise, the numbering will start as specified with #arg[numbers-start]. 523 | ] 524 | 525 | #argument("showlines", default: false)[ 526 | If set to #value(true), no blank lines will be stripped from the start and end of the code. Otherwise, those lines will be removed from the output. 527 | 528 | Line numbering will not be adjusted to the removed lines (other than with #arg[showrange]). 529 | ] 530 | 531 | #argument("frame", types: "function", default: "code-frame")[ 532 | A function of one argument to frame the source code. The default is #cmd[code-frame]. #value(none) disables any frame. 533 | ] 534 | ] 535 | 536 | #command("sourcefile", arg[code], arg(file: none), arg(lang: auto), sarg[args])[ 537 | Takes a text string #arg[code] loaded via the #cmd[read] function and passes it to #cmd-[sourcecode] for display. If #arg[file] is given, the code language is guessed by the file's extension. Otherwise, #arg[lang] can be provided explicitly. 538 | 539 | Any other #arg[args] will be passed to #cmd-[sourcecode]. 540 | 541 | #example(raw("#sourcefile(read(\"typst.toml\"), file:\"typst.toml\")"))[ 542 | #codelst.sourcefile(read("../typst.toml"), lang: "toml") 543 | ] 544 | 545 | #ibox[ 546 | The idea for #cmd-[sourcefile] was to read the provided filename without the need for the user to call #cmd-[read]. Due to the security measure, that packages can only read files from their own directory, the call to #cmd-[read] needs to happen outside #cmd-[sourcefile] in the document. 547 | 548 | For this reason, the command differs from #cmd-[sourcecode] only insofar as it accepts a #dtype("string") instead of `raw` #dtype("content"). 549 | 550 | Future releases might use the #arg[filename] for other purposes, though. 551 | 552 | To deal with this, simply add the following code to the top of your document to define a local alias for #cmd-[sourcefile]: 553 | 554 | ```typ 555 | #let codelst-sourcefile = sourcefile 556 | #let sourcefile( filename, ..args ) = codelst-sourcefile(read(filename), file:filename, ..args) 557 | ``` 558 | ] 559 | ] 560 | 561 | #command("lineref", arg[label], arg(supplement: "line"))[ 562 | Creates a reference to a code line with a label. #arg[label] is the label to reference. 563 | 564 | #example[```` 565 | #sourcecode[```java 566 | class HelloWorld { 567 | public static void main( String[] args ) { // 568 | System.out.println("Hello World!"); 569 | } 570 | } 571 | ```] 572 | 573 | See #lineref() for a main method in Java. 574 | ````][ 575 | #codelst.sourcecode[```java 576 | class HelloWorld { 577 | public static void main( String[] args ) { // 578 | System.out.println("Hello World!"); 579 | } 580 | } 581 | ```] 582 | 583 | See line 2 for a main method in Java. 584 | ] 585 | 586 | How to set labels for lines, refer to the documentation of #arg[label-regex] at #cmdref("sourcecode"). 587 | ] 588 | 589 | #command( 590 | "code-frame", 591 | ..args( 592 | fill: luma(250), 593 | stroke: 1pt + luma(200), 594 | inset: (x: 5pt, y: 10pt), 595 | radius: 4pt, 596 | [code], 597 | ), 598 | )[ 599 | Convenience function to create a #cmd-[block] to wrap code inside. The arguments are passed to #doc("layout/block"). 600 | 601 | The default values create the default gray box around source code. 602 | 603 | Should be used with the #arg[frame] argument in #cmd-[sourcecode]. 604 | 605 | #example[``` 606 | #code-frame(lorem(20)) 607 | ```] 608 | 609 | #example[```` 610 | #sourcecode( 611 | frame: code-frame.with( 612 | fill: green.lighten(90%), 613 | stroke: green 614 | ) 615 | )[```typc 616 | lorem(20) 617 | ```] 618 | ````] 619 | ] 620 | 621 | #command( 622 | "codelst", 623 | ..args( 624 | tag: "codelst", 625 | reversed: false, 626 | ), 627 | sarg[sourcecode-args], 628 | )[ 629 | Sets up a default style for raw blocks. Read @sec-catchall for details on how it works. 630 | 631 | #sourcecode[```typ 632 | #show: codelst() 633 | ```] 634 | ] 635 | 636 | = Limitations and alternatvies 637 | 638 | == Limitations and Issues 639 | 640 | To lay out the code and line numbers correctly, CODELST needs to know the available space before calculating the correct sizes. This will lead to problems when changing the layout of the code later on, for example with a #var-[show] rule. 641 | 642 | The way line numbers are laid out, the alignment might drift off for large code blocks. Page breaks are a major cause for this. If applicable, it can help to split large blocks of code into smaller chunks, for example by using #arg[showrange]. 643 | 644 | The insets for line highlights are slightly off. 645 | 646 | == Alternatives 647 | 648 | There are some alternatives to CODELST that fill similar purposes, but have more or other functionality. If CODELST does not suit your needs, one of those might do the trick. 649 | 650 | / #gitlink("platformer/typst-algorithms"): _Typst module for writing algorithms. Use the algo function for writing pseudocode and the code function for writing code blocks with line numbers._ 651 | / #gitlink("hugo-s29/typst-algo"): _This package helps you typeset [pseudo] algorithms in Typst._ 652 | 653 | -------------------------------------------------------------------------------- /src/codelst.typ: -------------------------------------------------------------------------------- 1 | 2 | #let codelst-counter = counter("@codelst-line-numbers") 3 | 4 | // Counts the number of blanks (of a specific type) 5 | // the line starts with. 6 | #let codelst-count-blanks( line, char:"\t" ) = { 7 | let m = line.match(regex("^" + char + "+")) 8 | if m != none { 9 | return m.end 10 | } else { 11 | return 0 12 | } 13 | } 14 | 15 | // Counts the maximum number of whitespace (of similar type) 16 | // all lines have in common. 17 | #let codelst-gobble-count( code-lines ) = { 18 | let gobble = 9223372036854775807 19 | let _c = none 20 | for line in code-lines { 21 | if line.trim().len() == 0 { continue } 22 | if not line.at(0) in (" ", "\t") { 23 | return 0 24 | } else { 25 | if _c == none { _c = line.at(0) } 26 | gobble = calc.min(gobble, codelst-count-blanks(line, char:_c)) 27 | } 28 | } 29 | return gobble 30 | } 31 | 32 | // Removes whitespace from the start of each line. 33 | #let codelst-gobble-blanks( code-lines, gobble ) = { 34 | if gobble == auto { 35 | gobble = codelst-gobble-count(code-lines) 36 | } 37 | 38 | // Convert tabs to spaces and remove unnecessary whitespace 39 | return code-lines.map((line) => { 40 | if line.len() > 0 { 41 | line = line.slice(gobble) 42 | } 43 | line 44 | }) 45 | } 46 | 47 | // Alias for the numbering function 48 | #let codelst-numbering = numbering 49 | 50 | // Creates a copy of the given raw element with 51 | // some optional overwrites for some options. 52 | // If a text is provided, the text from the original raw element 53 | // is ignored. 54 | #let codelst-raw-copy( code, text:none, ..overrides ) = { 55 | let args = (:) 56 | for k in ("syntaxes", "theme", "align", "lang", "block") { 57 | if code.has(k) { 58 | args.insert(k, code.at(k)) 59 | } 60 | } 61 | for (k, v) in overrides.named() { 62 | args.insert(k, v) 63 | } 64 | if text == none { 65 | raw(..args, code.text) 66 | } else { 67 | raw(..args, text) 68 | } 69 | } 70 | 71 | // Default format for code frames 72 | #let code-frame( 73 | fill: luma(250), 74 | stroke: .6pt + luma(200), 75 | inset: (x: .45em, y: .65em), 76 | radius: 3pt, 77 | clip: false, 78 | code 79 | ) = block( 80 | fill: fill, 81 | stroke: stroke, 82 | inset: inset, 83 | radius: radius, 84 | breakable: true, 85 | width: 100%, 86 | clip: clip, 87 | code 88 | ) 89 | 90 | // Default format for line numbers 91 | #let codelst-lno( lno ) = text(.8em, luma(160), raw(lno)) 92 | 93 | #let sourcecode( 94 | lang: auto, 95 | 96 | numbering: "1", 97 | numbers-start: auto, 98 | numbers-side: left, 99 | numbers-width: auto, 100 | numbers-style: codelst-lno, 101 | numbers-align: right+horizon, 102 | numbers-first: 1, 103 | numbers-step: 1, 104 | // continue-numbering: false, 105 | 106 | gutter: 10pt, 107 | 108 | tab-size: 2, 109 | gobble: auto, 110 | 111 | highlighted: (), 112 | highlight-color: rgb(234, 234,189), 113 | label-regex: regex("// <([a-z-]{3,})>$"), 114 | highlight-labels: false, 115 | 116 | showrange: none, 117 | showlines: false, 118 | 119 | frame: code-frame, 120 | syntaxes: (), 121 | theme: none, 122 | 123 | code 124 | ) = { 125 | if code.func() != raw { 126 | code = code.children.find((c) => c.func() == raw) 127 | } 128 | assert.ne(code, none, message: "Missing raw content.") 129 | 130 | let line-numbers = numbering != none 131 | let numbers-format = numbering 132 | let continue-numbering = false // TODO: How to implement? 133 | 134 | let code-lang = lang 135 | if lang == auto { 136 | if code.has("lang") { 137 | code-lang = code.lang 138 | } else { 139 | code-lang = "plain" 140 | } 141 | } 142 | let code-lines = code.text.split("\n") 143 | let line-count = code-lines.len() 144 | 145 | // Reduce lines to range 146 | if showrange != none { 147 | assert.eq(showrange.len(), 2) 148 | showrange = ( 149 | calc.clamp(calc.min(..showrange), 1, line-count) - 1, 150 | calc.clamp(calc.max(..showrange), 1, line-count) 151 | ) 152 | code-lines = code-lines.slice(..showrange) 153 | if numbers-start == auto { 154 | numbers-start = showrange.first() + 1 155 | } 156 | } else { 157 | showrange = (0, line-count) 158 | } 159 | 160 | // Starting line number 161 | if numbers-start == auto { 162 | numbers-start = 1 163 | if not continue-numbering { 164 | codelst-counter.update(0) 165 | } 166 | } else { 167 | codelst-counter.update(numbers-start - 1) 168 | } 169 | 170 | if not showlines { 171 | let trim-start = code-lines.position((line) => line.trim() != "") 172 | let trim-end = code-lines.rev().position((line) => line.trim() != "") 173 | showrange = (showrange.first() + trim-start, showrange.last() - trim-end) 174 | code-lines = code-lines.slice(trim-start, code-lines.len() - trim-end) 175 | numbers-start = numbers-start + trim-start 176 | } 177 | 178 | // Parse labels 179 | let labels = (:) 180 | if label-regex != none { 181 | for (i, line) in code-lines.enumerate() { 182 | let m = line.match(label-regex) 183 | if m != none { 184 | labels.insert(str(i + numbers-start), m.captures.at(0)) 185 | if highlight-labels { 186 | highlighted.push(i + numbers-start) 187 | } 188 | } 189 | } 190 | } 191 | 192 | if frame == none { 193 | frame = (b) => b 194 | } 195 | 196 | show raw.where(block: true): it => { 197 | let code-lines = it.lines.slice( 198 | showrange.first(), 199 | calc.min( 200 | showrange.last(), 201 | it.lines.len() 202 | ) 203 | ) 204 | // TODO: Somehow one blank line gets removed from the raw text (seems like a bug). This adds them back, if necessary. 205 | if showrange.last() > it.lines.len() { 206 | code-lines += ("",) * (showrange.last() - it.lines.len()) 207 | } 208 | let line-count = code-lines.len() 209 | 210 | // Numbering function 211 | let next-lno() = { 212 | codelst-counter.step() 213 | context codelst-counter.display((lno) => [ 214 | #if lno >= numbers-first and calc.rem(lno - numbers-first, numbers-step) == 0 [ 215 | #numbers-style(codelst-numbering(numbering, lno)) 216 | ] 217 | #if str(lno) in labels { label(labels.at(str(lno))) } 218 | ]) 219 | } 220 | 221 | let code-table = context { 222 | // Measure the maximum width of the line numbers 223 | // We need to measure every line, since the numbers 224 | // are styled and could have unexpected formatting 225 | // (e.g. line 10 is extra big) 226 | let numbers-width = numbers-width 227 | if numbering != none and numbers-width == auto { 228 | numbers-width = calc.max( 229 | ..{(0pt,) + range(numbers-first - 1, line-count, step:numbers-step).map((lno) => measure( 230 | numbers-style(codelst-numbering(numbering, lno + numbers-start)) 231 | ).width)} 232 | ) + .1em 233 | } 234 | 235 | table( 236 | columns: if numbering == none { 237 | (1fr,) 238 | } else if numbers-side != right { 239 | (numbers-width, gutter, 1fr) 240 | } else { 241 | (1fr, gutter, numbers-width) 242 | }, 243 | column-gutter: 0pt, 244 | row-gutter: 0pt, 245 | 246 | stroke:none, 247 | inset: (x: 0pt, y: .25em), 248 | 249 | fill: (c, r) => { 250 | if r + numbers-start in highlighted { 251 | highlight-color 252 | } else { 253 | none 254 | } 255 | }, 256 | align: (c, r) => { 257 | if numbering != none { 258 | if numbers-side != right and c == 0 or (numbers-side == right and c == 2) { 259 | return numbers-align 260 | } 261 | } 262 | return start 263 | }, 264 | 265 | ..if numbering == none { 266 | code-lines 267 | } else if numbers-side != right { 268 | code-lines.map((l) => (next-lno(), none, l)).flatten() 269 | } else { 270 | code-lines.map((l) => (l, none, next-lno())).flatten() 271 | } 272 | ) 273 | } 274 | 275 | frame[ 276 | #set align(start) 277 | #code-table 278 | ] 279 | } 280 | 281 | 282 | // Create actual raw element 283 | //// Prepare code text 284 | code-lines = code.text.split("\n") 285 | 286 | ///// Gobble whitespace from the start of lines 287 | code-lines = codelst-gobble-blanks(code-lines, gobble) 288 | ///// Remove labels 289 | code-lines = code-lines.map((line) => line.replace(label-regex, "")) 290 | 291 | let code-opts = ( 292 | block: true, 293 | lang: code-lang, 294 | syntaxes: syntaxes, 295 | tab-size: tab-size 296 | ) 297 | if theme != none { 298 | code-opts.insert("theme", theme) 299 | } 300 | raw( 301 | ..code-opts, 302 | code-lines.join("\n") 303 | ) 304 | } 305 | 306 | #let sourcefile( code, file:none, lang:auto, ..args ) = { 307 | if file != none and lang == auto { 308 | let m = file.match(regex("\.([a-z0-9]+)$")) 309 | if m != none { 310 | lang = m.captures.first() 311 | } 312 | } else if lang == auto { 313 | lang = "plain" 314 | } 315 | sourcecode( ..args, raw(code, lang:lang, block:true)) 316 | } 317 | 318 | #let lineref( label, supplement:"line" ) = context { 319 | let lines = query(selector(label)) 320 | assert.ne(lines, (), message: "Label <" + str(label) + "> does not exists.") 321 | [#supplement #{context numbering("1", ..codelst-counter.at(lines.first().location()))}] 322 | } 323 | 324 | #let codelst-styles( body ) = { 325 | show figure.where(kind: raw): set block(breakable: true) 326 | 327 | body 328 | } 329 | 330 | #let codelst( 331 | tag: "codelst", 332 | reversed: false, 333 | ..args 334 | ) = { 335 | if not reversed { 336 | return (body) => { 337 | show raw.where(lang: tag): (code) => { 338 | let code-lines = code.text.split("\n") 339 | let lang = code-lines.remove(0).trim().slice(1) 340 | sourcecode(..args, 341 | codelst-raw-copy(code, text:code-lines.join("\n")) 342 | ) 343 | } 344 | body 345 | } 346 | } else { 347 | return (body) => { 348 | show raw: (code) => { 349 | if code.text.starts-with(":" + tag) { 350 | sourcecode(..args, codelst-raw-copy(code, text:code.text.slice(tag.len() + 1))) 351 | } else { 352 | code 353 | } 354 | } 355 | body 356 | } 357 | } 358 | } 359 | -------------------------------------------------------------------------------- /tbump.toml: -------------------------------------------------------------------------------- 1 | # Uncomment this if your project is hosted on GitHub: 2 | github_url = "https://github.com/jneug/typst-codelst/" 3 | 4 | [version] 5 | current = "2.0.2" 6 | 7 | # Example of a semver regexp. 8 | # Make sure this matches current_version before 9 | # using tbump 10 | regex = ''' 11 | (?P\d+) 12 | \. 13 | (?P\d+) 14 | \. 15 | (?P\d+) 16 | ''' 17 | 18 | [git] 19 | message_template = "Bump to {new_version}" 20 | tag_template = "v{new_version}" 21 | 22 | [[file]] 23 | src = "typst.toml" 24 | search = 'version = "{current_version}"' 25 | 26 | [[file]] 27 | src = "README.md" 28 | search = '\(v{current_version}\)' 29 | [[file]] 30 | src = "README.md" 31 | search = "codelst:{current_version}" 32 | 33 | [[file]] 34 | src = "docs/manual.typ" 35 | search = 'version:\s*"{current_version}",' 36 | [[file]] 37 | src = "docs/manual.typ" 38 | search = 'codelst:{current_version}' 39 | [[file]] 40 | src = "docs/manual.typ" 41 | search = 'codelst-{current_version}' 42 | 43 | [[before_commit]] 44 | name = "compile manual" 45 | cmd = "typst compile --root . docs/manual.typ" 46 | [[before_commit]] 47 | name = "compile examples" 48 | cmd = "typst compile --root . docs/example.typ" 49 | 50 | -------------------------------------------------------------------------------- /tests/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jneug/typst-codelst/2374c6c6a8035e12d0f340ed7fe65858e9648019/tests/.DS_Store -------------------------------------------------------------------------------- /tests/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jneug/typst-codelst/2374c6c6a8035e12d0f340ed7fe65858e9648019/tests/.gitkeep -------------------------------------------------------------------------------- /tests/gobble/.gitignore: -------------------------------------------------------------------------------- 1 | # added by typst-test 2 | -------------------------------------------------------------------------------- /tests/gobble/ref/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jneug/typst-codelst/2374c6c6a8035e12d0f340ed7fe65858e9648019/tests/gobble/ref/1.png -------------------------------------------------------------------------------- /tests/gobble/test.typ: -------------------------------------------------------------------------------- 1 | Hello World 2 | -------------------------------------------------------------------------------- /tests/test-spaces.txt: -------------------------------------------------------------------------------- 1 | /* 2 | * Example taken from 3 | * https://typst.app/docs/tutorial/formatting/ 4 | */ 5 | #show "ArtosFlow": name => box[ 6 | #box(image( 7 | "logo.svg", 8 | height: 0.7em, 9 | )) 10 | #name 11 | ] 12 | 13 | // Long line that breaks 14 | This report is embedded in the ArtosFlow project. ArtosFlow is a project of the Artos Institute. 15 | 16 | /* 17 | Very long line without linebreak 18 | with a preceeding block comment 19 | */ 20 | This_report_is_embedded_in_the_ArtosFlow_project._ArtosFlow_is_a_project_of_the_Artos_Institute. 21 | 22 | // End example 23 | -------------------------------------------------------------------------------- /tests/test-tabs.txt: -------------------------------------------------------------------------------- 1 | /* 2 | * Example taken from 3 | * https://typst.app/docs/tutorial/formatting/ 4 | */ 5 | #show "ArtosFlow": name => box[ 6 | #box(image( 7 | "logo.svg", 8 | height: 0.7em, 9 | )) 10 | #name 11 | ] 12 | 13 | // Long line that breaks 14 | This report is embedded in the ArtosFlow project. ArtosFlow is a project of the Artos Institute. 15 | 16 | /* 17 | Very long line without linebreak 18 | with a preceeding block comment 19 | */ 20 | This_report_is_embedded_in_the_ArtosFlow_project._ArtosFlow_is_a_project_of_the_Artos_Institute. 21 | 22 | // End example 23 | -------------------------------------------------------------------------------- /tests/test1/ref/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jneug/typst-codelst/2374c6c6a8035e12d0f340ed7fe65858e9648019/tests/test1/ref/1.png -------------------------------------------------------------------------------- /tests/test1/test.typ: -------------------------------------------------------------------------------- 1 | Hello World 2 | -------------------------------------------------------------------------------- /typst.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "codelst" 3 | version = "2.0.2" 4 | entrypoint = "src/codelst.typ" 5 | authors = ["Jonas Neugebauer"] 6 | license = "MIT" 7 | description = "A typst package to render sourcecode." 8 | homepage = "https://github.com/jneug/typst-codelst" 9 | repository = "https://github.com/jneug/typst-codelst" 10 | compiler = "0.12.0" 11 | categories = ["components", "layout"] 12 | disciplines = ["computer-science", "mathematics", "education", "linguistics"] 13 | keywords = ["sourcecode", "code", "syntax-highlighting", "raw", "line numbers"] 14 | exclude = ["README.md", "CHANGELOG.md", "Justfile", "docs", "example.typ", "example.pdf", "manual.pdf", "manual.typ", "tbump.toml"] 15 | --------------------------------------------------------------------------------