Esempio n. 1
0
    def show(self):
        """
        Convert the outline to xhtml and display the results in a browser.

        If browser_command is set, this command will be used to launch the browser.
        If it is not set, or if the command fails, the default browser will be used.
        Setting browser_command to a bad command will slow down browser launch.

        """
        tempdir = g.os_path_finalize_join(tempfile.gettempdir(), 'leo_show')
        if not g.os_path_exists(tempdir):
            os.mkdir(tempdir)
        filename = g.sanitize_filename(self.myFileName)
        filepath = g.os_path_finalize_join(tempdir, filename + '.html')
        self.write(filepath, self.xhtml, basedir='', path='')
        url = "file://%s" % filepath
        msg = ''
        if self.browser_command:
            g.trace(self.browser_command)
            try:
                subprocess.Popen([self.browser_command, url])
                return
            except Exception:
                msg = 'can\'t open browser using \n    %s\n'%self.browser_command + \
                'Using default browser instead.'
        if msg:
            self.announce_fail(msg)
        webbrowser.open(url)
Esempio n. 2
0
 def clean_file_name(self, c, p, ext):
     '''Compute the file name when subdirectories mirror the node's hierarchy in Leo.'''
     trace = False and not g.unitTesting
     use_extentions = c.config.getBool(
         'open_with_uses_derived_file_extensions')
     ancestors, found = [], False
     for p2 in p.self_and_parents():
         h = p.anyAtFileNodeName()
         if not h:
             h = p2.h  # Not an @file node: use the entire header
         elif use_extentions and not found:
             # Found the nearest ancestor @<file> node.
             found = True
             base, ext2 = g.os_path_splitext(h)
             if p2 == p: h = base
             if ext2: ext = ext2
         ancestors.append(g.sanitize_filename(h))
     # The base directory is <tempdir>/Leo<id(v)>.
     ancestors.append("Leo" + str(id(p.v)))
     # Build temporary directories.
     td = os.path.abspath(tempfile.gettempdir())
     while len(ancestors) > 1:
         td = os.path.join(td, ancestors.pop())
         if not os.path.exists(td):
             # if trace: g.trace('creating',td)
             os.mkdir(td)
     # Compute the full path.
     name = ancestors.pop() + ext
     path = os.path.join(td, name)
     if trace: g.trace(path)
     return path
 def clean_file_name(self, c, ext, p):
     '''Compute the file name when subdirectories mirror the node's hierarchy in Leo.'''
     trace = False and not g.unitTesting
     use_extentions = c.config.getBool('open_with_uses_derived_file_extensions')
     ancestors, found = [], False
     for p2 in p.self_and_parents():
         h = p2.anyAtFileNodeName()
         if not h:
             h = p2.h # Not an @file node: use the entire header
         elif use_extentions and not found:
             # Found the nearest ancestor @<file> node.
             found = True
             base, ext2 = g.os_path_splitext(h)
             if p2 == p: h = base
             if ext2: ext = ext2
         ancestors.append(g.sanitize_filename(h))
     # The base directory is <tempdir>/Leo<id(v)>.
     ancestors.append("Leo" + str(id(p.v)))
     # Build temporary directories.
     td = os.path.abspath(tempfile.gettempdir())
     while len(ancestors) > 1:
         td = os.path.join(td, ancestors.pop())
         if not os.path.exists(td):
             # if trace: g.trace('creating',td)
             os.mkdir(td)
     # Compute the full path.
     name = ancestors.pop() + ext
     path = os.path.join(td, name)
     if trace: g.trace(path)
     return path
Esempio n. 4
0
 def legacy_file_name(self, c, p, ext):
     '''Compute a legacy file name for unsupported operating systems.'''
     try:
         leoTempDir = getpass.getuser() + "_" + "Leo"
     except Exception:
         leoTempDir = "LeoTemp"
         g.es("Could not retrieve your user name.")
         g.es("Temporary files will be stored in: %s" % leoTempDir)
     td = os.path.join(os.path.abspath(tempfile.gettempdir()), leoTempDir)
     if not os.path.exists(td):
         os.mkdir(td)
     name = g.sanitize_filename(p.h) + '_' + str(id(p.v)) + ext
     path = os.path.join(td, name)
     return path
 def legacy_file_name(self, c, ext, p):
     '''Compute a legacy file name for unsupported operating systems.'''
     try:
         leoTempDir = getpass.getuser() + "_" + "Leo"
     except Exception:
         leoTempDir = "LeoTemp"
         g.es("Could not retrieve your user name.")
         g.es("Temporary files will be stored in: %s" % leoTempDir)
     td = os.path.join(os.path.abspath(tempfile.gettempdir()), leoTempDir)
     if not os.path.exists(td):
         os.mkdir(td)
     name = g.sanitize_filename(p.h) + '_' + str(id(p.v)) + ext
     path = os.path.join(td, name)
     return path
