from os import path

import yaml
from bs4 import BeautifulSoup

from src.processors.processors import process_code_blocks, process_header_ids

root_folder = path.dirname(path.dirname(__file__))

titles = {}


def process_titles(row_titles, title_prefix, suffix):
    url = title_prefix + "/" + row_titles["url"].replace("./", "") + suffix
    titles[url] = row_titles["title"]
    if "content" not in row_titles:
        return
    for child_titles in row_titles["content"]:
        process_titles(child_titles, title_prefix, suffix)


def load_api_titles():
    api_title_files_path = path.join(root_folder, 'api', 'latest', 'jvm', 'stdlib', 'index.yml')
    with open(api_title_files_path) as title_files:
        process_titles(yaml.load(title_files)[0], 'latest/jvm/stdlib', '')

    test_title_files_path = path.join(root_folder, 'api', 'latest', 'kotlin.test', 'index.yml')
    with open(test_title_files_path) as title_files:
        process_titles(yaml.load(title_files)[0], 'latest/kotlin.test', '.html')


def get_api_page(build_mode: bool, page_path):
    if not page_path.endswith('.html'):
        page_path += '.html'
    if len(titles) == 0:
        try:
            load_api_titles()
        except FileNotFoundError as e:
            if build_mode:
                raise e
            else:
                print("API module is not included: ", e)

    file_path = path.join(root_folder, 'api', page_path)
    if not path.exists(file_path):
        return None
    with open(file_path) as html_file:
        html_content = BeautifulSoup(html_file.read(), 'html.parser')
        html_content = process_code_blocks(html_content)
        html_content = process_header_ids(html_content)
        return {
            "title": titles[page_path],
            "content": html_content
        }

@@ -251,17 +251,61 @@
 ix, 
-suffix):%0A
+path_folder, suffix):%0A    if path_folder == '.':%0A    
     
@@ -376,16 +376,107 @@
  suffix%0A
+    else:%0A        url = title_prefix + row_titles%5B%22url%22%5D.split(path_folder, 1)%5B1%5D + suffix%0A
     titl
@@ -653,16 +653,29 @@
 _prefix,
+ path_folder,
  suffix)
@@ -919,16 +919,21 @@
 stdlib',
+ '.',
  '')%0A%0A  
@@ -1071,32 +1071,32 @@
 as title_files:%0A
-
         process_
@@ -1150,16 +1150,32 @@
 n.test',
+  'kotlin-test',
  '.html'

Support both relative and absolute urls for titles