Ticket #10637: add_sws2rst_4.patch

File add_sws2rst_4.patch, 3.4 KB (added by pang, 10 years ago)

add_sws2rst_4.patch (replaces earlier versions)

  • sage-sage

    # HG changeset patch
    # User Pablo Angulo <pablo.angulo@uam.es>
    # Date 1302082947 -7200
    # Node ID 8ce7166ca6e6d10a218a3c824abdcb5703ce4d6e
    # Parent  d105c4881ddaeaaaa369dc0f9bba009defbf6e29
    replace spaces by underscore, works when the sws file is not in the current dir
    
    diff --git a/sage-sage b/sage-sage
    a b  
    2525    echo "  -python [...]       -- run the Python interpreter"
    2626    echo "  -R [...]            -- run Sage's R with given arguments"
    2727    echo "  -singular [...]     -- run Sage's singular with given arguments"
     28    echo "  -sws2rst <sws document>  Generates a rst file from the given document"
    2829    echo "  -root               -- print the Sage root directory"
    2930    echo "  -t [options] <files|dir>"
    3031    echo "                      -- test examples in .py, .pyx, .sage or .tex files"
     
    378379    exit $?
    379380fi
    380381
     382if [ "$1" = '-sws2rst' -o "$1" = '--sws2rst' ]; then
     383   cd "$CUR"
     384   shift
     385   sage-sws2rst "$@"
     386   exit $?
     387fi
     388
    381389if [ "$1" = '-twistd'  -o "$1" = '--twistd' ]; then
    382390    cd "$CUR"
    383391    shift
  • new file sage-sws2rst

    diff --git a/sage-sws2rst b/sage-sws2rst
    new file mode 100755
    - +  
     1#!/usr/bin/env python
     2# -*- coding: utf-8 -*-
     3
     4import sys
     5import tarfile
     6import os
     7import shutil
     8import codecs
     9import tempfile
     10from sagenb.misc.worksheet2rst import worksheet2rst
     11
     12def process_sws(file_name):
     13   
     14    sws_file = tarfile.open(file_name, mode='r:bz2')
     15    #TODO: python complains about using tempnam, but I don't
     16    #know hot to fix it or see any danger
     17#    tempname = os.tempnam('.')
     18    tempname = os.path.join(tempfile.gettempdir(), file_name)
     19    sws_file.extractall(tempname)
     20    base_name = os.path.split(os.path.splitext(file_name)[0])[1]
     21    base_name_clean = base_name.replace(' ','_')
     22
     23    #Images
     24    images_dir = base_name_clean + '_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), os.path.join(images_dir, image.replace(' ','_')))
     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),
     43                         os.path.join(images_dir, 'cell_%s_%s'%(cell,image)))
     44   
     45    #read html file, parse it, write rst file
     46    file = codecs.open(os.path.join('.',tempname,'sage_worksheet','worksheet.html'),
     47                          mode='r',
     48                          encoding='utf-8')
     49    html_text = file.read()
     50    file.close()
     51    rst_text = worksheet2rst(html_text, images_dir = images_dir)
     52    rst_file = base_name_clean + '.rst'
     53    out_file = codecs.open(rst_file, mode='w',
     54                  encoding='utf-8')
     55    out_file.write(rst_text)
     56    out_file.close()
     57   
     58    shutil.rmtree(tempname)
     59   
     60if __name__=='__main__':
     61    if len(sys.argv)<=1:
     62        print 'First argument should be a sws file'
     63        sys.exit()
     64    for file_name in sys.argv[1:]:
     65        print file_name
     66        process_sws(file_name)
     67
     68