1parameters:
2  TargetFolder: ''
3
4steps:
5  - task: PythonScript@0
6    displayName: MashUp Generated Index Site so its served from default site location
7    inputs:
8      scriptSource: inline
9      script: |
10        import argparse
11        import os
12        import logging
13        import re
14        import shutil
15        from io import open
16
17        SITE_INDEX = r'${{ parameters.SourceDirectory }}\docfx_project\_site'
18        TOC_HTML_REGEX = r"\.\./toc.html"
19        NAV_TOC_HTML_REGEX = r"api/"
20        PREV_DIR_REGEX = r"\.\./"
21
22        def locate_htmlfiles(directory):
23            html_set = []
24            for root, dirs, files in os.walk(directory):
25                for file in files:
26                    html_set.append(os.path.join(root, file))
27            return html_set
28
29        def process_html(content):
30            content = re.sub(TOC_HTML_REGEX, 'navtoc.html', content)
31            content = re.sub(PREV_DIR_REGEX, '', content)
32            return content
33
34        def process_navtoc(content):
35            content = re.sub(NAV_TOC_HTML_REGEX, '', content)
36            return content
37
38        if __name__ == "__main__":
39            html_files = locate_htmlfiles(os.path.join(SITE_INDEX, 'api'))
40            navtoc_location = os.path.join(SITE_INDEX, 'toc.html')
41
42            # Process the main toc.html and rename it to navtoc.html
43            try:
44                logging.info(
45                    "Process {}.".format(navtoc_location)
46                )
47                with open(navtoc_location, "r", encoding="utf8") as navtoc_stream:
48                    navtoc_content = navtoc_stream.read()
49                new_navtoc_content = process_navtoc(navtoc_content)
50                logging.info("Process {}.".format(navtoc_content))
51                with open(navtoc_location, "w", encoding="utf8") as html_stream:
52                    html_stream.write(new_navtoc_content)
53            except Exception as e:
54                logging.error(e)
55                exit(1)
56
57            # Rename main toc.html to navtoc.html
58            os.rename(navtoc_location, os.path.join(SITE_INDEX, 'navtoc.html'))
59
60            # Process all html in api directory
61            for html_location in html_files:
62                try:
63                    logging.info(
64                        "Process {}.".format(html_location)
65                    )
66                    with open(html_location, "r", encoding="utf8") as html_stream:
67                        html_content = html_stream.read()
68                    new_content = process_html(html_content)
69                    logging.info("Process {}.".format(html_location))
70                    with open(html_location, "w", encoding="utf8") as html_stream:
71                        html_stream.write(new_content)
72                except Exception as e:
73                    logging.error(e)
74                    exit(1)
75
76            # Move all files from api to main site home directory
77            for html_location in html_files:
78                shutil.copy(html_location, SITE_INDEX)
79
80            # Delete API Directory
81            shutil.rmtree(os.path.join(SITE_INDEX, 'api'))