|
| 1 | +import hashlib |
1 | 2 | import json |
2 | 3 | import logging |
3 | 4 | import os |
4 | | -import uuid |
5 | 5 | from urllib.parse import unquote as urlunquote |
6 | 6 | from urllib.parse import urlsplit, urlunsplit |
7 | 7 |
|
@@ -140,71 +140,55 @@ def on_post_page(self, output, page, config, **kwargs): |
140 | 140 | if not os.path.exists(page_dir): |
141 | 141 | os.makedirs(page_dir) |
142 | 142 |
|
143 | | - for swagger_ui_ele in swagger_ui_list: |
144 | | - if swagger_ui_ele.has_attr("grouped"): |
145 | | - grouped_list.append(swagger_ui_ele) |
146 | | - continue |
147 | | - |
148 | | - cur_id = str(uuid.uuid4())[:8] |
149 | | - iframe_filename = f"swagger-{cur_id}.html" |
150 | | - iframe_id_list.append(cur_id) |
| 143 | + def render_template(openapi_spec_url, swagger_ui_ele): |
151 | 144 | cur_options = self.process_options(config, swagger_ui_ele) |
152 | 145 | cur_oath2_prop = self.process_oath2_prop(swagger_ui_ele) |
153 | 146 | oauth2_redirect_url = cur_options.pop("oauth2RedirectUrl", "") |
154 | 147 | if not oauth2_redirect_url: |
155 | 148 | oauth2_redirect_url = default_oauth2_redirect_file |
156 | 149 |
|
157 | | - openapi_spec_url = self.path_to_url( |
158 | | - page.file, swagger_ui_ele.get("src", "") |
159 | | - ) |
160 | | - output_from_parsed_template = template.render( |
| 150 | + template_output = template.render( |
161 | 151 | css_dir=css_dir, |
162 | 152 | extra_css_files=extra_css_files, |
163 | 153 | js_dir=js_dir, |
164 | 154 | background=self.config["background"], |
165 | | - id=cur_id, |
| 155 | + id="{{ID_PLACEHOLDER}}", # ID is unknown yet - it's the hash of the content. |
166 | 156 | openapi_spec_url=openapi_spec_url, |
167 | 157 | oauth2_redirect_url=oauth2_redirect_url, |
168 | 158 | validatorUrl=self.config["validatorUrl"], |
169 | 159 | options_str=json.dumps(cur_options, indent=4)[1:-1], |
170 | 160 | oath2_prop_str=json.dumps(cur_oath2_prop), |
171 | 161 | ) |
| 162 | + cur_id = hashlib.sha256(template_output.encode()).hexdigest()[:8] |
| 163 | + iframe_filename = f"swagger-{cur_id}.html" |
| 164 | + template_output = template_output.replace("{{ID_PLACEHOLDER}}", cur_id) |
172 | 165 | with open(os.path.join(page_dir, iframe_filename), "w") as f: |
173 | | - f.write(output_from_parsed_template) |
| 166 | + f.write(template_output) |
174 | 167 | self.replace_with_iframe(soup, swagger_ui_ele, cur_id, iframe_filename) |
175 | 168 |
|
| 169 | + for swagger_ui_ele in swagger_ui_list: |
| 170 | + if swagger_ui_ele.has_attr("grouped"): |
| 171 | + grouped_list.append(swagger_ui_ele) |
| 172 | + continue |
| 173 | + |
| 174 | + openapi_spec_url = self.path_to_url( |
| 175 | + page.file, swagger_ui_ele.get("src", "") |
| 176 | + ) |
| 177 | + render_template( |
| 178 | + openapi_spec_url=openapi_spec_url, swagger_ui_ele=swagger_ui_ele |
| 179 | + ) |
| 180 | + |
176 | 181 | if grouped_list: |
177 | | - cur_id = str(uuid.uuid4())[:8] |
178 | | - iframe_filename = f"swagger-{cur_id}.html" |
179 | | - iframe_id_list.append(cur_id) |
180 | 182 | openapi_spec_url = [] |
181 | 183 | for swagger_ui_ele in grouped_list: |
182 | 184 | cur_url = self.path_to_url(page.file, swagger_ui_ele.get("src", "")) |
183 | 185 | cur_name = swagger_ui_ele.get("name", swagger_ui_ele.get("src", "")) |
184 | 186 | openapi_spec_url.append({"url": cur_url, "name": cur_name}) |
185 | 187 |
|
186 | 188 | # only use options from first grouped swagger ui tag |
187 | | - cur_options = self.process_options(config, grouped_list[0]) |
188 | | - cur_oath2_prop = self.process_oath2_prop(grouped_list[0]) |
189 | | - oauth2_redirect_url = cur_options.pop("oauth2RedirectUrl", "") |
190 | | - if not oauth2_redirect_url: |
191 | | - oauth2_redirect_url = default_oauth2_redirect_file |
192 | | - |
193 | | - output_from_parsed_template = template.render( |
194 | | - css_dir=css_dir, |
195 | | - extra_css_files=extra_css_files, |
196 | | - js_dir=js_dir, |
197 | | - background=self.config["background"], |
198 | | - id=cur_id, |
199 | | - openapi_spec_url=openapi_spec_url, |
200 | | - oauth2_redirect_url=oauth2_redirect_url, |
201 | | - validatorUrl=self.config["validatorUrl"], |
202 | | - options_str=json.dumps(cur_options, indent=4)[1:-1], |
203 | | - oath2_prop_str=json.dumps(cur_oath2_prop), |
| 189 | + render_template( |
| 190 | + openapi_spec_url=openapi_spec_url, swagger_ui_ele=grouped_list[0] |
204 | 191 | ) |
205 | | - with open(os.path.join(page_dir, iframe_filename), "w") as f: |
206 | | - f.write(output_from_parsed_template) |
207 | | - self.replace_with_iframe(soup, grouped_list[0], cur_id, iframe_filename) |
208 | 192 | # only keep first grouped swagger ui tag |
209 | 193 | for rest_swagger_ui_ele in grouped_list[1:]: |
210 | 194 | rest_swagger_ui_ele.extract() |
|
0 commit comments