Skip to content

Commit

Permalink
FIX: Simply interface with template layer for next and prev metadata …
Browse files Browse the repository at this point in the history
…in notebooks (#333)

* simply by not including metadata if a relation is not found in the notebook

* convert to issue warning messages

* improve code to skip title generation if not required
  • Loading branch information
mmcky authored Jun 18, 2020
1 parent 7667925 commit f37fb50
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions sphinxcontrib/jupyter/builders/jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,45 +217,51 @@ def update_Metadata(self, docname, nb):
if related and related[2]:
try:
link = self.get_relative_uri(docname, related[2])
title_relation = titles[related[2]]
# Filter out non-text elements like index entries
if len(title_relation.children) > 1:
text_nodes = [item for item in title_relation if isinstance(item, docutils.nodes.Text)]
title = "".join([item.astext() for item in text_nodes])
else:
title = title_relation.children[0].astext()
# link is document uri (i.e. docname) as specified in index
if link in self.config.jupyter_nextprev_ignore:
pass
else:
title_relation = titles[related[2]]
# Filter out non-text elements like index entries
if len(title_relation.children) > 1:
text_nodes = [item for item in title_relation if isinstance(item, docutils.nodes.Text)]
title = "".join([item.astext() for item in text_nodes])
else:
title = title_relation.children[0].astext()
# Set next_doc metadata
next_doc = {
'link': link,
'title': title
}
nb.metadata.next_doc = next_doc
except KeyError:
nb.metadata.next_doc = False
self.logger.warning(
"[NB Metadata] No next_doc relation is found for: {}"
.format(docname))
if related and related[1]:
try:
link = self.get_relative_uri(docname, related[1])
title_relation = titles[related[2]]
# Filter out non-text elements like index entries
if len(title_relation.children) > 1:
text_nodes = [item for item in title_relation if isinstance(item, docutils.nodes.Text)]
title = "".join([item.astext() for item in text_nodes])
else:
title = title_relation.children[0].astext()
# link is document uri (i.e. docname) as specified in index
if link in self.config.jupyter_nextprev_ignore:
pass
else:
title_relation = titles[related[1]]
# Filter out non-text elements like index entries
if len(title_relation.children) > 1:
text_nodes = [item for item in title_relation if isinstance(item, docutils.nodes.Text)]
title = "".join([item.astext() for item in text_nodes])
else:
title = title_relation.children[0].astext()
# Set prev_doc metadata
prev_doc = {
'link': link,
'title': title
}
nb.metadata.prev_doc = prev_doc
except KeyError:
nb.metadata.prev_doc = False
self.logger.warning(
"[NB Metadata] No prev_doc relation is found for: {}"
.format(docname))
# Set Compile Datetime
nb.metadata.date = time.time()
return nb
Expand Down

0 comments on commit f37fb50

Please sign in to comment.