Ticket #10637: add_sws2rst_2.patch

File add_sws2rst_2.patch, 4.3 KB (added by pang, 10 years ago)

replaces the first patch

  • sage-sage

    # HG changeset patch
    # User Pablo Angulo <pablo.angulo@uam.es>
    # Date 1295454069 -3600
    # Node ID 3f10bed04e62201a5d646d50e4f143e4d89f52d7
    # Parent  afec3d7d46feb80352aef4ff7f5d4211f015b66c
    [mq]: add_sws2rst.patch
    
    diff -r afec3d7d46fe -r 3f10bed04e62 sage-sage
    a b  
    145145    echo "                            and output a patch"
    146146    echo "  -startuptime        -- display how long each component of Sage takes to"
    147147    echo "                         start up"
     148    echo "  -sws2rst <sws document>  Generates a rst file from the given document"
    148149    echo "  -t [options] <files|dir>"
    149150    echo "                      -- test examples in .py, .pyx, .sage or .tex files"
    150151    echo "                         options:"
     
    700701   echo -n " Do you want to proceed? [y/n] "
    701702   read CHOICE
    702703   while [ "$CHOICE" != "y" -a "$CHOICE" != "n" ]; do
    703            echo -n " Choose one of y, n: "
    704            read CHOICE
     704      echo -n " Choose one of y, n: "
     705      read CHOICE
    705706   done
    706707   if [ $CHOICE = 'n' ]; then
    707            exit 0
     708       exit 0
    708709   fi
    709710   build_sage -b "$@"
    710711   exit $?
     
    804805       PKG_NAME=`echo "$PKG" | sed -e "s/\.spkg$//"`
    805806       PKG_NAME=`basename "$PKG_NAME"`
    806807       case $PKG in
    807            /*)
    808                sage-spkg $OPT "$PKG" 2>&1 | (trap "" SIGINT; tee -a ../install.log "$SAGE_LOGS/$PKG_NAME".log)
    809            ;;
    810            *)
    811                sage-spkg $OPT "$CUR/$PKG" 2>&1 | (trap "" SIGINT; tee -a ../install.log "$SAGE_LOGS/$PKG_NAME".log)
    812            ;;
     808       /*)
     809           sage-spkg $OPT "$PKG" 2>&1 | (trap "" SIGINT; tee -a ../install.log "$SAGE_LOGS/$PKG_NAME".log)
     810       ;;
     811       *)
     812           sage-spkg $OPT "$CUR/$PKG" 2>&1 | (trap "" SIGINT; tee -a ../install.log "$SAGE_LOGS/$PKG_NAME".log)
     813       ;;
    813814       esac
    814815
    815816       if [ $? -ne 0 ]; then
     
    10081009    python "$SAGE_LOCAL"/bin/sage-startuptime.py
    10091010    exit $?
    10101011fi
     1012
     1013if [ "$1" = '-sws2rst' -o "$1" = '--sws2rst' ]; then
     1014   cd "$CUR"
     1015   shift
     1016   sage-sws2rst "$@"
     1017   exit $?
     1018fi
     1019
    10111020if [ "$1" = '-gthread' -o "$1" = '-qthread' -o "$1" = '-q4thread' -o "$1" = '-wthread' -o "$1" = '-pylab' ]; then
    10121021    sage "$1"
    10131022    exit $?
  • new file sage-sws2rst

    diff -r afec3d7d46fe -r 3f10bed04e62 sage-sws2rst
    - +  
     1#!/usr/bin/env python
     2# -*- coding: utf-8 -*-
     3
     4import sys
     5import tarfile
     6import os
     7import shutil
     8import codecs
     9from sagenb.misc.worksheet2rst import worksheet2rst
     10
     11if __name__=='__main__':
     12    if len(sys.argv)<=1:
     13        print 'First argument should be a sws file'
     14        sys.exit()
     15   
     16    sws_file = tarfile.open(sys.argv[1], mode='r:bz2')
     17    #TODO: python complains about using tempnam, but I don't
     18    #know hot to fix it or see any danger
     19    tempname = os.tempnam('.')
     20    sws_file.extractall(tempname)
     21    base_name = os.path.splitext(sys.argv[1])[0]
     22
     23    #Images
     24    images_dir = base_name + '_media'
     25    if not os.path.exists(images_dir):
     26        os.mkdir(images_dir)
     27   
     28    #"data" dir
     29    data_path = os.path.join(tempname,'sage_worksheet','data')
     30    if os.path.exists(data_path):
     31        for image in os.listdir(data_path):
     32            try:
     33                shutil.move(os.path.join(data_path, image), images_dir)
     34            except shutil.Error:
     35                pass
     36
     37    #cells
     38    cells_path = os.path.join(tempname,'sage_worksheet','cells')   
     39    for cell in os.listdir(cells_path):
     40        cell_path = os.path.join(cells_path, cell)
     41        for image in os.listdir(cell_path):
     42            shutil.copy2(os.path.join(cell_path, image), images_dir)
     43            shutil.move(os.path.join(images_dir, image),
     44                        os.path.join(images_dir, 'cell_%s_%s'%(cell,image)))
     45   
     46    #read html file, parse it, write rst file
     47    file = codecs.open(os.path.join('.',tempname,'sage_worksheet','worksheet.html'),
     48                          mode='r',
     49                          encoding='utf-8')
     50    html_text = file.read()
     51    file.close()
     52    rst_text = worksheet2rst(html_text, images_dir = images_dir)
     53    rst_file = base_name + '.rst'
     54    out_file = codecs.open(rst_file, mode='w',
     55                  encoding='utf-8')
     56    out_file.write(rst_text)
     57    out_file.close()
     58
     59    shutil.rmtree(tempname)