From 649ac8860d4828ab91733d7bfcf11d7126bc66ef Mon Sep 17 00:00:00 2001 From: Anish Lakhwara Date: Thu, 3 Feb 2022 04:15:36 +1000 Subject: [PATCH] move list making into it's own template file, link tags in lists to their index, create untagged index, fix various small bugs --- src/kitaab-public/build/TagHelper.py | 23 +++-- src/kitaab-public/build/build.py | 90 ++++++++----------- src/kitaab-public/build/config.py | 5 +- src/kitaab-public/grab/makePublic.py | 2 +- src/kitaab-public/partial/index_table.html | 10 ++- src/kitaab-public/partial/list.html | 32 +++++++ src/kitaab-public/partial/main.html | 14 ++- .../{grab => shared}/TagHelper.py | 11 ++- src/kitaab-public/shared/config.py | 21 +++++ 9 files changed, 139 insertions(+), 69 deletions(-) create mode 100644 src/kitaab-public/partial/list.html rename src/kitaab-public/{grab => shared}/TagHelper.py (90%) create mode 100644 src/kitaab-public/shared/config.py diff --git a/src/kitaab-public/build/TagHelper.py b/src/kitaab-public/build/TagHelper.py index dd2cc03..5ec28f2 100644 --- a/src/kitaab-public/build/TagHelper.py +++ b/src/kitaab-public/build/TagHelper.py @@ -1,10 +1,7 @@ -import ctags from ctags import CTags, TagEntry import sys from icecream import ic -import os -import shutil -import fileinput +from os import listdir, path # TODO don't hardcode the path def loadTagFile(path='/home/anish/vimwiki/.vimwiki_tags'): @@ -59,12 +56,24 @@ def makeDictFromTags(tagFile, key='name', value='file'): tag = tagFile.next(entry) return ret -def getAllFiles(): +def getAllFilesWithTags(): # 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(): # Returns a dictionary with: # - key as tag titles # - values as files that have the key in their tags - return makeDictFromTags(loadTagFile()) + all_tags = makeDictFromTags(loadTagFile()) + all_tags['untagged'] = getUntaggedFiles() + return all_tags diff --git a/src/kitaab-public/build/build.py b/src/kitaab-public/build/build.py index 58b89c5..d45f7ed 100644 --- a/src/kitaab-public/build/build.py +++ b/src/kitaab-public/build/build.py @@ -35,7 +35,7 @@ if args.prod: def generate_config(): pass -def generate_page(template_file=config.template_file, options=generate_config()): +def generate_page(template_file=config.template_main, options=generate_config()): pass def generate_navigation(template_nav=config.template_nav): @@ -54,7 +54,7 @@ def add_file_path(pages, root_dir): 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 all_tags = TagHelper.getAllTags() 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 = add_file_path(pages, config.content_folder[0]) 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) - metaIndexSubPages.append(generate_sub_pages(entries, 5, config.content_folder[0], True, index)) # Build the files by filling out the template file_loader = FileSystemLoader('') env = Environment(loader=file_loader) # regular index pagse use main template - template = env.get_template(config.template_file) - 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) + 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.capitalize() + ' index', config=config) slugfile = config.build_folder + index + '_index.html' with open(slugfile, 'w', encoding=encoding) as fobj: 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 + # This function creates the tag_index.html page file_loader = FileSystemLoader('') env = Environment(loader=file_loader) template = env.get_template(template) - # TODO tag_index subpage is sbroken - # create this page by parsing data instead of pages, like untagged below - print(metaIndexSubPages) - 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) + all_tags = TagHelper.getAllTags() + parsed_set = {} + for k, v in all_tags.items(): + 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' with open(slugfile, 'w', encoding=encoding) as fobj: 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. @@ -99,6 +102,8 @@ def generate_html_pages(site_folder, entries, template, sub_pages_list, template file_loader = FileSystemLoader('') env = Environment(loader=file_loader) 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="%s"%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 @@ -128,7 +133,7 @@ def get_entry_title(page): if '%title' in textContent: textContent = textContent.replace('%title','') else: - textContent = page.split('/')[1].rstrip('.wiki') + textContent = page.split('/')[1][:-5] # remove .wiki from the end of the file name except FileNotFoundError: #print(f"Page not found, but present in tags: {page}") return None @@ -412,38 +417,15 @@ def clean_path(path): # Generate the list of sub pages for each section def generate_sub_pages(entries, num, folder, title, title_name=""): - # Sort entries by date using the iso_date format 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) selected_entries = entries[:num] - # Create the list - sub_page_list = "" - - # 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"

{title_name.capitalize()}

