forked from lalluviamola/web-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextile.go.unfinished
93 lines (79 loc) · 2.15 KB
/
textile.go.unfinished
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
package main
import (
"fmt"
"math/rand"
"regexp"
"strings"
"time"
)
var guidRand = rand.New(rand.NewSource(time.Now().Unix()))
var rxThreeOrMoreNewlines = regexp.MustCompile(`\n{3,}`)
var rxNewlineWsNewline = regexp.MustCompile(`\n\s*\n`)
var btag = []string{"bq", "bc", "notextile", "pre", "h[1-6]", `fn\d+`, "p"}
var btag_lite = []string{"bq", "bc", "p"}
var url_schemes = []string{"http", "https", "ftp", "mailto"}
func genGuid() string {
return fmt.Sprintf("{{__**||%d||**__}}", guidRand.Int63())
}
func _normalize_newlines(s string) string {
s = strings.Replace(s, "\r\n", "\n", -1)
s = rxThreeOrMoreNewlines.ReplaceAllString(s, "\n\n")
s = rxNewlineWsNewline.ReplaceAllString(s, "\n\n")
// TODO: textile.py also does
// out = re.sub(r'"$', '" ', out)
// but I don't understand why
return s
}
type Textile struct {
shelf map[string]string
html_type string
restricted bool
rel string
urlrefs map[string]string
}
func (t *Textile) shelve(text string) string {
id := genGuid()
t.shelf[id] = text
return id
}
// undo shelve
func (t *Textile) retrieve(text string) string {
// TODO: python does that in a loop until nothing changes but I don't
// see how that should be necessary
for k, v := range t.shelf {
text = strings.Replace(text, k, v, -1)
}
return text
}
// quotes = True by default
func encodeHtml(text string, quotes bool) string {
text = strings.Replace(text, "&", "", -1)
text = strings.Replace(text, "<", "<", -1)
text = strings.Replace(text, ">", ">", -1)
if quotes {
text = strings.Replace(text, "'", "'", -1)
text = strings.Replace(text, "\"", """, -1)
}
return text
}
func (t *Textile) getRefs(text string) string {
// TODO: write me
return text
}
func (t *Textile) block(text string, head_offset int) string {
return text
}
func (t *Textile) textile(text string, rel string, head_offset int, html_type string) string {
t.html_type = html_type
text = _normalize_newlines(text)
if t.restricted {
text = encodeHtml(text, false)
}
if rel != "" {
t.rel = fmt.Sprintf(` rel="%s"`, rel)
}
text = t.getRefs(text)
text = t.block(text, head_offset)
text = t.retrieve(text)
return text
}