Skip to content

Commit

Permalink
Use custom template (that includes bootstrap) for schema docs
Browse files Browse the repository at this point in the history
  • Loading branch information
drdavella committed Mar 25, 2019
1 parent a498255 commit a8a99fe
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
10 changes: 7 additions & 3 deletions sphinx_asdf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import os

from .nodes import add_asdf_nodes
from .directives import AsdfAutoschemas, AsdfSchema, schema_def
from .connections import (autogenerate_schema_docs, add_labels_to_nodes,
on_build_finished)
from .connections import (autogenerate_schema_docs, handle_page_context,
add_labels_to_nodes, on_build_finished)

from sphinx.builders.html import StandaloneHTMLBuilder


def setup(app):
Expand All @@ -18,9 +21,10 @@ def setup(app):
add_asdf_nodes(app)

app.add_css_file('sphinx_asdf.css')
app.add_javascript('sphinx_asdf.js')
app.add_js_file('sphinx_asdf.js')

app.connect('builder-inited', autogenerate_schema_docs)
app.connect('html-page-context', handle_page_context)
app.connect('doctree-read', add_labels_to_nodes)
app.connect('build-finished', on_build_finished)

Expand Down
11 changes: 11 additions & 0 deletions sphinx_asdf/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
from sphinx.util.fileutil import copy_asset
from sphinx.util.docutils import new_document

from .nodes import schema_doc
from .directives import schema_def


TEMPLATE_PATH = os.path.join(os.path.dirname(__file__), 'templates')


def find_autoasdf_directives(env, filename):

parser = RSTParser()
Expand Down Expand Up @@ -86,6 +90,13 @@ def autogenerate_schema_docs(app):
create_schema_docs(app, schemas)


def handle_page_context(app, pagename, templatename, ctx, doctree):
# Use custom template when rendering pages containing schema documentation.
# This allows us to selectively include bootstrap
if doctree is not None and doctree.traverse(schema_doc):
return os.path.join(TEMPLATE_PATH, 'schema.html')


def add_labels_to_nodes(app, document):
labels = app.env.domaindata['std']['labels']
anonlabels = app.env.domaindata['std']['anonlabels']
Expand Down
7 changes: 4 additions & 3 deletions sphinx_asdf/directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from sphinx.util.docutils import SphinxDirective

from .md2rst import md2rst
from .nodes import (toc_link, schema_header_title, schema_title,
from .nodes import (toc_link, schema_doc, schema_header_title, schema_title,
schema_description, schema_properties, schema_property,
schema_property_name, schema_property_details,
schema_combiner_body, schema_combiner_list,
Expand Down Expand Up @@ -92,7 +92,8 @@ def run(self):

title = self._parse_title(schema.get('title', ''), schema_file)

docnodes = [title]
docnodes = schema_doc()
docnodes.append(title)

description = schema.get('description', '')
if description:
Expand Down Expand Up @@ -123,7 +124,7 @@ def run(self):
docnodes.append(section_header(text=ORIGINAL_SCHEMA_SECTION_TITLE))
docnodes.append(nodes.literal_block(text=raw_content, language='yaml'))

return docnodes
return [docnodes]

def _create_toc(self, schema):
toc = nodes.compound()
Expand Down
11 changes: 11 additions & 0 deletions sphinx_asdf/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
""")


class schema_doc(nodes.compound):
"""Marker for the top level of the ASDF schema document"""

def visit_html(self, node):
pass

def depart_html(self, node):
pass


class schema_title(nodes.compound):

def visit_html(self, node):
Expand Down Expand Up @@ -199,6 +209,7 @@ def depart_html(self, node):


custom_nodes = [
schema_doc,
schema_title,
toc_link,
schema_header_title,
Expand Down
5 changes: 5 additions & 0 deletions sphinx_asdf/templates/schema.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends "!page.html" %}
{% block extrahead %}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
{% endblock %}

0 comments on commit a8a99fe

Please sign in to comment.