##// END OF EJS Templates
don't recommend `ipython notebook --matplotlib`
don't recommend `ipython notebook --matplotlib`

File last commit:

r11452:69026871
r11799:09095dbb
Show More
sphinx.py
264 lines | 9.2 KiB | text/x-python | PythonLexer
Jonathan Frederic
Transformer refactor
r10436 """Module that allows custom Sphinx parameters to be set on the notebook and
Jonathan Frederic
Cleanup and refactor, transformers
r10674 on the 'other' object passed into Jinja. Called prior to Jinja conversion
process.
Jonathan Frederic
Added Sphinx transformer....
r9772 """
Jonathan Frederic
Transformer refactor
r10436 #-----------------------------------------------------------------------------
# Copyright (c) 2013, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
Jonathan Frederic
Cleanup and refactor, transformers
r10674
Jonathan Frederic
Transformer refactor
r10436 from __future__ import print_function, absolute_import
Jonathan Frederic
Added Sphinx transformer....
r9772
Jonathan Frederic
Transformer refactor
r10436 # Stdlib imports
Jonathan Frederic
Added Sphinx transformer....
r9772 # Used to find Sphinx package location
import sphinx
import os.path
# Used to set the default date to today's date
from datetime import date
Jonathan Frederic
Transformer refactor
r10436 # Third-party imports
Jonathan Frederic
Added pygments syntax highlighting....
r9780 # Needed for Pygments latex definitions.
from pygments.formatters import LatexFormatter
Jonathan Frederic
Transformer refactor
r10436 # Our own imports
# Configurable traitlets
from IPython.utils.traitlets import Unicode, Bool
Jonathan Frederic
Added Sphinx transformer....
r9772 # Needed to override transformer
MinRK
s/ConfigurableTransformer/Transformer/
r11452 from .base import (Transformer)
Jonathan Frederic
Added Sphinx transformer....
r9772
Brian E. Granger
Fixing import for nbconvert.
r11089 from IPython.nbconvert.utils import console
Jonathan Frederic
Cleanup and refactor, transformers
r10674
Jonathan Frederic
Transformer refactor
r10436 #-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------
Jonathan Frederic
Cleanup and refactor, transformers
r10674
MinRK
s/ConfigurableTransformer/Transformer/
r11452 class SphinxTransformer(Transformer):
Jonathan Frederic
Added Sphinx transformer....
r9772 """
Sphinx utility transformer.
This transformer is used to set variables needed by the latex to build
Sphinx stylized templates.
"""
Matthias BUSSONNIER
pylint plus sphinx default to non-interactive.
r10875 interactive = Bool(False, config=True, help="""
Jonathan Frederic
Cleanup and refactor, transformers
r10674 Allows you to define whether or not the Sphinx exporter will prompt
you for input during the conversion process. If this is set to false,
the author, version, release, date, and chapter_style traits should
be set.
""")
Jonathan Frederic
Added Sphinx transformer....
r9772
author = Unicode("Unknown Author", config=True, help="Author name")
Jonathan Frederic
Cleanup and refactor, transformers
r10674 version = Unicode("", config=True, help="""
Version number
You can leave this blank if you do not want to render a version number.
Example: "1.0.0"
""")
Jonathan Frederic
Added Sphinx transformer....
r9772
Jonathan Frederic
Cleanup and refactor, transformers
r10674 release = Unicode("", config=True, help="""
Release name
You can leave this blank if you do not want to render a release name.
Example: "Rough Draft"
""")
Jonathan Frederic
Added Sphinx transformer....
r9772
Jonathan Frederic
Cleanup and refactor, transformers
r10674 publish_date = Unicode("", config=True, help="""
Publish date
This is the date to render on the document as the publish date.
Leave this blank to default to todays date.
Example: "June 12, 1990"
""")
Jonathan Frederic
Added Sphinx transformer....
r9772
Jonathan Frederic
Cleanup and refactor, transformers
r10674 chapter_style = Unicode("Bjarne", config=True, help="""
Sphinx chapter style
This is the style to use for the chapter headers in the document.
You may choose one of the following:
"Bjarne" (default)
"Lenny"
"Glenn"
"Conny"
"Rejne"
"Sonny" (used for international documents)
""")
Jonathan Frederic
Added Sphinx transformer....
r9772
Jonathan Frederic
Cleanup and refactor, transformers
r10674 output_style = Unicode("notebook", config=True, help="""
Nbconvert Ipython
notebook input/output formatting style.
You may choose one of the following:
"simple (recommended for long code segments)"
"notebook" (default)
""")
Jonathan Frederic
Added notebook style input highlighting.
r9939
Jonathan Frederic
Added option to center output and remove title.
r9949 center_output = Bool(False, config=True, help="""
Jonathan Frederic
Cleanup and refactor, transformers
r10674 Optional attempt to center all output. If this is false, no additional
formatting is applied.
""")
Jonathan Frederic
Added option to center output and remove title.
r9949
use_headers = Bool(True, config=True, help="""
Jonathan Frederic
Cleanup and refactor, transformers
r10674 Whether not a header should be added to the document.
""")
Jonathan Frederic
Added option to center output and remove title.
r9949
Jonathan Frederic
Cleanup and refactor, transformers
r10674 #Allow the user to override the title of the notebook (useful for
#fancy document titles that the file system doesn't support.)
Jonathan Frederic
Added the ability to override the document title (useful for batch jobs)
r10174 overridetitle = Unicode("", config=True, help="")
Jonathan Frederic
Cleanup and refactor, transformers
r10674
Jonathan Frederic
Added the ability to override the document title (useful for batch jobs)
r10174
Matthias BUSSONNIER
do not ask to inherit __call__ but call
r10834 def call(self, nb, resources):
Jonathan Frederic
Changed SphinxTransformer to inherit...
r9775 """
Sphinx transformation to apply on each notebook.
Jonathan Frederic
Cleanup and refactor, transformers
r10674
Parameters
----------
nb : NotebookNode
Notebook being converted
resources : dictionary
Additional resources used in the conversion process. Allows
transformers to pass variables into the Jinja engine.
Jonathan Frederic
Changed SphinxTransformer to inherit...
r9775 """
Jonathan Frederic
Sphinx specific metadata now stored in _draft namespace.
r9918
# TODO: Add versatile method of additional notebook metadata. Include
# handling of multiple files. For now use a temporay namespace,
# '_draft' to signify that this needs to change.
Jonathan Frederic
Cleanup and refactor, transformers
r10674 if not "sphinx" in resources:
resources["sphinx"] = {}
Jonathan Frederic
Sphinx specific metadata now stored in _draft namespace.
r9918
Jonathan Frederic
Added Sphinx transformer....
r9772 if self.interactive:
# Prompt the user for additional meta data that doesn't exist currently
# but would be usefull for Sphinx.
Jonathan Frederic
Transformers in traitlet lists now, new _init_ methods,...
r11383 resources["sphinx"]["author"] = self._prompt_author()
resources["sphinx"]["version"] = self._prompt_version()
resources["sphinx"]["release"] = self._prompt_release()
resources["sphinx"]["date"] = self._prompt_date()
Jonathan Frederic
Added Sphinx transformer....
r9772
# Prompt the user for the document style.
Jonathan Frederic
Cleanup and refactor, transformers
r10674 resources["sphinx"]["chapterstyle"] = self._prompt_chapter_title_style()
resources["sphinx"]["outputstyle"] = self._prompt_output_style()
Jonathan Frederic
Added option to center output and remove title.
r9949
# Small options
Brian E. Granger
Fixing import for nbconvert.
r11089 resources["sphinx"]["centeroutput"] = console.prompt_boolean("Do you want to center the output? (false)", False)
resources["sphinx"]["header"] = console.prompt_boolean("Should a Sphinx document header be used? (true)", True)
Jonathan Frederic
Added Sphinx transformer....
r9772 else:
# Try to use the traitlets.
Jonathan Frederic
Transformers in traitlet lists now, new _init_ methods,...
r11383 resources["sphinx"]["author"] = self.author
resources["sphinx"]["version"] = self.version
resources["sphinx"]["release"] = self.release
Jonathan Frederic
Added Sphinx transformer....
r9772
Jonathan Frederic
Added option to center output and remove title.
r9949 # Use todays date if none is provided.
Jonathan Frederic
Transformers in traitlet lists now, new _init_ methods,...
r11383 if self.publish_date:
resources["sphinx"]["date"] = self.publish_date
elif len(resources['metadata']['modified_date'].strip()) == 0:
resources["sphinx"]["date"] = date.today().strftime("%B %-d, %Y")
Jonathan Frederic
Added Sphinx transformer....
r9772 else:
Jonathan Frederic
Transformers in traitlet lists now, new _init_ methods,...
r11383 resources["sphinx"]["date"] = resources['metadata']['modified_date']
Jonathan Frederic
Added option to center output and remove title.
r9949
# Sphinx traitlets.
Jonathan Frederic
Cleanup and refactor, transformers
r10674 resources["sphinx"]["chapterstyle"] = self.chapter_style
resources["sphinx"]["outputstyle"] = self.output_style
resources["sphinx"]["centeroutput"] = self.center_output
resources["sphinx"]["header"] = self.use_headers
Jonathan Frederic
Added Sphinx transformer....
r9772
# Find and pass in the path to the Sphinx dependencies.
Jonathan Frederic
Fixed posix path in sphinx transformer
r11200 resources["sphinx"]["texinputs"] = os.path.realpath(os.path.join(sphinx.__file__, "..", "texinputs"))
Jonathan Frederic
Added Sphinx transformer....
r9772
Jonathan Frederic
Added pygments syntax highlighting....
r9780 # Generate Pygments definitions for Latex
Jonathan Frederic
Cleanup and refactor, transformers
r10674 resources["sphinx"]["pygment_definitions"] = self._generate_pygments_latex_def()
Jonathan Frederic
Added pygments syntax highlighting....
r9780
Jonathan Frederic
Added the ability to override the document title (useful for batch jobs)
r10174 if not (self.overridetitle == None or len(self.overridetitle.strip()) == 0):
Jonathan Frederic
Transformers in traitlet lists now, new _init_ methods,...
r11383 resources['metadata']['name'] = self.overridetitle
Jonathan Frederic
Added the ability to override the document title (useful for batch jobs)
r10174
Jonathan Frederic
Added Sphinx transformer....
r9772 # End
Jonathan Frederic
Cleanup and refactor, transformers
r10674 return nb, resources
Jonathan Frederic
Added pygments syntax highlighting....
r9780
def _generate_pygments_latex_def(self):
Jonathan Frederic
Cleanup and refactor, transformers
r10674 """
Generate the pygments latex definitions that allows pygments
to work in latex.
"""
Jonathan Frederic
Added pygments syntax highlighting....
r9780 return LatexFormatter().get_style_defs()
Jonathan Frederic
Added Sphinx transformer....
r9772
Jonathan Frederic
Cleanup and refactor, transformers
r10674
Jonathan Frederic
Added Sphinx transformer....
r9772 def _prompt_author(self):
Jonathan Frederic
Cleanup and refactor, transformers
r10674 """
Prompt the user to input an Author name
"""
Brian E. Granger
Fixing import for nbconvert.
r11089 return console.input("Author name: ")
Jonathan Frederic
Cleanup and refactor, transformers
r10674
Jonathan Frederic
Added Sphinx transformer....
r9772
def _prompt_version(self):
Jonathan Frederic
Cleanup and refactor, transformers
r10674 """
prompt the user to enter a version number
"""
Brian E. Granger
Fixing import for nbconvert.
r11089 return console.input("Version (ie ""1.0.0""): ")
Jonathan Frederic
Cleanup and refactor, transformers
r10674
Jonathan Frederic
Added Sphinx transformer....
r9772
def _prompt_release(self):
Jonathan Frederic
Cleanup and refactor, transformers
r10674 """
Prompt the user to input a release name
"""
Brian E. Granger
Fixing import for nbconvert.
r11089 return console.input("Release Name (ie ""Rough draft""): ")
Jonathan Frederic
Cleanup and refactor, transformers
r10674
Jonathan Frederic
Added Sphinx transformer....
r9772
Jonathan Frederic
Transformers in traitlet lists now, new _init_ methods,...
r11383 def _prompt_date(self, resources):
Jonathan Frederic
Cleanup and refactor, transformers
r10674 """
Prompt the user to enter a date
"""
Jonathan Frederic
Transformers in traitlet lists now, new _init_ methods,...
r11383 if resources['metadata']['modified_date']:
default_date = resources['metadata']['modified_date']
else:
default_date = date.today().strftime("%B %-d, %Y")
Brian E. Granger
Fixing import for nbconvert.
r11089 user_date = console.input("Date (deafults to \"" + default_date + "\"): ")
Jonathan Frederic
Added Sphinx transformer....
r9772 if len(user_date.strip()) == 0:
user_date = default_date
return user_date
Jonathan Frederic
Cleanup and refactor, transformers
r10674
Jonathan Frederic
Added notebook style input highlighting.
r9939 def _prompt_output_style(self):
Jonathan Frederic
Cleanup and refactor, transformers
r10674 """
MinRK
various spelling and capitalization fixes
r11046 Prompts the user to pick an IPython output style.
Jonathan Frederic
Cleanup and refactor, transformers
r10674 """
Jonathan Frederic
Added notebook style input highlighting.
r9939
# Dictionary of available output styles
styles = {1: "simple",
2: "notebook"}
#Append comments to the menu when displaying it to the user.
Jonathan Frederic
Changed default input/output formatting to notebook style.
r10037 comments = {1: "(recommended for long code segments)",
2: "(default)"}
Jonathan Frederic
Added notebook style input highlighting.
r9939
Brian E. Granger
Fixing import for nbconvert.
r11089 return console.prompt_dictionary(styles, default_style=2, menu_comments=comments)
Jonathan Frederic
Cleanup and refactor, transformers
r10674
Jonathan Frederic
Added notebook style input highlighting.
r9939
Jonathan Frederic
Added Sphinx transformer....
r9772 def _prompt_chapter_title_style(self):
Jonathan Frederic
Cleanup and refactor, transformers
r10674 """
Prompts the user to pick a Sphinx chapter style
"""
Jonathan Frederic
Added Sphinx transformer....
r9772
# Dictionary of available Sphinx styles
styles = {1: "Bjarne",
2: "Lenny",
3: "Glenn",
4: "Conny",
5: "Rejne",
6: "Sonny"}
Jonathan Frederic
Generalized dictionary based menu input prompting.
r9938 #Append comments to the menu when displaying it to the user.
comments = {1: "(default)",
6: "(for international documents)"}
Brian E. Granger
Fixing import for nbconvert.
r11089 return console.prompt_dictionary(styles, menu_comments=comments)
Matthias BUSSONNIER
do not ask to inherit __call__ but call
r10834