Esempio n. 6
0
    def show(self):

        """
        Convert the outline to xhtml and display the results in a browser.

        If browser_command is set, this command will be used to launch the browser.
        If it is not set, or if the command fails, the default browser will be used.
        Setting browser_command to a bad command will slow down browser launch.

        """

        tempdir = g.os_path_finalize_join(tempfile.gettempdir(),'leo_show')

        if not g.os_path_exists(tempdir):
            os.mkdir(tempdir)

        filename = g.sanitize_filename(self.myFileName)
        filepath = g.os_path_finalize_join(tempdir, filename + '.html')

        self.write(filepath, self.xhtml, basedir='', path='')

        url = "file://%s" % filepath

        msg = ''
        if self.browser_command:

            g.trace(self.browser_command)

            try:
                subprocess.Popen([self.browser_command, url])
                return True
            except Exception:
                msg = 'can\'t open browser using \n    %s\n'%self.browser_command + \
                'Using default browser instead.'

        if msg:
            self.announce_fail(msg)

        webbrowser.open(url)
Esempio n. 7
0
#@@c
import leo.core.leoGlobals as g

if g.isPython3:
    import io
    StringIO = io.StringIO
else:
    import StringIO
    StringIO = StringIO.StringIO
#@-<< imports >>
if 1: # Format a fixed node.
    h = '@button format-code'
    p = g.findNodeAnywhere(c,h)
#@+<< options >>
#@+node:ekr.20100811091636.5995: *3* << options >>
fn = '%s.rst.txt' % (g.sanitize_filename(p.h))
     # 'format-code.rst.txt'

# g.es('output file',repr(fn))

