Ticket #6673: trac_6673-jsmath_macros_docs_v2.patch

File trac_6673-jsmath_macros_docs_v2.patch, 11.1 KB (added by mpatel, 12 years ago)

Template version. Depends on #6187, #6586. Apply only this patch.

  • doc/common/builder.py

    # HG changeset patch
    # User Mitesh Patel <qed777@gmail.com>
    # Date 1249369289 25200
    # Node ID 9253c5a9c56437bec750f00f14fb60cbbf232473
    # Parent  01bf1e710774c5524241933a40417955af984f70
    #6673, set up jsMath macros for Sage docs; template version for Sphinx 0.6.2
    
    diff --git a/doc/common/builder.py b/doc/common/builder.py
    a b if __name__ == '__main__': 
    988988    if options.jsmath:
    989989        os.environ['SAGE_DOC_JSMATH'] = "True"
    990990
     991        # Avoid a apparent circular import overload.
     992        import sage.all
     993        from sage.misc.latex_macros import sage_jsmath_macros_jinja
     994
     995        # Escape the single quotes for the command-line.
     996        macros = sage_jsmath_macros_jinja.replace("'", "\'")
     997        ALLSPHINXOPTS += ' -A sage_doc_jsmath_macros="%s" ' % macros
     998
    991999    if options.sphinx_opts:
    9921000        ALLSPHINXOPTS += options.sphinx_opts.replace(',', ' ') + " "
    9931001
  • doc/common/conf.py

    diff --git a/doc/common/conf.py b/doc/common/conf.py
    a b if 'SAGE_DOC_JSMATH' in os.environ: 
    1818    extensions.append('sphinx.ext.jsmath')
    1919else:
    2020    extensions.append('sphinx.ext.pngmath')
    21 jsmath_path = 'easy/load.js'
     21jsmath_path = 'jsmath_sage.js'
    2222
    2323# Add any paths that contain templates here, relative to this directory.
    2424templates_path = [os.path.join(SAGE_DOC, 'common/templates'), 'templates']
  • new file doc/common/static/jsmath_sage.js_t

    diff --git a/doc/common/static/jsmath_sage.js_t b/doc/common/static/jsmath_sage.js_t
    new file mode 100644
    - +  
     1/**********************************************************************
     2 *
     3 *   Customize the values given below to suit your needs.
     4 *   You can make additional copies of this file with
     5 *   different customizated settings if you need to load
     6 *   jsMath with different parameters.
     7 *
     8 *   Load this page via:
     9 *
     10 *   <SCRIPT SRC="path-to-jsMath/easy/load.js"></SCRIPT>
     11 *
     12 *   (If you are including this file into your page via Server-Side
     13 *   Includes, you should remove line above.)
     14 *
     15 *   You can make copies of this file with different settings
     16 *   if you need to have several different configurations.
     17 *
     18 **********************************************************************/
     19
     20    if (!window.jsMath) {window.jsMath = {}}
     21
     22//  Move the jsMath button 20 pixels from the right edge (apparently
     23//  in some browsers, it covers up the scroll bar)
     24jsMath.styles = {
     25    '#jsMath_button' : 'position:fixed; bottom:1px; right:20px; '
     26        + 'background-color:white; border: solid 1px #959595; margin:0px; '
     27        + 'padding: 0px 3px 1px 3px; z-index:102; color:black; '
     28        + 'text-decoration:none; font-size:x-small; width:auto; cursor:hand;'
     29}
     30
     31jsMath.Easy = {
     32    //
     33    //  The URL of the root jsMath directory on your server
     34    //  (it must be in the same domain as the HTML page).
     35    //  It should include "http://yoursite.com/", or should
     36    //  be relative to the root of your server.  It is possible
     37    //  to be a relative URL, but it will be relative to the
     38    //  HTML page loading this file.
     39    //
     40    //  If you leave this blank, jsMath will try to look it up from
     41    //  the URL where it loaded this file, but that may not work.
     42    //
     43    root: "",
     44
     45    //
     46    //  The default scaling factor for mathematics compared to the
     47    //  surrounding text.
     48    //
     49    scale: 115,
     50
     51    //
     52    //  1 means use the autoload plug-in to decide if jsMath should be loaded
     53    //  0 means always load jsMath
     54    //
     55    autoload: 1,
     56
     57    //
     58    //  Setting any of these will cause the tex2math plugin to be used
     59    //  to add the <DIV> and <SPAN> tags that jsMath needs.  See the
     60    //  documentation for the tex2math plugin for more information.
     61    //
     62    processSlashParens: 1,       // process \(...\) in text?
     63    processSlashBrackets: 1,     // process \[...\] in text?
     64    processDoubleDollars: 1,     // process $$...$$ in text?
     65    processSingleDollars: 0,     // process $...$ in text?
     66    processLaTeXenvironments: 0, // process \begin{xxx}...\end{xxx} outside math mode?
     67    fixEscapedDollars: 0,        // convert \$ to $ outside of math mode?
     68    doubleDollarsAreInLine: 0,   // make $$...$$ be in-line math?
     69    allowDisableTag: 1,          // allow ID="tex2math_off" to disable tex2math?
     70    //
     71    //  If you want to use your own custom delimiters for math instead
     72    //  of the usual ones, then uncomment the following four lines and
     73    //  insert your own delimiters within the quotes.  You may want to
     74    //  turn off processing of the dollars and other delimiters above
     75    //  as well, though you can use them in combination with the
     76    //  custom delimiters if you wish.  See the tex2math documentation
     77    //  for more details.
     78    //
     79    //customDelimiters: [
     80    //  '[math]','[/math]',        // to begin and end in-line math
     81    //  '[display]','[/display]'   // to begin and end display math
     82    //],
     83
     84    //
     85    //  Disallow the use of the @(...) mechanism for including raw HTML
     86    //  in the contents of \hbox{}?  (If used in a content-management system
     87    //  where users are allowed to enter mathematics, setting this to 0
     88    //  would allow them to enter arbitrary HTML code within their
     89    //  math formulas, and that poses a security risk.)
     90    //
     91    safeHBoxes: 1,
     92
     93    //
     94    //  Show TeX source when mathematics is double-clicked?
     95    //
     96    allowDoubleClicks: 1,
     97
     98    //
     99    //  Show jsMath font warning messages?  (Disabling this prevents yours
     100    //  users from finding out that they can have a better experience on your
     101    //  site by installing some fonts, so don't disable this).
     102    //
     103    showFontWarnings: 0,
     104
     105    //
     106    //  Use "Process" or "ProcessBeforeShowing".  See the jsMath
     107    //  author's documentation for the difference between these
     108    //  two routines.
     109    //
     110    method: "Process",
     111
     112    //
     113    //  List of plug-ins and extensions that you want to be
     114    //  loaded automatically.  E.g.
     115    //      ["plugins/mimeTeX.js","extensions/AMSsymbols.js"]
     116    //
     117    loadFiles: [
     118        "extensions/verb.js",
     119        "extensions/moreArrows.js",
     120        "extensions/AMSmath.js",
     121        "extensions/AMSsymbols.js"
     122    ],
     123
     124    //
     125    //  List of fonts to load automatically.  E.g.
     126    //      ["cmmib10"]
     127    //
     128    loadFonts: [],
     129
     130    //
     131    //  List of macros to define.  These are of the form
     132    //      name: value
     133    //  where 'value' is the replacement text for the macro \name.
     134    //  The 'value' can also be [value,n] where 'value' is the replacement
     135    //  text and 'n' is the number of parameters for the macro.
     136    //  Note that backslashes must be doubled in the replacement string.
     137    //  E.g.,
     138    //      {
     139    //        RR: '{\\bf R}',
     140    //        bold: ['{\\bf #1}', 1]
     141    //      }
     142    //
     143    // Sage-specific jsMath macros.
     144    macros: {
     145        {{ sage_doc_jsmath_macros }}
     146    },
     147
     148    //
     149    //  Allow jsMath to enter global mode?
     150    //  (Uses frames, so may not always work with complex web sites)
     151    //
     152    allowGlobal: 1,
     153
     154    //
     155    //  Disable image fonts?  (In case you don't load them on your server.)
     156    //
     157    // If we set this to 1, we do not need to load plugins/noImageFonts.js
     158    noImageFonts: 1
     159};
     160
     161/****************************************************************/
     162/****************************************************************/
     163//
     164//            DO NOT MAKE CHANGES BELOW THIS
     165//
     166/****************************************************************/
     167/****************************************************************/
     168
     169jsMath.Easy.find_root = function () {
     170    if (jsMath.Easy.root == "") {
     171        jsMath.Easy.root = document.getElementsByTagName("script");
     172        for (var i = 0; i < jsMath.Easy.root.length; i++) {
     173            if (jsMath.Easy.root[i].src.match(/doctools.js$/)) {
     174                jsMath.Easy.root = jsMath.Easy.root[i].src.replace(/doctools.js$/, "");
     175                return;
     176            }
     177        }
     178        jsMath.Easy.root = jsMath.Easy.root[jsMath.Easy.root.length-1].src
     179        if (jsMath.Easy.root.match(/\/easy\/[^\/]*$/)) {
     180            jsMath.Easy.root = jsMath.Easy.root.replace(/\/easy\/[^\/]*$/,"");
     181        } else {
     182            jsMath.Easy.root = jsMath.Easy.root.replace(/\/(jsMath\/(easy\/)?)?[^\/]*$/,"/jsMath");
     183        }
     184    }
     185};
     186
     187jsMath.Easy.find_root();
     188
     189jsMath.Easy.root = jsMath.Easy.root.replace(/\/$/,""); // trim trailing "/" if any
     190
     191document.write('<SCRIPT SRC="'+jsMath.Easy.root+'/jsMath-easy-load.js"><'+'/SCRIPT>');
  • sage/misc/latex_macros.py

    diff --git a/sage/misc/latex_macros.py b/sage/misc/latex_macros.py
    a b def convert_latex_macro_to_jsmath(macro) 
    118118
    119119        sage: from sage.misc.latex_macros import convert_latex_macro_to_jsmath
    120120        sage: convert_latex_macro_to_jsmath('\\newcommand{\\ZZ}{\\Bold{Z}}')
    121         "jsMath.Macro('ZZ','\\\\Bold{Z}')"
     121        "jsMath.Macro('ZZ','\\\\Bold{Z}');"
    122122        sage: convert_latex_macro_to_jsmath('\\newcommand{\\GF}[1]{\\Bold{F}_{#1}}')
    123         "jsMath.Macro('GF','\\\\Bold{F}_{#1}',1)"
     123        "jsMath.Macro('GF','\\\\Bold{F}_{#1}',1);"
    124124    """
    125125    left_bracket = macro.find('[')
    126126    right_bracket = macro.find('[')
    def convert_latex_macro_to_jsmath(macro) 
    139139        args_str = "," + str(num_args)
    140140    else:
    141141        args_str = ""
    142     return "jsMath.Macro('" + name + "','" + defn + "'" + args_str + ")"
     142    return "jsMath.Macro('" + name + "','" + defn + "'" + args_str + ");"
     143
     144def convert_latex_macro_to_jsmath_easy(macro):
     145    r"""
     146    This converts a LaTeX macro definition (\newcommand...) to a
     147    definition for jsMath's easy/load.js macro array.
     148
     149    INPUT:
     150
     151    -  ``macro`` - LaTeX macro definition
     152
     153    EXAMPLES::
     154
     155        sage: from sage.misc.latex_macros import convert_latex_macro_to_jsmath_easy
     156        sage: convert_latex_macro_to_jsmath_easy('\\newcommand{\\ZZ}{\\Bold{Z}}')
     157        "ZZ : '{\\\\Bold{Z}}'"
     158        sage: convert_latex_macro_to_jsmath_easy('\\newcommand{\\GF}[1]{\\Bold{F}_{#1}}')
     159        "GF : ['{\\\\Bold{F}_{#1}}', 1]"
     160    """
     161    left_bracket = macro.find('[')
     162    right_bracket = macro.find('[')
     163    if left_bracket >= 0:
     164        right_bracket = macro.find(']')
     165        num_args = macro[left_bracket+1:right_bracket]
     166    else:
     167        num_args = 0
     168    start_name = macro.find('{') + 1  # add one to go past the backslash
     169    end_name = macro.find('}')
     170    name = macro[start_name+1:end_name]
     171    start_defn = macro.find('{', end_name)
     172    end_defn = macro.rfind('}')
     173    defn = macro[start_defn+1: end_defn].replace('\\', '\\\\')
     174    if num_args > 0:
     175        args_str = "," + str(num_args)
     176        return name + " : ['{" + defn + "}', " + str(num_args) + ']'
     177    else:
     178        return name + " : '{" + defn + "}'"
    143179
    144180# To add a new macro for use in the Sage documentation, add a list or
    145181# tuple to the following list.  Each list (or tuple) should have the
    sage_configurable_latex_macros = ["\\new 
    178214sage_latex_macros += sage_configurable_latex_macros
    179215
    180216sage_jsmath_macros = [convert_latex_macro_to_jsmath(m) for m in sage_latex_macros]
     217sage_jsmath_macros_easy = [convert_latex_macro_to_jsmath_easy(m) for m in sage_latex_macros]
     218
     219# Escape again, because Jinja strings are not raw.
     220sage_jsmath_macros_jinja = ', '.join(sage_jsmath_macros_easy).replace('\\', '\\\\')