"""Utility function for installing MathJax javascript library into
the notebook's 'static' directory, for offline use.

Authors:

* Min RK
"""

#-----------------------------------------------------------------------------
#  Copyright (C) 2008-2011  The IPython Development Team
#
#  Distributed under the terms of the BSD License.  The full license is in
#  the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

import os
import shutil
import urllib2
import tempfile
import tarfile

from IPython.frontend.html import notebook as nbmod

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

def install_mathjax(tag='v1.1', replace=False):
    """Download and install MathJax for offline use.
    
    This will install mathjax to the 'static' dir in the IPython notebook
    package, so it will fail if the caller does not have write access
    to that location.
    
    MathJax is a ~15MB download, and ~150MB installed.
    
    Parameters
    ----------
    
    replace : bool [False]
        Whether to remove and replace an existing install.
    tag : str ['v1.1']
        Which tag to download. Default is 'v1.1', the current stable release,
        but alternatives include 'v1.1a' and 'master'.
    """
    mathjax_url = "https://github.com/mathjax/MathJax/tarball/%s"%tag
    
    nbdir = os.path.dirname(os.path.abspath(nbmod.__file__))
    static = os.path.join(nbdir, 'static')
    dest = os.path.join(static, 'mathjax')
    
    # check for existence and permissions
    if not os.access(static, os.W_OK):
        raise IOError("Need have write access to %s"%static)
    if os.path.exists(dest):
        if replace:
            if not os.access(dest, os.W_OK):
                raise IOError("Need have write access to %s"%dest)
            print "removing previous MathJax install"
            shutil.rmtree(dest)
        else:
            print "offline MathJax apparently already installed"
            return
    
    # download mathjax
    print "Downloading mathjax source..."
    response = urllib2.urlopen(mathjax_url)
    print "done"
    # use 'r|gz' stream mode, because socket file-like objects can't seek:
    tar = tarfile.open(fileobj=response.fp, mode='r|gz')
    topdir = tar.firstmember.path
    print "Extracting to %s"%dest
    tar.extractall(static)
    # it will be mathjax-MathJax-<sha>, rename to just mathjax
    os.rename(os.path.join(static, topdir), dest)


__all__ = ['install_mathjax']

@@ -746,46 +746,40 @@
 hon.
-frontend.html import notebook as nbmod
+utils.path import locate_profile
 %0A%0A#-
@@ -971,19 +971,19 @@
 x(tag='v
-1.1
+2.0
 ', repla
@@ -990,16 +990,27 @@
 ce=False
+, dest=None
 ):%0A    %22
@@ -1070,17 +1070,27 @@
     
-This will
+You can use this to
  ins
@@ -1109,139 +1109,165 @@
  to 
-the 'static' dir in the IPython notebook%0A    package, so it will fail if the caller does not have write access%0A    to that location
+a location on your static file%0A    path. This includes the %60static%60 directory within your IPython profile,%0A    which is the default location for this install
 .%0A  
@@ -1467,19 +1467,19 @@
  str %5B'v
-1.1
+2.0
 '%5D%0A     
@@ -1517,19 +1517,19 @@
 lt is 'v
-1.1
+2.0
 ', the c
@@ -1589,17 +1589,16 @@
 de 'v1.1
-a
 ' and 'm
@@ -1609,16 +1609,291 @@
 r'.%0A
+    dest : path%0A        The path to the directory in which mathjax will be installed.%0A        The default is %60IPYTHONDIR/profile_default/static%60.%0A        dest must be on your notebook static_path when you run the notebook server.%0A        The default location works for this.%0A
     %22%22%22%0A
     
@@ -1888,16 +1888,21 @@
     %22%22%22%0A
+    %0A
     math
@@ -1958,17 +1958,19 @@
 ball/%25s%22
-%25
+ %25 
 tag%0A    
@@ -1978,107 +1978,173 @@
     
-nbdir = os.path.dirname(os.path.abspath(nbmod.__file__))%0A    static = os.path.join(nbdir, 'static')
+if dest is None:%0A        dest = os.path.join(locate_profile('default'), 'static')%0A    %0A    if not os.path.exists(dest):%0A        os.mkdir(dest)%0A    %0A    static = dest
 %0A   
@@ -2321,17 +2321,19 @@
 s to %25s%22
-%25
+ %25 
 static)%0A
@@ -2482,25 +2482,27 @@
 ccess to %25s%22
-%25
+ %25 
 dest)%0A      
@@ -2748,12 +2748,35 @@
 urce
-...%22
+ from %25s ...%22 %25 mathjax_url
 %0A   
@@ -3027,17 +3027,19 @@
 g to %25s%22
-%25
+ %25 
 dest%0A   

install mathjax to profile by default, instead of IPython tree