blob: 7170bd89ec0ca4e273d2c6ae9ce776fd71bc11ef (
plain)
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
|
package main
import (
"bufio"
"fmt"
"strings"
)
type Markup []byte
type LineType int8
const (
TextLine LineType = iota
LinkLine
HeadingLine
UnorderedListItem
)
func getLineType(line string) LineType {
if strings.HasPrefix(line, "=>") {
return LinkLine
}
if strings.HasPrefix(line, "#") {
return HeadingLine
}
if strings.HasPrefix(line, "* ") {
return UnorderedListItem
}
return TextLine
}
func (m Markup) title() string {
text := string(m)
scanner := bufio.NewScanner(strings.NewReader(text))
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "# ") {
return strings.TrimPrefix(line, "# ")
}
}
return ""
}
func (m Markup) html() string {
retval := ""
var line string
var lineType LineType
var lastType LineType = -1
text := string(m)
scanner := bufio.NewScanner(strings.NewReader(text))
for scanner.Scan() {
line = scanner.Text()
lineType = getLineType(line)
if lineType != UnorderedListItem && lastType == UnorderedListItem {
retval += "\n</ul>"
}
if line == "" {
continue
}
switch lineType {
case LinkLine:
retval += fmt.Sprintf("\n<pre>%s</pre>", line)
case HeadingLine:
retval += fmt.Sprintf("\n<h3>%s</h3>", line)
case UnorderedListItem:
if lastType != UnorderedListItem {
retval += "\n<ul>"
}
item := strings.TrimPrefix(line, "*")
retval += fmt.Sprintf("\n\t<li>%s</li>", item)
default:
retval += fmt.Sprintf("\n<p>%s</p>", line)
}
lastType = lineType
}
if lineType == UnorderedListItem {
retval += "\n</ul>"
}
retval = strings.TrimPrefix(retval, "\n")
return retval
}
|