Ticket #6495: trac_6495-redirect_html.2.patch

File trac_6495-redirect_html.2.patch, 3.8 KB (added by hivert, 7 years ago)
  • doc/common/builder.py

    # HG changeset patch
    # User Mike Hansen <mhansen@gmail.com>
    # Date 1359046028 0
    # Node ID ee9b88e93e64e7c2aacff6fadc89c525de58a2bb
    # Parent  8bececb7898445d5cb6fe97ee58930a0e9d34835
    Fix it so that old URLs to the reference manual work
    
    diff --git a/doc/common/builder.py b/doc/common/builder.py
    a b class WebsiteBuilder(DocBuilder): 
    330330    def html(self):
    331331        """
    332332        After we've finished building the website index page, we copy
    333         everything one directory up.
     333        everything one directory up.  Then, we walk the reference manual,
     334        and add html files the
    334335        """
    335336        DocBuilder.html(self)
    336337        html_output_dir = self._output_dir('html')
    class WebsiteBuilder(DocBuilder): 
    338339                 os.path.realpath(os.path.join(html_output_dir, '..')),
    339340                 ignore_errors=False)
    340341
     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(shorter_path.split(os.sep))
     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
    341404    def clean(self):
    342405        """
    343406        When we clean the output for the website index, we need to