jaspervdj / patat

Terminal-based presentations using Pandoc
GNU General Public License v2.0
2.37k stars 60 forks source link

Evaluation of PHP code #163

Closed mattsches closed 5 months ago

mattsches commented 5 months ago

I'm struggling to evaluate PHP code. I suppose the correct command would be php -r or php --run.

---
patat:
  eval:
    php:
      command: php -r
      fragment: true
      replace: false
      wrap: code
...

# PHP Example

```php
$foo = get_defined_constants();

results in:

# PHP Example

    $foo = get_defined_constants(); 

     php -r : exit code 1                                 
    Error in argument 1, char 2: no argument for option r 

This is probably because of command line variable substitution done by shell as mentioned in the note here (scroll down to the -r --run option).

Unfortunately, my shell foo is not strong enough to resolve this in the context of patat (or on the command line). Any ideas?

jaspervdj commented 5 months ago

There's probably many ways to do this... One solution would be to wrap the input (cat -) in <? ... ?> and pipe that to PHP:

---
patat:
  eval:
    php:
      command: '(echo "<?"; cat -; echo "?>") | php'
...

# Hello world

Some PHP:

```php
print("Hello, world!");
```
mattsches commented 5 months ago

Sorry, it's not working for me. The output is:

# Code Example

    print("Hello, world!"); 

    <?                        
    print("Hello, world!");?> 

I wonder if there's a way to replace double quotes with single quotes like so: php -r '$foo = get_defined_constants(); var_dump($foo);'? I can't find a way to achieve this.

It's probably not even a patat issue, but more of a general bash issue I guess.

mattsches commented 5 months ago

I figured out that my PHP installation has disabled the short_open_tags setting (which is the default nowadays, I guess, but am not sure), so this now works for me:

---
patat:
  eval:
    php:
      command: '(echo "<?php"; cat -; echo "?>") | php'
...

# Hello world

Some PHP:

```php
print("Hello, world!");

Thanks a lot for your support! :smile: