| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 # | 5 # |
| 6 # This is a Sphinx extension. | 6 # This is a Sphinx extension. |
| 7 # | 7 # |
| 8 | 8 |
| 9 from __future__ import print_function | 9 from __future__ import print_function |
| 10 import codecs | 10 import codecs |
| 11 from collections import namedtuple, OrderedDict | 11 from collections import namedtuple, OrderedDict |
| 12 import os | 12 import os |
| 13 import string | 13 import string |
| 14 from docutils import nodes | 14 from docutils import nodes |
| 15 from docutils.parsers.rst import Directive, directives | 15 from docutils.parsers.rst import Directive, directives |
| 16 from sphinx.util.osutil import ensuredir | 16 from sphinx.util.osutil import ensuredir |
| 17 from sphinx.builders.html import StandaloneHTMLBuilder | 17 from sphinx.builders.html import StandaloneHTMLBuilder |
| 18 from sphinx.writers.html import HTMLWriter | 18 from sphinx.writers.html import HTMLWriter |
| 19 from sphinx.writers.html import SmartyPantsHTMLTranslator as HTMLTranslator | 19 from sphinx.writers.html import SmartyPantsHTMLTranslator as HTMLTranslator |
| 20 from sphinx.util.console import bold | 20 from sphinx.util.console import bold |
| 21 | 21 |
| 22 # PEPPER_VERSION = "31" | 22 # PEPPER_VERSION = "31" |
| 23 | 23 |
| 24 # TODO(eliben): it may be interesting to use an actual Sphinx template here at | 24 # TODO(eliben): it may be interesting to use an actual Sphinx template here at |
| 25 # some point. | 25 # some point. |
| 26 PAGE_TEMPLATE = string.Template(r''' | 26 PAGE_TEMPLATE = string.Template(r''' |
| 27 {{+bindTo:partials.standard_nacl_article}} | 27 {{+bindTo:partials.${doc_template}}} |
| 28 | 28 |
| 29 ${doc_body} | 29 ${doc_body} |
| 30 | 30 |
| 31 {{/partials.standard_nacl_article}} | 31 {{/partials.${doc_template}}} |
| 32 '''.lstrip()) | 32 '''.lstrip()) |
| 33 | 33 |
| 34 | 34 |
| 35 # Path to the top-level YAML table-of-contents file for the chromesite | 35 # Path to the top-level YAML table-of-contents file for the chromesite |
| 36 BOOK_TOC_TEMPLATE = '_book_template.yaml' | 36 BOOK_TOC_TEMPLATE = '_book_template.yaml' |
| 37 | 37 |
| 38 | 38 |
| 39 class ChromesiteHTMLTranslator(HTMLTranslator): | 39 class ChromesiteHTMLTranslator(HTMLTranslator): |
| 40 """ Custom HTML translator for chromesite output. | 40 """ Custom HTML translator for chromesite output. |
| 41 | 41 |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 important fields: | 238 important fields: |
| 239 body - document contents | 239 body - document contents |
| 240 title | 240 title |
| 241 current_page_name | 241 current_page_name |
| 242 Some special pages (genindex, etc.) may not have some of the fields, so | 242 Some special pages (genindex, etc.) may not have some of the fields, so |
| 243 fetch them conservatively. | 243 fetch them conservatively. |
| 244 """ | 244 """ |
| 245 if not 'body' in context: | 245 if not 'body' in context: |
| 246 return | 246 return |
| 247 | 247 |
| 248 template = context.get('meta', {}).get('template', 'standard_nacl_article') |
| 249 title = context.get('title', '') |
| 250 body = context.get('body', '') |
| 251 |
| 248 # codecs.open is the fast Python 2.x way of emulating the encoding= argument | 252 # codecs.open is the fast Python 2.x way of emulating the encoding= argument |
| 249 # in Python 3's builtin open. | 253 # in Python 3's builtin open. |
| 250 with codecs.open(filename, 'w', encoding='utf-8') as f: | 254 with codecs.open(filename, 'w', encoding='utf-8') as f: |
| 251 f.write(PAGE_TEMPLATE.substitute( | 255 f.write(PAGE_TEMPLATE.substitute( |
| 252 doc_title=context.get('title', ''), | 256 doc_template=template, |
| 253 doc_body=context.get('body'))) | 257 doc_title=title, |
| 258 doc_body=body)) |
| 254 | 259 |
| 255 def _conditional_chromesite(self, s): | 260 def _conditional_chromesite(self, s): |
| 256 # return s if self.chromesite_production_mode else '' | 261 # return s if self.chromesite_production_mode else '' |
| 257 return s | 262 return s |
| 258 | 263 |
| 259 def _conditional_nonprod(self, s): | 264 def _conditional_nonprod(self, s): |
| 260 # return s if not self.chromesite_production_mode else '' | 265 # return s if not self.chromesite_production_mode else '' |
| 261 return '' | 266 return '' |
| 262 | 267 |
| 263 | 268 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 288 from sphinx.builders import linkcheck | 293 from sphinx.builders import linkcheck |
| 289 import urllib2 | 294 import urllib2 |
| 290 linkcheck.HeadRequest = urllib2.Request | 295 linkcheck.HeadRequest = urllib2.Request |
| 291 | 296 |
| 292 app.add_directive('naclcode', NaclCodeDirective) | 297 app.add_directive('naclcode', NaclCodeDirective) |
| 293 app.add_builder(ChromesiteBuilder) | 298 app.add_builder(ChromesiteBuilder) |
| 294 | 299 |
| 295 # "Production mode" for local testing vs. on-server documentation. | 300 # "Production mode" for local testing vs. on-server documentation. |
| 296 app.add_config_value('chromesite_kill_internal_links', default='0', | 301 app.add_config_value('chromesite_kill_internal_links', default='0', |
| 297 rebuild='html') | 302 rebuild='html') |
| OLD | NEW |