Ticket #10637: add_sws2rst_3.patch

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

add_sws2rst_3.patch (replaces earlier versions)

  • sage-sage

    # HG changeset patch
    # User Pablo Angulo <pablo.angulo@uam.es>
    # Date 1302082947 -7200
    # Node ID fd0126a70d1877b5cbecd0b701ae74db2abf10ad
    # Parent  e18b4ae3fcbe4afd1642b94e8646f8973fdf0716
    added file sage-sws2rst
    
    diff -r e18b4ae3fcbe -r fd0126a70d18 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 -r e18b4ae3fcbe -r fd0126a70d18 sage-sws2rst
    - +  
     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.splitext(file_name)[0]
     21
     22    #Images
     23    images_dir = base_name + '_media'
     24    if not os.path.exists(images_dir):
     25        os.mkdir(images_dir)
     26   
     27    #"data" dir
     28    data_path = os.path.join(tempname,'sage_worksheet','data')
     29    if os.path.exists(data_path):
     30        for image in os.listdir(data_path):
     31            try:
     32                shutil.move(os.path.join(data_path, image), images_dir)
     33            except shutil.Error:
     34                pass
     35
     36    #cells
     37    cells_path = os.path.join(tempname,'sage_worksheet','cells')   
     38    for cell in os.listdir(cells_path):
     39        cell_path = os.path.join(cells_path, cell)
     40        for image in os.listdir(cell_path):
     41            shutil.copy2(os.path.join(cell_path, image), images_dir)
     42            shutil.move(os.path.join(images_dir, 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 + '.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