#!ipy

"""
++++++++
 rst.py
++++++++

  author: Jesse Wiles
  copyright: Quadralay Corporation, 2007.
  
  Handler for text/x-rst content type.
"""

# system imports
from System import DateTime
from System.Diagnostics import Process, ProcessWindowStyle
from System.IO import File, Path

# XSL ref/imports
import clr
clr.AddReference('System.Xml')

from System.Xml import XmlReader, XmlReaderSettings, XmlWriter, XmlWriterSettings
from System.Xml.Xsl import XslCompiledTransform

# local imports
from abstracts import PyToolAdapter
import utils

class Rst(PyToolAdapter):
    def duplicate(self):
        if self._env.uri.Scheme == 'http':
            data = utils.get_web_uri(self._env.uri.OriginalString)
            
        elif self._env.uri.Scheme == 'file':
            fp = open(self._env.uri.LocalPath, 'rb')
            data = fp.Read()
            fp.close()

        else:
            raise "ERROR: '%s' is not a supported URI scheme." % self._env.uri.Scheme

        tmppath = '%s~' % (self._env.dupDocPath)

        fp = open(tmppath, 'wb')
        fp.write(data)
        fp.close()

        progdir = Path.Combine(Path.GetDirectoryName(globals()['__file__']), 'rst2xml')
        progexe = Path.Combine(progdir, 'rst2xml.exe')

        cmd = '"%s" "%s" > "%s"' % (progexe, tmppath, self._env.dupDocPath)

        batpath = self._env.dupDocPath + '.bat'
        bat = open(batpath, 'wb')
        bat.write(cmd)
        bat.close()

        proc = Process()
        proc.StartInfo.FileName = batpath
        proc.StartInfo.WindowStyle = ProcessWindowStyle.Minimized
        proc.Start()
        proc.WaitForExit()
        proc.Close()

        File.Delete(tmppath)
        File.Delete(batpath)

    def xml(self):

        xslpath = Path.Combine(self._env.xslDir, 'rstxml2wif.xsl')

        readerSettings = XmlReaderSettings()
        readerSettings.ProhibitDtd = False

        reader = XmlReader.Create(self._env.docPath, readerSettings)

        writerSettings = XmlWriterSettings()
        writerSettings.Indent = True
        writerSettings.IndentChars = ' '

        writer = XmlWriter.Create(self._env.xmlPath, writerSettings)

        transform = XslCompiledTransform()
        transform.Load(xslpath)
        try:
            transform.Transform(reader, writer)

        finally:
            reader.Close()
            writer.Close()

        # touch original doc path
        File.SetLastWriteTime(self._env.originalDocPath, DateTime.Now)

    def scan_doc(self):
        self._env.paras['{Heading Styles}'] = ''
        self._env.paras['rst-section'] = '{Heading Styles}'
        self._env.paras['rst-section-1'] = 'rst-section'
        self._env.paras['rst-section-2'] = 'rst-section'
        self._env.paras['rst-section-3'] = 'rst-section'
        self._env.paras['rst-section-4'] = 'rst-section'
        self._env.paras['rst-section-5'] = 'rst-section'

        self._env.paras['{Paragraph Styles}'] = ''
        self._env.paras['rst-paragraph'] = '{Paragraph Styles}'

        self._env.paras['{Indented Styles}'] = ''
        self._env.paras['rst-paragraph-indent'] = '{Indented Styles}'
        self._env.paras['rst-paragraph-indent-1'] = 'rst-paragraph-indent'
        self._env.paras['rst-paragraph-indent-2'] = 'rst-paragraph-indent'
        self._env.paras['rst-paragraph-indent-3'] = 'rst-paragraph-indent'

        self._env.paras['{Numbered Styles}'] = ''
        self._env.paras['rst-paragraph-numbered'] = '{Numbered Styles}'
        self._env.paras['rst-paragraph-numbered-1'] = 'rst-paragraph-numbered'
        self._env.paras['rst-paragraph-numbered-2'] = 'rst-paragraph-numbered'

        self._env.paras['{Bulleted Styles}'] = ''
        self._env.paras['rst-paragraph-bulleted'] = '{Bulleted Styles}'
        self._env.paras['rst-paragraph-bulleted-1'] = 'rst-paragraph-bulleted'
        self._env.paras['rst-paragraph-bulleted-2'] = 'rst-paragraph-bulleted'

        self._env.paras['{Literal Styles}'] = ''
        self._env.paras['rst-literal-block'] = '{Literal Styles}'
        
        self._env.paras['{Table Styles}'] = ''
        self._env.paras['rst-table'] = '{Table Styles}'
        self._env.paras['rst-paragraph-cell-head'] = 'rst-table'
        self._env.paras['rst-paragraph-cell-body'] = 'rst-table'
        self._env.paras['rst-paragraph-cell-foot'] = 'rst-table'

        self._env.charas['rst-literal'] = ''

        self._env.tables['rst1'] = ''