logological / gpp

GPP, a generic preprocessor
https://logological.org/gpp
GNU Lesser General Public License v3.0
200 stars 31 forks source link

Ability to #include a file with all lines indented #68

Open computator opened 4 months ago

computator commented 4 months ago

I would like to be able to include one file inside another, but have the contents of the included file all indented by a certain amount. For example:

test.inc

multiline
example

test.yaml

example:
  content: |
    #include_indented test.inc
  other: asdf

Intended result:

example:
  content: |
    multiline
    example
  other: asdf

I am not sure if this is currently possible with gpp only? If so, I couldn't figure out how. Perhaps there's a way to use the output of include in a macro to then prefix all the lines with a string?

The only solution I could come up with so far is using exec, which I would prefer to avoid if possible.

logological commented 3 months ago

There's no built-in support for this, though your YAML example makes for a compelling use case.

I suspect that you could kludge together a solution that defines the newline itself as a macro that echoes a certain number of spaces followed by its argument. For this you'd need to define a new mode that allows the newline to be a macro name, and everything that follows it up to the next newline its argument. You'd have to activate this mode before the include macro, and probably turn it off again afterwards. However, I don't have time to experiment to test this suspicion.

Probably exec would be a simpler solution. (Either that, or I actually implement an include_indented meta-macro, though I won't have time to do that soon.)

computator commented 3 months ago

Thanks for the response. I might have to try that out!