defaultOptionsDict = {

    # The following options are the most important visually.
    'show_doc_parts_as_paragraphs': True,
    'number-code-lines': False,

    # The following options are definitely used in the script.
    'generate-rst-header-comment': True,
    'output-file-name': fn,
    'show_headlines': True,
    'show_options_nodes': False,
Esempio n. 8
0
def openWithTempFilePath(self, v, ext):
    """Return the path to the temp file corresponding to v and ext.
       Replaces the Commands method."""

    #TL: Added support creating temporary directory structure based on node's
    #    hierarchy in Leo's outline.
    c = self
    if c.config.getBool('open_with_clean_filenames'):
        atFileFound = False  #Track when first ancestor @file found
        #Build list of all of node's parents
        ancestor = []
        p = c.p
        while p:
            hs = p.isAnyAtFileNode()  #Get file name if we're at a @file node
            if not hs:
                hs = p.h  #Otherwise, use the entire header
            else:
                #@verbatim
                #@file type node
                if c.config.getBool('open_with_uses_derived_file_extensions'):
                    #Leo configured to use node's derived file's extension
                    if (atFileFound == False):
                        atFileFound = True  #no need to look any more.
                        #Found first ancestor @file node in outline
                        atFileBase, atFileExt = g.os_path_splitext(hs)
                        if (p == c.p):
                            #node to edit is an @file, Move ext from hs to ext
                            hs = atFileBase
                        if atFileExt:  #It has an extension
                            ext = atFileExt  #use it

            #Remove unsupported directory & file name characters
            #if(os.name == "dos" or os.name == "nt"):
            if 1:
                hsClean = ""
                for ch in hs:
                    if ch in g.string.whitespace:  #Convert tabs to spaces
                        hsClean += ' '
                    elif ch in ('\\', '/', ':', '|', '<', '>', '*',
                                '"'):  #Not allowed in Dos/Windows
                        hsClean += '_'
                    elif ch in ('"'):  #Leo code can't handle the "
                        hsClean += '\''  #replace with '
                    else:
                        hsClean += ch
                #Windows directory and file names can't end with a period
                if hsClean.endswith('.'):
                    hsClean += '_'
            else:
                hsClean = g.sanitize_filename(hs)
            #Add node's headstring (filtered) to the list of ancestors
            ancestor.append(hsClean)
            p = p.parent()

        #Put temporary directory structure under <tempdir>\Leo<uniqueId> directory
        ancestor.append("Leo" + str(id(v)))

        #Build temporary directory
        td = os.path.abspath(tempfile.gettempdir())
        #Loop through all of node's ancestors
        while len(ancestor) > 1:
            #Add each ancestor of node from nearest to farthest
            td = os.path.join(td, ancestor.pop())  #Add next subdirectory
            if not os.path.exists(td):
                os.mkdir(td)
        #Add filename with extension to the path (last entry in ancestor list)
        name = ancestor.pop() + ext
    else:
        #Use old method for unsupported operating systems
        try:
            leoTempDir = getpass.getuser() + "_" + "Leo"
        except:
            leoTempDir = "LeoTemp"
            g.es("Could not retrieve your user name.")
            g.es("Temporary files will be stored in: %s" % leoTempDir)
        td = os.path.join(os.path.abspath(tempfile.gettempdir()), leoTempDir)
        if not os.path.exists(td):
            os.mkdir(td)
        name = g.sanitize_filename(v.h) + '_' + str(id(v)) + ext

    path = os.path.join(td, name)

    return path
Esempio n. 9
0
#@@c
import leo.core.leoGlobals as g

if g.isPython3:
    import io
    StringIO = io.StringIO
else:
    import StringIO
    StringIO = StringIO.StringIO
#@-<< imports >>
if 1:  # Format a fixed node.
    h = '@button format-code'
    p = g.findNodeAnywhere(c, h)
#@+<< options >>
#@+node:ekr.20100811091636.5995: *3* << options >>
fn = '%s.rst.txt' % (g.sanitize_filename(p.h))
# 'format-code.rst.txt'

# g.es('output file',repr(fn))

defaultOptionsDict = {

    # The following options are the most important visually.
    'show_doc_parts_as_paragraphs': True,
    'number-code-lines': False,

    # The following options are definitely used in the script.
    'generate-rst-header-comment': True,
    'output-file-name': fn,
    'show_headlines': True,
    'show_options_nodes': False,
Esempio n. 10
0
def openWithTempFilePath (self,v,ext):

    """Return the path to the temp file corresponding to v and ext.
       Replaces the Commands method."""

    #TL: Added support creating temporary directory structure based on node's
    #    hierarchy in Leo's outline.
    c = self
    if c.config.getBool('open_with_clean_filenames'):
        atFileFound = False   #Track when first ancestor @file found
        #Build list of all of node's parents
        ancestor = []
        p = c.p
        while p:
            hs = p.isAnyAtFileNode() #Get file name if we're at a @file node
            if not hs:
                hs = p.h  #Otherwise, use the entire header
            else:
#@verbatim
                #@file type node
                if c.config.getBool('open_with_uses_derived_file_extensions'):
                    #Leo configured to use node's derived file's extension
                    if(atFileFound == False):
                        atFileFound = True #no need to look any more.
                        #Found first ancestor @file node in outline
                        atFileBase,atFileExt = g.os_path_splitext(hs)
                        if(p == c.p):
                            #node to edit is an @file, Move ext from hs to ext
                            hs = atFileBase
                        if atFileExt: #It has an extension
                            ext = atFileExt #use it

            #Remove unsupported directory & file name characters
            #if(os.name == "dos" or os.name == "nt"):
            if 1:
                hsClean = ""
                for ch in hs:
                    if ch in g.string.whitespace: #Convert tabs to spaces
                        hsClean += ' '
                    elif ch in ('\\','/',':','|','<','>','*', '"'): #Not allowed in Dos/Windows
                        hsClean += '_'
                    elif ch in ('"'): #Leo code can't handle the "
                        hsClean += '\''   #replace with '
                    else:
                        hsClean += ch
                #Windows directory and file names can't end with a period
                if hsClean.endswith( '.' ):
                    hsClean += '_'
            else:
                hsClean = g.sanitize_filename(hs)
            #Add node's headstring (filtered) to the list of ancestors
            ancestor.append(hsClean)
            p = p.parent()

        #Put temporary directory structure under <tempdir>\Leo<uniqueId> directory
        ancestor.append( "Leo" + str(id(v)))

        #Build temporary directory
        td = os.path.abspath(tempfile.gettempdir())
        #Loop through all of node's ancestors
        while len(ancestor) > 1:
            #Add each ancestor of node from nearest to farthest
            td = os.path.join(td, ancestor.pop()) #Add next subdirectory
            if not os.path.exists(td):
                os.mkdir(td)
        #Add filename with extension to the path (last entry in ancestor list)
        name = ancestor.pop() + ext
    else:
        #Use old method for unsupported operating systems
        try:
            leoTempDir = getpass.getuser() + "_" + "Leo"
        except:
            leoTempDir = "LeoTemp"
            g.es("Could not retrieve your user name.")
            g.es("Temporary files will be stored in: %s" % leoTempDir)
        td = os.path.join(os.path.abspath(tempfile.gettempdir()), leoTempDir)
        if not os.path.exists(td):
            os.mkdir(td)
        name = g.sanitize_filename(v.h) + '_' + str(id(v)) + ext

    path = os.path.join(td,name)

    return path