@@ -112,101 +112,99 @@ def on_post_page(self, output, page, config, **kwargs):
112112 iframe_id_list = []
113113 grouped_list = []
114114
115- if len (swagger_ui_list ) == 0 :
116- return str (soup )
117-
118- css_dir = utils .get_relative_url (
119- utils .normalize_url ("assets/stylesheets/" ), page .url
120- )
121- js_dir = utils .get_relative_url (
122- utils .normalize_url ("assets/javascripts/" ), page .url
123- )
124- default_oauth2_redirect_file = utils .get_relative_url (
125- utils .normalize_url ("assets/swagger-ui/oauth2-redirect.html" ), page .url
126- )
127- env = Environment (
128- loader = FileSystemLoader (os .path .join (base_path , "swagger-ui" ))
129- )
130- template = env .get_template ("swagger.html" )
131- extra_css_files = list (map (
132- lambda f : utils .get_relative_url (utils .normalize_url (f ), page .url ),
133- self .config ["extra_css" ],
134- ))
135-
136- page_dir = os .path .dirname (
137- os .path .join (config ["site_dir" ], urlunquote (page .url ))
138- )
139- if not os .path .exists (page_dir ):
140- os .makedirs (page_dir )
141-
142- for swagger_ui_ele in swagger_ui_list :
143- if swagger_ui_ele .has_attr ("grouped" ):
144- grouped_list .append (swagger_ui_ele )
145- continue
146-
147- cur_id = str (uuid .uuid4 ())[:8 ]
148- iframe_filename = f"swagger-{ cur_id } .html"
149- iframe_id_list .append (cur_id )
150- cur_options = self .process_options (config , swagger_ui_ele )
151- cur_oath2_prop = self .process_oath2_prop (swagger_ui_ele )
152- oauth2_redirect_url = cur_options .pop ("oauth2RedirectUrl" , "" )
153- if not oauth2_redirect_url :
154- oauth2_redirect_url = default_oauth2_redirect_file
155-
156- openapi_spec_url = self .path_to_url (
157- page .file , swagger_ui_ele .get ("src" , "" )
115+ if len (swagger_ui_list ) > 0 :
116+ css_dir = utils .get_relative_url (
117+ utils .normalize_url ("assets/stylesheets/" ), page .url
118+ )
119+ js_dir = utils .get_relative_url (
120+ utils .normalize_url ("assets/javascripts/" ), page .url
158121 )
159- output_from_parsed_template = template .render (
160- css_dir = css_dir ,
161- extra_css_files = extra_css_files ,
162- js_dir = js_dir ,
163- background = self .config ["background" ],
164- id = cur_id ,
165- openapi_spec_url = openapi_spec_url ,
166- oauth2_redirect_url = oauth2_redirect_url ,
167- validatorUrl = self .config ["validatorUrl" ],
168- options_str = json .dumps (cur_options , indent = 4 )[1 :- 1 ],
169- oath2_prop_str = json .dumps (cur_oath2_prop ),
122+ default_oauth2_redirect_file = utils .get_relative_url (
123+ utils .normalize_url ("assets/swagger-ui/oauth2-redirect.html" ), page .url
170124 )
171- with open (os .path .join (page_dir , iframe_filename ), "w" ) as f :
172- f .write (output_from_parsed_template )
173- self .replace_with_iframe (soup , swagger_ui_ele , cur_id , iframe_filename )
174-
175- if grouped_list :
176- cur_id = str (uuid .uuid4 ())[:8 ]
177- iframe_filename = f"swagger-{ cur_id } .html"
178- iframe_id_list .append (cur_id )
179- openapi_spec_url = []
180- for swagger_ui_ele in grouped_list :
181- cur_url = self .path_to_url (page .file , swagger_ui_ele .get ("src" , "" ))
182- cur_name = swagger_ui_ele .get ("name" , swagger_ui_ele .get ("src" , "" ))
183- openapi_spec_url .append ({"url" : cur_url , "name" : cur_name })
184-
185- # only use options from first grouped swagger ui tag
186- cur_options = self .process_options (config , grouped_list [0 ])
187- cur_oath2_prop = self .process_oath2_prop (grouped_list [0 ])
188- oauth2_redirect_url = cur_options .pop ("oauth2RedirectUrl" , "" )
189- if not oauth2_redirect_url :
190- oauth2_redirect_url = default_oauth2_redirect_file
191-
192- output_from_parsed_template = template .render (
193- css_dir = css_dir ,
194- extra_css_files = extra_css_files ,
195- js_dir = js_dir ,
196- background = self .config ["background" ],
197- id = cur_id ,
198- openapi_spec_url = openapi_spec_url ,
199- oauth2_redirect_url = oauth2_redirect_url ,
200- validatorUrl = self .config ["validatorUrl" ],
201- options_str = json .dumps (cur_options , indent = 4 )[1 :- 1 ],
202- oath2_prop_str = json .dumps (cur_oath2_prop ),
125+ env = Environment (
126+ loader = FileSystemLoader (os .path .join (base_path , "swagger-ui" ))
203127 )
204- with open (os .path .join (page_dir , iframe_filename ), "w" ) as f :
205- f .write (output_from_parsed_template )
206- self .replace_with_iframe (soup , grouped_list [0 ], cur_id , iframe_filename )
207- # only keep first grouped swagger ui tag
208- for rest_swagger_ui_ele in grouped_list [1 :]:
209- rest_swagger_ui_ele .extract ()
128+ template = env .get_template ("swagger.html" )
129+ extra_css_files = list (map (
130+ lambda f : utils .get_relative_url (utils .normalize_url (f ), page .url ),
131+ self .config ["extra_css" ],
132+ ))
133+
134+ page_dir = os .path .dirname (
135+ os .path .join (config ["site_dir" ], urlunquote (page .url ))
136+ )
137+ if not os .path .exists (page_dir ):
138+ os .makedirs (page_dir )
139+
140+ for swagger_ui_ele in swagger_ui_list :
141+ if swagger_ui_ele .has_attr ("grouped" ):
142+ grouped_list .append (swagger_ui_ele )
143+ continue
144+
145+ cur_id = str (uuid .uuid4 ())[:8 ]
146+ iframe_filename = f"swagger-{ cur_id } .html"
147+ iframe_id_list .append (cur_id )
148+ cur_options = self .process_options (config , swagger_ui_ele )
149+ cur_oath2_prop = self .process_oath2_prop (swagger_ui_ele )
150+ oauth2_redirect_url = cur_options .pop ("oauth2RedirectUrl" , "" )
151+ if not oauth2_redirect_url :
152+ oauth2_redirect_url = default_oauth2_redirect_file
153+
154+ openapi_spec_url = self .path_to_url (
155+ page .file , swagger_ui_ele .get ("src" , "" )
156+ )
157+ output_from_parsed_template = template .render (
158+ css_dir = css_dir ,
159+ extra_css_files = extra_css_files ,
160+ js_dir = js_dir ,
161+ background = self .config ["background" ],
162+ id = cur_id ,
163+ openapi_spec_url = openapi_spec_url ,
164+ oauth2_redirect_url = oauth2_redirect_url ,
165+ validatorUrl = self .config ["validatorUrl" ],
166+ options_str = json .dumps (cur_options , indent = 4 )[1 :- 1 ],
167+ oath2_prop_str = json .dumps (cur_oath2_prop ),
168+ )
169+ with open (os .path .join (page_dir , iframe_filename ), "w" ) as f :
170+ f .write (output_from_parsed_template )
171+ self .replace_with_iframe (soup , swagger_ui_ele , cur_id , iframe_filename )
172+
173+ if grouped_list :
174+ cur_id = str (uuid .uuid4 ())[:8 ]
175+ iframe_filename = f"swagger-{ cur_id } .html"
176+ iframe_id_list .append (cur_id )
177+ openapi_spec_url = []
178+ for swagger_ui_ele in grouped_list :
179+ cur_url = self .path_to_url (page .file , swagger_ui_ele .get ("src" , "" ))
180+ cur_name = swagger_ui_ele .get ("name" , swagger_ui_ele .get ("src" , "" ))
181+ openapi_spec_url .append ({"url" : cur_url , "name" : cur_name })
182+
183+ # only use options from first grouped swagger ui tag
184+ cur_options = self .process_options (config , grouped_list [0 ])
185+ cur_oath2_prop = self .process_oath2_prop (grouped_list [0 ])
186+ oauth2_redirect_url = cur_options .pop ("oauth2RedirectUrl" , "" )
187+ if not oauth2_redirect_url :
188+ oauth2_redirect_url = default_oauth2_redirect_file
189+
190+ output_from_parsed_template = template .render (
191+ css_dir = css_dir ,
192+ extra_css_files = extra_css_files ,
193+ js_dir = js_dir ,
194+ background = self .config ["background" ],
195+ id = cur_id ,
196+ openapi_spec_url = openapi_spec_url ,
197+ oauth2_redirect_url = oauth2_redirect_url ,
198+ validatorUrl = self .config ["validatorUrl" ],
199+ options_str = json .dumps (cur_options , indent = 4 )[1 :- 1 ],
200+ oath2_prop_str = json .dumps (cur_oath2_prop ),
201+ )
202+ with open (os .path .join (page_dir , iframe_filename ), "w" ) as f :
203+ f .write (output_from_parsed_template )
204+ self .replace_with_iframe (soup , grouped_list [0 ], cur_id , iframe_filename )
205+ # only keep first grouped swagger ui tag
206+ for rest_swagger_ui_ele in grouped_list [1 :]:
207+ rest_swagger_ui_ele .extract ()
210208
211209 js_code = soup .new_tag ("script" )
212210 # trigger from iframe body ResizeObserver
@@ -221,17 +219,31 @@ def on_post_page(self, output, page, config, **kwargs):
221219 }
222220 """
223221 # listen scroll event to update modal position in iframe
224- js_code .string += f"""
225- const iframe_id_list = { json .dumps (iframe_id_list )} ;
226- """
227222 js_code .string += """
223+ let iframe_id_list = []
224+ var iframes = document.getElementsByClassName("swagger-ui-iframe");
225+ for (var i = 0; i < iframes.length; i++) {
226+ iframe_id_list.push(iframes[i].getAttribute("id"))
227+ }
228+ """
229+ if len (iframe_id_list ) == 0 :
230+ js_code .string += """
231+ let ticking = true;
232+ """
233+ else :
234+ js_code .string += """
228235 let ticking = false;
236+ """
237+ js_code .string += """
229238 document.addEventListener('scroll', function(e) {
230239 if (!ticking) {
231240 window.requestAnimationFrame(()=> {
232241 let half_vh = window.innerHeight/2;
233242 for(var i = 0; i < iframe_id_list.length; i++) {
234243 let element = document.getElementById(iframe_id_list[i])
244+ if(element==null){
245+ return
246+ }
235247 let diff = element.getBoundingClientRect().top
236248 if(element.contentWindow.update_top_val){
237249 element.contentWindow.update_top_val(half_vh - diff)
0 commit comments