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