grim/youtrack-import
Clone
Summary
Browse
Changes
Graph
Convert creole links and code to markdown. Fixes YI-24
2020-01-19, Gary Kramlich
608238fe4050
Convert creole links and code to markdown. Fixes YI-24
package
bitbucket
import
(
"fmt"
"regexp"
"strings"
"hg.sr.ht/~grim/youtrack-import/youtrack"
)
var
(
creoleLinkRegex
=
regexp
.
MustCompile
(
`\[\[(.+?)\|(.+?)\]\]`
)
creoleCodeRegex
=
regexp
.
MustCompile
(
"```"
+
`\r\n#!((.*?)\r\n)`
)
)
func
replaceCreoleLinks
(
message
string
)
string
{
output
:=
message
matches
:=
creoleLinkRegex
.
FindAllStringSubmatch
(
message
,
-
1
)
for
_
,
match
:=
range
matches
{
old
:=
match
[
0
]
uri
:=
match
[
1
]
text
:=
match
[
2
]
replacement
:=
fmt
.
Sprintf
(
"[%s](%s)"
,
text
,
uri
)
output
=
strings
.
Replace
(
output
,
old
,
replacement
,
1
)
}
return
output
}
func
replaceCreoleCode
(
message
string
)
string
{
output
:=
message
matched
:=
false
matches
:=
creoleCodeRegex
.
FindAllStringSubmatch
(
message
,
-
1
)
for
_
,
match
:=
range
matches
{
old
:=
match
[
0
]
lang
:=
match
[
2
]
matched
=
true
replacement
:=
fmt
.
Sprintf
(
"```%s\n"
,
lang
)
output
=
strings
.
Replace
(
output
,
old
,
replacement
,
1
)
}
if
matched
{
fmt
.
Printf
(
"output: %q\n"
,
output
)
}
return
output
}
func
replaceCreole
(
m
map
[
string
]
*
youtrack
.
User
,
message
string
)
string
{
output
:=
message
output
=
replaceCreoleLinks
(
output
)
output
=
replaceCreoleCode
(
output
)
return
output
}