move list making into it's own template file, link tags in lists to their index, create untagged index, fix various small bugs

This commit is contained in:
Anish Lakhwara 2022-02-03 04:15:36 +10:00
parent 541dad60b0
commit 649ac8860d
9 changed files with 139 additions and 69 deletions

View file

@ -1,10 +1,7 @@
import ctags
from ctags import CTags, TagEntry from ctags import CTags, TagEntry
import sys import sys
from icecream import ic from icecream import ic
import os from os import listdir, path
import shutil
import fileinput
# TODO don't hardcode the path # TODO don't hardcode the path
def loadTagFile(path='/home/anish/vimwiki/.vimwiki_tags'): def loadTagFile(path='/home/anish/vimwiki/.vimwiki_tags'):
@ -59,12 +56,24 @@ def makeDictFromTags(tagFile, key='name', value='file'):
tag = tagFile.next(entry) tag = tagFile.next(entry)
return ret return ret
def getAllFiles(): def getAllFilesWithTags():
# Returns a set containing all the files we wish to make public # Returns a set containing all the files we wish to make public
return makeSetFromTags(loadTagFile(tagFile)) return makeSetFromTags(loadTagFile())
def getAllFiles():
files = [f for f in listdir('/home/anish/vimwiki') if '.wiki' in f and path.isfile('/home/anish/vimwiki/' + f)]
files = set(files)
return files
def getUntaggedFiles():
allFiles = getAllFiles()
allTags = getAllFilesWithTags()
return allFiles - allTags
def getAllTags(): def getAllTags():
# Returns a dictionary with: # Returns a dictionary with:
# - key as tag titles # - key as tag titles
# - values as files that have the key in their tags # - values as files that have the key in their tags
return makeDictFromTags(loadTagFile()) all_tags = makeDictFromTags(loadTagFile())
all_tags['untagged'] = getUntaggedFiles()
return all_tags

View file

