typst / typst

A new markup-based typesetting system that is powerful and easy to learn.
https://typst.app
Apache License 2.0
35.76k stars 954 forks source link

Directory walking #2123

Open RuofengX opened 1 year ago

RuofengX commented 1 year ago

Description

I want use typst to compose type that "3 screenshots per page, landscape, images from a folder".

I want typest to behavior like css but the output is pdf. When I trying to achieve this, there are some missing future:

Use Case

Use typst as a universal type set tool.

RuofengX commented 1 year ago
#set page("a4", flipped: true)

#let cell = rect.with(
  inset: 8pt,
)

#grid(
  columns: (auto, auto, auto),
  rows: (100%),
  gutter: 8pt,
  cell[#image("./images/1.png")],
  cell[#image("./images/2.png")],
  cell[#image("./images/3.png")],
  cell[#image("./images/4.png")],
  cell[#image("./images/5.png")],
  cell[#image("./images/6.png")],
  cell[#image("./images/7.png")],
)

Such code will compose some paper of 7 images on it, 3 per page. But I have a folder of many screenshots, maybe 100 pictures. Is there some tool(package or module) could handle these scanery?

LuxxxLucy commented 1 year ago

I recommend try with the preprocessing approach (using "typst-pyimage") first.

LuxxxLucy commented 1 year ago

@laurmaedje do we want to implement a function for directory walking?

Maybe adding a interface named "list_dir" which takes a path and returns an array?

laurmaedje commented 1 year ago

I'm not yet completely decided, but I'm open to discussing it. It should of course be restricted to the project/package directory like file loading. And there are further design decisions about what exactly it returns (what about folders for instance: is there a way to tell whether an array item is a file or folder). And then we need a good and efficient interface for the World trait.

LuxxxLucy commented 1 year ago

How about a simple design like this (two new function)

  1. list_dir(path: str): returns a list of strings under the given path, which includes files and sub-folders.
  2. is_dir(path: str): returns whether a path is a directory.

I think this should be a minimum design; if a path is not a directory but a filename, then we should use functions such as "read" (we might also need to add some debug log and throw the error should we read a directory, instead of a file).

DrakeAxelrod commented 1 year ago

My use case for the capability to walk a directory has to do with sorting files based on content and then sorting based on a metric. The requirement for this functionality is for writing a penetration testing report in which I need to walk through a directory and import/include/sort findings based on cvss scores.

NZRosto commented 10 months ago

Not sure if this is still on the table, but I thought I'd just mention my use case on the off-chance it helps anyone.

I am designing a recipe book, each recipe is a Yaml file in a sub-dir of the project. My Typst book iterates through the recipes to produce the formatted output. It currently does this with a hard-coded set of paths, but it would be nice to be able to programmatically walk through all the files in that sub-dir that's effectively part of the "source" of the project.

I did think about making the recipes Typst files instead of Yaml, but that wouldn't change much as there does not seem to be any form of glob importing currently. If there is a neat way of doing this already, I don't know about it sorry.

Anyway just my two cents, I still think this issue is relevant!

lucifer1004 commented 5 months ago

Hi guys, I just made PR #4380 which provides a readdir function. This function returns the names of a directory's contents as an array of strings.

#readdir("test-folder")

renders something like below

("a.txt", "c", "b.json")

Thus we can do things like

#for file in files {
  if file.ends-with(".txt") {
    read("test-folder/" + file)
  }
}
Doom4535 commented 1 day ago

I'd also like to have this ability for automatically including the contents of certain folders into an appendix; my current approach is:

= Appendix
#show: appendix
== Matlab Code
=== ber_curves.m
#let text = read("matlab/ber_curves.m")
#raw(text, lang: "matlab")
=== Exsample_aliasing.m
#let text = read("matlab/Exsample_aliasing.m")
#raw(text, lang: "matlab")
...

I would much rather be able to just point it at the matlab folder and have it add an entry for each file that is present there.