Skip to content

Commit 08d015c

Browse files
author
minuhin
committed
update readme, fix bug with jinja template
1 parent 19d2646 commit 08d015c

File tree

6 files changed

+51
-32
lines changed

6 files changed

+51
-32
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018
3+
Copyright (c) 2020
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+33-24
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
1-
![](https://img.shields.io/pypi/v/foliantcontrib.swaggerdoc.svg)
1+
2+
[![](https://img.shields.io/pypi/v/foliantcontrib.swaggerdoc.svg)](https://pypi.org/project/foliantcontrib.swaggerdoc/) [![](https://img.shields.io/github/v/tag/foliant-docs/foliantcontrib.swaggerdoc.svg?label=GitHub)](https://github.com/foliant-docs/foliantcontrib.swaggerdoc)
23

34
# Swagger API Docs Generator for Foliant
45

5-
This preprocessor generates Markdown documentation from [Swagger](https://swagger.io/) spec files . It uses [Jinja2](http://jinja.pocoo.org/) templating engine or [widdershins](https://github.com/mermade/widdershins) for generating Markdown from swagger spec files.
6+
![](img/swagger.png)
7+
8+
*The static site on the picture was built with [Slate](https://foliant-docs.github.io/docs/backends/slate/) backend together with SwaggerDoc preprocessor*
9+
10+
This preprocessor generates Markdown documentation from [Swagger](https://swagger.io/) spec files. It uses [Jinja2](http://jinja.pocoo.org/) templating engine or [Widdershins](https://github.com/mermade/widdershins) for generating Markdown from swagger spec files.
611

712
## Installation
813

914
```bash
1015
$ pip install foliantcontrib.swaggerdoc
1116
```
1217

18+
This preprocessor requires [Widdershins](https://github.com/Mermade/widdershins) to be installed on your system (unless you are using [Foliant with Full Docker Image](https://foliant-docs.github.io/docs/tutorials/full_docker/)):
19+
20+
```
21+
npm install -g widdershins
22+
```
23+
1324
## Config
1425

15-
To enable the preprocessor, add `pgsqldoc` to `preprocessors` section in the project config:
26+
To enable the preprocessor, add `swaggerdoc` to `preprocessors` section in the project config:
1627

1728
```yaml
1829
preprocessors:
@@ -24,24 +35,22 @@ The preprocessor has a number of options:
2435
```yaml
2536
preprocessors:
2637
- swaggerdoc:
27-
json_url: http://localhost/swagger.json
28-
json_path: swagger.json
38+
spec_url: http://localhost/swagger.json
39+
spec_path: swagger.json
2940
additional_json_path: tags.json
30-
mode: 'jinja'
41+
mode: widdershins
3142
template: swagger.j2
3243
environment: env.yaml
3344

3445
```
3546

36-
`json_url`
47+
`spec_url`
3748
: URL to Swagger spec file. If it is a list — preprocessor takes the first url which works.
3849

39-
> even though the parameters are called *json*\_url and *json*\_path, yaml format is supported too. Parameters may be softly renamed in future.
40-
41-
`json_path`
50+
`spec_path`
4251
: Local path to Swagger spec file (relative to project dir).
4352

44-
> If both url and path are specified — preprocessor first tries to fetch JSON from url, and then (if that fails) looks for the file on local path.
53+
> If both url and path are specified — preprocessor first tries to fetch spec from url, and then (if that fails) looks for the file on local path.
4554
4655
`additional_json_path`
4756
: Only for `jinja` mode. Local path to swagger spec file with additional info (relative to project dir). It will be merged into original spec file, *not overriding existing fields*.
@@ -78,12 +87,12 @@ You can also specify some parameters (or all of them) in the tag options:
7887

7988
Introduction text for API documentation.
8089

81-
<swaggerdoc json_url="http://localhost/swagger.json"
90+
<swaggerdoc spec_url="http://localhost/swagger.json"
8291
mode="jinja"
8392
template="swagger.j2">
8493
</swaggerdoc>
8594

86-
<swaggerdoc json_url="http://localhost/swagger.json"
95+
<swaggerdoc spec_url="http://localhost/swagger.json"
8796
mode="widdershins"
8897
environment="env.yml">
8998
</swaggerdoc>
@@ -95,16 +104,6 @@ This way you can have documentation from several different Swagger spec files in
95104

96105
## Customizing output
97106

98-
### Jinja
99-
100-
> `jinja` mode is deprecated. It may be removed in future
101-
102-
In `jinja` mode the output markdown is generated by the [Jinja2](http://jinja.pocoo.org/) template. In this template all fields from Swagger spec file are available under the dictionary named `swagger_data`.
103-
104-
To customize the output create a template which suits your needs. Then supply the path to it in the `template` parameter.
105-
106-
If you wish to use the default template as a starting point, build the foliant project with `swaggerdoc` preprocessor turned on. After the first build the default template will appear in your foliant project dir under name `swagger.j2`.
107-
108107
### Widdershins
109108

110109
In `widdershins` mode the output markdown is generated by [widdershins](https://github.com/mermade/widdershins) Node.js application. It supports customizing the output with [doT.js](https://github.com/olado/doT) templates.
@@ -116,7 +115,17 @@ In `widdershins` mode the output markdown is generated by [widdershins](https://
116115
```yaml
117116
preprocessors:
118117
- swaggerdoc:
119-
json_path: swagger.yml
118+
spec_path: swagger.yml
120119
environment:
121120
user_templates: !path ./widdershins_templates/
122121
```
122+
123+
### Jinja
124+
125+
> `jinja` mode is deprecated. It may be removed in future
126+
127+
In `jinja` mode the output markdown is generated by the [Jinja2](http://jinja.pocoo.org/) template. In this template all fields from Swagger spec file are available under the dictionary named `swagger_data`.
128+
129+
To customize the output create a template which suits your needs. Then supply the path to it in the `template` parameter.
130+
131+
If you wish to use the default template as a starting point, build the foliant project with `swaggerdoc` preprocessor turned on. After the first build the default template will appear in your foliant project dir under name `swagger.j2`.

changelog.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 1.2.1
2+
3+
- Fix spec path issue.
4+
- Fix: jinja mode default template wansn't copied.
5+
16
# 1.2.0
27

38
- Add `spec_path` and `spec_url` parameters.

foliant/preprocessors/swaggerdoc/swaggerdoc.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def __init__(self, *args, **kwargs):
6666

6767
def _gather_specs(self,
6868
urls: list,
69-
path_: PosixPath or None) -> PosixPath:
69+
path_: str or PosixPath or None) -> PosixPath:
7070
"""
7171
Download first swagger spec from the url list; copy it into the
7272
temp dir and return path to it. If all urls fail — check path_ and
@@ -88,7 +88,7 @@ def _gather_specs(self,
8888

8989
if path_:
9090
dest = self._swagger_tmp / f'swagger_spec'
91-
if not path_.exists():
91+
if not Path(path_).exists():
9292
self._warning(f"Can't find file {path_}. Skipping.")
9393
else: # file exists
9494
copyfile(str(path_), str(dest))
@@ -107,10 +107,14 @@ def _process_jinja(self,
107107
else:
108108
add = yaml.safe_load(open(additional, encoding="utf8"))
109109
data = {**add, **data}
110-
111110
if options.is_default('template') and not Path(options['template']).exists():
112-
copyfile(resource_filename(__name__, 'template/' +
113-
self.defaults['template']), options['template'])
111+
copyfile(
112+
resource_filename(
113+
__name__,
114+
'template/' + self.defaults['template']
115+
),
116+
options['template']
117+
)
114118
return self._to_md(data, options['template'])
115119

116120
def _process_widdershins(self,
@@ -184,7 +188,8 @@ def process_swaggerdoc_blocks(self, block) -> str:
184188
('json_path',),
185189
('spec_url',),
186190
('spec_path',)],
187-
validators={'mode': validate_in(self._modes)})
191+
validators={'mode': validate_in(self._modes)},
192+
defaults=self.defaults)
188193
self.logger.debug(f'Processing swaggerdoc tag in {self.current_filepath}')
189194
spec_url = options['spec_url'] or options['json_url']
190195
if spec_url and isinstance(spec_url, str):

img/swagger.png

457 KB
Loading

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
description=SHORT_DESCRIPTION,
1717
long_description=LONG_DESCRIPTION,
1818
long_description_content_type='text/markdown',
19-
version='1.2.0',
19+
version='1.2.1',
2020
author='Daniil Minukhin',
2121
author_email='[email protected]',
2222
packages=['foliant.preprocessors.swaggerdoc'],

0 commit comments

Comments
 (0)