" - 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 + file_loader = FileSystemLoader('') + env = Environment(loader=file_loader) + template = env.get_template(config.template_list) # TODO maybe not hardcode + sub_page_list = template.render(entries=selected_entries, title_name=title_name, title=title) return sub_page_list @@ -514,12 +496,15 @@ def generate_website(): os.makedirs(config.build_folder + config.medias_folder) # Get main html template - template = open(config.template_file, 'r').read() - home_page = create_home_page(template, config.build_folder) + file_loader = FileSystemLoader('') + 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 = [] + home_pageSubList = [] for folder in config.content_folder: pages = glob.glob(folder + '**/*.wiki', recursive=True) 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 home_pageSubList = generate_sub_pages(entries, 50, folder, False) - home_page = home_page.replace('content_list', home_pageSubList + - "content_list") - home_page = home_page.replace('page_date', "") + #home_page = home_page.replace('content_list', home_pageSubList + + # "content_list") + #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) for entry in entries: @@ -544,12 +529,13 @@ def generate_website(): # Once all sections have been processed, finish the home page # Removes the "content_list" in the partial - home_page = home_page.replace('content_list', "") - home_page = home_page.replace('name_of_site', config.name_of_site) - home_page = home_page.replace('site_meta_description', - config.site_meta_description) - home_page = home_page.replace( - 'twitter_name', config.twitter_name) + #home_page = home_page.replace('content_list', "") + #home_page = home_page.replace('name_of_site', config.name_of_site) + #home_page = home_page.replace('site_meta_description', + # config.site_meta_description) + #home_page = home_page.replace( + # '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" with open(slug_file, 'w', encoding=encoding) as fobj: diff --git a/src/kitaab-public/build/config.py b/src/kitaab-public/build/config.py index 19dc6e6..2ef5493 100644 --- a/src/kitaab-public/build/config.py +++ b/src/kitaab-public/build/config.py @@ -1,14 +1,15 @@ 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 = "" 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_main = "partial/main.html" template_nav = "partial/nav.html" template_index = "partial/index_table.html" +template_list = "partial/list.html" rss_template = "partial/rss.xml" rss_item_template = "partial/item.xml" name_of_site = "Kitaab" diff --git a/src/kitaab-public/grab/makePublic.py b/src/kitaab-public/grab/makePublic.py index 3263dbc..701fce8 100644 --- a/src/kitaab-public/grab/makePublic.py +++ b/src/kitaab-public/grab/makePublic.py @@ -16,7 +16,7 @@ from censor import privateList censor = privateList # 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 = ... def loadNetwork(path='network.json'): diff --git a/src/kitaab-public/partial/index_table.html b/src/kitaab-public/partial/index_table.html index 27869be..60eb0a9 100644 --- a/src/kitaab-public/partial/index_table.html +++ b/src/kitaab-public/partial/index_table.html @@ -45,13 +45,19 @@ media="screen" title="no title" charset="utf-8">
+ {{config.name_of_site}} +
+
+ {{page_navigation}} +

{{page_title}}

{% set count = 0 %} - {% for index_url in index_urls %} - + {% for index_url, c in index_urls.items() %} + {% set count = count + 1 %} + {{count}} {% if count == 5 %} diff --git a/src/kitaab-public/partial/list.html b/src/kitaab-public/partial/list.html new file mode 100644 index 0000000..1057253 --- /dev/null +++ b/src/kitaab-public/partial/list.html @@ -0,0 +1,32 @@ +{% if title %} +

{{title_name.capitalize()}}

+{% endif %} + diff --git a/src/kitaab-public/partial/main.html b/src/kitaab-public/partial/main.html index aa667b0..3446914 100644 --- a/src/kitaab-public/partial/main.html +++ b/src/kitaab-public/partial/main.html @@ -45,16 +45,24 @@ media="screen" title="no title" charset="utf-8">
- {{config.name_of_site}} + {{config.name_of_site}}
{{ page_navigation }}

{{page_title}}

- {{page_tags}} + {% if page_tags %} + {% for page in page_tags %} + + {{page}} + + {% endfor %} + {% endif %} + {% if page_date %} {{page_date}} - {{page_body}} + {% endif %} + {{page_body}}
diff --git a/src/kitaab-public/grab/TagHelper.py b/src/kitaab-public/shared/TagHelper.py similarity index 90% rename from src/kitaab-public/grab/TagHelper.py rename to src/kitaab-public/shared/TagHelper.py index f393a9c..5ec28f2 100644 --- a/src/kitaab-public/grab/TagHelper.py +++ b/src/kitaab-public/shared/TagHelper.py @@ -56,7 +56,7 @@ def makeDictFromTags(tagFile, key='name', value='file'): tag = tagFile.next(entry) return ret -def getAllFilesWithTgas(): +def getAllFilesWithTags(): # Returns a set containing all the files we wish to make public return makeSetFromTags(loadTagFile()) @@ -65,8 +65,15 @@ def getAllFiles(): files = set(files) return files +def getUntaggedFiles(): + allFiles = getAllFiles() + allTags = getAllFilesWithTags() + return allFiles - allTags + def getAllTags(): # Returns a dictionary with: # - key as tag titles # - values as files that have the key in their tags - return makeDictFromTags(loadTagFile()) + all_tags = makeDictFromTags(loadTagFile()) + all_tags['untagged'] = getUntaggedFiles() + return all_tags diff --git a/src/kitaab-public/shared/config.py b/src/kitaab-public/shared/config.py new file mode 100644 index 0000000..19dc6e6 --- /dev/null +++ b/src/kitaab-public/shared/config.py @@ -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
{{index_url}} {{index_url}} {{c}}