From e946825e0fe944abd6cec57511476d599b323d0b Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 5 Jan 2018 02:16:30 +0100 Subject: [PATCH] Move docs compiler --- .../generator.py => compiler/docs/compiler.py | 44 ++++++++++++++----- .../docs/template}/page.txt | 0 .../docs/template}/toctree.txt | 0 3 files changed, 32 insertions(+), 12 deletions(-) rename docs/source/generator.py => compiler/docs/compiler.py (72%) rename {docs/source/_templates => compiler/docs/template}/page.txt (100%) rename {docs/source/_templates => compiler/docs/template}/toctree.txt (100%) diff --git a/docs/source/generator.py b/compiler/docs/compiler.py similarity index 72% rename from docs/source/generator.py rename to compiler/docs/compiler.py index a24e29fb..c335ede5 100644 --- a/docs/source/generator.py +++ b/compiler/docs/compiler.py @@ -15,23 +15,26 @@ # # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . - +import ast import os import shutil -functions_path = "../../pyrogram/api/functions" -functions_base = "functions" +home = "compiler/docs" +destination = "docs/source" -types_path = "../../pyrogram/api/types" +functions_path = "pyrogram/api/functions" +types_path = "pyrogram/api/types" + +functions_base = "functions" types_base = "types" shutil.rmtree(types_base, ignore_errors=True) shutil.rmtree(functions_base, ignore_errors=True) -with open("_templates/page.txt") as f: +with open(home + "/template/page.txt") as f: page_template = f.read() -with open("_templates/toctree.txt") as f: +with open(home + "/template/toctree.txt") as f: toctree = f.read() @@ -46,15 +49,22 @@ def generate(source_path, base): if not i.startswith("__"): build("/".join([path, i]), level=level + 1) except NotADirectoryError: - name = "".join([str(j.title()) for j in os.path.splitext(i)[0].split("_")]) + with open(path + "/" + i) as f: + p = ast.parse(f.read()) + + for node in ast.walk(p): + if isinstance(node, ast.ClassDef): + name = node.name + + # name = "".join([str(j.title()) for j in os.path.splitext(i)[0].split("_")]) full_path = os.path.basename(path) + "/" + name + ".rst" if level: full_path = base + "/" + full_path - os.makedirs(os.path.dirname(full_path), exist_ok=True) + os.makedirs(os.path.dirname(destination + "/" + full_path), exist_ok=True) - with open(full_path, "w") as f: + with open(destination + "/" + full_path, "w") as f: f.write( page_template.format( title=name, @@ -87,7 +97,7 @@ def generate(source_path, base): inner_path = base + "/index" + ".rst" - with open(inner_path, "w") as f: + with open(destination + "/" + inner_path, "w") as f: f.write( toctree.format( title=k.title(), @@ -99,5 +109,15 @@ def generate(source_path, base): f.write("\n") -generate(types_path, types_base) -generate(functions_path, functions_base) +def start(): + generate(types_path, types_base) + generate(functions_path, functions_base) + + +if "__main__" == __name__: + functions_path = "../../pyrogram/api/functions" + types_path = "../../pyrogram/api/types" + home = "." + destination = "../../docs/source" + + start() diff --git a/docs/source/_templates/page.txt b/compiler/docs/template/page.txt similarity index 100% rename from docs/source/_templates/page.txt rename to compiler/docs/template/page.txt diff --git a/docs/source/_templates/toctree.txt b/compiler/docs/template/toctree.txt similarity index 100% rename from docs/source/_templates/toctree.txt rename to compiler/docs/template/toctree.txt