diff --git a/nb_wiki.py b/nb_wiki.py index 421894e..f4976f5 100644 --- a/nb_wiki.py +++ b/nb_wiki.py @@ -5,6 +5,8 @@ import socketserver import shutil list_of_tags = {} copy_extensions = [".gif",".jpg",".png", ".html", ".stl"] +backlinks = {} +html_files = [] output_dir = "" base_url = "" @@ -87,6 +89,7 @@ def process_files(base_dir, files): output = open(output_path, "w+") output.write(template.split("{{content}}")[0]) output.close() + html_files.append(output_path) for line in file_content.split("\n"): if doing_list: if line[0:1] == "*": @@ -114,7 +117,7 @@ def process_files(base_dir, files): output = open(output_path, "a+") output.write("

") 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.write("

") output.close() @@ -124,7 +127,7 @@ def process_files(base_dir, files): output.close() #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 link_offset = 0 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 for u in range(i + 2, len(line)): if line[u-1] == "]" and line[u] == "]" : - process_link(output, link_text) + process_link(output, link_text, relative_path) # link_offset = u + 1 break @@ -218,7 +221,8 @@ def process_tag(output, page_title, parent_link, base_dir , tag_text): else: 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_path = os.path.join(base_url, link_text) if not "|" in link_text: @@ -226,12 +230,18 @@ def process_link(output, link_text): output.write('' + link_text + '') else: output.write('' + link_text + '') + if link_text not in backlinks.keys(): + backlinks[link_text + ".html"] = [] + backlinks[link_text + ".html"].append(relative_path) else: split_link = link_text.split("|") if os.path.splitext(split_link[0].rstrip())[1] in copy_extensions: output.write('' + split_link[1] + '') else: output.write('' + split_link[1] + '') + 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): output.write('' + link_text + '') @@ -244,7 +254,7 @@ def process_list(output_path, lines, page_title, base_dir, other_bit): o.write("
  • ") o.close() #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.write("
  • ") o.close() @@ -269,6 +279,7 @@ def build_index(base_dir): list_of_notebooks.append(directory) o = open(output_file, "a+") template = template.replace("{{breadcrumbs}}","") + template = template.replace("{{backlinks}}","") template = template.replace('{{pagetitle}}', "rmgr's wiki") o.write(template.split("{{content}}")[0]) o.write('