Skip to content

Commit d0f9446

Browse files
committed
move escape to utils
1 parent 603e91e commit d0f9446

File tree

2 files changed

+60
-55
lines changed

2 files changed

+60
-55
lines changed

adminifier/utils/escape.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package utils
2+
3+
import "strings"
4+
5+
// Esc escapes a string for use in a wikifier template
6+
func Esc(s string) string {
7+
8+
// escape existing escapes
9+
s = strings.Replace(s, "\\", "\\\\", -1)
10+
11+
// ecape curly brackets
12+
s = strings.Replace(s, "{", "\\{", -1)
13+
s = strings.Replace(s, "}", "\\}", -1)
14+
15+
// fix comments (see wikifier#62)
16+
s = strings.Replace(s, "/*", "\\/*", -1)
17+
18+
return s
19+
}
20+
21+
// EscFmt escapes a string for use in wikifier formatted text
22+
func EscFmt(s string) string {
23+
s = Esc(s)
24+
s = strings.Replace(s, "[", "\\[", -1)
25+
s = strings.Replace(s, "]", "\\]", -1)
26+
return s
27+
}
28+
29+
// EscLink is like EscFmt except also escapes pipe for [[ links ]]
30+
func EscLink(s string) string {
31+
s = EscFmt(s)
32+
return strings.Replace(s, "|", "\\|", -1)
33+
}
34+
35+
// EscListMapValue is like EscFmt except also escapes semicolon
36+
func EscListMapValue(s string) string {
37+
s = EscFmt(s)
38+
return strings.Replace(s, ";", "\\;", -1)
39+
}
40+
41+
// EscMapKey is like EscFmt except also escapes colon and semicolon
42+
func EscMapKey(s string) string {
43+
s = EscListMapValue(s)
44+
return strings.Replace(s, ":", "\\:", -1)
45+
}

markdown/quiki.go

+15-55
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"regexp"
99
"strings"
1010

11+
"github.com/cooper/quiki/adminifier/utils"
1112
"github.com/russross/blackfriday/v2"
1213
)
1314

