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