-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathj2m.go
More file actions
executable file
·153 lines (145 loc) · 3.89 KB
/
j2m.go
File metadata and controls
executable file
·153 lines (145 loc) · 3.89 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package j2m
import (
"fmt"
"regexp"
"strconv"
"strings"
)
type jiration struct {
re *regexp.Regexp
repl interface{}
}
// JiraToMD takes a string in Jira Markdown, and outputs Github Markdown
func JiraToMD(str string) string {
jirations := []jiration{
{ // UnOrdered Lists
re: regexp.MustCompile(`(?m)^[ \t]*(\*+)\s+`),
repl: func(groups []string) string {
_, stars := groups[0], groups[1]
return strings.Repeat(" ", len(stars)-1) + "* "
},
},
{ //Ordered Lists
re: regexp.MustCompile(`(?m)^[ \t]*(#+)\s+`),
repl: func(groups []string) string {
_, nums := groups[0], groups[1]
return strings.Repeat(" ", len(nums)-1) + "1. "
},
},
{ //Headers 1-6
re: regexp.MustCompile(`(?m)^h([0-6])\.(.*)$`),
repl: func(groups []string) string {
_, level, content := groups[0], groups[1], groups[2]
i, _ := strconv.Atoi(level)
return strings.Repeat("#", i) + content
},
},
{ // Bold
re: regexp.MustCompile(`\*(\S[^*]*)\*`),
repl: "**$1**",
},
{ // Italic
re: regexp.MustCompile(`\b\_(\S[^_]*)\_`),
repl: "*$1*",
},
{ // Monospaced text
re: regexp.MustCompile(`\{\{([^}]+)\}\}`),
repl: "`$1`",
},
{ // Citations (buggy)
re: regexp.MustCompile(`\?\?((?:.[^?]|[^?].)+)\?\?`),
repl: "<cite>$1</cite>",
},
{ // Inserts
re: regexp.MustCompile(`\+([^+]*)\+`),
repl: "<ins>$1</ins>",
},
{ // Superscript
re: regexp.MustCompile(`\^([^^]*)\^`),
repl: "<sup>$1</sup>",
},
{ // Subscript
re: regexp.MustCompile(`~([^~]*)~`),
repl: "<sub>$1</sub>",
},
{ // Strikethrough
re: regexp.MustCompile(`(\s+)-(\S+.*?\S)-(\s+)`),
repl: "$1~~$2~~$3",
},
{ // Code Block
re: regexp.MustCompile(`\{code(:([a-z]+))?([:|]?(title|borderStyle|borderColor|borderWidth|bgColor|titleBGColor)=.+?)*\}`),
repl: "```$2",
},
{ // Code Block End
re: regexp.MustCompile(`{code}`),
repl: "```",
},
{ // Pre-formatted text
re: regexp.MustCompile(`{noformat}`),
repl: "```",
},
{ // Un-named Links
re: regexp.MustCompile(`(?U)\[([^|]+?)\]`),
repl: "<$1>",
},
{ // Images
re: regexp.MustCompile(`!(.+)!`),
repl: "",
},
{ // Named Links
re: regexp.MustCompile(`\[(.+?)\|(.+?)\]`),
repl: "[$1]($2)",
},
{ // Single Paragraph Blockquote
re: regexp.MustCompile(`(?m)^bq\.\s+`),
repl: "> ",
},
{ // Remove color: unsupported in md
re: regexp.MustCompile(`(?m)\{color:[^}]+\}(.*)\{color\}`),
repl: "$1",
},
{ // panel into table
re: regexp.MustCompile(`(?m)\{panel:title=([^}]*)\}\n?(.*?)\n?\{panel\}`),
repl: "\n| $1 |\n| --- |\n| $2 |",
},
{ //table header
re: regexp.MustCompile(`(?m)^[ \t]*((?:\|\|.*?)+\|\|)[ \t]*$`),
repl: func(groups []string) string {
_, headers := groups[0], groups[1]
reBarred := regexp.MustCompile(`\|\|`)
singleBarred := reBarred.ReplaceAllString(headers, "|")
fillerRe := regexp.MustCompile(`\|[^|]+`)
return "\n" + singleBarred + "\n" + fillerRe.ReplaceAllString(singleBarred, "| --- ")
},
},
{ // remove leading-space of table headers and rows
re: regexp.MustCompile(`(?m)^[ \t]*\|`),
repl: "|",
},
}
for _, jiration := range jirations {
switch v := jiration.repl.(type) {
case string:
str = jiration.re.ReplaceAllString(str, v)
case func([]string) string:
str = replaceAllStringSubmatchFunc(jiration.re, str, v)
default:
fmt.Printf("I don't know about type %T!\n", v)
}
}
return str
}
// https://gist.github.com/elliotchance/d419395aa776d632d897
func replaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]string) string) string {
result := ""
lastIndex := 0
for _, v := range re.FindAllSubmatchIndex([]byte(str), -1) {
groups := []string{}
for i := 0; i < len(v); i += 2 {
groups = append(groups, str[v[i]:v[i+1]])
}
result += str[lastIndex:v[0]] + repl(groups)
lastIndex = v[1]
}
return result + str[lastIndex:]
}