| 1 | package APISchema::Generator::Markdown::Formatter; | ||
| 2 | use 5.014; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # core | ||
| 7 | use Exporter qw(import); | ||
| 8 | our @EXPORT = qw(type json pretty_json code restriction desc anchor method methods content_type http_status http_status_code); | ||
| 9 | |||
| 10 | # cpan | ||
| 11 | use HTTP::Status qw(status_message); | ||
| 12 | use URI::Escape qw(uri_escape_utf8); | ||
| 13 | use JSON::XS (); | ||
| 14 | my $JSON = JSON::XS->new->canonical(1); | ||
| 15 | |||
| 16 | use constant +{ | ||
| 17 | RESTRICTIONS => [qw(required max_items min_items max_length min_length maximum minimum pattern)], | ||
| 18 | SHORT_DESCRIPTION_LENGTH => 100, | ||
| 19 | }; | ||
| 20 | |||
| 21 | sub type ($) { | ||
| 22 | my $def = shift; | ||
| 23 | my $bar = '|'; | ||
| 24 | |||
| 25 | my $ref = ref $def ? $def->{'$ref'} : $def; | ||
| 26 | if ($ref) { | ||
| 27 | $ref = $ref =~ s!^#/resource/!!r; | ||
| 28 | my $ref_text = "`$ref`"; | ||
| 29 | my $name = $ref =~ s!/.*$!!r; | ||
| 30 | $ref_text = sprintf('[%s](#%s)', $ref_text, anchor(resource => $name)) if $name; | ||
| 31 | return $ref_text; | ||
| 32 | } | ||
| 33 | |||
| 34 | my $type = $def->{type}; | ||
| 35 | if ($type) { | ||
| 36 | return sprintf '`%s`', $type unless ref $type; | ||
| 37 | return join $bar, map { code($_) } @{$type} if ref $type eq 'ARRAY'; | ||
| 38 | } | ||
| 39 | |||
| 40 | return join $bar, map { code($_) } @{$def->{enum}} if $def->{enum}; | ||
| 41 | |||
| 42 | return 'undefined'; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub json ($) { | ||
| 46 | my $x = shift; | ||
| 47 | if (ref $x eq 'SCALAR') { | ||
| 48 | if ($$x eq 1) { | ||
| 49 | $x = 'true'; | ||
| 50 | } elsif ($$x eq 0) { | ||
| 51 | $x = 'false'; | ||
| 52 | } | ||
| 53 | } elsif (ref $x) { | ||
| 54 | $x = $JSON->encode($x); | ||
| 55 | } else { | ||
| 56 | $x = $JSON->encode([$x]); | ||
| 57 | $x =~ s/^\[(.*)\]$/$1/; | ||
| 58 | } | ||
| 59 | return $x; | ||
| 60 | } | ||
| 61 | |||
| 62 | my $PRETTY_JSON = JSON::XS->new->canonical(1)->indent(1)->pretty(1); | ||
| 63 | sub pretty_json ($) { | ||
| 64 | my $x = shift; | ||
| 65 | if (ref $x) { | ||
| 66 | $x = $PRETTY_JSON->encode($x); | ||
| 67 | } else { | ||
| 68 | $x = $PRETTY_JSON->encode([$x]); | ||
| 69 | $x =~ s/^\[\s*(.*)\s*\]\n$/$1/; | ||
| 70 | } | ||
| 71 | return $x; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _code ($) { | ||
| 75 | my $text = shift; | ||
| 76 | return '' unless defined $text; | ||
| 77 | if ($text =~ /[`|]/) { | ||
| 78 | $text =~ s/[|]/|/g; | ||
| 79 | return sprintf '<code>%s</code>', $text; | ||
| 80 | } | ||
| 81 | return sprintf '`%s`', $text; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub code ($) { | ||
| 85 | my $text = shift; | ||
| 86 | return '' unless defined $text; | ||
| 87 | return _code json $text; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub anchor ($$) { | ||
| 91 | my ($label, $obj) = @_; | ||
| 92 | my $name = ref $obj ? $obj->title : $obj; | ||
| 93 | return sprintf '%s-%s', $label, uri_escape_utf8($name); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub restriction ($) { | ||
| 97 | my $def = shift; | ||
| 98 | return '' unless (ref $def) eq 'HASH'; | ||
| 99 | |||
| 100 | my @result = (); | ||
| 101 | for my $r (sort @{+RESTRICTIONS}) { | ||
| 102 | next unless defined $def->{$r}; | ||
| 103 | |||
| 104 | if (ref $def->{$r}) { | ||
| 105 | push @result, _code sprintf "$r%s", json $def->{$r}; | ||
| 106 | } else { | ||
| 107 | push @result, _code sprintf "$r(%s)", json $def->{$r}; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | return join ' ', @result; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub desc ($) { | ||
| 114 | my $text = shift || ''; | ||
| 115 | $text = $text =~ s/[\r\n].*\z//sr; | ||
| 116 | $text = substr($text, 0, SHORT_DESCRIPTION_LENGTH) . '...' | ||
| 117 | if length($text) > SHORT_DESCRIPTION_LENGTH; | ||
| 118 | return $text; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub method ($) { | ||
| 122 | my $method = shift; | ||
| 123 | return $method->[0] if (ref $method || '') eq 'ARRAY'; | ||
| 124 | return $method; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub methods ($) { | ||
| 128 | my $method = shift; | ||
| 129 | return join ', ', map { _code($_) } @$method | ||
| 130 | if (ref $method || '') eq 'ARRAY'; | ||
| 131 | return _code($method); | ||
| 132 | } | ||
| 133 | |||
| 134 | sub content_type ($) { | ||
| 135 | my $type = shift; | ||
| 136 | return '-' unless length($type); | ||
| 137 | return "`$type`"; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub http_status ($) { | ||
| 141 | my $code = shift; | ||
| 142 | return undef unless $code; | ||
| 143 | return join(' ', $code, status_message($code)); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub http_status_code { | ||
| 147 | return _code http_status shift; | ||
| 148 | } | ||
| 149 | |||
| 150 | 1; |