-
Notifications
You must be signed in to change notification settings - Fork 0
/
paste.go
133 lines (111 loc) · 3.01 KB
/
paste.go
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
package paste
// TODO: find a better name for this package.
import (
"bytes"
"context"
"crypto/rand"
"errors"
"fmt"
"math/big"
"net/http"
"time"
"github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/alecthomas/chroma/v2/styles"
"github.com/mortezadadgar/spaste/internal/config"
)
var ErrNoPasteFound = errors.New("no paste found")
type Module struct {
ID int `json:"-"`
Text string `json:"text,omitempty"`
Lang string `json:"lang,omitempty"`
LineCount int `json:"linecount,omitempty"`
Address string `json:"address,omitempty"`
TimeStamp string `json:"-"`
}
type TemplateData struct {
Address string
TextHighlighted string
LineCount int
Lang string
Message string
IncludeHome bool
}
type store interface {
Create(ctx context.Context, paste Module) error
Get(ctx context.Context, addr string) (Module, error)
}
type Paste struct {
store store
config config.Config
}
// New returns a instance of Paste.
func New(store store, config config.Config) Paste {
return Paste{
store: store,
config: config,
}
}
// Create creates a new paste in store.
func (u Paste) Create(r *http.Request, m Module) (string, error) {
address := m.Address
var err error
if len(m.Address) == 0 {
address, err = makeAddress(u.config.AddressLength, m.Lang)
if err != nil {
return "", fmt.Errorf("failed to generate paste address: %v", err)
}
}
m = Module{
Text: m.Text,
Lang: m.Lang,
LineCount: m.LineCount,
Address: address,
TimeStamp: time.Now().Format(time.DateTime),
}
return address, u.store.Create(r.Context(), m)
}
// Get gets paste by its address.
func (u Paste) Get(r *http.Request, addr string) (Module, error) {
return u.store.Get(r.Context(), addr)
}
// Render render text in selected syntax highlighted language.
func (u Paste) Render(m Module) (string, error) {
lexer := lexers.Get(m.Lang)
if lexer == nil {
lexer = lexers.Fallback
}
lexer = chroma.Coalesce(lexer)
formatter := html.New(html.WithClasses(true))
iterator, err := lexer.Tokenise(nil, string(m.Text))
if err != nil {
return "", fmt.Errorf("failed to tokenise code: %v", err)
}
// TODO: why it's hardcoded.
style := styles.Get("doom-one")
if style == nil {
style = styles.Fallback
}
buf := new(bytes.Buffer)
err = formatter.Format(buf, style, iterator)
if err != nil {
return "", fmt.Errorf("failed to format code: %v", err)
}
return buf.String(), nil
}
func makeAddress(length int64, lang string) (string, error) {
if length == 0 {
return "", fmt.Errorf("generating empty address is not allowed")
}
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
buffer := make([]byte, length)
for i := range buffer {
r, err := rand.Int(rand.Reader, big.NewInt(length))
if err != nil {
return "", fmt.Errorf("failed to generate random addresses: %v", err)
}
buffer[i] = charset[r.Int64()]
}
return fmt.Sprintf("%s.%s", buffer, lang), nil
}