-
Notifications
You must be signed in to change notification settings - Fork 1
/
MarkdownToBBCode.py
79 lines (71 loc) · 2.63 KB
/
MarkdownToBBCode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#
# Base on codes:
# https://gist.github.com/sma/1513929
# https://github.com/m3mnoch/MarkdownToBBCode/blob/master/MarkdownToBBCode.py
#
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):
def translate(p="%s", g=1):
def inline(m):
s = m.group(g)
#
# Headers
#
s = re.sub(r"^#\s+(.*?)\s*$", "[h1]\\1[/h1]", s) # # Header first level
s = re.sub(r"^##\s+(.*?)\s*$", "[h2]\\1[/h2]", s) # ## Header second level
s = re.sub(r"^###\s+(.*?)\s*$", "[h3]\\1[/h3]", s) # ### Header third level
s = re.sub(r"^####\s+(.*?)\s*$", "[h4]\\1[/h4]", s)# #### Header fourth level
#
# Lists
#
s = re.sub(r"(?m)^[-+*]\s+(.*)$", translate("№[list]\n[*]%s\n[/list]"), s) # + Marked + List
s = re.sub(r"(?m)^\d+\.\s+(.*)$", translate("№[list=1]\n[*]%s\n[/list]"), s) # 1. Numbered 2. List
#
# Quote
#
s = re.sub(r"^> (.*)$", "[quote]\\1[/quote]", s) # > Quote
#
# Thematic break
#
s = re.sub(r"^-{3}(\s*)$", "[hr]", s)
return p % s
return inline
#
# URL and images
#
s = re.sub(r"!\[(.*?)\]\((.*?)\)", "[img]\\2[/img]", s) # ![IMG description](URL of image), alt attribute not supported in many forums
s = re.sub(r"\[(.*?)\]\((.*?)\)", "[url=\\2]\\1[/url]", s)# [URL description](URL link)
s = re.sub(r"<(https?:\S+)>", "[url]\\1[/url]", s) # <URL>
#
# Code
#
s = re.sub(r"`{3}([^`]+)`{3}", "[code]\\1[/code]", s)# ```Multiline\n\code```
s = re.sub(r"`([^`]+)`", "[code]\\1[/code]", s) # `Code`
s = re.sub(r"(?m)^ {4}(.*)$", "№[code]\\1[/code]", s)# Code fragment after 4 spaces
s = re.sub(r"(?m)^\t(.*)$", "№[code]\\1[/code]", s) # Code fragment after tab
#
# Bold and italic
#
s = re.sub(r"_{2}([\s\S]+?)_{2}", "[b]\\1[/b]", s) # __Bold__
s = re.sub(r"_([^_]+?)_", "[i]\\1[/i]", s) # _Italic_ Needs hack (?<=\s), because _ symbol often use in URLs
s = re.sub(r"\*{2}([\s\S]+?)\*{2}", "[b]\\1[/b]", s)# **Bold**
s = re.sub(r"\*([^\*]+?)\*", "[i]\\1[/i]", s) # *Italic*.
#
# Strikethrough text
#
s = re.sub(r"~{2}([\s\S]+?)~{2}", "[s]\\1[\s]", s)
#
# Dependencies. Not delete these lines!
#
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"\[/list]\n\[list(=1)?]\n", "", s)
s = re.sub(r"\[/quote]\n\[quote]", "\n", s)
return s