@ -35,7 +35,7 @@ if args.prod:
def generate_config(): def generate_config():
pass pass
def generate_page(template_file=config.template_file, options=generate_config()): def generate_page(template_file=config.template_main, options=generate_config()):
pass pass
def generate_navigation(template_nav=config.template_nav): def generate_navigation(template_nav=config.template_nav):
@ -54,7 +54,7 @@ def add_file_path(pages, root_dir):
return ret return ret
def generate_index_pages(template=config.template_index, template_nav=config.template_nav): def generate_index_pages(template=config.template_main, template_nav=config.template_nav):
template = template template = template
all_tags = TagHelper.getAllTags() all_tags = TagHelper.getAllTags()
index_tags = config.nav_tags index_tags = config.nav_tags
@ -63,30 +63,33 @@ def generate_index_pages(template=config.template_index, template_nav=config.tem
pages = all_tags[index] pages = all_tags[index]
pages = add_file_path(pages, config.content_folder[0]) pages = add_file_path(pages, config.content_folder[0])
entries = create_entries(pages) entries = create_entries(pages)
# TODO this broke as well, not sure what happened to entries, but the whole page is blank?
subPages = generate_sub_pages(entries, len(entries), config.content_folder[0], False) subPages = generate_sub_pages(entries, len(entries), config.content_folder[0], False)
metaIndexSubPages.append(generate_sub_pages(entries, 5, config.content_folder[0], True, index))
# Build the files by filling out the template # Build the files by filling out the template
file_loader = FileSystemLoader('') file_loader = FileSystemLoader('')
env = Environment(loader=file_loader) env = Environment(loader=file_loader)
# regular index pagse use main template # regular index pagse use main template
template = env.get_template(config.template_file) template = env.get_template(template)
current_index_template = template.render(page_body=subPages, page_date=None, page_tags=None, page_navigation=generate_navigation(template_nav), page_title='index', config=config) current_index_template = template.render(page_body=subPages, page_date=None, page_tags=None, page_navigation=generate_navigation(template_nav), page_title=index.capitalize() + ' index', config=config)
slugfile = config.build_folder + index + '_index.html' slugfile = config.build_folder + index + '_index.html'
with open(slugfile, 'w', encoding=encoding) as fobj: with open(slugfile, 'w', encoding=encoding) as fobj:
fobj.write(current_index_template) fobj.write(current_index_template)
generate_table_index()
def generate_table_index(template=config.template_index, template_nav=config.template_nav):
# build the metaIndex page # build the metaIndex page
# This function creates the tag_index.html page
file_loader = FileSystemLoader('') file_loader = FileSystemLoader('')
env = Environment(loader=file_loader) env = Environment(loader=file_loader)
template = env.get_template(template) template = env.get_template(template)
# TODO tag_index subpage is sbroken all_tags = TagHelper.getAllTags()
# create this page by parsing data instead of pages, like untagged below parsed_set = {}
print(metaIndexSubPages) for k, v in all_tags.items():
current_index_template = template.render(index_urls=metaIndexSubPages, page_date=None, page_tags=None, page_navigation=generate_navigation(template_nav), page_title='index', config=config) parsed_set[k] = len(v)
current_index_template = template.render(index_urls=parsed_set, page_date=None, page_tags=None, page_navigation=generate_navigation(template_nav), page_title='Piles', config=config)
slugfile = config.build_folder + 'tag_index.html' slugfile = config.build_folder + 'tag_index.html'
with open(slugfile, 'w', encoding=encoding) as fobj: with open(slugfile, 'w', encoding=encoding) as fobj:
fobj.write(current_index_template) fobj.write(current_index_template)
# TODO import TagHelper.get_untagged() and make one of the indexs pages
# Generates html files in the site folder, using the entries and the template. # Generates html files in the site folder, using the entries and the template.
@ -99,6 +102,8 @@ def generate_html_pages(site_folder, entries, template, sub_pages_list, template
file_loader = FileSystemLoader('') file_loader = FileSystemLoader('')
env = Environment(loader=file_loader) env = Environment(loader=file_loader)
template = env.get_template(template) template = env.get_template(template)
if len(entry['tags']) > 1:
entry['tags'] = entry['tags'].split(' ')
page_template = template.render(page_body=entry['pageContent'], page_date="<date>%s</date>"%entry['date'], page_tags=entry['tags'], naem_of_site="Kitaab", page_navigation=nav_html, page_title=entry['title'], build_url=entry['parent_text'], config=config) page_template = template.render(page_body=entry['pageContent'], page_date="<date>%s</date>"%entry['date'], page_tags=entry['tags'], naem_of_site="Kitaab", page_navigation=nav_html, page_title=entry['title'], build_url=entry['parent_text'], config=config)
# Checking if content folder exists # Checking if content folder exists
@ -128,7 +133,7 @@ def get_entry_title(page):
if '%title' in textContent: if '%title' in textContent:
textContent = textContent.replace('%title','') textContent = textContent.replace('%title','')
else: else:
textContent = page.split('/')[1].rstrip('.wiki') textContent = page.split('/')[1][:-5] # remove .wiki from the end of the file name
except FileNotFoundError: except FileNotFoundError:
#print(f"Page not found, but present in tags: {page}") #print(f"Page not found, but present in tags: {page}")
return None return None
@ -412,38 +417,15 @@ def clean_path(path):
# Generate the list of sub pages for each section # Generate the list of sub pages for each section
def generate_sub_pages(entries, num, folder, title, title_name=""): def generate_sub_pages(entries, num, folder, title, title_name=""):
# Sort entries by date using the iso_date format # Sort entries by date using the iso_date format
entries.sort(key=lambda x: x["iso_date"], reverse=True) entries.sort(key=lambda x: x["iso_date"], reverse=True)
# Take n number of entries (5 for the home, all for the sub-section pages) # Take n number of entries (5 for the home, all for the sub-section pages)
selected_entries = entries[:num] selected_entries = entries[:num]
# Create the list # Create the list
sub_page_list = "<ul class='listing'>" file_loader = FileSystemLoader('')
for entry in selected_entries: env = Environment(loader=file_loader)
if title: template = env.get_template(config.template_list) # TODO maybe not hardcode
link_url = entry["slug"] sub_page_list = template.render(entries=selected_entries, title_name=title_name, title=title)
else:
link_url = entry["file"] + ".html"
if entry["file"] != "index":
entry_string = "<li><div style='overflow: hidden;'><h3 style='float:left;'><a href='" + \
link_url + \
"'>" + entry["title"] + \
"</a> </h3><p style='float:right;'> {} </p> </div> {} </li>".format(entry['date'], entry["tags"])
sub_page_list += entry_string
sub_page_list += "</ul>"
# If a title is necessary, use the folder name
# This only gets applied for metaIndexSubPages in the generate_index_pages function
if title:
title = f"<h2><a href={title_name}_index.html>{title_name.capitalize()}</h2>"
sub_page_list = title + sub_page_list
if config.flat_build:
sub_page_link = build_url + title_name + "_index.html"
else:
sub_page_link = build_url + folder
return sub_page_list return sub_page_list
@ -514,12 +496,15 @@ def generate_website():
os.makedirs(config.build_folder + config.medias_folder) os.makedirs(config.build_folder + config.medias_folder)
# Get main html template # Get main html template
template = open(config.template_file, 'r').read() file_loader = FileSystemLoader('')
home_page = create_home_page(template, config.build_folder) env = Environment(loader=file_loader)
home_page = env.get_template(config.template_main) # TODO maybe not hardcode
#home_page = create_home_page(template, config.build_folder)
generate_index_pages(config.template_index, config.template_nav) generate_index_pages(config.template_main, config.template_nav)
rss_entries = [] rss_entries = []
home_pageSubList = []
for folder in config.content_folder: for folder in config.content_folder:
pages = glob.glob(folder + '**/*.wiki', recursive=True) pages = glob.glob(folder + '**/*.wiki', recursive=True)
entries = create_entries(pages) entries = create_entries(pages)
@ -528,11 +513,11 @@ def generate_website():
# For each section, create a short listing of sub pages and add it to the home page # For each section, create a short listing of sub pages and add it to the home page
home_pageSubList = generate_sub_pages(entries, 50, folder, False) home_pageSubList = generate_sub_pages(entries, 50, folder, False)
home_page = home_page.replace('content_list', home_pageSubList + #home_page = home_page.replace('content_list', home_pageSubList +
"content_list") # "content_list")
home_page = home_page.replace('page_date', "") #home_page = home_page.replace('page_date', "")
generate_html_pages(config.build_folder, entries, config.template_file, generate_html_pages(config.build_folder, entries, config.template_main,
sub_pages_list, config.template_nav) sub_pages_list, config.template_nav)
for entry in entries: for entry in entries:
@ -544,12 +529,13 @@ def generate_website():
# Once all sections have been processed, finish the home page # Once all sections have been processed, finish the home page
# Removes the "content_list" in the partial # Removes the "content_list" in the partial
home_page = home_page.replace('content_list', "") #home_page = home_page.replace('content_list', "")
home_page = home_page.replace('name_of_site', config.name_of_site) #home_page = home_page.replace('name_of_site', config.name_of_site)
home_page = home_page.replace('site_meta_description', #home_page = home_page.replace('site_meta_description',
config.site_meta_description) # config.site_meta_description)
home_page = home_page.replace( #home_page = home_page.replace(
'twitter_name', config.twitter_name) # 'twitter_name', config.twitter_name)
home_page = home_page.render(page_body=home_pageSubList, page_date=None, page_tags=None, page_navigation=generate_navigation(), page_title="Recent Files", config=config)
slug_file = config.build_folder + "recents.html" slug_file = config.build_folder + "recents.html"
with open(slug_file, 'w', encoding=encoding) as fobj: with open(slug_file, 'w', encoding=encoding) as fobj:

View file

@ -1,14 +1,15 @@
content_folder = ['raw'] content_folder = ['raw']
nav_tags = ['dev', 'way', 'recipe', 'ops', 'blog', 'rock', 'phil', 'story', 'blog'] nav_tags = ['dev', 'way', 'recipe', 'ops', 'blog', 'rock', 'phil', 'story', 'blog', 'untagged']
relative_build_url = "" relative_build_url = ""
absolute_build_url = "https://anish.lakhwara.com/" absolute_build_url = "https://anish.lakhwara.com/"
files_folder = '' #TODO files_folder = '' #TODO
build_folder = "output/" build_folder = "output/"
assets_folder = "asset/" assets_folder = "asset/"
medias_folder = "media/" medias_folder = "media/"
template_file = "partial/main.html" template_main = "partial/main.html"
template_nav = "partial/nav.html" template_nav = "partial/nav.html"
template_index = "partial/index_table.html" template_index = "partial/index_table.html"
template_list = "partial/list.html"
rss_template = "partial/rss.xml" rss_template = "partial/rss.xml"
rss_item_template = "partial/item.xml" rss_item_template = "partial/item.xml"
name_of_site = "Kitaab" name_of_site = "Kitaab"

View file

@ -16,7 +16,7 @@ from censor import privateList
censor = privateList censor = privateList
# TODO put this in config too... # TODO put this in config too...
filter = ["dream", "gratitude", "review", "jrnl", "piracy", "work", "job", "debt", "people", "priv"] filter = ["dream", "gratitude", "review", "jrnl", "piracy", "work", "job", "debt", "people", "priv", "threatdefence"]
#TODO create = ... #TODO create = ...
def loadNetwork(path='network.json'): def loadNetwork(path='network.json'):

View file

@ -45,13 +45,19 @@ media="screen" title="no title" charset="utf-8">
<body class="flow-large container"> <body class="flow-large container">
<header> <header>
<a href="recents.html" aria-label="{{page_title}}">{{config.name_of_site}}</a>
</header>
<main>
{{page_navigation}}
<h1> {{page_title}} </h1>
<div id='piles' style="float: right;"> <div id='piles' style="float: right;">
<table style="width:100%"> <table style="width:100%">
{% set count = 0 %} {% set count = 0 %}
<tr> <tr>
{% for index_url in index_urls %} {% for index_url, c in index_urls.items() %}
<td>{{index_url}}</td> <td> <a href="{{index_url}}_index.html">{{index_url}}</a> {{c}}</td>
{% set count = count + 1 %} {% set count = count + 1 %}
{{count}}
{% if count == 5 %} {% if count == 5 %}
</tr> </tr>
<tr> <tr>

View file

@ -0,0 +1,32 @@
{% if title %}
<h2><a href={{title_name}}_index.html>{{title_name.capitalize()}}</a></h2>
{% endif %}
<ul class='listing'>
{% for entry in entries %}
<li>
<div style='overflow: hidden;'>
<h3 style='float:left;'>
{% if title %}
<a href='{{entry['slug']}}'>{{entry["title"]}}</a>
{% else %}
<!--
TODO
Sometimes the entry doesn't output a title and I'm not entirely sure why
--!>
{% if entry['title'] %}
<a href='{{entry['file']}}.html'>{{entry["title"]}}</a>
{% else %}
<a href='{{entry['file']}}.html'>{{entry['file']}}</a>
{% endif %}
{% endif %}
</h3>
<p style='float:right;'>{{entry['date']}}</p>
</div>
{% for page in entry['tags'].split(' ') %}
<a href="{{page}}_index.html">
{{page}}
</a>
{% endfor %}
</li>
{% endfor %}
</ul>

View file

@ -45,16 +45,24 @@ media="screen" title="no title" charset="utf-8">
<body class="flow-large container"> <body class="flow-large container">
<header> <header>
<a href="home.html" aria-label="{{page_title}}">{{config.name_of_site}}</a> <a href="home.html" aria-label="{{config.name_of_site}}">{{config.name_of_site}}</a>
</header> </header>
<main> <main>
{{ page_navigation }} {{ page_navigation }}
<h1> {{page_title}} </h1> <h1> {{page_title}} </h1>
<div style='max-width:460px;'> <div style='max-width:460px;'>
<article class="flow"> <article class="flow">
{{page_tags}} {% if page_tags %}
{% for page in page_tags %}
<a href="{{page}}_index.html">
{{page}}
</a>
{% endfor %}
{% endif %}
{% if page_date %}
{{page_date}} {{page_date}}
{{page_body}} {% endif %}
{{page_body}}
</article> </article>
</div> </div>
</main> </main>

View file

@ -56,7 +56,7 @@ def makeDictFromTags(tagFile, key='name', value='file'):
tag = tagFile.next(entry) tag = tagFile.next(entry)
return ret return ret
def getAllFilesWithTgas(): def getAllFilesWithTags():
# Returns a set containing all the files we wish to make public # Returns a set containing all the files we wish to make public
return makeSetFromTags(loadTagFile()) return makeSetFromTags(loadTagFile())
@ -65,8 +65,15 @@ def getAllFiles():
files = set(files) files = set(files)
return files return files
def getUntaggedFiles():
allFiles = getAllFiles()
allTags = getAllFilesWithTags()
return allFiles - allTags
def getAllTags(): def getAllTags():
# Returns a dictionary with: # Returns a dictionary with:
# - key as tag titles # - key as tag titles
# - values as files that have the key in their tags # - values as files that have the key in their tags
return makeDictFromTags(loadTagFile()) all_tags = makeDictFromTags(loadTagFile())
all_tags['untagged'] = getUntaggedFiles()
return all_tags

View file

@ -0,0 +1,21 @@
content_folder = ['raw']
nav_tags = ['dev', 'way', 'recipe', 'ops', 'blog', 'rock', 'phil', 'story', 'blog']
relative_build_url = ""
absolute_build_url = "https://anish.lakhwara.com/"
files_folder = '' #TODO
build_folder = "output/"
assets_folder = "asset/"
medias_folder = "media/"
template_file = "partial/main.html"
template_nav = "partial/nav.html"
template_index = "partial/index_table.html"
rss_template = "partial/rss.xml"
rss_item_template = "partial/item.xml"
name_of_site = "Kitaab"
site_meta_description = ""
twitter_name = "@aynish@merveilles.town"
home_name = "Home"
see_all = "See more"
go_back = "Go back to"
date_format = "EU" # Choose between EU day-month-year or ISO year-mont-day
flat_build = True # True or False, if True, all files will be written in the root of the website