detect_urls.py :  » Development » Leo » Leo-4.7.1-final » leo » plugins » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Leo 
Leo » Leo 4.7.1 final » leo » plugins » detect_urls.py
#@+leo-ver=4-thin
#@+node:ekr.20060506070443.1:@thin detect_urls.py
#@<< docstring >>
#@+node:vpe.20060426084738:<< docstring >>
"""
Colorizes URLs everywhere in node's body on node selection or saving.

Double click on any URL launches it in default browser.

URL regex:  (http|https|file|ftp)://[^\s'"]+[\w=/]

Related plugins:  color_markup.py; rClick.py
"""
#@-node:vpe.20060426084738:<< docstring >>
#@nl
#@@language python
#@@tabwidth -4

__version__ = "0.3"
#@<< version history >>
#@+node:RV20090910.20100110154407.7439:<< version history >>
#@+at
# 
# Originally written by ???
# 
# 0.1 ???: Initial version.
# 
# 0.2 VR: Detect URLs with protocol type 'file' and detect a new URL also
#         after a save operation.
# 
# 0.3 VR: Display info about this plugin, when executing cmd 
# 'print-plugins-info'.
#@-at
#@-node:RV20090910.20100110154407.7439:<< version history >>
#@nl
#@<< imports >>
#@+node:RV20090910.20100110154407.7437:<< imports >>
import leo.core.leoGlobals as g
import leo.core.leoPlugins as leoPlugins
import re
#@-node:RV20090910.20100110154407.7437:<< imports >>
#@nl

url_regex = re.compile(r"""(http|https|file|ftp)://[^\s'"]+[\w=/]""")

#@+others
#@+node:RV20090910.20100110154407.7438:init()
def init():
    ok = not g.app.unitTesting
    if ok:
        leoPlugins.registerHandler("bodydclick1", openURL)
        leoPlugins.registerHandler("select2", colorizeURLs)
        leoPlugins.registerHandler("save2", colorizeURLs)
        g.plugin_signon(__name__)
    return ok
#@-node:RV20090910.20100110154407.7438:init()
#@+node:vpe.20060305064323.5:openURL()
def openURL(tag,keywords):
    c = keywords.get("c")

    w = c.frame.body.bodyCtrl
    s = w.getAllText()
    ins = w.getInsertPoint()
    row,col = g.convertPythonIndexToRowCol(s,ins)
    i,j = g.getLine(s,ins)
    line = s[i:j]
    # g.trace(repr(line))

    for match in url_regex.finditer(line):
        if match.start() <= col <= match.end():
            url = match.group()
            if 0: # I have no idea why this code was present.
                start,end = match.start(), match.end()
                c.frame.body.setSelectionRange("%s.%s" %(row,start), "%s.%s" %(row,end))
                w.setSelectionRange(start,end)
            if not g.app.unitTesting:
                try:
                    import webbrowser
                    webbrowser.open(url)
                except:
                    g.es("exception opening " + url)
                    g.es_exception()
            return url # force to skip word selection if url found
#@-node:vpe.20060305064323.5:openURL()
#@+node:vpe.20060426062042:colorizeURLs()
def colorizeURLs(tag,keywords):
    c = keywords.get("c")
    if not c: return

    c.frame.body.tag_configure("URL", underline=1, foreground="blue")
    w = c.frame.body.bodyCtrl
    s = w.getAllText()
    lines = s.split('\n')
    n = 0 # The number of characters before the present line.
    for line in lines:
        for match in url_regex.finditer(line):
            start, end = match.start(), match.end()
            i,j = w.toGuiIndex(n+start),w.toGuiIndex(n+end)
            c.frame.body.tag_add('URL',i,j)
        n += len(line) + 1
#@-node:vpe.20060426062042:colorizeURLs()
#@-others
#@-node:ekr.20060506070443.1:@thin detect_urls.py
#@-leo
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.