From cea75a436505122efe738c35243d119cc3424968 Mon Sep 17 00:00:00 2001 From: Jeff Date: Mon, 11 Jun 2018 19:36:32 +0800 Subject: [PATCH] Adds new docs index for methods can be used as bots (#840) --- telethon_generator/generators/docs.py | 63 ++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/telethon_generator/generators/docs.py b/telethon_generator/generators/docs.py index 2d2546f3..54d5eec2 100755 --- a/telethon_generator/generators/docs.py +++ b/telethon_generator/generators/docs.py @@ -96,23 +96,37 @@ def _build_menu(docs, filename, root, relative_main_index): docs.end_menu() -def _generate_index(folder, original_paths, root): +def _generate_index(folder, original_paths, root, + bots_index=False, bots_index_paths=()): """Generates the index file for the specified folder""" # Determine the namespaces listed here (as sub folders) # and the files (.html files) that we should link to namespaces = [] files = [] - for item in os.listdir(folder): - if os.path.isdir(os.path.join(folder, item)): - namespaces.append(item) - elif item != 'index.html': - files.append(item) + INDEX = 'index.html' + BOT_INDEX = 'botindex.html' + + if not bots_index: + for item in os.listdir(folder): + if os.path.isdir(os.path.join(folder, item)): + namespaces.append(item) + elif item not in (INDEX, BOT_INDEX): + files.append(item) + else: + # bots_index_paths should be a list of "namespace/method.html" + # or "method.html" + for item in bots_index_paths: + dirname = os.path.dirname(item) + if dirname and dirname not in namespaces: + namespaces.append(dirname) + elif not dirname and item not in (INDEX, BOT_INDEX): + files.append(item) paths = {k: _get_relative_path(v, folder, folder=True) for k, v in original_paths.items()} # Now that everything is setup, write the index.html file - filename = os.path.join(folder, 'index.html') + filename = os.path.join(folder, BOT_INDEX if bots_index else INDEX) with DocsWriter(filename, type_to_path=_get_path_for_type) as docs: # Title should be the current folder name docs.write_head(folder.title(), @@ -124,16 +138,33 @@ def _generate_index(folder, original_paths, root): relative_main_index=paths['index_all']) docs.write_title(_get_relative_path(folder, root, folder=True).title()) + if bots_index: + docs.write_text('These are the methods that you can use as a bot. ' + 'Click here to ' + 'view them all.'.format(INDEX)) + else: + docs.write_text('Click here to view the methods ' + 'that you can use as a bot.'.format(BOT_INDEX)) if namespaces: docs.write_title('Namespaces', level=3) docs.begin_table(4) namespaces.sort() for namespace in namespaces: # For every namespace, also write the index of it + namespace_paths = [] + if bots_index: + for item in bots_index_paths: + if os.path.dirname(item) == namespace: + namespace_paths.append(os.path.basename(item)) _generate_index(os.path.join(folder, namespace), - original_paths, root) - docs.add_row(namespace.title(), - link=os.path.join(namespace, 'index.html')) + original_paths, root, + bots_index, namespace_paths) + if bots_index: + docs.add_row(namespace.title(), + link=os.path.join(namespace, BOT_INDEX) + else: + docs.add_row(namespace.title(), + link=os.path.join(namespace, INDEX)) docs.end_table() @@ -214,6 +245,7 @@ def _write_html_pages(tlobjects, errors, layer, input_res, output_dir): 'search.js': 'js/search.js', '404': '404.html', 'index_all': 'index.html', + 'bot_index': 'botindex.html', 'index_types': 'types/index.html', 'index_methods': 'methods/index.html', 'index_constructors': 'constructors/index.html' @@ -247,6 +279,7 @@ def _write_html_pages(tlobjects, errors, layer, input_res, output_dir): # Since the output directory is needed everywhere partially apply it now create_path_for = functools.partial(_get_create_path_for, output_dir) path_for_type = functools.partial(_get_path_for_type, output_dir) + bot_docs_paths = [] for tlobject in tlobjects: filename = create_path_for(tlobject) @@ -269,6 +302,8 @@ def _write_html_pages(tlobjects, errors, layer, input_res, output_dir): if tlobject.is_function: docs.write_text('Bots can{} use this method.' .format("" if tlobject.bot_usable else "'t")) + if tlobject.is_function and tlobject.bot_usable: + bot_docs_paths.append(filename) # Write the code definition for this TLObject docs.write_code(tlobject) @@ -383,6 +418,11 @@ def _write_html_pages(tlobjects, errors, layer, input_res, output_dir): docs.add_script(relative_src=paths['search.js']) docs.end_body() + temp = [] + for item in bot_docs_paths: + temp.append(os.path.sep.join(item.split(os.path.sep)[2:])) + bot_docs_paths = temp + # Find all the available types (which are not the same as the constructors) # Each type has a list of constructors associated to it, hence is a map for t, cs in type_to_constructors.items(): @@ -510,6 +550,9 @@ def _write_html_pages(tlobjects, errors, layer, input_res, output_dir): _generate_index(os.path.join(output_dir, folder), original_paths, output_dir) + _generate_index(os.path.join(output_dir, 'methods'), original_paths, + output_dir, True, bot_docs_paths) + # Write the final core index, the main index for the rest of files types = set() methods = []