<s> from PyQt4 import QtGui , QtCore <EOL> from PyQt4 . QtCore import Qt <EOL> from bs4 import BeautifulSoup <EOL> import re <EOL> def currentHyperlink ( cursor ) : <EOL> cursor . select ( QtGui . QTextCursor . WordUnderCursor ) <EOL> soup = BeautifulSoup ( cursor . selection ( ) . toHtml ( ) ) <EOL> a = soup . a <EOL> return a . get ( "<STR_LIT>" ) if a else None <EOL> def openHyperlink ( hyperlink ) : <EOL> link = QtCore . QUrl ( ) . fromUserInput ( hyperlink ) <EOL> QtGui . QDesktopServices . openUrl ( link ) <EOL> def removeHyperlink ( cursor ) : <EOL> text = cursor . selectedText ( ) <EOL> cursor . insertText ( text , QtGui . QTextCharFormat ( ) ) <EOL> class Link ( QtGui . QDialog ) : <EOL> def __init__ ( self , parent = None , forEditing = False ) : <EOL> QtGui . QDialog . __init__ ( self , parent ) <EOL> self . parent = parent <EOL> self . cursor = self . parent . text . textCursor ( ) <EOL> self . forEditing = forEditing <EOL> self . initUI ( ) <EOL> def initUI ( self ) : <EOL> urlLabel = QtGui . QLabel ( "<STR_LIT>" , self ) <EOL> self . urlField = QtGui . QLineEdit ( self ) <EOL> textLabel = QtGui . QLabel ( "<STR_LIT>" , self ) <EOL> self . textField = QtGui . QLineEdit ( self ) <EOL> if self . forEditing : <EOL> self . urlField . setText ( currentHyperlink ( self . cursor ) ) <EOL> self . textField . setText ( self . cursor . selectedText ( ) ) <EOL> else : <EOL> self . urlField . setText ( self . cursor . selectedText ( ) ) <EOL> insertButton = QtGui . QPushButton ( "<STR_LIT>" , self ) <EOL> insertButton . clicked . connect ( self . insert ) <EOL> layout = QtGui . QGridLayout ( ) <EOL> layout . addWidget ( urlLabel , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> layout . addWidget ( self . urlField , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> layout . addWidget ( textLabel , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> layout . addWidget ( self . textField , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> layout . addWidget ( insertButton , <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> self . setLayout ( layout ) <EOL> self . setFixedSize ( <NUM_LIT> , <NUM_LIT> ) <EOL> self . move ( <NUM_LIT> , <NUM_LIT> ) <EOL> self . setWindowTitle ( "<STR_LIT>" ) <EOL> def insert ( self ) : <EOL> url = QtCore . QUrl ( ) . fromUserInput ( self . urlField . text ( ) ) <EOL> text = self . textField . text ( ) <EOL> link = "<STR_LIT>" . format ( url . toString ( ) , text ) <EOL> if self . forEditing : <EOL> self . cursor . select ( QtGui . QTextCursor . WordUnderCursor ) <EOL> self . cursor . removeSelectedText ( ) <EOL> self . cursor . insertHtml ( link ) <EOL> self . close ( ) </s>
