Difference between revisions of "User:WindBOT/LinkClass"

From Team Fortress Wiki
Jump to: navigation, search
m
m
Line 60: Line 60:
 
def __unicode__(self):
 
def __unicode__(self):
 
if self.getType() == u'internal':
 
if self.getType() == u'internal':
if self.getLabel() in (self.getLink().replace(u'_', u' '), self.getLink()):
+
label = self.getLabel()
return u'[[' + self.getLabel() + u']]'
+
tmpLink = self.getLink()
return u'[[' + self.getLink() + u'|' + self.getLabel() + u']]'
+
tmpLink2 = tmpLink.replace(u'_', u' ')
 +
if label in (tmpLink2, tmpLink):
 +
return u'[[' + label + u']]'
 +
elif label and tmpLink and (label[0].lower() == tmpLink[0].lower() and tmpLink[1:] == label[1:]) or (label[0].lower() == tmpLink2[0].lower() and tmpLink2[1:] == label[1:]):
 +
return u'[[' + label + u']]'
 +
elif tmpLink and label and len(label) > len(tmpLink) and (label.lower().find(tmpLink2.lower()) != -1 or label.lower().find(tmpLink.lower()) != -1):
 +
index = max(label.lower().find(tmpLink2.lower()), label.lower().find(tmpLink.lower()))
 +
if label[:index].find(u' ') == -1 and label[index+len(tmpLink):].find(u' ') == -1:
 +
return label[:index] + u(link(u'[[' + tmpLink + u'|' + label[index:index+len(tmpLink)] + u']]')) + label[index+len(tmpLink):]
 +
return u'[[' + tmpLink + u'|' + label + u']]'
 
if self.getType() == u'external':
 
if self.getType() == u'external':
if self.getLabel() is None:
+
if label is None:
return u'[' + self.getLink() + u']'
+
return u'[' + tmpLink + u']'
return u'[' + self.getLink() + u' ' + self.getLabel() + u']'
+
return u'[' + tmpLink + u' ' + label + u']'
 
return self.getBody()</pre>
 
return self.getBody()</pre>

Revision as of 20:43, 27 August 2010

class link:
	def __init__(self, content):
		content = u(content)
		self.setBody(content)
		self.setType(u'unknown')
		self.joined = False
		if len(content) > 2:
			if content[:2] == u'[[' and content[-2:] == u']]':
				split = content[2:-2].split(u'|')
				if len(split) in (1, 2):
					self.setType(u'internal')
					lnk = split[0]
					if lnk.find(u':') == -1:
						lnk = lnk.replace(u'_', u' ')
					self.setLink(lnk)
					if len(split) == 2:
						self.setLabel(split[1])
					else:
						self.setLabel(split[0])
						self.joined = True
			elif content[0] == u'[' and content[-1] == u']':
				split = content[1:-1].split(u' ', 1)
				self.setType(u'external')
				self.setLink(split[0])
				if len(split) == 2:
					self.setLabel(split[1])
				else:
					self.setLabel(None)
	def getType(self):
		return u(self.kind)
	def getBody(self):
		return u(self.body)
	def getLink(self):
		return u(self.link)
	def getLabel(self):
		if self.label is None:
			return None
		if self.joined:
			return self.getLink()
		return u(self.label)
	def setType(self, kind):
		self.kind = u(kind)
	def setBody(self, body):
		self.body = u(body)
	def setLink(self, link):
		self.link = u(link)
		if self.joined:
			self.label = u(link)
	def setLabel(self, label):
		if label is None:
			self.label = None
		else:
			self.label = u(label)
		if self.joined:
			self.link = u(label)
	def __str__(self):
		return self.__unicode__()
	def __repr__(self):
		return self.__unicode__().__repr__()
	def __unicode__(self):
		if self.getType() == u'internal':
			label = self.getLabel()
			tmpLink = self.getLink()
			tmpLink2 = tmpLink.replace(u'_', u' ')
			if label in (tmpLink2, tmpLink):
				return u'[[' + label + u']]'
			elif label and tmpLink and (label[0].lower() == tmpLink[0].lower() and tmpLink[1:] == label[1:]) or (label[0].lower() == tmpLink2[0].lower() and tmpLink2[1:] == label[1:]):
				return u'[[' + label + u']]'
			elif tmpLink and label and len(label) > len(tmpLink) and (label.lower().find(tmpLink2.lower()) != -1 or label.lower().find(tmpLink.lower()) != -1):
				index = max(label.lower().find(tmpLink2.lower()), label.lower().find(tmpLink.lower()))
				if label[:index].find(u' ') == -1 and label[index+len(tmpLink):].find(u' ') == -1:
					return label[:index] + u(link(u'[[' + tmpLink + u'|' + label[index:index+len(tmpLink)] + u']]')) + label[index+len(tmpLink):]
			return u'[[' + tmpLink + u'|' + label + u']]'
		if self.getType() == u'external':
			if label is None:
				return u'[' + tmpLink + u']'
			return u'[' + tmpLink + u' ' + label + u']'
		return self.getBody()