sile-typesetter / sile-typesetter.github.io

Github Pages source code for https://sile-typesetter.org
Creative Commons Zero v1.0 Universal
3 stars 4 forks source link

I can't run byhand.lua #40

Closed jodros closed 1 year ago

jodros commented 1 year ago

Hi, I'm trying to use SILE as a library as told in its manual but getting the same error every time:

lua: byhand.lua:29: attempt to call a nil value (field 'typesetter')
stack traceback:
        byhand.lua:29: in main chunk
        [C]: in ?
alerque commented 1 year ago

The byhand.lua example is commented out from the website examples because it is known to be broken right now. This issue on SILE explains a little bit of why.

It wouldn't take that much work to fix this, there just hasn't been a huge motivation. SILE has gotten a lot more modular since that issue was opened and it would be much less work to fix now than it was when we broken it.

To summarize that issue and what you would have to do if you want to write a byhand.lua example today, you basically need to copy most of the content of the main script sile. The current byhand.lua source code has a couple of rough path handling blurbs, but the current sile script has basically the first 75 lines devoted to setting up Lua paths. To run a library script you'd need about 2/3 of those, although some of the complexity shows is because those paths cover the bases for when SILE is installed using system Lua libraries and when it is bundled with vendored copies. Presumably for your case you'd only need one or the other. You can also skip a few other bits, but generally those paths need to be integrated into your Lua runtime environment somehow.

Then you can cherry pick the handful of lines relevant to your situation from the remaining 75 lines. Looking it over it looks like the biggest change since byhand.lua was touched is the class instantiator is different now...

You know what? It would take me less time to fix this than explain it. Stand by.

alerque commented 1 year ago

Done. You should be able to use this file and run lua byhand.lua byhand.pdf to get a PDF file.

I will note there are a couple of ways this approach is a bit hacky that we'd still like to fix:

  1. That big block at the top noted as being copied from sile itself should get stashed somewhere in sile it can be reused without mixing it up with the CLI handling code.

  2. This only works if you have SILE installed as an app and just side load it into an existing Lua script. It isn't exactly packaged and isolated for us as a library that you could, say, install with luarocks install sile and integrate into your other projects without install hastles. I've wanted to finish sorting out SILE's internals so that would be possible. We're closer than we were a year ago but not quite there yet.

  3. There is a new alternative that is probably much nicer to use. Recently I've added a Lua inputter so that any time SILE would read an input file as SIL or XML it can also read LUA. The Lua code can return a string (and rely on CLI options to pass document class and options), or it can return a table that is an AST to be processed (or a module, but that's not relevant here), or it can return a function. Returning a function is basically like embedding the little bit of code from the byhand.lua example that is not copied from the CLI interface and just using the CLI to build it. I just added an example to to this repo called luainput.lua and you can generate a PDF from it with:

    $ sile luainput.lua -o output.pdf

    You should get basically the same output as the byhand.lua example with a lot less mess.

jodros commented 1 year ago

Great Thank you very much!