File Coverage

blib/lib/Swagger2/Markdown.pm
Criterion Covered Total %
statement 38 38 100.0
branch 1 2 50.0
condition n/a
subroutine 12 12 100.0
pod 2 2 100.0
total 53 54 98.1


line stmt bran cond sub pod time code
1             package Swagger2::Markdown;
2              
3             =head1 NAME
4              
5             Swagger2::Markdown - convert a Swagger2 spec to various markdown formats
6              
7             =for html
8             Build Status
9             Coverage Status
10              
11             =head1 VERSION
12              
13             0.11
14              
15             =head1 SYNOPSIS
16              
17             use strict;
18             use warnings;
19              
20             use Swagger2;
21             use Swagger2::Markdown;
22              
23             my $s2md = Swagger2::Markdown->new(
24             swagger2 => Swagger2->new->load( $path_to_swagger_spec )
25             );
26              
27             my $api_bp_str = $s2md->api_blueprint;
28              
29             my $markdown_str = $s2md->markdown( %pod_markdown_opts );
30              
31             =head1 DESCRIPTION
32              
33             This module allows you to convert a swagger specification file to API Blueprint
34             markdown and basic markdown. Note that you may need to add C values to your
35             swagger config file to get better markdown output.
36              
37             =cut
38              
39 2     2   214112 use strict;
  2         2  
  2         46  
40 2     2   8 use warnings;
  2         2  
  2         36  
41              
42 2     2   893 use Moo;
  2         17506  
  2         8  
43 2     2   2905 use Types::Standard qw/ :all /;
  2         91431  
  2         16  
44 2     2   50360 use Template;
  2         33624  
  2         73  
45 2     2   1047 use Swagger2::Markdown::API::Blueprint;
  2         6  
  2         73  
46 2     2   1441 use Pod::Markdown;
  2         80063  
  2         329  
47              
48             our $VERSION = '0.11';
49              
50             =head1 ATTRIBUTES
51              
52             =head2 swagger2
53              
54             The L object, required at instantiation
55              
56             =cut
57              
58             has 'swagger2' => (
59             is => 'ro',
60             isa => InstanceOf['Swagger2'],
61             required => 1,
62             );
63              
64             has '_template' => (
65             is => 'ro',
66             isa => InstanceOf['Template'],
67             default => sub {
68             return Template->new({
69              
70             });
71             },
72             );
73              
74             =head1 METHODS
75              
76             =cut
77              
78             =head2 markdown
79              
80             Returns a string of markdown using the L parser - the pod string is
81             retrieved from the ->pod method of L. As the parser is L
82             you can pass in a hash of arguments that will be passed on to the L
83             instantiation call:
84              
85             my $markdown = $s2md->markdown( %pod_markdown_opts );
86              
87             =cut
88              
89             sub markdown {
90 10     10 1 11020 my ( $self,%options ) = @_;
91              
92 10         103 my $parser = Pod::Markdown->new(
93             match_encoding => 1,
94             %options
95             );
96              
97             {
98             # Pod::Markdown will escape any markdown reserved chars, but since we can
99             # include markdown in the swagger config we *don't* want to do that. this
100             # hack stops Pod::Markdown escaping the reserved chars, since there isn't
101             # an option to prevent the module doing so
102 2     2   25 no warnings 'redefine';
  2         4  
  2         531  
  10         1970  
103 10     304   128 *Pod::Markdown::_escape_inline_markdown = sub { return $_[1] };
  304         139275  
104 10     139   70 *Pod::Markdown::_escape_paragraph_markdown = sub { return $_[1] };
  139         4140  
105             }
106              
107 10         50 $parser->output_string( \my $markdown );
108 10         1520 $parser->parse_string_document(
109             $self->swagger2->pod->to_string
110             );
111              
112 10         2746 return $markdown;
113             }
114              
115             =head2 api_blueprint
116              
117             Returns a string of markdown in API Blueprint format. Because API Blueprint is more
118             of a documentation orientated approach there are some sections that it contains that
119             are not present in the swagger2 spec. Refer to the API Blueprint specification for
120             the following terms: L
121              
122             You should add C sections to the swagger2 config to define which
123             format of API Blueprint output you want and to add extra summary and method
124             documentation. The main layout of the API Blueprint file is defined as so in the top
125             level of the swagger config file (YAML example here with defaults shown):
126              
127             x-api-blueprint:
128             resource_section: method_uri
129             action_section: method_uri
130             attributes: false
131             simple: false
132             data_structures: false
133              
134             Possible values for resource_section are:
135              
136             uri - #
137             name_uri - # []
138             method_uri - #
139             name_method_uri - # [ ]
140              
141             Possible values for action_section are:
142              
143             method - ##
144             name_method - ## []
145             name_method_uri - ## [ ]
146             method_uri - #
147              
148             Possible values for C are true and false - if true the Attributes
149             sections will be created in the API Blueprint output.
150              
151             Possible values for C are true and false - if true then only the resource
152             section headers will be printed.
153              
154             Possible values for C are true and false - if true then only a
155             Data Structures section will be output to show definitions, and those request or
156             response parameters that reference those (using C<$ref>) will also reference the
157             Data Structures section.
158              
159             For paths needing extra documentation you can add an C section to
160             the path like so (again, YAML example here):
161              
162             paths:
163             /message:
164             x-api-blueprint:
165             group: Messages
166             summary: My Message
167             description: |
168             The description that will appear under the group section
169             group_description: |
170             The description that will appear under the resource_section header
171              
172             C and C should be self explanatory, C will make the API
173             Blueprint output use grouping resources format
174              
175             You can add examples to the parameters section of a method using C:
176              
177             paths:
178             /messages:
179             get:
180             parameters:
181             - in: query
182             ...
183             x-example: 3
184              
185             =cut
186              
187             sub api_blueprint {
188 10     10 1 10634 my ( $self,$args ) = @_;
189              
190 10         10 my $output;
191              
192             $self->_template->process(
193             Swagger2::Markdown::API::Blueprint->template,
194             {
195             # api blueprint output config
196             o => $self->swagger2->api_spec->data->{'x-api-blueprint'},
197             # expanded
198             e => $self->swagger2->expand->api_spec->data,
199             # compacted
200             c => $self->swagger2->api_spec->data,
201             # definitions
202             d => $self->swagger2->api_spec->data->{definitions},
203             },
204 10 50       106 \$output,
205             ) || die $self->_template->error;
206              
207 10         12936 return $output;
208             }
209              
210             =head1 EXAMPLES
211              
212             See the tests in this distribution - for example t/swagger/foo.yaml will map
213             to t/markdown/foo.md when called with ->markdown and t/api_blueprint/foo.md
214             when called with ->api_blueprint.
215              
216             =head1 SEE ALSO
217              
218             L
219              
220             L
221              
222             =head1 BUGS
223              
224             Certainly. This has only been tested against the example markdown files on
225             the API Blueprint github repo, and for that i had to generate the swagger
226             files by hand.
227              
228             =head1 AUTHOR
229              
230             Lee Johnson - C
231              
232             =head1 LICENSE
233              
234             This library is free software; you can redistribute it and/or modify it under
235             the same terms as Perl itself. If you would like to contribute documentation
236             please raise an issue / pull request:
237              
238             https://github.com/Humanstate/swagger2-markdown
239              
240             =cut
241              
242             __PACKAGE__->meta->make_immutable;
243              
244             # vim: ts=4:sw=4:et
245