@@ -277,16 +278,16 @@ func (r *QuikiRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering
277278
case blackfriday.Text:
278279
s := string(node.Literal)
279280
if node.Parent.Type == blackfriday.Link {
280-
r.addText(w, quikiEscLink(s))
281+
r.addText(w, utils.EscLink(s))
281282
} else if node.Parent.Type == blackfriday.Paragraph && node.Parent.Parent.Type == blackfriday.Item {
282-
r.addText(w, quikiEscListMapValue(s))
283+
r.addText(w, utils.EscListMapValue(s))
283284
} else if node.Parent.Type == blackfriday.Item {
284-
r.addText(w, quikiEscListMapValue(s))
285+
r.addText(w, utils.EscListMapValue(s))
285286
} else if node.Parent.Type == blackfriday.Heading {
286287
r.heading += s
287-
r.addText(w, quikiEscFmt(s))
288+
r.addText(w, utils.EscFmt(s))
288289
} else {
289-
r.addText(w, quikiEscFmt(s))
290+
r.addText(w, utils.EscFmt(s))
290291
}
291292

292293
// newline
@@ -328,7 +329,7 @@ func (r *QuikiRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering
328329
if r.Flags&SkipHTML != 0 {
329330
break
330331
}
331-
html := quikiEscFmt(string(node.Literal))
332+
html := utils.EscFmt(string(node.Literal))
332333
r.addText(w, "[html:"+html+"]")
333334

334335
// link
@@ -344,7 +345,7 @@ func (r *QuikiRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering
344345
} else {
345346
if entering {
346347
link := string(r.addAbsPrefix(dest))
347-
link = quikiEscLink(link)
348+
link = utils.EscLink(link)
348349
if hashIdx := strings.IndexByte(link, '#'); hashIdx != -1 {
349350
r.linkDest = strings.TrimSuffix(link[:hashIdx], ".md") + link[hashIdx:]
350351
} else {
@@ -374,7 +375,7 @@ func (r *QuikiRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering
374375
dest := node.LinkData.Destination
375376
dest = r.addAbsPrefix(dest)
376377
// FIXME: if dest is not relative, we can't display this image
377-
r.addText(w, "~image {\n file: "+quikiEsc(string(dest))+";\n alt: ")
378+
r.addText(w, "~image {\n file: "+utils.Esc(string(dest))+";\n alt: ")
378379
} else {
379380
// FIXME: can we do anything with node.LinkData.Title?
380381
r.out(w, []byte(";\n}"))
@@ -385,9 +386,9 @@ func (r *QuikiRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering
385386
var code string
386387
if node.Parent != nil && node.Parent.Parent != nil &&
387388
node.Parent.Type == blackfriday.Paragraph && node.Parent.Parent.Type == blackfriday.Item {
388-
code = quikiEscListMapValue(string(node.Literal))
389+
code = utils.EscListMapValue(string(node.Literal))
389390
} else {
390-
code = quikiEscFmt(string(node.Literal))
391+
code = utils.EscFmt(string(node.Literal))
391392
}
392393
r.addText(w, "[c]"+code+"[/c]")
393394

@@ -448,7 +449,7 @@ func (r *QuikiRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering
448449
r.cr(w)
449450
r.addText(w, "~html {\n")
450451
r.indent++
451-
r.addText(w, quikiEsc(string(node.Literal)))
452+
r.addText(w, utils.Esc(string(node.Literal)))
452453
r.indent--
453454
r.addText(w, "\n}")
454455
r.cr(w)
@@ -581,14 +582,14 @@ func (r *QuikiRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering
581582
r.cr(w)
582583

583584
// TODO: count opening and closing brackets.
584-
// if they match, use brace-escape rather than quikiEsc()
585+
// if they match, use brace-escape rather than utils.Esc()
585586
r.addText(w, "~code ")
586587
if lang := codeLanguage(node.Info); lang != "" {
587588
r.addText(w, "["+lang+"] ")
588589
}
589590
r.addText(w, "{\n")
590591
r.indent++
591-
r.addText(w, quikiEsc(string(node.Literal)))
592+
r.addText(w, utils.Esc(string(node.Literal)))
592593
r.indent--
593594
r.addText(w, "}")
594595

@@ -677,7 +678,7 @@ func (r *QuikiRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering
677678
func (r *QuikiRenderer) RenderFooter(w io.Writer, ast *blackfriday.Node) {
678679
// title must be done after the heading is extracted
679680
if r.Title != "" {
680-
io.WriteString(w, "\n@page.title: "+quikiEscFmt(r.Title)+";\n")
681+
io.WriteString(w, "\n@page.title: "+utils.EscFmt(r.Title)+";\n")
681682
}
682683
}
683684

@@ -693,44 +694,3 @@ func (r *QuikiRenderer) RenderHeader(w io.Writer, ast *blackfriday.Node) {
693694
io.WriteString(w, "toc{}\n\n")
694695
}
695696
}
696-
697-
func quikiEsc(s string) string {
698-
699-
// escape existing escapes
700-
s = strings.Replace(s, "\\", "\\\\", -1)
701-
702-
// ecape curly brackets
703-
s = strings.Replace(s, "{", "\\{", -1)
704-
s = strings.Replace(s, "}", "\\}", -1)
705-
706-
// fix comments (see wikifier#62)
707-
s = strings.Replace(s, "/*", "\\/*", -1)
708-
709-
return s
710-
}
711-
712-
// like quikiEsc except also escapes formatting tags
713-
func quikiEscFmt(s string) string {
714-
s = quikiEsc(s)
715-
s = strings.Replace(s, "[", "\\[", -1)
716-
s = strings.Replace(s, "]", "\\]", -1)
717-
return s
718-
}
719-
720-
// like quikiEscFmt except also escapes pipe for [[ links ]]
721-
func quikiEscLink(s string) string {
722-
s = quikiEscFmt(s)
723-
return strings.Replace(s, "|", "\\|", -1)
724-
}
725-
726-
// like quikiEscFmt except also escapes semicolon
727-
func quikiEscListMapValue(s string) string {
728-
s = quikiEscFmt(s)
729-
return strings.Replace(s, ";", "\\;", -1)
730-
}
731-
732-
// like quikiEscFmt except also escapes colon and semicolon
733-
func quikiEscMapKey(s string) string {
734-
s = quikiEscListMapValue(s)
735-
return strings.Replace(s, ":", "\\:", -1)
736-
}

0 commit comments

Comments
 (0)