# 18 May - 22 May 2020 ###### tags: `Study Note` ## Monday, 18 May 2020 - Continue analyze the difference between the FAPI file with FH_Lib - redesign the code ## Tuesday, 19 May 2020 - implement the code ## Wednesday, 20 May 2020 - continue implementing the code ## Thursday, 21 May 2020 - continue implementing the code ## Friday, 22 May 2020 Result code ``` #!/usr/bin/python # import sys import re file_header = """.. Copyright (c) 2019 Intel .. .. Licensed under the Apache License, Version 2.0 (the "License"); .. you may not use this file except in compliance with the License. .. You may obtain a copy of the License at .. .. http://www.apache.org/licenses/LICENSE-2.0 .. .. Unless required by applicable law or agreed to in writing, software .. distributed under the License is distributed on an "AS IS" BASIS, .. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. .. See the License for the specific language governing permissions and .. limitations under the License. """ def open_files(filename): global f f = open(filename, encoding="utf8") def decode_contents(): global f global content_list content_list = [] h1_count = 0 in_contents = False while(True): isH1 = False current_line = f.readline() if current_line == "\n": # ganti line continue if in_contents: if current_line == "Figures\n": # end of content f.seek(0) # Reset the files print(content_list) break next_line = f.readline() while next_line != "\n": current_line += ' ' + next_line next_line = f.readline() current_line = re.sub('[^A-Za-z0-9 .,\[\]{}/\_]+', '', re.sub('<[^>]+>`__', '', current_line)).strip() if re.match('Appendix [0-9]+ [A-Za-z0-9 ]+', current_line): # delete the page numbering heading = current_line.rsplit(' ', 1)[0] isH1 = True h1_count += 1 else: section_tag = current_line.split(' ', 1)[0].split('.') if len(section_tag) == 1: isH1 = True h1_count += 1 elif len(section_tag) == 2 and section_tag[1] == '0': isH1 = True h1_count += 1 # delete the section tag & page numbering heading = current_line.split(' ', 1)[1].rsplit(' ', 1)[0] print(str(h1_count) + " " + heading if isH1 else " " + heading) content_list.append([isH1, heading]) if current_line == "Contents\n": in_contents = True def create_index(): global content_list global file_header index_format = ["Contents", "Revision History"] new_file = open("index.rst", "w", encoding="utf-8") new_file.write(file_header) # differentiate the doc title with other docs lines = f.readline() new_file.write(lines) new_file.write("=" * (len(lines)-1)+"\n\n") # copy and paste until meet the contents lines = "" while(lines != index_format[0]+"\n"): # new_file.write("aa" + lines + "aa \n") if lines == "\n" or lines == "": lines = f.readline() continue next_line = "" while(next_line != '\n'): lines += next_line next_line = f.readline() new_file.write("| " + lines + "\n") lines = f.readline() # Contents new_file.write(""" Contents -------- .. toctree:: :maxdepth: 2 :caption: Contents: """) for i in content_list: # write all rst files that will generated if i[0]: new_file.write(" " + i[1].replace(' ', '-').replace(',', '')+".rst\n") while lines != index_format[1] + "\n": # skip lines until find the next index part lines = f.readline() # write other index parts index_sub = 1 new_file.write("\n") # line padding while len(lines) != 2 and not re.match('[A-Za-z0-9 ][\n]',lines): # check until next content list # lines = re.sub('[^A-Za-z0-9 .,\[\]{}]+', '', re.sub('<[^>]+>', '', lines)).strip() if index_sub < len(index_format): if lines == index_format[index_sub] + "\n": # mark the sub heading new_file.write(lines) new_file.write("-"*(len(lines)-1)) index_sub += 1 else: new_file.write(lines) else: new_file.write(lines) lines = f.readline() # Close index file new_file.close() def transfer_doc(): global content_list header_list = [] next_line = "" for i in content_list: if i[0]: header_list.append(i[1]) print(header_list) # scroll to next H1 prev_line = next_line next_line = f.readline() scroll_to = header_list[0] while prev_line != scroll_to + "\n": prev_line = next_line next_line = f.readline() print("169\n") copy_until = "" for i in range(0, len(header_list)): # set current header rst_header = header_list[i] if re.match('Appendix [0-9]+ [A-Za-z0-9 ]+', rst_header): rst_header = rst_header.split(' ', 2)[2] # Generate the new file new_file = open(rst_header.replace(' ', '-').replace(',', '').replace('/', '')+".rst", "w", encoding="utf-8") new_file.write(file_header) # Assume the cursor is pointing on the right line while prev_line != rst_header and not re.match(copy_until, prev_line): print("a") prev_line = next_line next_line = f.readline() new_file.write(rst_header + "\n") new_file.write("="*len(rst_header)+"\n") while prev_line != "\n": print("b") prev_line = next_line next_line = f.readline() # set stop header if i != len(header_list)-1: next_header = header_list[i+1] if re.match('Appendix [0-9]+ [A-Za-z0-9 ]+', next_header): # it's an appendix next_header = next_header.split(' ', 2)[2] copy_until = '[0-9]+. ' + next_header + '\n' else: copy_until = next_header + '[\n]' # it's not appendix else: next_header = "None" copy_until = '[^A-Za-z0-9# ][\n]' # rst contents # gak nemu - # while prev_line == "\n": prev_line = next_line next_line = f.readline() print("219\n") while not re.match(copy_until, next_line): # break fo the last line in case can't break by the while if prev_line == "": break # removing the original .. |image.... tag if prev_line[0:9] == ".. |image": prev_line = next_line next_line = f.readline() prev_line = next_line next_line = f.readline() # add the image based on Figure tag. if re.match(r"Figure [0-9]*[.] +", prev_line): image_path = "images/" + re.sub(r"[\s\/\\]", "-", re.sub(r"Figure [0-9]*[.] ", "", prev_line).strip())+".jpg" new_file.write(".. image:: " + image_path + "\n") new_file.write(" :width: 400\n") new_file.write(" :alt: " + prev_line + "\n") print(prev_line) else: new_file.write(prev_line.strip(' ')) # print(rst_header + " : "+ prev_line) prev_line = next_line next_line = f.readline() # close the file new_file.close() print(i) print(len(header_list)) def main(): filename = "O-RAN_FH_Lib_SAS.rst" # filename = "5G_FAPI_IAPI_Translator_Module.rst" open_files(filename) decode_contents() create_index() transfer_doc() # print command line arguments # for arg in sys.argv[1:]: # print(arg) if __name__ == "__main__": main() ```