pietroppeter / nimib

nimib 🐳 - nim 👑 driven ⛵ publishing ✍
https://pietroppeter.github.io/nimib/
MIT License
175 stars 10 forks source link

Code shown with `nbJsShowSource` is not as in source (but comes from ast) #154

Closed pietroppeter closed 1 year ago

pietroppeter commented 1 year ago

I am using nbJsShowSource for showing p5js examples (see e.g. https://pietroppeter.github.io/nim-p5/polygons.html). The code comes from ast instead of being read from source, with all classical problems (no comments and weird indents).

HugoGranstrom commented 1 year ago

Ah right, I opted for not using codeAsInSource for nbJs because of the composability issues. But using it just for the code shown by nbJsShowSource could work. How does that sound like?

pietroppeter commented 1 year ago

yep, that sounds fine!

HugoGranstrom commented 1 year ago

Hmm thinking a bit more about it, I don't think we should do this actually. If the nbJs block is defined in another file, codeAsInSource will not be able to locate it and error, which is bad. The error is because codeAsInSource only looks in the current file. The problem is that the current way we are doing this is to mark the block as showSource after it has been created. Hence we would have to run the codeAsInSource for every single nbJs block because the user may want to show it. Do you see the problem?

So the solution imo is to just do a nimibCode with the nbJsFromCode instead if you really care about formatting:

nimibCode:
  nbJsFromCode:
    # Comment
    echo "Hello world"

# vs 

nbJsFromCode:
  # Comment
  echo "Hello world"

nbJsShowSource()
pietroppeter commented 1 year ago

yeah, I guess you are right, it complicates stuff too much. So, now I am thinking, should we remove (with a deprecation period) nbJsShowSource? I am having trouble thinking what is the problem it tries to solve... (not showing code, since it is suboptimal for it). I am looking at the source and it complicates a lot of stuff (we support the message, we have special support for rendering...). we did not have nimibCode before, but now we do...

HugoGranstrom commented 1 year ago

I agree, nimibCode makes nbJsShowSource deprecated. There's no hurry to get this done, we just have to remember to include this in the next release.

pietroppeter commented 1 year ago

mmh, in p5nim I am not sure I want to use nimibCode since it would add the additional noise of showing nbJsFromCode and an additional layer of indentation. I was thinking of adding an additional nbJsFromCodeDisplay (or another name if we find a better one) maybe like this:

template nbJsFromCodeDisplay*(args: varargs[untyped]):
  newNbCodeBlock("nbJsFromCodeDisplay", args[^1]):
    discard
  nbJsFromCode(args)

I would then need to add partials and renderPlan as in nbCode.

HugoGranstrom commented 1 year ago

Yeah, if we are going to support it, this seems like the easiest way 👍

pietroppeter commented 1 year ago

an even easier way that generalizes to other templates (restricted to template with single body argument, but you should always be able to wrap a more complex template in one with a single body argument) implement with nbCodeDisplay in #158