From edcb998af4da0a59402a3568a842837002582b98 Mon Sep 17 00:00:00 2001 From: Pawel Sikora Date: Tue, 7 Dec 2021 05:26:10 +0100 Subject: [PATCH 1/2] fix: handle missing pages in mkdocs nav section Add handling of missing (may be on purpose) navigation section (nav) in mkdocs.yml config file. If pages are missing in nav, plugin will create the required pages from the filesystem structure, e.g. parent1/parent2/.../your_page.md: # My Page ... In the following manner: Confluence Space: * Main Parent: * Parent1: * Parent2: ...: - My Page Fix for Issue: #2 --- mkdocs_with_confluence/plugin.py | 57 ++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/mkdocs_with_confluence/plugin.py b/mkdocs_with_confluence/plugin.py index 051f299..f40478a 100644 --- a/mkdocs_with_confluence/plugin.py +++ b/mkdocs_with_confluence/plugin.py @@ -6,6 +6,7 @@ import shutil import requests import mimetypes import mistune +from time import sleep from mkdocs.config import config_options from mkdocs.plugins import BasePlugin from md2cf.confluence_renderer import ConfluenceRenderer @@ -38,12 +39,26 @@ class MkdocsWithConfluence(BasePlugin): def on_nav(self, nav, config, files): MkdocsWithConfluence.tab_nav = [] navigation_items = nav.__repr__() + for n in navigation_items.split("\n"): - # print(f"* {n}") leading_spaces = len(n) - len(n.lstrip(" ")) spaces = leading_spaces * " " if "Page" in n: - p = spaces + self.__get_page_title(n) + try: + self.page_title = self.__get_page_title(n) + if self.page_title is None: + raise AttributeError + except AttributeError: + self.page_local_path = self.__get_page_url(n) + print( + f"WARN - Page from path {self.page_local_path} has no" + f" entity in the mkdocs.yml nav section. It will be uploaded" + f" to the Confluence, but you may not see it on the web server!" + ) + self.page_local_name = self.__get_page_name(n) + self.page_title = self.page_local_name + + p = spaces + self.page_title MkdocsWithConfluence.tab_nav.append(p) if "Section" in n: s = spaces + self.__get_section_title(n) @@ -51,8 +66,11 @@ class MkdocsWithConfluence(BasePlugin): def on_files(self, files, config): pages = files.documentation_pages() - self.flen = len(pages) - print(f"Number of Files: {self.flen}") + try: + self.flen = len(pages) + print(f"Number of Files in directory tree: {self.flen}") + except 0: + print("ERR: You have no documentation pages" "in the directory tree, please add at least one!") def on_post_template(self, output_content, template_name, config): if self.config["verbose"] is False and self.config["debug"] is False: @@ -247,8 +265,24 @@ class MkdocsWithConfluence(BasePlugin): time.sleep(1) # if self.config['debug']: - print(f"Trying to ADD page '{page.title}' to parent0({parent}) ID: {parent_id}") + + if parent_id is None: + for i in range(11): + while parent_id is None: + try: + self.add_page(page.title, parent_id, confluence_body) + except requests.exceptions.HTTPError: + print( + f"ERR - HTTP error on adding page. It probably occured due to " + f"parent ID('{parent_id}') page is not YET synced on server. Retry nb {i}/10..." + ) + sleep(5) + parent_id = self.find_page_id(parent) + break + self.add_page(page.title, parent_id, confluence_body) + + print(f"Trying to ADD page '{page.title}' to parent0({parent}) ID: {parent_id}") for i in MkdocsWithConfluence.tab_nav: if page.title in i: n_kol = len(i + "INFO - Mkdocs With Confluence:" + " *NEW PAGE*") @@ -271,11 +305,22 @@ class MkdocsWithConfluence(BasePlugin): def on_page_content(self, html, page, config, files): return html + def __get_page_url(self, section): + return re.search("url='(.*)'\\)", section).group(1)[:-1] + ".md" + + def __get_page_name(self, section): + return os.path.basename(re.search("url='(.*)'\\)", section).group(1)[:-1]) + def __get_section_title(self, section): return re.search("Section\\(title='(.*)'\\)", section).group(1) def __get_page_title(self, section): - return re.search("\\s*Page\\(title='(.*)',", section).group(1) + r = re.search("\\s*Page\\(title='(.*)',", section) + try: + return r.group(1) + except AttributeError: + name = self.__get_page_url(section) + print(f"ERR - Page '{name}' doesn't exist in the mkdocs.yml nav section!") def add_attachment(self, page_name, filepath): if self.config["verbose"]: From 9e2541c5fedcef32ed51b175cbf1d1808de7c416 Mon Sep 17 00:00:00 2001 From: Pawel Sikora Date: Tue, 4 Jan 2022 21:32:28 +0100 Subject: [PATCH 2/2] cleanup and bump to 0.2.5 --- mkdocs_with_confluence/plugin.py | 171 +++++++++++++++++++++---------- setup.py | 2 +- 2 files changed, 117 insertions(+), 56 deletions(-) diff --git a/mkdocs_with_confluence/plugin.py b/mkdocs_with_confluence/plugin.py index f40478a..93ccd16 100644 --- a/mkdocs_with_confluence/plugin.py +++ b/mkdocs_with_confluence/plugin.py @@ -1,11 +1,13 @@ import time import os +import sys import re import tempfile import shutil import requests import mimetypes import mistune +import contextlib from time import sleep from mkdocs.config import config_options from mkdocs.plugins import BasePlugin @@ -15,6 +17,19 @@ from os import environ TEMPLATE_BODY = "

