| 1 | package YAML::YAML2ADoc; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use v5.32; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use experimental qw(switch); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | our $VERSION = '0.1.0'; | ||
| 8 | |||
| 9 | 0 | ▶ | use enum qw(TEXT YAML); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use constant { | ||
| 11 | SOURCE_MARKER => "----\n", | ||
| 12 | SOURCE_STYLE => "[source,yaml]\n", | ||
| 13 | }; | ||
| 14 | |||
| 15 | sub strip_comment { return $_[0] =~ s/^\h*#\h*//r } | ||
| 16 | |||
| 17 | sub run { | ||
| 18 | my ($output, $inputs_ref) = @_; | ||
| 19 | @ARGV = @$inputs_ref; | ||
| 20 | |||
| 21 | if ($output ne '-') { | ||
| 22 | # Change the default handler for print to the file | ||
| 23 | open my $out, '>', $output | ||
| 24 | or die "Failed to open output $output $!"; | ||
| 25 | select $out; | ||
| 26 | } | ||
| 27 | |||
| 28 | my $state = TEXT; | ||
| 29 | while (my $line = <<>>) { | ||
| 30 | given ($state) { | ||
| 31 | when (TEXT) { | ||
| 32 | given ($line) { | ||
| 33 | # empty lines are ignored | ||
| 34 | when (/^$/) { next } | ||
| 35 | # still in TEXT | ||
| 36 | when (/^\h*#/) { print strip_comment($line) } | ||
| 37 | # leaving text | ||
| 38 | default { | ||
| 39 | print "\n", SOURCE_STYLE, SOURCE_MARKER, $line; | ||
| 40 | $state = YAML; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | when (YAML) { | ||
| 45 | given ($line) { | ||
| 46 | # empty lines are ignored | ||
| 47 | when (/^$/) { next } | ||
| 48 | # leaving YAML | ||
| 49 | when (/^\h*#/) { | ||
| 50 | print SOURCE_MARKER, "\n", strip_comment($line); | ||
| 51 | $state = TEXT; | ||
| 52 | } | ||
| 53 | # still in YAML | ||
| 54 | default { print $line } | ||
| 55 | } | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | # Put the final closing marker in case we finished in YAML | ||
| 61 | print SOURCE_MARKER if ($state == YAML); | ||
| 62 | } | ||
| 63 | |||
| 64 | 1; | ||
| 65 | |||
| 66 | |||
| 67 | __END__ | ||