Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[waiting] Generate content ID references instead of relative URLs. #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions deconstrst/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from os import path

import requests
from urllib.parse import urljoin
from docutils import nodes
from sphinx.builders.html import JSONHTMLBuilder
from sphinx.util import jsonimpl
Expand Down Expand Up @@ -48,18 +49,22 @@ def dump_context(self, context, filename):
"layout_key": context["deconst_layout_key"]
}

n = context.get("next")
p = context.get("prev")
rel_next, rel_previous = None, None
for rellink in context.get("rellinks"):
if rellink[2] == "N":
rel_next = rellink
elif rellink[2] == "P":
rel_previous = rellink

if n:
if rel_next:
envelope["next"] = {
"url": n["link"],
"title": n["title"]
"contentID": self._content_id_for_docname(rel_next[0]),
"title": rel_next[1]
}
if p:
if rel_previous:
envelope["previous"] = {
"url": p["link"],
"title": p["title"]
"contentID": self._content_id_for_docname(rel_previous[0]),
"title": rel_previous[1]
}

if context["display_toc"]:
Expand Down Expand Up @@ -92,6 +97,30 @@ def post_process_images(self, doctree):
for node in doctree.traverse(nodes.image):
node['uri'] = self._publish_entry(node['uri'])

def get_relative_uri(self, from_, to, typ=None):
"""
Generate a content ID directive that deconst will use to map an href
to the correct content at presentation-time.
"""

to_content_id = self._content_id_for_docname(to, typ=None)
return "{{ to('" + to_content_id + "') }}"

def _content_id_for_docname(self, docname, typ=None):
"""
Generate a normalized content ID that corresponds to a Sphinx document
name.
"""

base = self.deconst_config.content_id_base
doc_uri = self.get_target_uri(docname, typ=type)
doc_content_id = urljoin(base, doc_uri, allow_fragments=True)

if doc_content_id and doc_content_id[-1] == "/":
doc_content_id = doc_content_id[:-1]

return doc_content_id

def _publish_entry(self, srcfile):
# TODO guess the content-type

Expand Down