You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.2 KiB

  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use 5.10.0;
  5. use Cwd;
  6. use File::Basename;
  7. use Text::Markdown::Discount;
  8. use IPC::System::Simple qw(capturex);
  9. # Enable html5 block-level tags:
  10. Text::Markdown::Discount::with_html5_tags();
  11. my $flags = Text::Markdown::Discount::MKD_EXTRA_FOOTNOTE();
  12. my $markdown = Text::Markdown::Discount->new;
  13. my $cwd = Cwd::getcwd();
  14. my $full_source = '';
  15. while (my $source = get_input()) {
  16. # A simple preprocessor:
  17. my ($basename, $dir) = fileparse($ARGV); # get path of target file
  18. chdir $dir;
  19. $source =~ s{```php(.*?)```}{handle_block($1);}egs;
  20. chdir $cwd;
  21. $full_source .= $source;
  22. }
  23. print $full_source;
  24. # actually spit out the contents of the slide
  25. sub get_input {
  26. local $/ = undef;
  27. my $source = <>;
  28. return $source;
  29. }
  30. sub handle_block {
  31. my ($block) = @_;
  32. my $cmd;
  33. file_put_contents('/tmp/phpslideoutput.php', $block);
  34. my $result = `php /tmp/phpslideoutput.php`;
  35. chomp($result);
  36. if ($result) {
  37. return "```php${block}```\n\nOutput:\n\n```${result}\n```";
  38. } else {
  39. return "```php${block}```";
  40. }
  41. }
  42. sub file_put_contents {
  43. my ($file, $contents) = @_;
  44. open(my $fh, '>', $file)
  45. or die "Unable to open $file for writing: $!";
  46. print $fh $contents;
  47. close $fh;
  48. }