| 342 | self.create_html_redirects() |
| 343 | |
| 344 | def create_html_redirects(self): |
| 345 | """ |
| 346 | Writes a number of small HTML files of files which used to |
| 347 | exist before splitting the reference manual into multiple |
| 348 | documents. These just redirect to the correct file after the |
| 349 | splitting. |
| 350 | """ |
| 351 | # The simple html template which will cause a redirect to the |
| 352 | # correct file |
| 353 | html_template = """<html><head> |
| 354 | <meta HTTP-EQUIV="REFRESH" content="0; url=%s"> |
| 355 | </head><body></body></html>""" |
| 356 | |
| 357 | reference_dir = os.path.abspath(os.path.join(self._output_dir('html'), |
| 358 | '..', 'reference')) |
| 359 | reference_builder = ReferenceBuilder('reference') |
| 360 | refdir = os.path.join(os.environ['SAGE_DOC'], 'en', 'reference') |
| 361 | for document in reference_builder.get_all_documents(refdir): |
| 362 | #path is the directory above reference dir |
| 363 | path = os.path.abspath(os.path.join(reference_dir, '..')) |
| 364 | |
| 365 | # the name of the subdocument |
| 366 | document_name = document.split('/')[1] |
| 367 | |
| 368 | # the sage directory within a subdocument, for example |
| 369 | # /path/to/.../output/html/en/reference/algebras/sage |
| 370 | sage_directory = os.path.join(path, document, 'sage') |
| 371 | |
| 372 | # Walk through all of the files in the sage_directory |
| 373 | for dirpath, dirnames, filenames in os.walk(sage_directory): |
| 374 | # a string like reference/algebras/sage/algebras |
| 375 | short_path = dirpath[len(path)+1:] |
| 376 | |
| 377 | # a string like sage/algebras |
| 378 | shorter_path = os.path.join(*short_path.split(os.sep)[2:]) |
| 379 | |
| 380 | #Make the shorter path directory |
| 381 | try: |
| 382 | os.makedirs(os.path.join(reference_dir, shorter_path)) |
| 383 | except OSError: |
| 384 | pass |
| 385 | |
| 386 | for filename in filenames: |
| 387 | if not filename.endswith('html'): |
| 388 | continue |
| 389 | |
| 390 | # the name of the html file we are going to create |
| 391 | redirect_filename = os.path.join(reference_dir, shorter_path, filename) |
| 392 | |
| 393 | # the number of levels up we need to use in the relative url |
| 394 | levels_up = len(os.path.split(shorter_path)) |
| 395 | |
| 396 | # the relative url that we will redirect to |
| 397 | redirect_url = "/".join(['..']*levels_up + [document_name, shorter_path, filename]) |
| 398 | |
| 399 | # write the html file which performs the redirect |
| 400 | with open(redirect_filename, 'w') as f: |
| 401 | f.write(html_template % redirect_url) |
| 402 | |
| 403 | |