CDSoft / panda

Panda is a Pandoc Lua filter that works on internal Pandoc's AST. Panda is heavily inspired by [abp](http:/cdelord.fr/abp) reimplemented as a Pandoc Lua filter.
https://cdelord.fr/panda
GNU General Public License v3.0
44 stars 5 forks source link

`cmd` allow to specify file extension #11

Closed lontivero closed 2 years ago

lontivero commented 2 years ago

For some absurd/historical reason the F# interactive console for scripts requires the name of the script files to end the extension .fsx. That means that something that should work perfectly well like this:

```{.fsx cmd="dotnet fsi" }
open System
Console.WriteLine("Hello from F#!")
```

Simply doesn't work. Instead I get the following error:

"~/Documents/Wiki/src/wasabi/wasabito_compact_filters.md" 50L, 729B written
^@^@error FS0226: The file extension of '/tmp/panda_script-57a468d967123abc/script.dotnet' is not r
ecognized. Source files must have extension .fs, .fsi, .fsx, .fsscript, .ml or .mli.^@^@Error runni
ng filter /home/lontivero/.local/bin/panda.lua:^@PandocLuaError "/home/lontivero/.local/bin/panda.l
ua:434: script error"^@stack traceback:^@^I/home/lontivero/.local/bin/panda.lua:422: in upvalue 'ru
n_script'^@^I/home/lontivero/.local/bin/panda.lua:446: in function </home/lontivero/.local/bin/pand
a.lua:440>^@
Press ENTER or type command to continue

I don't see any obvious way to make panda to save the script with a given extension.


Just to prove the problem is the script file extension, if I hardcode th extension .fsx in the run_script function as follow:

image

It works:

image

lontivero commented 2 years ago

I created an executable script called fsx:

dotnet fsi --exec "$@"

And I have added it to the PATH. This works because panda adds then the .fsx extension to the file. But it feels somehow wrong to me.

CDSoft commented 2 years ago

I guess this is due to an old Microsoft convention to define the file type according to its file extension. On Unix/Linux, the file type is given by the file content which is way simpler.

Panda has a very bad heuristic to guess the appropriate extension that may only work for cmd.exe (the extension is the name of the interpretor without its extension (cmd.exe -> .cmd).

I can change this "heuristic" by an easier one. For diagrams, the image extension can be explicitly given in the command (e.g. %o.png).

Then it would be possible to write {.fsx cmd="dotnet fsi %s.fsi" } (%s.fsi would be the name of a temporary file with the extension .fsi).

lontivero commented 2 years ago

Yes! I like that idea. FTR: F# inherited these conventions from OCaml so, this time is not MS.


Update: ocaml scripts work okay regardless of the extension so, it is MS

CDSoft commented 2 years ago

I pushed a small update with explicit extension in the command line and a few predefined extensions.

{cmd=python} is equivalent to {cmd="python %s"} and {cmd="python %s.py"}

Some extensions are predefined too for Python, Lua, shell scripts, cmd for Windows and F#. I.e. {cmd="dotnet fsi"} is equivalent to {cmd="dotnet fsi %s"} and {cmd="dotnet fsi %s.fs"}.

lontivero commented 2 years ago

Tested. Just a clarification: the extension for f# scripts is .fsx (.fs is also a f# extension but for files that are part of a project) so, i think it would be better to change. Anyway, this is much better now and this works excellently:

```{.fsx icmd="dotnet fsi --exec %s.fsx" }
open System
Console.WriteLine("Thank you @CDSoft!");
```
CDSoft commented 2 years ago

Ok, I'll fix the .fsx extension.