TEMPLATE

" +@contextlib.contextmanager +def nostdout(): + save_stdout = sys.stdout + sys.stdout = DummyFile() + yield + sys.stdout = save_stdout + + +class DummyFile(object): + def write(self, x): + pass + + class MkdocsWithConfluence(BasePlugin): _id = 0 config_scheme = ( @@ -61,7 +76,20 @@ class MkdocsWithConfluence(BasePlugin): p = spaces + self.page_title MkdocsWithConfluence.tab_nav.append(p) if "Section" in n: - s = spaces + self.__get_section_title(n) + try: + self.section_title = self.__get_section_title(n) + if self.section_title is None: + raise AttributeError + except AttributeError: + self.section_local_path = self.__get_page_url(n) + print( + f"WARN - Section from path {self.section_local_path} has no" + f" entity in the mkdocs.yml nav section. It will be uploaded" + f" to the Confluence, but you may not see it on the web server!" + ) + self.section_local_name = self.__get_section_title(n) + self.section_title = self.section_local_name + s = spaces + self.section_title MkdocsWithConfluence.tab_nav.append(s) def on_files(self, files, config): @@ -93,7 +121,7 @@ class MkdocsWithConfluence(BasePlugin): else: print( "INFO - Mkdocs With Confluence: Exporting MKDOCS pages to Confluence " - "turned ON by var {env_name}==1!" + f"turned ON by var {env_name}==1!" ) self.enabled = True else: @@ -126,25 +154,27 @@ class MkdocsWithConfluence(BasePlugin): print("-", end="", flush=True) print(f"] ({MkdocsWithConfluence._id} / {self.flen})", end="\r", flush=True) - if self.config["verbose"]: - print(f"\nHandling Page '{page.title}' (And Parent Nav Pages if necessary):\n") + if self.config["debug"]: + print(f"\nDEBUG - Handling Page '{page.title}' (And Parent Nav Pages if necessary):\n") if not all(self.config_scheme): - print("ERR: YOU HAVE EMPTY VALUES IN YOUR CONFIG. ABORTING") + print("DEBUG - ERR: YOU HAVE EMPTY VALUES IN YOUR CONFIG. ABORTING") return markdown try: - if self.config["verbose"]: - print("Get section first parent title...: ") + if self.config["debug"]: + print("DEBUG - Get section first parent title...: ") try: + parent = self.__get_section_title(page.ancestors[0].__repr__()) except IndexError as e: - print( - f'ERR({e}): No second parent! Assuming self.config["parent_page_name"]' - f"{self.config['parent_page_name']}..." - ) + if self.config["debug"]: + print( + f"DEBUG - WRN({e}): No first parent! Assuming " + f"DEBUG - {self.config['parent_page_name']}..." + ) parent = None - if self.config["verbose"]: - print(f"{parent}") + if self.config["debug"]: + print(f"DEBUG - {parent}") if not parent: parent = self.config["parent_page_name"] @@ -153,23 +183,30 @@ class MkdocsWithConfluence(BasePlugin): else: main_parent = self.config["space"] - if self.config["verbose"]: - print("Get section second parent title...: ") + if self.config["debug"]: + print("DEBUG - Get section second parent title...: ") try: parent1 = self.__get_section_title(page.ancestors[1].__repr__()) except IndexError as e: - print(f"ERR({e}) No second parent! Assuming second parent is main parent: {main_parent}...") + if self.config["debug"]: + print( + f"DEBUG - ERR({e}) No second parent! Assuming " + f"second parent is main parent: {main_parent}..." + ) parent1 = None - if self.config["verbose"]: + if self.config["debug"]: print(f"{parent}") if not parent1: parent1 = main_parent - if self.config["verbose"]: - print(f"ONLY ONE PARENT FOUND. ASSUMING AS A FIRST NODE after main parent config {main_parent}") + if self.config["debug"]: + print( + f"DEBUG - ONLY ONE PARENT FOUND. ASSUMING AS A " + f"FIRST NODE after main parent config {main_parent}" + ) - if self.config["verbose"]: - print(f"PARENT0: {parent}, PARENT1: {parent1}, MAIN PARENT: {main_parent}") + if self.config["debug"]: + print(f"DEBUG - PARENT0: {parent}, PARENT1: {parent1}, MAIN PARENT: {main_parent}") tf = tempfile.NamedTemporaryFile(delete=False) f = open(tf.name, "w") @@ -178,11 +215,11 @@ class MkdocsWithConfluence(BasePlugin): try: for match in re.finditer(r'img src="file://(.*)" s', markdown): if self.config["debug"]: - print(f"FOUND IMAGE: {match.group(1)}") + print(f"DEBUG - FOUND IMAGE: {match.group(1)}") files.append(match.group(1)) except AttributeError as e: if self.config["debug"]: - print(f"WARN(({e}): No images found in markdown. Proceed..") + print(f"DEBUG - WARN(({e}): No images found in markdown. Proceed..") new_markdown = re.sub( r'