def _build_tree(directory): only_name = directory.split("/")[-1] to_return = {"children": [], "name": only_name, "body": "", "nav": ""} for _, dirs, files in walk(directory): for dir_name in dirs: to_return["children"].append( _build_tree(directory + "/" + dir_name)) for file_name in sorted(files): if not file_name.endswith(".markdown"): continue subsection_name = re.compile(r'[a-zA-Z][a-zA-Z0-9_]*') thing = subsection_name.search(file_name) slug = thing.group().lower() name = thing.group().replace("_", " ") rendered_section_name = \ '<h2 class="perma" id="{slug}">{name} <a href="#{slug}">¶</a></h2>'.format( slug=slug, name=name) opened = open(directory + "/" + file_name) all_text = opened.read() slimmin = Slimdown() to_return["body"] = ''.join([ to_return["body"], rendered_section_name, slimmin.render(all_text) ]) to_return["nav"] = ''.join([ to_return["nav"], '<li><a href="#{slug}">{name}</a></li>'.format(slug=slug, name=name) ]) opened.close() # Build the thing for the main section slug = only_name.lower().replace(" ", "_") name = only_name.replace("_", " ") rendered_section_name = \ '<h2 class="perma" id="{slug}">{name} <a href="#{slug}">¶</a></h2>'.format( slug=slug, name=name) to_return["body"] = ''.join([ '<div class="doc_chunk">', rendered_section_name, to_return["body"], "</div>" ]) master_nav = '<a href="#{slug}">{name}</a>'.format( slug=only_name.lower().replace(" ", "_"), name=only_name) to_return["nav"] = "<li>{master_nav}<ul>{nav}</ul></li>".format( master_nav=master_nav, nav=to_return["nav"]) return to_return
def build_blog_context(default_context): default_context['POSTS'] = [] slimmin = Slimdown() for post in listdir(POSTS_DIR): if not post.endswith(".markdown"): continue new_post = {} dashes_seen = 0 reading_meta = True muh_file = open(POSTS_DIR + post) all_text = "" for line in muh_file: stripped = line.strip() if stripped == '---': dashes_seen += 1 if reading_meta and dashes_seen < 2: continue elif reading_meta and dashes_seen >= 2: reading_meta = False continue if reading_meta and ':' in line: split_line = stripped.split(":") new_post[split_line[0]] = split_line[1] if not reading_meta: all_text += line new_post['content'] = json.dumps(slimmin.render(all_text)) new_post['preview'] = new_post['content'][:300] + "…" # ThIs DoEsNt WoRk WiTh JsON StuFf # SO FUCKIIN HACK IT new_post['link'] = "blog/{}".format(post.replace("markdown", "json")) new_post['filename'] = post new_post['built_filename'] = post.replace("markdown", "json") default_context['POSTS'].append(new_post) muh_file.close() default_context['POSTS'] = sorted(default_context['POSTS'], key=lambda x: x["date"], reverse=True) for index in range(len(default_context['POSTS'])): # Hacks on f*****g hacks if index != len(default_context['POSTS']) - 1: default_context['POSTS'][index]['comma'] = ',' else: default_context['POSTS'][index]['comma'] = '' return default_context
def build_blog_context(default_context): default_context['POSTS'] = [] slimmin = Slimdown() for post in listdir(POSTS_DIR): if not post.endswith(".markdown"): continue new_post = {} dashes_seen = 0 reading_meta = True muh_file = open(POSTS_DIR + post) all_text = "" for line in muh_file: stripped = line.strip() if stripped == '---': dashes_seen += 1 if reading_meta and dashes_seen < 2: continue elif reading_meta and dashes_seen >= 2: reading_meta = False continue if reading_meta and ':' in line: split_line = stripped.split(":") new_post[split_line[0]] = split_line[1] if not reading_meta: all_text += line new_post['content'] = slimmin.render(all_text) new_post['preview'] = new_post['content'][:300] + "…" new_post['link'] = "blog/{}".format(post.replace("markdown", "html")) new_post['filename'] = post new_post['built_filename'] = post.replace("markdown", "html") default_context['POSTS'].append(new_post) muh_file.close() default_context['POSTS'] = sorted(default_context['POSTS'], key=lambda x: x["date"], reverse=True) return default_context
def _build_tree(directory): only_name = directory.split("/")[-1] to_return = { "children": [], "name": only_name, "body": "", "nav": "" } for _, dirs, files in walk(directory): for dir_name in dirs: to_return["children"].append(_build_tree(directory + "/" + dir_name)) for file_name in sorted(files): if not file_name.endswith(".markdown"): continue subsection_name = re.compile(r'[a-zA-Z][a-zA-Z0-9_]*') thing = subsection_name.search(file_name) slug = thing.group().lower() name = thing.group().replace("_", " ") rendered_section_name = \ '<h2 class="perma" id="{slug}">{name} <a href="#{slug}">¶</a></h2>'.format( slug=slug, name=name) opened = open(directory + "/" + file_name) all_text = opened.read() slimmin = Slimdown() to_return["body"] = ''.join([to_return["body"], rendered_section_name, slimmin.render(all_text)]) to_return["nav"] = ''.join([to_return["nav"], '<li><a href="#{slug}">{name}</a></li>'.format(slug=slug, name=name)]) opened.close() # Build the thing for the main section slug = only_name.lower().replace(" ", "_") name = only_name.replace("_", " ") rendered_section_name = \ '<h2 class="perma" id="{slug}">{name} <a href="#{slug}">¶</a></h2>'.format( slug=slug, name=name) to_return["body"] = ''.join(['<div class="doc_chunk">', rendered_section_name, to_return["body"], "</div>"]) master_nav = '<a href="#{slug}">{name}</a>'.format(slug=only_name.lower().replace(" ", "_"), name=only_name) to_return["nav"] = "<li>{master_nav}<ul>{nav}</ul></li>".format(master_nav=master_nav, nav=to_return["nav"]) return to_return