import requests
from   pyquery import PyQuery as pq

def normalStyle(types, names, escape = False):
	padTo = 0
	for t in types:
		# Find out which has the longest
		tempLen = len(t)
		if tempLen > padTo:
			padTo = tempLen
	if escape:
		partdown = '\`\`\`\n'
	else:
		partdown = '```\n'
	for i in range(0, len(types)):
		partdown += types[i].rjust(padTo, ' ') + ' : ' + names[i] + '\n'
	if escape:
		partdown += '\`\`\`'
	else:
		partdown += '```'
	return partdown

def mdStyle(types, names, escape = False):
	padTo = 0
	for t in types:
		# Find out which has the longest
		ty = "<" + t.replace(' ', '-') + ":"
		tempLen = len(ty)
		if tempLen > padTo:
			padTo = tempLen
	if escape:
		partdown = '\`\`\`md\n'
	else:
		partdown = '```md\n'
	for i in range(0, len(types)):
		t = "<" + types[i].replace(' ', '-') + ":"
		partdown += t.ljust(padTo, ' ') + " " + names[i] + '>\n'
	if escape:
		partdown += '\`\`\`'
	else:
		partdown += '```'
	return partdown

def mdBlockStyle(types, names, escape = False):
	padTo = 0
	for t in types:
		# Find out which has the longest
		ty = "[" + t
		tempLen = len(ty)
		if tempLen > padTo:
			padTo = tempLen
	if escape:
		partdown = '\`\`\`md\n'
	else:
		partdown = '```md\n'
	for i in range(0, len(types)):
		ty = "[" + types[i]
		t = "| " + ty.rjust(padTo, ' ') + "]["
		partdown += t.rjust(padTo, ' ') + names[i] + ']\n'
	if escape:
		partdown += '\`\`\`'
	else:
		partdown += '```'
	return partdown

def boldStyle(types, names, escape = False):
	partdown = ''
	for i in range(0, len(types)):
		if escape:
			partdown += "\*\*" + types[i] + ":\*\* " + names[i] + '\n'
		else:
			partdown += "**" + types[i] + ":** " + names[i] + '\n'
	partdown = partdown[:-1]
	return partdown

def boldItalicStyle(types, names, escape = False):
	partdown = ''
	for i in range(0, len(types)):
		if escape:
			partdown += "\*\*\*" + types[i] + ":\*\*\* \*" + names[i] + '\*\n'
		else:
			partdown += "***" + types[i] + ":*** *" + names[i] + '*\n'
	partdown = partdown[:-1]
	return partdown
		
def getMarkdown( url, style = None, escape = False):
	# Ensure we're using a list
	url = url.replace('/b/', '/list/')
	if not style:
		style = 'normal'
	try:
		response = requests.get(url)
	except Exception:
		return None
	dom = pq(response.text)
	
	# Experimental crap because developing while not at home
	table = dom('table.manual-zebra').children('tbody').children()
	for child in table:
		if len(child.children()) > 2:
			# We should have enough to get our info
			type = child.children().eq(0)
			name = child.children().eq(2)
			print("{}: {}".format(type, name))
		#type = children[0].text_content().strip().replace('\r', '').replace('\n', ' ').replace('\t', ' ')
		#name = children[2].text_content().strip().replace('\r', '').replace('\n', ' ').replace('\t', ' ')
		#print('Type: "{}"\nName: "{}"'.format(type, name))
	
	names = []
	types = []
	for e in dom('td.component-type.tl'):
		text = e.text_content().strip()
		text = text.replace('\r', '').replace('\n', ' ').replace('\t', ' ')
		text = ' '.join(text.split())
		if text == "":
			if not len(types):
				# Nothing yet - this is weird
				text = '-'
			else:
				text = types[len(types)-1]
		types.append(text)
	for e in dom('td.component-name.tl'):
		text = e.text_content().strip()
		text = text.replace('\r', '').replace('\n', ' ').replace('\t', ' ')
		text = ' '.join(text.split())
		names.append(text)
	if not len(types):
		return None
	if not len(types) == len(names):
		# Only way this would happen that I've seen so far, is with custom entries
		return 'custom'
	partout = ''
	if style.lower() == 'md':
		partout = mdStyle(types, names, escape)
	elif style.lower() == 'mdblock':
		partout = mdBlockStyle(types, names, escape)
	elif style.lower() == 'bold':
		partout = boldStyle(types, names, escape)
	elif style.lower() == 'bolditalic':
		partout = boldItalicStyle(types, names, escape)
	else:
		partout = normalStyle(types, names, escape)
	return partout

@@ -2404,31 +2404,98 @@
 :%0A%09%09
-if len(child.children()
+try:%0A%09%09%09children = child.children()%0A%09%09except AttributeError:%0A%09%09%09continue%0A%09%09if len(children
 ) %3E 

Update PCPP.py