Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Enable html_link_suffix #22

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions tinkerer/ext/aggregator.py
Expand Up @@ -32,6 +32,8 @@ def make_aggregated_pages(app):
"next": {},
"posts": []
}

context["html_link_suffix"] = app.config.html_link_suffix
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the suffix have to go into the context? The context gets sent to Jinja so it can be accessed from the templates but I don't see any reference to it in the templates. Can't we just use the value from app.config directly?


# add posts to context
for post in posts:
Expand All @@ -50,7 +52,7 @@ def make_aggregated_pages(app):
# following pages prev-link to previous page (titled as "Newer")
pagename = "page%d" % (i + 1)
context["prev"]["title"] = UIStr.NEWER
context["prev"]["link"] = "index.html" if i == 1 else "page%d.html" % i
context["prev"]["link"] = "index" if i == 1 else "page%d%s" % (i, context["html_link_suffix"])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't index also get an extension?

context["title"] = UIStr.PAGE_FMT % (i + 1)

if i == len(groups) - 1:
Expand All @@ -59,7 +61,7 @@ def make_aggregated_pages(app):
else:
# other pages next-link to following page (titled as "Older")
context["next"]["title"] = UIStr.OLDER
context["next"]["link"] = "page%d.html" % (i + 2)
context["next"]["link"] = "page%d%s" % ((i + 2), context["html_link_suffix"])

context["archive_title"] = UIStr.BLOG_ARCHIVE

Expand Down
4 changes: 3 additions & 1 deletion tinkerer/ext/filing.py
Expand Up @@ -42,8 +42,10 @@ def run(self):
if item not in env.filing[name]:
env.filing[name][item] = []
env.filing[name][item].append(env.docname)

# "link + html_suffix" is TypeError???
env.blog_metadata[env.docname].filing[name].append(
(utils.name_from_title(item), item))
("%s%s" % (utils.name_from_title(item), env.blog_metadata[env.docname].html_link_suffix), item))

return []

Expand Down
5 changes: 5 additions & 0 deletions tinkerer/ext/metadata.py
Expand Up @@ -50,6 +50,7 @@ def __init__(self):
self.author = None
self.filing = { "tags": [], "categories": [] }
self.comments, self.comment_count = False, False
self.html_link_suffix = None
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, are these changes really needed here? Metadata should be specific to one post (like its title or author). The suffix is a global so it will be the same for all instances of Metadata. Wouldn't it make more sense to just pull the value from app.config whenever we need it?

self.num = Metadata.num
Metadata.num += 1

Expand Down Expand Up @@ -98,6 +99,7 @@ def get_metadata(app, docname):
if not match:
return

metadata.html_link_suffix = app.config.html_link_suffix
metadata.is_post = True
metadata.link = docname
metadata.date = datetime.datetime.strptime(match.group(), "%Y/%m/%d/")
Expand Down Expand Up @@ -156,6 +158,9 @@ def add_metadata(app, pagename, context):
# page data
context['website'] = app.config.website

# blog html link suffix
context["html_link_suffix"] = app.config.html_link_suffix

# blog tagline and pages
context["tagline"] = app.config.tagline
context["description"] = app.config.description
Expand Down
30 changes: 16 additions & 14 deletions tinkerer/ext/patch.py
Expand Up @@ -91,45 +91,47 @@ def patch_aggregated_metadata(context):
metadata.body,
metadata.link[:11], # first 11 characters is path (YYYY/MM/DD/)
metadata.link[11:], # following characters represent filename
True) # hyperlink title to post
True, # hyperlink title to post
True, # replace read more link
context["html_link_suffix"])
metadata.body = strip_xml_declaration(metadata.body)



def patch_links(body, docpath, docname=None, link_title=False, replace_read_more_link=True):
def patch_links(body, docpath, docname=None, link_title=False, replace_read_more_link=True, docsuffix=".html"):
'''
Parses the document body and calls patch_node from the document root
to fix hyperlinks. Also hyperlinks document title. Returns resulting
XML as string.
'''
in_str = convert(body).encode("utf-8")
doc = xml.dom.minidom.parseString(in_str)
patch_node(doc, docpath, docname)
patch_node(doc, docpath, docname, docsuffix)

body = doc.toxml()
if docname and replace_read_more_link:
body = make_read_more_link(body, docpath, docname)
body = make_read_more_link(body, docpath, docname, docsuffix)

if link_title:
return hyperlink_title(body, docpath, docname)
return hyperlink_title(body, docpath, docname, docsuffix)
else:
return body



def hyperlink_title(body, docpath, docname):
def hyperlink_title(body, docpath, docname, docsuffix):
"""
Hyperlink titles by embedding appropriate a tag inside
h1 tags (which should only be post titles).
"""
body = body.replace("<h1>", '<h1><a href="%s.html">' %
(docpath + docname), 1)
body = body.replace("<h1>", '<h1><a href="%s%s">' %
(docpath + docname, docsuffix), 1)
body = body.replace("</h1>", "</a></h1>", 1)
return body



def make_read_more_link(body, docpath, docname):
def make_read_more_link(body, docpath, docname, docsuffix):
"""
Create "read more" link if marker exists.
"""
Expand All @@ -144,8 +146,8 @@ def make_read_more_link(body, docpath, docname):
# when the .. more:: directive comes after a subsection:
body += "</div>" * (body.count("<div") - body.count("</div") - 1)

return body + ('<p><a class="readmore" href="%s.html#more">%s</a></p></div>' %
(docpath + docname, UIStr.READ_MORE))
return body + ('<p><a class="readmore" href="%s%s#more">%s</a></p></div>' %
(docpath + docname, docsuffix, UIStr.READ_MORE))



Expand All @@ -157,7 +159,7 @@ def collapse_path(path_url):



def patch_node(node, docpath, docname=None):
def patch_node(node, docpath, docname=None, docsuffix=".html"):
'''
Recursively patches links in nodes.
'''
Expand All @@ -184,7 +186,7 @@ def patch_node(node, docpath, docname=None):
# html anchor with missing post.html
# e.g. href="2012/08/23/#the-cross-compiler"
# now href="2012/08/23/a_post.html#the-cross-compiler"
ref.value = ref.value.replace("/#", "/%s.html#" % docname)
ref.value = ref.value.replace("/#", "/%s%s#" % (docname, docsuffix))

# normalize urls so "2012/08/23/../../../_static/" becomes
# "_static/" - we can use normpath for this, just make sure
Expand All @@ -194,7 +196,7 @@ def patch_node(node, docpath, docname=None):

# recurse
for node in node.childNodes:
patch_node(node, docpath, docname)
patch_node(node, docpath, docname, docsuffix)



Expand Down
4 changes: 2 additions & 2 deletions tinkerer/themes/boilerplate/layout.html
Expand Up @@ -120,7 +120,7 @@
<span>
{{ text_filed_under }}:
{% for link, category in post_categories -%}
<a href="{{ pathto('categories/' + link + '.html', 1) }}">{{ category }}</a>
<a href="{{ pathto('categories/' + link, 1) }}">{{ category }}</a>
{%- if not loop.last -%}, {% endif -%}
{% endfor -%}
</span>
Expand All @@ -135,7 +135,7 @@
<span>
{{ text_tags }}:
{% for link, tag in post_tags -%}
<a href="{{ pathto('tags/' + link + '.html', 1) }}">{{ tag }}</a>
<a href="{{ pathto('tags/' + link, 1) }}">{{ tag }}</a>
{%- if not loop.last -%}, {% endif -%}
{% endfor -%}
</span>
Expand Down