slightyl nicer parsing

This commit is contained in:
Anish Lakhwara 2023-04-04 00:28:38 +10:00
parent a785d208f9
commit ea98ec57c4
3 changed files with 56 additions and 3 deletions

View file

@ -62,7 +62,6 @@ def generate_index_pages(template=config.template_main, template_nav=config.temp
pages = all_tags[index]
pages = add_file_path(pages, config.files_folder)
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.files_folder, False)
# Build the files by filling out the template
@ -125,6 +124,7 @@ def generate_html_pages(site_folder, entries, template, sub_pages_list, template
head=generate_head(page_title=entry['title']),
page_body=entry['pageContent'],
page_date="<date>%s</date>" % entry['date'],
page_update="<date>%s</date>" % entry['update'],
page_tags=entry['tags'],
name_of_site="Kitaab",
page_navigation=nav_html,
@ -288,6 +288,24 @@ def remove_date(page):
print(f'Unexpected error in removing date in {page}')
return page
def remove_update(page):
try:
regex = r"%update [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}"
page = re.sub(regex, '', page)
return page
except Exception as e:
print(f'Unexpected error in removing date in {page}')
return page
def remove_links(page):
try:
regex = r"%link *"
page = re.sub(regex, '', page)
return page
except Exception as e:
print(f'Unexpected error in removing date in {page}')
return page
def replacement_function(replace):
replace = replace.group()
@ -329,6 +347,28 @@ def get_date(page):
# print(f"Could not parse date")
return None
def get_update(page):
try:
regex = r"%update [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}"
match = re.findall(regex, page)[0].strip('%update')
return match
except:
# print(f"Could not parse date")
return None
def get_links(page):
try:
regex = r"%link *"
match = re.findall(regex, page)[0].strip('%link')
ret = []
for m in match:
m = m.group()
m = m.strip("%link")
ret.append(m)
return ret
except:
# print(f"Could not parse date")
return None
def parse_headings(page):
try:
@ -360,6 +400,7 @@ def create_entries(pages):
markdown_text = open(page, 'r').read()
except UnicodeDecodeError:
print(f"Couldn't parse {title}, {page}")
continue
markdown_text = parse_headings(markdown_text)
markdown_text = replace_generated(markdown_text)
markdown_text = style_iframes(markdown_text)
@ -367,9 +408,13 @@ def create_entries(pages):
markdown_text = fix_wiki_links(markdown_text, path["folder"])
tags = get_tags(markdown_text)
date = get_date(markdown_text)
update = get_update(markdown_text)
links = get_links(markdown_text)
markdown_text = remove_tags(markdown_text)
markdown_text = remove_title(markdown_text)
markdown_text = remove_date(markdown_text)
markdown_text = remove_update(markdown_text)
markdown_text = remove_links(markdown_text)
pageContent = markdown(markdown_text)
# Create the page object with all the informations we need
@ -383,9 +428,14 @@ def create_entries(pages):
tempPage['date'] = date
else:
tempPage['date'] = path['iso_date'][:-3]
if update:
tempPage['update'] = update
else:
tempPage['update'] = tempPage['date']
tempPage['title'] = fix_amp(title)
tempPage['pageContent'] = pageContent
tempPage['tags'] = tags
tempPage['links'] = links
fullContent.append(tempPage)

View file

@ -12,7 +12,7 @@
TODO
Sometimes the entry doesn't output a title and I'm not entirely sure why
--!>
{% if entry['title'] %}
{% if entry['title']|length > 0 %}
<a href='{{entry['file']}}.html'>{{entry["title"]}}</a>
{% else %}
<a href='{{entry['file']}}.html'>{{entry['file']}}</a>

View file

@ -20,7 +20,10 @@
{% endfor %}
{% endif %}
{% if page_date %}
{{page_date}}
<p> published {{page_date}} </p>
{% endif %}
{% if page_update %}
<p> updated {{page_update}} </p>
{% endif %}
{{page_body}}
</article>