Add backlink generation
This commit is contained in:
parent
63b6f818ac
commit
0a5f18dafb
50
nb_wiki.py
50
nb_wiki.py
|
@ -5,6 +5,8 @@ import socketserver
|
||||||
import shutil
|
import shutil
|
||||||
list_of_tags = {}
|
list_of_tags = {}
|
||||||
copy_extensions = [".gif",".jpg",".png", ".html", ".stl"]
|
copy_extensions = [".gif",".jpg",".png", ".html", ".stl"]
|
||||||
|
backlinks = {}
|
||||||
|
html_files = []
|
||||||
|
|
||||||
output_dir = ""
|
output_dir = ""
|
||||||
base_url = ""
|
base_url = ""
|
||||||
|
@ -87,6 +89,7 @@ def process_files(base_dir, files):
|
||||||
output = open(output_path, "w+")
|
output = open(output_path, "w+")
|
||||||
output.write(template.split("{{content}}")[0])
|
output.write(template.split("{{content}}")[0])
|
||||||
output.close()
|
output.close()
|
||||||
|
html_files.append(output_path)
|
||||||
for line in file_content.split("\n"):
|
for line in file_content.split("\n"):
|
||||||
if doing_list:
|
if doing_list:
|
||||||
if line[0:1] == "*":
|
if line[0:1] == "*":
|
||||||
|
@ -114,7 +117,7 @@ def process_files(base_dir, files):
|
||||||
output = open(output_path, "a+")
|
output = open(output_path, "a+")
|
||||||
output.write("<p>")
|
output.write("<p>")
|
||||||
output.close()
|
output.close()
|
||||||
search_line_for_links(output_path, line, base_dir, os.path.splitext(other_bit)[0])
|
search_line_for_links(output_path, line, base_dir, os.path.splitext(other_bit)[0], other_bit)
|
||||||
output = open(output_path, "a+")
|
output = open(output_path, "a+")
|
||||||
output.write("</p>")
|
output.write("</p>")
|
||||||
output.close()
|
output.close()
|
||||||
|
@ -124,7 +127,7 @@ def process_files(base_dir, files):
|
||||||
output.close()
|
output.close()
|
||||||
#return
|
#return
|
||||||
|
|
||||||
def search_line_for_links(output_path, line, base_dir, page_title):
|
def search_line_for_links(output_path, line, base_dir, page_title, relative_path):
|
||||||
found_link = False
|
found_link = False
|
||||||
link_offset = 0
|
link_offset = 0
|
||||||
filename = output_path.replace(base_dir, '')
|
filename = output_path.replace(base_dir, '')
|
||||||
|
@ -140,7 +143,7 @@ def search_line_for_links(output_path, line, base_dir, page_title):
|
||||||
found_link = True
|
found_link = True
|
||||||
for u in range(i + 2, len(line)):
|
for u in range(i + 2, len(line)):
|
||||||
if line[u-1] == "]" and line[u] == "]" :
|
if line[u-1] == "]" and line[u] == "]" :
|
||||||
process_link(output, link_text)
|
process_link(output, link_text, relative_path)
|
||||||
# link_offset = u + 1
|
# link_offset = u + 1
|
||||||
break
|
break
|
||||||
|
|
||||||
|
@ -218,7 +221,8 @@ def process_tag(output, page_title, parent_link, base_dir , tag_text):
|
||||||
else:
|
else:
|
||||||
list_of_tags[tag_text] = [{"path": output_path, "title": page_title}]
|
list_of_tags[tag_text] = [{"path": output_path, "title": page_title}]
|
||||||
|
|
||||||
def process_link(output, link_text):
|
def process_link(output, link_text, relative_path):
|
||||||
|
global backlinks
|
||||||
link_text = link_text.replace(":","/")
|
link_text = link_text.replace(":","/")
|
||||||
link_path = os.path.join(base_url, link_text)
|
link_path = os.path.join(base_url, link_text)
|
||||||
if not "|" in link_text:
|
if not "|" in link_text:
|
||||||
|
@ -226,12 +230,18 @@ def process_link(output, link_text):
|
||||||
output.write('<a href="' + os.path.join(base_url,link_text) + '">' + link_text + '</a>')
|
output.write('<a href="' + os.path.join(base_url,link_text) + '">' + link_text + '</a>')
|
||||||
else:
|
else:
|
||||||
output.write('<a href="' + os.path.join(base_url,link_text) + '.html">' + link_text + '</a>')
|
output.write('<a href="' + os.path.join(base_url,link_text) + '.html">' + link_text + '</a>')
|
||||||
|
if link_text not in backlinks.keys():
|
||||||
|
backlinks[link_text + ".html"] = []
|
||||||
|
backlinks[link_text + ".html"].append(relative_path)
|
||||||
else:
|
else:
|
||||||
split_link = link_text.split("|")
|
split_link = link_text.split("|")
|
||||||
if os.path.splitext(split_link[0].rstrip())[1] in copy_extensions:
|
if os.path.splitext(split_link[0].rstrip())[1] in copy_extensions:
|
||||||
output.write('<a href="' + os.path.join(base_url,split_link[0]) + '">' + split_link[1] + '</a>')
|
output.write('<a href="' + os.path.join(base_url,split_link[0]) + '">' + split_link[1] + '</a>')
|
||||||
else:
|
else:
|
||||||
output.write('<a href="' + os.path.join(base_url,split_link[0]) + '.html">' + split_link[1] + '</a>')
|
output.write('<a href="' + os.path.join(base_url,split_link[0]) + '.html">' + split_link[1] + '</a>')
|
||||||
|
if split_link[0] + ".html" not in backlinks.keys():
|
||||||
|
backlinks[split_link[0]+".html"] = []
|
||||||
|
backlinks[split_link[0]+".html"].append(relative_path)
|
||||||
def process_external_link(output, link_text):
|
def process_external_link(output, link_text):
|
||||||
output.write('<a href="' + link_text + '">' + link_text + '</a>')
|
output.write('<a href="' + link_text + '">' + link_text + '</a>')
|
||||||
|
|
||||||
|
@ -244,7 +254,7 @@ def process_list(output_path, lines, page_title, base_dir, other_bit):
|
||||||
o.write("<li>")
|
o.write("<li>")
|
||||||
o.close()
|
o.close()
|
||||||
#search_line_for_links(output_path,line[2:len(line)], page_title)
|
#search_line_for_links(output_path,line[2:len(line)], page_title)
|
||||||
search_line_for_links(output_path, line[2:len(line)], base_dir, os.path.splitext(other_bit)[0])
|
search_line_for_links(output_path, line[2:len(line)], base_dir, os.path.splitext(other_bit)[0], other_bit)
|
||||||
o = open(output_path, "a+")
|
o = open(output_path, "a+")
|
||||||
o.write("</li>")
|
o.write("</li>")
|
||||||
o.close()
|
o.close()
|
||||||
|
@ -269,6 +279,7 @@ def build_index(base_dir):
|
||||||
list_of_notebooks.append(directory)
|
list_of_notebooks.append(directory)
|
||||||
o = open(output_file, "a+")
|
o = open(output_file, "a+")
|
||||||
template = template.replace("{{breadcrumbs}}","")
|
template = template.replace("{{breadcrumbs}}","")
|
||||||
|
template = template.replace("{{backlinks}}","")
|
||||||
template = template.replace('{{pagetitle}}', "rmgr's wiki")
|
template = template.replace('{{pagetitle}}', "rmgr's wiki")
|
||||||
o.write(template.split("{{content}}")[0])
|
o.write(template.split("{{content}}")[0])
|
||||||
o.write('<ul>')
|
o.write('<ul>')
|
||||||
|
@ -290,6 +301,7 @@ def build_directories(base_dir):
|
||||||
o = open(output_file, "a+")
|
o = open(output_file, "a+")
|
||||||
template_file = open(template_location, "r")
|
template_file = open(template_location, "r")
|
||||||
template = template_file.read()
|
template = template_file.read()
|
||||||
|
template = template.replace("{{backlinks}}","")
|
||||||
split_path = splitall(directory.replace(base_dir,"")[1:])
|
split_path = splitall(directory.replace(base_dir,"")[1:])
|
||||||
breadcrumbs = '> <a href="/">/</a> '
|
breadcrumbs = '> <a href="/">/</a> '
|
||||||
for d in range(0,len(split_path)-1):
|
for d in range(0,len(split_path)-1):
|
||||||
|
@ -325,6 +337,33 @@ def build_tag_pages(output_path):
|
||||||
f.write(template.split("{{content}}")[1])
|
f.write(template.split("{{content}}")[1])
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
def process_backlinks():
|
||||||
|
global output_dir
|
||||||
|
# Loop over all html files
|
||||||
|
for html_file in html_files:
|
||||||
|
html_file_relative = html_file.replace(output_dir + "/", "")
|
||||||
|
# Check if path exists in backlinks dictionary
|
||||||
|
if html_file_relative in backlinks.keys():
|
||||||
|
# If so build out backlinks section and insert
|
||||||
|
backlinks_section = "<h3>Backlinks</h3><ul>"
|
||||||
|
for link in backlinks[html_file_relative]:
|
||||||
|
backlinks_section += '<li><a href="/' + link + '">' + os.path.split(link)[1] + '</a></li>'
|
||||||
|
backlinks_section += "</ul>"
|
||||||
|
o = open(html_file, "r")
|
||||||
|
html = o.read()
|
||||||
|
o.close()
|
||||||
|
o = open(html_file, 'w')
|
||||||
|
o.write(html.replace("{{backlinks}}", backlinks_section))
|
||||||
|
o.close()
|
||||||
|
# Else blank backlinks anchor
|
||||||
|
else:
|
||||||
|
o = open(html_file, "r")
|
||||||
|
html = o.read()
|
||||||
|
o.close()
|
||||||
|
o = open(html_file, 'w')
|
||||||
|
o.write(html.replace("{{backlinks}}", ""))
|
||||||
|
o.close()
|
||||||
|
|
||||||
class Handler(http.server.SimpleHTTPRequestHandler):
|
class Handler(http.server.SimpleHTTPRequestHandler):
|
||||||
def __init__(self,*args,**kwargs):
|
def __init__(self,*args,**kwargs):
|
||||||
super().__init__(*args, directory="tmp",**kwargs)
|
super().__init__(*args, directory="tmp",**kwargs)
|
||||||
|
@ -345,6 +384,7 @@ def run_wiki(_output_dir, _base_url, serve, _template):
|
||||||
build_index(base_dir)
|
build_index(base_dir)
|
||||||
build_directories(output_dir)
|
build_directories(output_dir)
|
||||||
build_tag_pages(output_dir)
|
build_tag_pages(output_dir)
|
||||||
|
process_backlinks()
|
||||||
if serve:
|
if serve:
|
||||||
with socketserver.TCPServer(("",8111), Handler) as httpd:
|
with socketserver.TCPServer(("",8111), Handler) as httpd:
|
||||||
print("serving at port 8111")
|
print("serving at port 8111")
|
||||||
|
|
Loading…
Reference in New Issue