-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2023 OrangeX4 <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Tablem | ||
|
||
Write markdown-like tables easily. | ||
|
||
## Example | ||
|
||
Have a look at the source [here](./examples/example.typ). | ||
|
||
![Example](./examples/example.png) | ||
|
||
|
||
## Usage | ||
|
||
You can simply copy the markdown table and paste it in `tablem` function. | ||
|
||
```typ | ||
#import "@preview/tablem:0.1.0": tablem | ||
#tablem[ | ||
| *Name* | *Location* | *Height* | *Score* | | ||
| ------ | ---------- | -------- | ------- | | ||
| John | Second St. | 180 cm | 5 | | ||
| Wally | Third Av. | 160 cm | 10 | | ||
] | ||
``` | ||
|
||
And you can use custom render function. | ||
|
||
```typ | ||
#import "@preview/tablex:0.0.6": tablex, hlinex | ||
#import "@preview/tablem:0.1.0": tablem | ||
#let three-line-table = tablem.with( | ||
render: (columns: auto, ..args) => { | ||
tablex( | ||
columns: columns, | ||
auto-lines: false, | ||
align: center + horizon, | ||
hlinex(y: 0), | ||
hlinex(y: 1), | ||
..args, | ||
hlinex(), | ||
) | ||
} | ||
) | ||
#three-line-table[ | ||
| *Name* | *Location* | *Height* | *Score* | | ||
| ------ | ---------- | -------- | ------- | | ||
| John | Second St. | 180 cm | 5 | | ||
| Wally | Third Av. | 160 cm | 10 | | ||
] | ||
``` | ||
|
||
![Example](./examples/example.png) | ||
|
||
|
||
## `tablem` function | ||
|
||
```typ | ||
#let tablem( | ||
render: table, | ||
ignore-second-row: true, | ||
..args, | ||
body | ||
) = { .. } | ||
``` | ||
|
||
**Arguments:** | ||
|
||
- `render`: [`(columns: int, ..args) => { .. }`] — Custom render function, default to be `table`, receiving a integer-type columns, which is the count of first row. `..args` is the combination of `args` of `tablem` function and children genenerated from `body`. | ||
- `ignore-second-row`: [`boolean`] — Whether to ignore the second row (something like `|---|`). | ||
- `args`: [`any`] — Some arguments you want to pass to `render` function. | ||
- `body`: [`content`] — The markdown-like table. There should be no extra line breaks in it. | ||
|
||
|
||
## Limitations | ||
|
||
Cell merging has not yet been implemented. | ||
|
||
|
||
## License | ||
|
||
This project is licensed under the MIT License. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#import "@preview/tablex:0.0.6": tablex, hlinex | ||
#import "../tablem.typ": tablem | ||
|
||
#set page(width: 30em, height: auto) | ||
|
||
#tablem[ | ||
| *Name* | *Location* | *Height* | *Score* | | ||
| ------ | ---------- | -------- | ------- | | ||
| John | Second St. | 180 cm | 5 | | ||
| Wally | Third Av. | 160 cm | 10 | | ||
] | ||
|
||
#let three-line-table = tablem.with( | ||
render: (columns: auto, ..args) => { | ||
tablex( | ||
columns: columns, | ||
auto-lines: false, | ||
align: center + horizon, | ||
hlinex(y: 0), | ||
hlinex(y: 1), | ||
..args, | ||
hlinex(), | ||
) | ||
} | ||
) | ||
|
||
#three-line-table[ | ||
| *Name* | *Location* | *Height* | *Score* | | ||
| ------ | ---------- | -------- | ------- | | ||
| John | Second St. | 180 cm | 5 | | ||
| Wally | Third Av. | 160 cm | 10 | | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// convert a sequence to a array splited by "|" | ||
#let _tablem-tokenize(seq) = { | ||
let res = () | ||
for cont in seq.children { | ||
if cont.func() == text { | ||
res = res + cont.text | ||
.split("|") | ||
.map(s => text(s.trim())) | ||
.intersperse([|]) | ||
.filter(it => it.text != "") | ||
} else { | ||
res.push(cont) | ||
} | ||
} | ||
res | ||
} | ||
|
||
// trim first or last empty space content from array | ||
#let _arr-trim(arr) = { | ||
if arr.len() >= 2 { | ||
if arr.first() == [ ] { | ||
arr = arr.slice(1) | ||
} | ||
if arr.last() == [ ] { | ||
arr = arr.slice(0, -1) | ||
} | ||
arr | ||
} else { | ||
arr | ||
} | ||
} | ||
|
||
// compose table cells | ||
#let _tablem-compose(arr) = { | ||
let res = () | ||
let column-num = 0 | ||
res = arr | ||
.split([|]) | ||
.map(_arr-trim) | ||
.map(subarr => subarr.sum(default: [])) | ||
_arr-trim(res) | ||
} | ||
|
||
#let tablem( | ||
render: table, | ||
ignore-second-row: true, | ||
..args, | ||
body | ||
) = { | ||
let arr = _tablem-compose(_tablem-tokenize(body)) | ||
// use the count of first row as columns | ||
let columns = 0 | ||
for item in arr { | ||
if item == [ ] { | ||
break | ||
} | ||
columns += 1 | ||
} | ||
// split rows with [ ] | ||
let res = () | ||
let count = 0 | ||
for item in arr { | ||
if count < columns { | ||
res.push(item) | ||
count += 1 | ||
} else { | ||
assert.eq(item, [ ], message: "There should be a linebreak.") | ||
count = 0 | ||
} | ||
} | ||
// remove secound row | ||
if (ignore-second-row) { | ||
let len = res.len() | ||
res = res.slice(0, calc.min(columns, len)) + res.slice(calc.min(2 * columns, len)) | ||
} | ||
// render with custom render function | ||
render(columns: columns, ..args, ..res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "tablem" | ||
version = "0.1.0" | ||
authors = ["OrangeX4"] | ||
entrypoint = "tablem.typ" | ||
exclude = ["./examples"] | ||
keywords = ["tablem", "markdown", "table"] | ||
license = "MIT" | ||
repository = "https://github.com/OrangeX4/typst-tablem" | ||
description = "Write markdown-like tables easily." |