Fix syntax
This commit is contained in:
parent
88db9014a7
commit
a6c0d8df3d
@ -12,63 +12,82 @@ from md2cf.confluence_renderer import ConfluenceRenderer
|
|||||||
|
|
||||||
TEMPLATE_BODY = "<p> TEMPLATE </p>"
|
TEMPLATE_BODY = "<p> TEMPLATE </p>"
|
||||||
|
|
||||||
|
|
||||||
class MkdocsWithConfluence(BasePlugin):
|
class MkdocsWithConfluence(BasePlugin):
|
||||||
_id = 0
|
_id = 0
|
||||||
config_scheme = (
|
config_scheme = (
|
||||||
('host_url', config_options.Type(str, default=None)),
|
("host_url", config_options.Type(str, default=None)),
|
||||||
('space', config_options.Type(str, default=None)),
|
("space", config_options.Type(str, default=None)),
|
||||||
('parent_page_name', config_options.Type(str, default=None)),
|
("parent_page_name", config_options.Type(str, default=None)),
|
||||||
('username', config_options.Type(str, default=None)),
|
("username", config_options.Type(str, default=None)),
|
||||||
('password', config_options.Type(str, default=None)),
|
("password", config_options.Type(str, default=None)),
|
||||||
('enabled_if_env', config_options.Type(str, default=None)),
|
("enabled_if_env", config_options.Type(str, default=None)),
|
||||||
('verbose', config_options.Type(bool, default=False)),
|
("verbose", config_options.Type(bool, default=False)),
|
||||||
('debug', config_options.Type(bool, default=False)),
|
("debug", config_options.Type(bool, default=False)),
|
||||||
('dryrun', config_options.Type(bool, default=False)),
|
("dryrun", config_options.Type(bool, default=False)),
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.enabled = True
|
self.enabled = True
|
||||||
self.confluence_renderer = ConfluenceRenderer(use_xhtml=True)
|
self.confluence_renderer = ConfluenceRenderer(use_xhtml=True)
|
||||||
self.confluence_mistune = mistune.Markdown(renderer=self.confluence_renderer)
|
self.confluence_mistune = mistune.Markdown(renderer=self.confluence_renderer)
|
||||||
self.simple_log = True
|
self.simple_log = False
|
||||||
self.flen = 1
|
self.flen = 1
|
||||||
|
|
||||||
|
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)
|
||||||
|
MkdocsWithConfluence.tab_nav.append(p)
|
||||||
|
if "Section" in n:
|
||||||
|
s = spaces + self.__get_section_title(n)
|
||||||
|
MkdocsWithConfluence.tab_nav.append(s)
|
||||||
|
|
||||||
def on_files(self, files, config):
|
def on_files(self, files, config):
|
||||||
self.flen = len(files.documentation_pages())
|
pages = files.documentation_pages()
|
||||||
|
self.flen = len(pages)
|
||||||
print(f"Number of Files: {self.flen}")
|
print(f"Number of Files: {self.flen}")
|
||||||
|
|
||||||
#def on_pre_template(self, template, template_name, config):
|
|
||||||
def on_post_template(self, output_content, template_name, config):
|
def on_post_template(self, output_content, template_name, config):
|
||||||
if self.config['verbose'] is False and self.config['debug'] is False:
|
if self.config["verbose"] is False and self.config["debug"] is False:
|
||||||
self.simple_log = True
|
self.simple_log = True
|
||||||
print("INFO - Mkdocs With Confluence: Start exporting markdown pages... (simple logging)")
|
print("INFO - Mkdocs With Confluence: Start exporting markdown pages... (simple logging)")
|
||||||
else:
|
else:
|
||||||
self.simple_log = False
|
self.simple_log = False
|
||||||
|
|
||||||
def on_config(self, config):
|
def on_config(self, config):
|
||||||
if 'enabled_if_env' in self.config:
|
if "enabled_if_env" in self.config:
|
||||||
env_name = self.config['enabled_if_env']
|
env_name = self.config["enabled_if_env"]
|
||||||
if env_name:
|
if env_name:
|
||||||
self.enabled = os.environ.get(env_name) == '1'
|
self.enabled = os.environ.get(env_name) == "1"
|
||||||
if not self.enabled:
|
if not self.enabled:
|
||||||
print(
|
print(
|
||||||
'WARNING - Mkdocs With Confluence: Exporting MKDOCS pages to Confluence turned OFF: '
|
"WARNING - Mkdocs With Confluence: Exporting MKDOCS pages to Confluence turned OFF: "
|
||||||
f'(set environment variable {env_name} to 1 to enable)'
|
f"(set environment variable {env_name} to 1 to enable)"
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
print("INFO - Mkdocs With Confluence: Exporting MKDOCS pages to Confluence turned ON by var {env_name}==1!")
|
print(
|
||||||
|
"INFO - Mkdocs With Confluence: Exporting MKDOCS pages to Confluence "
|
||||||
|
"turned ON by var {env_name}==1!"
|
||||||
|
)
|
||||||
self.enabled = True
|
self.enabled = True
|
||||||
else:
|
else:
|
||||||
print(
|
print(
|
||||||
'WARNING - Mkdocs With Confluence: Exporting MKDOCS pages to Confluence turned OFF: '
|
"WARNING - Mkdocs With Confluence: Exporting MKDOCS pages to Confluence turned OFF: "
|
||||||
f'(set environment variable {env_name} to 1 to enable)'
|
f"(set environment variable {env_name} to 1 to enable)"
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
print("INFO - Mkdocs With Confluence: Exporting MKDOCS pages to Confluence turned ON by default!")
|
print("INFO - Mkdocs With Confluence: Exporting MKDOCS pages to Confluence turned ON by default!")
|
||||||
self.enabled = True
|
self.enabled = True
|
||||||
|
|
||||||
if self.config['dryrun']:
|
if self.config["dryrun"]:
|
||||||
print("WARNING - Mkdocs With Confluence - DRYRUN MODE turned ON")
|
print("WARNING - Mkdocs With Confluence - DRYRUN MODE turned ON")
|
||||||
self.dryrun = True
|
self.dryrun = True
|
||||||
else:
|
else:
|
||||||
@ -76,39 +95,39 @@ class MkdocsWithConfluence(BasePlugin):
|
|||||||
|
|
||||||
def on_page_markdown(self, markdown, page, config, files):
|
def on_page_markdown(self, markdown, page, config, files):
|
||||||
MkdocsWithConfluence._id += 1
|
MkdocsWithConfluence._id += 1
|
||||||
self.pw = self.config['password']
|
self.pw = self.config["password"]
|
||||||
self.user = self.config['username']
|
self.user = self.config["username"]
|
||||||
|
|
||||||
if self.enabled:
|
if self.enabled:
|
||||||
if self.simple_log is True:
|
if self.simple_log is True:
|
||||||
print("INFO - Mkdocs With Confluence: Page export progress: [", end='', flush=True)
|
print("INFO - Mkdocs With Confluence: Page export progress: [", end="", flush=True)
|
||||||
for i in range(MkdocsWithConfluence._id):
|
for i in range(MkdocsWithConfluence._id):
|
||||||
print("#", end='', flush=True)
|
print("#", end="", flush=True)
|
||||||
for j in range(self.flen - MkdocsWithConfluence._id):
|
for j in range(self.flen - MkdocsWithConfluence._id):
|
||||||
print("-", end='', flush=True)
|
print("-", end="", flush=True)
|
||||||
print(f"] ({MkdocsWithConfluence._id} / {self.flen})", end='\r', flush=True)
|
print(f"] ({MkdocsWithConfluence._id} / {self.flen})", end="\r", flush=True)
|
||||||
|
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print(f"\nHandling Page '{page.title}' (And Parent Nav Pages if necessary):\n")
|
print(f"\nHandling Page '{page.title}' (And Parent Nav Pages if necessary):\n")
|
||||||
if not all(self.config_scheme):
|
if not all(self.config_scheme):
|
||||||
print("ERR: YOU HAVE EMPTY VALUES IN YOUR CONFIG. ABORTING")
|
print("ERR: YOU HAVE EMPTY VALUES IN YOUR CONFIG. ABORTING")
|
||||||
return markdown
|
return markdown
|
||||||
try:
|
try:
|
||||||
|
|
||||||
parent = self.__get_section_title(page.ancestors[0].__repr__())
|
parent = self.__get_section_title(page.ancestors[0].__repr__())
|
||||||
|
|
||||||
if self.config['parent_page_name'] is not None:
|
if self.config["parent_page_name"] is not None:
|
||||||
main_parent = self.config['parent_page_name']
|
main_parent = self.config["parent_page_name"]
|
||||||
else:
|
else:
|
||||||
main_parent = self.config['space']
|
main_parent = self.config["space"]
|
||||||
|
|
||||||
try:
|
|
||||||
parent1 = self.__get_section_title(page.ancestors[1].__repr__())
|
parent1 = self.__get_section_title(page.ancestors[1].__repr__())
|
||||||
except:
|
if not parent1:
|
||||||
parent1 = main_parent
|
parent1 = main_parent
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print(f"ONLY ONE PARENT FOUND. ASSUMING AS A FIRST NODE after main parent config {main_parent}")
|
print(f"ONLY ONE PARENT FOUND. ASSUMING AS A FIRST NODE after main parent config {main_parent}")
|
||||||
|
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print(f"PARENT0: {parent}, PARENT1: {parent1}, MAIN PARENT: {main_parent}")
|
print(f"PARENT0: {parent}, PARENT1: {parent1}, MAIN PARENT: {main_parent}")
|
||||||
|
|
||||||
tf = tempfile.NamedTemporaryFile(delete=False)
|
tf = tempfile.NamedTemporaryFile(delete=False)
|
||||||
@ -117,24 +136,42 @@ class MkdocsWithConfluence(BasePlugin):
|
|||||||
files = []
|
files = []
|
||||||
try:
|
try:
|
||||||
for match in re.finditer(r'img src="file://(.*)" s', markdown):
|
for match in re.finditer(r'img src="file://(.*)" s', markdown):
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print(f"FOUND IMAGE: {match.group(1)}")
|
print(f"FOUND IMAGE: {match.group(1)}")
|
||||||
files.append(match.group(1))
|
files.append(match.group(1))
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print("WARN: No images found in markdown. Proceed..")
|
print("WARN: No images found in markdown. Proceed..")
|
||||||
|
|
||||||
new_markdown = re.sub(r'<img src="file:///tmp/', '!', markdown)
|
new_markdown = re.sub(
|
||||||
new_markdown = re.sub(r'" style="page-break-inside: avoid;">', '!', new_markdown)
|
r'<img src="file:///tmp/', '<p><ac:image ac:height="250"><ri:attachment ri:filename="', markdown
|
||||||
|
)
|
||||||
|
new_markdown = re.sub(r'" style="page-break-inside: avoid;">', '"/></ac:image></p>', new_markdown)
|
||||||
confluence_body = self.confluence_mistune(new_markdown)
|
confluence_body = self.confluence_mistune(new_markdown)
|
||||||
|
confluence_body = re.sub(
|
||||||
|
r"RFSW_FEATURE-(\d+)",
|
||||||
|
'<ac:structured-macro ac:name="jira" ac:schema-version="1" '
|
||||||
|
+ 'ac:macro-id="095a3154-8f6f-47e9-9385-a24b26e849b1"><ac:parameter '
|
||||||
|
+ 'ac:name="server">JIRADC ONE(EE Jira 7.6)</ac:parameter><ac:parameter '
|
||||||
|
+ 'ac:name="columns">key,summary,type,created,updated,due,assignee,'
|
||||||
|
+ "reporter,priority,status,resolution</ac:parameter><ac:parameter "
|
||||||
|
+ 'ac:name="serverId">ab49870f-b66d-38ea-800d-1f3d4b744bbd</ac:parameter>'
|
||||||
|
+ '<ac:parameter ac:name="key">RFSW_FEATURE-\1',
|
||||||
|
confluence_body,
|
||||||
|
)
|
||||||
|
confluence_body = re.sub(
|
||||||
|
r"RFSW_-(\d+)", "RFSW_FEATURE-\1</ac:parameter></ac:structured-macro></p>", confluence_body
|
||||||
|
)
|
||||||
f.write(confluence_body)
|
f.write(confluence_body)
|
||||||
|
print(confluence_body)
|
||||||
page_name = page.title
|
page_name = page.title
|
||||||
new_name = "confluence_page_" + page_name.replace(" ", "_") + ".html"
|
new_name = "confluence_page_" + page_name.replace(" ", "_") + ".html"
|
||||||
shutil.copy(f.name, new_name)
|
shutil.copy(f.name, new_name)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print(f"\nUPDATING PAGE TO CONFLUENCE, DETAILS:\n"
|
print(
|
||||||
|
f"\nUPDATING PAGE TO CONFLUENCE, DETAILS:\n"
|
||||||
f"HOST: {self.config['host_url']}\n"
|
f"HOST: {self.config['host_url']}\n"
|
||||||
f"SPACE: {self.config['space']}\n"
|
f"SPACE: {self.config['space']}\n"
|
||||||
f"TITLE: {page.title}\n"
|
f"TITLE: {page.title}\n"
|
||||||
@ -144,43 +181,74 @@ class MkdocsWithConfluence(BasePlugin):
|
|||||||
|
|
||||||
page_id = self.find_page_id(page.title)
|
page_id = self.find_page_id(page.title)
|
||||||
if page_id is not None:
|
if page_id is not None:
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print(f"JUST ONE STEP FROM UPDATE OF PAGE '{page.title}' \n" \
|
print(
|
||||||
f"CHECKING IF PARENT PAGE ON CONFLUENCE IS THE SAME AS HERE")
|
f"JUST ONE STEP FROM UPDATE OF PAGE '{page.title}' \n"
|
||||||
|
f"CHECKING IF PARENT PAGE ON CONFLUENCE IS THE SAME AS HERE"
|
||||||
|
)
|
||||||
|
|
||||||
parent_name = self.find_parent_name_of_page(page.title)
|
parent_name = self.find_parent_name_of_page(page.title)
|
||||||
|
|
||||||
if parent_name == parent:
|
if parent_name == parent:
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print(f" - OK, Parents match. Continue...")
|
print(" - OK, Parents match. Continue...")
|
||||||
else:
|
else:
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print(f" - ERR, Parents does not match: '{parent}' =/= '{parent_name}' Abort before messing up...")
|
print(f" - ERR, Parents does not match: '{parent}' =/= '{parent_name}' Aborting...")
|
||||||
return markdown
|
return markdown
|
||||||
self.update_page(page.title, confluence_body)
|
self.update_page(page.title, confluence_body)
|
||||||
|
for i in MkdocsWithConfluence.tab_nav:
|
||||||
|
if page.title in i:
|
||||||
|
n_kol = len(i + " *NEW PAGE*")
|
||||||
|
print(f"INFO - Mkdocs With Confluence: {i} *UPDATE*")
|
||||||
else:
|
else:
|
||||||
|
# if self.config['debug']:
|
||||||
|
print(f"PAGE: {page.title}, PARENT0: {parent}, PARENT1: {parent1}, MAIN PARENT: {main_parent}")
|
||||||
parent_id = self.find_page_id(parent)
|
parent_id = self.find_page_id(parent)
|
||||||
if not parent_id:
|
self.wait_until(parent_id, 1, 10)
|
||||||
if not parent_id:
|
|
||||||
second_parent_id = self.find_page_id(parent1)
|
second_parent_id = self.find_page_id(parent1)
|
||||||
|
self.wait_until(second_parent_id, 1, 10)
|
||||||
|
main_parent_id = self.find_page_id(main_parent)
|
||||||
|
if not parent_id:
|
||||||
if not second_parent_id:
|
if not second_parent_id:
|
||||||
main_parent_id = self.find_page_id(main_parent)
|
main_parent_id = self.find_page_id(main_parent)
|
||||||
if not main_parent_id:
|
if not main_parent_id:
|
||||||
print("ERR: MAIN PARENT UNKNOWN. ABORTING!")
|
print("ERR: MAIN PARENT UNKNOWN. ABORTING!")
|
||||||
return markdown
|
return markdown
|
||||||
|
|
||||||
|
# if self.config['debug']:
|
||||||
|
print(f"ADD page '{parent1}' to main parent({main_parent}) ID: {main_parent_id}")
|
||||||
body = TEMPLATE_BODY.replace("TEMPLATE", parent1)
|
body = TEMPLATE_BODY.replace("TEMPLATE", parent1)
|
||||||
self.add_page(parent1, main_parent_id, body)
|
self.add_page(parent1, main_parent_id, body)
|
||||||
#time.sleep(1)
|
for i in MkdocsWithConfluence.tab_nav:
|
||||||
|
if parent1 in i:
|
||||||
|
n_kol = len(i + "INFO - Mkdocs With Confluence:" + " *NEW PAGE*")
|
||||||
|
print(f"INFO - Mkdocs With Confluence: {i} *NEW PAGE*")
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
# if self.config['debug']:
|
||||||
|
print(f"ADD page '{parent}' to parent1({parent1}) ID: {second_parent_id}")
|
||||||
body = TEMPLATE_BODY.replace("TEMPLATE", parent)
|
body = TEMPLATE_BODY.replace("TEMPLATE", parent)
|
||||||
self.add_page(parent, parent_id, body)
|
self.add_page(parent, second_parent_id, body)
|
||||||
#time.sleep(1)
|
for i in MkdocsWithConfluence.tab_nav:
|
||||||
|
if parent in i:
|
||||||
|
n_kol = len(i + "INFO - Mkdocs With Confluence:" + " *NEW PAGE*")
|
||||||
|
print(f"INFO - Mkdocs With Confluence: {i} *NEW PAGE*")
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
# if self.config['debug']:
|
||||||
|
print(f"ADD page '{page.title}' to parent0({parent}) ID: {parent_id}")
|
||||||
self.add_page(page.title, parent_id, confluence_body)
|
self.add_page(page.title, parent_id, confluence_body)
|
||||||
|
for i in MkdocsWithConfluence.tab_nav:
|
||||||
|
if page.title in i:
|
||||||
|
n_kol = len(i + "INFO - Mkdocs With Confluence:" + " *NEW PAGE*")
|
||||||
|
print(f"INFO - Mkdocs With Confluence: {i} *NEW PAGE*")
|
||||||
|
|
||||||
if files:
|
if files:
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print(f"\nUPLOADING ATTACHMENTS TO CONFLUENCE, DETAILS:\n"
|
print(f"\nUPLOADING ATTACHMENTS TO CONFLUENCE, DETAILS:\n" f"FILES: {files}\n")
|
||||||
f"FILES: {files}\n"
|
|
||||||
)
|
print(f"\033[A\033[F\033[{n_kol}G *NEW ATTACHMENTS({len(files)})*")
|
||||||
for f in files:
|
for f in files:
|
||||||
self.add_attachment(page.title, f)
|
self.add_attachment(page.title, f)
|
||||||
|
|
||||||
@ -193,19 +261,21 @@ class MkdocsWithConfluence(BasePlugin):
|
|||||||
return html
|
return html
|
||||||
|
|
||||||
def __get_section_title(self, section):
|
def __get_section_title(self, section):
|
||||||
return re.search("Section\(title=\'(.*)\'\)", section).group(1)
|
return re.search("Section\\(title='(.*)'\\)", section).group(1)
|
||||||
|
|
||||||
|
def __get_page_title(self, section):
|
||||||
|
return re.search("\\s*Page\\(title='(.*)',", section).group(1)
|
||||||
|
|
||||||
def add_attachment(self, page_name, filepath):
|
def add_attachment(self, page_name, filepath):
|
||||||
if self.config['verbose']:
|
if self.config["verbose"]:
|
||||||
print(f"INFO - Mkdocs With Confluence * {page_name} *NEW ATTACHMENT* {filepath}")
|
print(f"INFO - Mkdocs With Confluence * {page_name} *NEW ATTACHMENT* {filepath}")
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print(f" * Mkdocs With Confluence: Add Attachment: PAGE NAME: {page_name}, FILE: {filepath}")
|
print(f" * Mkdocs With Confluence: Add Attachment: PAGE NAME: {page_name}, FILE: {filepath}")
|
||||||
page_id = self.find_page_id(page_name)
|
page_id = self.find_page_id(page_name)
|
||||||
if page_id:
|
if page_id:
|
||||||
url = self.config['host_url'] + "/" + \
|
url = self.config["host_url"] + "/" + page_id + "/child/attachment/"
|
||||||
page_id + '/child/attachment/'
|
headers = {"X-Atlassian-Token": "no-check"} # no content-type here!
|
||||||
headers = {'X-Atlassian-Token': 'no-check'} #no content-type here!
|
if self.config["debug"]:
|
||||||
if self.config['debug']:
|
|
||||||
print(f"URL: {url}")
|
print(f"URL: {url}")
|
||||||
filename = filepath
|
filename = filepath
|
||||||
auth = (self.user, self.pw)
|
auth = (self.user, self.pw)
|
||||||
@ -213,155 +283,146 @@ class MkdocsWithConfluence(BasePlugin):
|
|||||||
# determine content-type
|
# determine content-type
|
||||||
content_type, encoding = mimetypes.guess_type(filename)
|
content_type, encoding = mimetypes.guess_type(filename)
|
||||||
if content_type is None:
|
if content_type is None:
|
||||||
content_type = 'multipart/form-data'
|
content_type = "multipart/form-data"
|
||||||
files = {'file': (filename, open(filename, 'rb'), content_type)}
|
files = {"file": (filename, open(filename, "rb"), content_type)}
|
||||||
|
|
||||||
if not self.dryrun:
|
if not self.dryrun:
|
||||||
r = requests.post(url, headers=headers, files=files, auth=auth)
|
r = requests.post(url, headers=headers, files=files, auth=auth)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
print ('OK!')
|
print("OK!")
|
||||||
else:
|
else:
|
||||||
print ('ERR!')
|
print("ERR!")
|
||||||
else:
|
else:
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print("PAGE DOES NOT EXISTS")
|
print("PAGE DOES NOT EXISTS")
|
||||||
|
|
||||||
def find_page_id(self, page_name):
|
def find_page_id(self, page_name):
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print(f"INFO - * Mkdocs With Confluence: Find Page ID: PAGE NAME: {page_name}")
|
print(f"INFO - * Mkdocs With Confluence: Find Page ID: PAGE NAME: {page_name}")
|
||||||
name_confl = page_name.replace(" ", "+")
|
name_confl = page_name.replace(" ", "+")
|
||||||
url = self.config['host_url'] + "?title=" + \
|
url = self.config["host_url"] + "?title=" + name_confl + "&spaceKey=" + self.config["space"] + "&expand=history"
|
||||||
name_confl + '&spaceKey=' + self.config['space'] + \
|
if self.config["debug"]:
|
||||||
'&expand=history'
|
|
||||||
if self.config['debug']:
|
|
||||||
print(f"URL: {url}")
|
print(f"URL: {url}")
|
||||||
auth = (self.user, self.pw)
|
auth = (self.user, self.pw)
|
||||||
r = requests.get(url, auth=auth)
|
r = requests.get(url, auth=auth)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
response_json = r.json()
|
response_json = r.json()
|
||||||
if response_json["results"]:
|
if response_json["results"]:
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print(f"ID: {response_json['results'][0]['id']}")
|
print(f"ID: {response_json['results'][0]['id']}")
|
||||||
return response_json['results'][0]['id']
|
return response_json["results"][0]["id"]
|
||||||
else:
|
else:
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print("PAGE DOES NOT EXIST")
|
print("PAGE DOES NOT EXIST")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def add_page(self, page_name, parent_page_id, page_content_in_storage_format):
|
def add_page(self, page_name, parent_page_id, page_content_in_storage_format):
|
||||||
if self.config['verbose']:
|
# if self.config['verbose']:
|
||||||
print(f"INFO - * Mkdocs With Confluence: {page_name} - *NEW PAGE*")
|
# print(f"INFO - * Mkdocs With Confluence: {page_name} - *NEW PAGE*")
|
||||||
if self.config['debug']:
|
|
||||||
|
if self.config["debug"]:
|
||||||
print(f" * Mkdocs With Confluence: Adding Page: PAGE NAME: {page_name}, parent ID: {parent_page_id}")
|
print(f" * Mkdocs With Confluence: Adding Page: PAGE NAME: {page_name}, parent ID: {parent_page_id}")
|
||||||
url = self.config['host_url'] + "/"
|
url = self.config["host_url"] + "/"
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print(f"URL: {url}")
|
print(f"URL: {url}")
|
||||||
headers = {'Content-Type': 'application/json'}
|
headers = {"Content-Type": "application/json"}
|
||||||
auth = (self.user, self.pw)
|
auth = (self.user, self.pw)
|
||||||
space = self.config['space']
|
space = self.config["space"]
|
||||||
data = {
|
data = {
|
||||||
"type": "page",
|
"type": "page",
|
||||||
"title": page_name,
|
"title": page_name,
|
||||||
"space":{"key": space},
|
"space": {"key": space},
|
||||||
"ancestors": [{"id": parent_page_id}],
|
"ancestors": [{"id": parent_page_id}],
|
||||||
"body":{
|
"body": {"storage": {"value": page_content_in_storage_format, "representation": "storage"}},
|
||||||
"storage": {
|
|
||||||
"value": page_content_in_storage_format,
|
|
||||||
"representation": "storage"
|
|
||||||
}
|
}
|
||||||
}
|
if self.config["debug"]:
|
||||||
}
|
print(f"DATA: {data}")
|
||||||
|
|
||||||
if not self.dryrun:
|
if not self.dryrun:
|
||||||
r = requests.post(url, json=data, headers=headers, auth=auth)
|
r = requests.post(url, json=data, headers=headers, auth=auth)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print ('OK!')
|
print("OK!")
|
||||||
else:
|
else:
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print ('ERR!')
|
print("ERR!")
|
||||||
|
|
||||||
def update_page(self, page_name, page_content_in_storage_format):
|
def update_page(self, page_name, page_content_in_storage_format):
|
||||||
page_id = self.find_page_id(page_name)
|
page_id = self.find_page_id(page_name)
|
||||||
if self.config['verbose']:
|
# if self.config['verbose']:
|
||||||
print(f"INFO - * Mkdocs With Confluence: {page_name} - *UPDATE*")
|
# print(f"INFO - * Mkdocs With Confluence: {page_name} - *UPDATE*")
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print(f" * Mkdocs With Confluence: Update PAGE ID: {page_id}, PAGE NAME: {page_name}")
|
print(f" * Mkdocs With Confluence: Update PAGE ID: {page_id}, PAGE NAME: {page_name}")
|
||||||
if page_id:
|
if page_id:
|
||||||
page_version = self.find_page_version(page_name)
|
page_version = self.find_page_version(page_name)
|
||||||
page_version = page_version + 1
|
page_version = page_version + 1
|
||||||
url = self.config['host_url'] + "/" + page_id
|
url = self.config["host_url"] + "/" + page_id
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print(f"URL: {url}")
|
print(f"URL: {url}")
|
||||||
pid = f"{page_id}"
|
headers = {"Content-Type": "application/json"}
|
||||||
headers = {'Content-Type': 'application/json'}
|
|
||||||
auth = (self.user, self.pw)
|
auth = (self.user, self.pw)
|
||||||
data = {
|
data = {
|
||||||
"id": page_id,
|
"id": page_id,
|
||||||
"title": page_name,
|
"title": page_name,
|
||||||
"type": "page",
|
"type": "page",
|
||||||
"space":{"key":"RFSW"},
|
"space": {"key": "RFSW"},
|
||||||
"body":{
|
"body": {"storage": {"value": page_content_in_storage_format, "representation": "storage"}},
|
||||||
"storage": {
|
"version": {"number": page_version},
|
||||||
"value": page_content_in_storage_format,
|
|
||||||
"representation": "storage"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"version":{"number": page_version}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if not self.dryrun:
|
if not self.dryrun:
|
||||||
r = requests.put(url, json=data, headers=headers, auth=auth)
|
r = requests.put(url, json=data, headers=headers, auth=auth)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print ('OK!')
|
print("OK!")
|
||||||
else:
|
else:
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print ('ERR!')
|
print("ERR!")
|
||||||
else:
|
else:
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print("PAGE DOES NOT EXIST YET!")
|
print("PAGE DOES NOT EXIST YET!")
|
||||||
|
|
||||||
def find_page_version(self, page_name):
|
def find_page_version(self, page_name):
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print(f"INFO - * Mkdocs With Confluence: Find PAGE VERSION, PAGE NAME: {page_name}")
|
print(f"INFO - * Mkdocs With Confluence: Find PAGE VERSION, PAGE NAME: {page_name}")
|
||||||
name_confl = page_name.replace(" ", "+")
|
name_confl = page_name.replace(" ", "+")
|
||||||
url = self.config['host_url'] + "?title=" + \
|
url = self.config["host_url"] + "?title=" + name_confl + "&spaceKey=" + self.config["space"] + "&expand=version"
|
||||||
name_confl + '&spaceKey=' + \
|
|
||||||
self.config['space'] + '&expand=version'
|
|
||||||
|
|
||||||
auth = (self.user, self.pw)
|
auth = (self.user, self.pw)
|
||||||
r = requests.get(url, auth=auth)
|
r = requests.get(url, auth=auth)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
response_json = r.json()
|
response_json = r.json()
|
||||||
if response_json["results"]:
|
if response_json["results"]:
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print(f"VERSION: {response_json['results'][0]['version']['number']}")
|
print(f"VERSION: {response_json['results'][0]['version']['number']}")
|
||||||
return response_json['results'][0]['version']['number']
|
return response_json["results"][0]["version"]["number"]
|
||||||
else:
|
else:
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print("PAGE DOES NOT EXISTS")
|
print("PAGE DOES NOT EXISTS")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def find_parent_name_of_page(self, name):
|
def find_parent_name_of_page(self, name):
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print(f"INFO - * Mkdocs With Confluence: Find PARENT OF PAGE, PAGE NAME: {name}")
|
print(f"INFO - * Mkdocs With Confluence: Find PARENT OF PAGE, PAGE NAME: {name}")
|
||||||
idp = self.find_page_id(name)
|
idp = self.find_page_id(name)
|
||||||
name_confl = name.replace(" ", "+")
|
url = self.config["host_url"] + "/" + idp + "?expand=ancestors"
|
||||||
url = self.config['host_url'] + "/" + idp + "?expand=ancestors"
|
|
||||||
|
|
||||||
auth = (self.user, self.pw)
|
auth = (self.user, self.pw)
|
||||||
r = requests.get(url, auth=auth)
|
r = requests.get(url, auth=auth)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
response_json = r.json()
|
response_json = r.json()
|
||||||
if response_json:
|
if response_json:
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print(f"PARENT NAME: {response_json['ancestors'][0]['title']}")
|
print(f"PARENT NAME: {response_json['ancestors'][0]['title']}")
|
||||||
return response_json['ancestors'][0]['title']
|
return response_json["ancestors"][0]["title"]
|
||||||
else:
|
else:
|
||||||
if self.config['debug']:
|
if self.config["debug"]:
|
||||||
print("PAGE DOES NOT HAVE PARENT")
|
print("PAGE DOES NOT HAVE PARENT")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def wait_until(self, condition, interval=0.1, timeout=1):
|
||||||
|
start = time.time()
|
||||||
|
while not condition and time.time() - start < timeout:
|
||||||
|
time.sleep(interval)
|
||||||
|
Loading…
Reference in New Issue
Block a user