#
# markdown replacement function originally found here:
# 	https://gist.github.com/sma/1513929
#
# note:
# 	only works on a subset of markdown.  if you're writing
#	links and images in markdown instead of bbcode, you're
#	probably doing it wrong.  formatting, and lists tho?
#	winner, winner, chicken dinner!
#

import sublime, sublime_plugin
import re, sys

class MarkdowntobbcodeCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		allcontent = sublime.Region(0, self.view.size())
		
		regionString = self.markdown_to_bbcode(str(self.view.substr(allcontent)))
		self.view.replace(edit, allcontent, regionString)

	def markdown_to_bbcode(self,s):
		links = {}
		codes = []
		def gather_link(m):
			links[m.group(1)]=m.group(2); return ""
		def replace_link(m):
			return "[url=%s]%s[/url]" % (links[m.group(2) or m.group(1)], m.group(1))
		def gather_code(m):
			codes.append(m.group(3)); return "[code=%d]" % len(codes)
		def replace_code(m):
			return "%s" % codes[int(m.group(1)) - 1]
		
		def translate(p="%s", g=1):
			def inline(m):
				s = m.group(g)
				s = re.sub(r"(`+)(\s*)(.*?)\2\1", gather_code, s)
				#s = re.sub(r"\[(.*?)\]\[(.*?)\]", replace_link, s) # not sure why this hates links.  *shrug*
				s = re.sub(r"\[(.*?)\]\((.*?)\)", "[url=\\2]\\1[/url]", s)
				s = re.sub(r"<(https?:\S+)>", "[url=\\1]\\1[/url]", s)
				s = re.sub(r"\B([*_]{2})\b(.+?)\1\B", "[b]\\2[/b]", s)
				s = re.sub(r"_([^_]+)_", "[i]\\1[/i]", s)
				s = re.sub(r"\*([^\*]+)\*", "[b]\\1[/b]", s)
				return p % s
			return inline
		
		s = re.sub(r"(?m)^\[(.*?)]:\s*(\S+).*$", gather_link, s)
		s = re.sub(r"(?m)^    (.*)$", "~[code]\\1[/code]", s)
		s = re.sub(r"(?m)^(\S.*)\n=+\s*$", translate("~[size=20pt][b]%s[/b][/size]"), s)
		s = re.sub(r"(?m)^(\S.*)\n-+\s*$", translate("~[size=14pt][b]%s[/b][/size]"), s)
		s = re.sub(r"(?m)^#\s+(.*?)\s*#*$", translate("~[size=20pt][b]%s[/b][/size]"), s)
		s = re.sub(r"(?m)^##\s+(.*?)\s*#*$", translate("~[size=14pt][b]%s[/b][/size]"), s)
		s = re.sub(r"(?m)^###\s+(.*?)\s*#*$", translate("~[b]%s[/b]"), s)
		s = re.sub(r"(?m)^> (.*)$", translate("~[quote]%s[/quote]"), s)
		s = re.sub(r"(?m)^[-+*]\s+(.*)$", translate("~[list]\n[*]%s\n[/list]"), s)
		s = re.sub(r"(?m)^\d+\.\s+(.*)$", translate("~[list=1]\n[*]%s\n[/list]"), s)
		s = re.sub(r"(?m)^((?!~).*)$", translate(), s)
		s = re.sub(r"(?m)^~\[", "[", s)
		s = re.sub(r"\[/code]\n\[code(=.*?)?]", "\n", s)
		s = re.sub(r"\[/quote]\n\[quote]", "\n", s)
		s = re.sub(r"\[/list]\n\[list(=1)?]\n", "", s)
		s = re.sub(r"(?m)\[code=(\d+)]", replace_code, s)
		
		return s
