-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
394 lines (360 loc) · 9.52 KB
/
parse.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package present
import (
"bufio"
"bytes"
"errors"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"net/url"
"regexp"
"strings"
"time"
"unicode"
"unicode/utf8"
"github.com/gernest/vectypresent/present/models"
)
var (
parsers = make(map[string]ParseFunc)
funcs = template.FuncMap{}
)
// Template returns an empty template with the action functions in its FuncMap.
func Template() *template.Template {
return template.New("").Funcs(funcs)
}
type ParseFunc func(ctx *Context, fileName string, lineNumber int, inputLine string) (models.Elem, error)
// Register binds the named action, which does not begin with a period, to the
// specified parser to be invoked when the name, with a period, appears in the
// present input text.
func Register(name string, parser ParseFunc) {
if len(name) == 0 || name[0] == ';' {
panic("bad name in Register: " + name)
}
parsers["."+name] = parser
}
// renderElem implements the elem template function, used to render
// sub-templates.
func renderElem(t *template.Template, e models.Elem) (template.HTML, error) {
var data interface{} = e
if s, ok := e.(models.Section); ok {
data = struct {
models.Section
Template *template.Template
}{s, t}
}
return execTemplate(t, e.TemplateName(), data)
}
func init() {
funcs["elem"] = renderElem
}
// execTemplate is a helper to execute a template and return the output as a
// template.HTML value.
func execTemplate(t *template.Template, name string, data interface{}) (template.HTML, error) {
b := new(bytes.Buffer)
err := t.ExecuteTemplate(b, name, data)
if err != nil {
return "", err
}
return template.HTML(b.String()), nil
}
func readLines(r io.Reader) (*models.Lines, error) {
var lines []string
s := bufio.NewScanner(r)
for s.Scan() {
lines = append(lines, s.Text())
}
if err := s.Err(); err != nil {
return nil, err
}
return &models.Lines{Line: 0, Text: lines}, nil
}
// A Context specifies the supporting context for parsing a presentation.
type Context struct {
// ReadFile reads the file named by filename and returns the contents.
ReadFile func(filename string) ([]byte, error)
}
// ParseMode represents flags for the Parse function.
type ParseMode int
const (
// If set, parse only the title and subtitle.
TitlesOnly ParseMode = 1
)
// Parse parses a document from r.
func (ctx *Context) Parse(r io.Reader, name string, mode ParseMode) (*models.Doc, error) {
doc := new(models.Doc)
lines, err := readLines(r)
if err != nil {
return nil, err
}
for i := lines.Line; i < len(lines.Text); i++ {
if strings.HasPrefix(lines.Text[i], "*") {
break
}
if isSpeakerNote(lines.Text[i]) {
doc.TitleNotes = append(doc.TitleNotes, lines.Text[i][2:])
}
}
err = parseHeader(doc, lines)
if err != nil {
return nil, err
}
if mode&TitlesOnly != 0 {
return doc, nil
}
// Authors
if doc.Authors, err = parseAuthors(lines); err != nil {
return nil, err
}
// Sections
if doc.Sections, err = parseSections(ctx, name, lines, []int{}); err != nil {
return nil, err
}
return doc, nil
}
// Parse parses a document from r. Parse reads assets used by the presentation
// from the file system using ioutil.ReadFile.
func Parse(r io.Reader, name string, mode ParseMode) (*models.Doc, error) {
ctx := Context{ReadFile: ioutil.ReadFile}
return ctx.Parse(r, name, mode)
}
// isHeading matches any section heading.
var isHeading = regexp.MustCompile(`^\*+ `)
// lesserHeading returns true if text is a heading of a lesser or equal level
// than that denoted by prefix.
func lesserHeading(text, prefix string) bool {
return isHeading.MatchString(text) && !strings.HasPrefix(text, prefix+"*")
}
// parseSections parses Sections from lines for the section level indicated by
// number (a nil number indicates the top level).
func parseSections(ctx *Context, name string, lines *models.Lines, number []int) ([]models.Section, error) {
var sections []models.Section
for i := 1; ; i++ {
// Next non-empty line is title.
text, ok := lines.NextNonEmpty()
for ok && text == "" {
text, ok = lines.Next()
}
if !ok {
break
}
prefix := strings.Repeat("*", len(number)+1)
if !strings.HasPrefix(text, prefix+" ") {
lines.Back()
break
}
section := models.Section{
Number: append(append([]int{}, number...), i),
Title: text[len(prefix)+1:],
}
text, ok = lines.NextNonEmpty()
for ok && !lesserHeading(text, prefix) {
var e models.Elem
r, _ := utf8.DecodeRuneInString(text)
switch {
case unicode.IsSpace(r):
i := strings.IndexFunc(text, func(r rune) bool {
return !unicode.IsSpace(r)
})
if i < 0 {
break
}
indent := text[:i]
var s []string
for ok && (strings.HasPrefix(text, indent) || text == "") {
if text != "" {
text = text[i:]
}
s = append(s, text)
text, ok = lines.Next()
}
lines.Back()
pre := strings.Join(s, "\n")
pre = strings.Replace(pre, "\t", " ", -1) // browsers treat tabs badly
pre = strings.TrimRightFunc(pre, unicode.IsSpace)
e = models.Text{Lines: []string{pre}, Pre: true}
case strings.HasPrefix(text, "- "):
var b []string
for ok && strings.HasPrefix(text, "- ") {
b = append(b, text[2:])
text, ok = lines.Next()
}
lines.Back()
e = models.List{Bullet: b}
case isSpeakerNote(text):
section.Notes = append(section.Notes, text[2:])
case strings.HasPrefix(text, prefix+"* "):
lines.Back()
subsecs, err := parseSections(ctx, name, lines, section.Number)
if err != nil {
return nil, err
}
for _, ss := range subsecs {
section.Elem = append(section.Elem, ss)
}
case strings.HasPrefix(text, "."):
args := strings.Fields(text)
if args[0] == ".background" {
section.Classes = append(section.Classes, "background")
section.Styles = append(section.Styles, "background-image: url('"+args[1]+"')")
break
}
parser := parsers[args[0]]
if parser == nil {
return nil, fmt.Errorf("%s:%d: unknown command %q\n", name, lines.Line, text)
}
t, err := parser(ctx, name, lines.Line, text)
if err != nil {
return nil, err
}
e = t
default:
var l []string
for ok && strings.TrimSpace(text) != "" {
if text[0] == '.' { // Command breaks text block.
lines.Back()
break
}
if strings.HasPrefix(text, `\.`) { // Backslash escapes initial period.
text = text[1:]
}
l = append(l, text)
text, ok = lines.Next()
}
if len(l) > 0 {
e = models.Text{Lines: l}
}
}
if e != nil {
section.Elem = append(section.Elem, e)
}
text, ok = lines.NextNonEmpty()
}
if isHeading.MatchString(text) {
lines.Back()
}
sections = append(sections, section)
}
return sections, nil
}
func parseHeader(doc *models.Doc, lines *models.Lines) error {
var ok bool
// First non-empty line starts header.
doc.Title, ok = lines.NextNonEmpty()
if !ok {
return errors.New("unexpected EOF; expected title")
}
for {
text, ok := lines.Next()
if !ok {
return errors.New("unexpected EOF")
}
if text == "" {
break
}
if isSpeakerNote(text) {
continue
}
const tagPrefix = "Tags:"
if strings.HasPrefix(text, tagPrefix) {
tags := strings.Split(text[len(tagPrefix):], ",")
for i := range tags {
tags[i] = strings.TrimSpace(tags[i])
}
doc.Tags = append(doc.Tags, tags...)
} else if t, ok := parseTime(text); ok {
doc.Time = t
} else if doc.Subtitle == "" {
doc.Subtitle = text
} else {
return fmt.Errorf("unexpected header line: %q", text)
}
}
return nil
}
func parseAuthors(lines *models.Lines) (authors []models.Author, err error) {
// This grammar demarcates authors with blanks.
// Skip blank lines.
if _, ok := lines.NextNonEmpty(); !ok {
return nil, errors.New("unexpected EOF")
}
lines.Back()
var a *models.Author
for {
text, ok := lines.Next()
if !ok {
return nil, errors.New("unexpected EOF")
}
// If we find a section heading, we're done.
if strings.HasPrefix(text, "* ") {
lines.Back()
break
}
if isSpeakerNote(text) {
continue
}
// If we encounter a blank we're done with this author.
if a != nil && len(text) == 0 {
authors = append(authors, *a)
a = nil
continue
}
if a == nil {
a = new(models.Author)
}
// Parse the line. Those that
// - begin with @ are twitter names,
// - contain slashes are links, or
// - contain an @ symbol are an email address.
// The rest is just text.
var el models.Elem
switch {
case strings.HasPrefix(text, "@"):
el = parseURL("http://twitter.com/" + text[1:])
case strings.Contains(text, ":"):
el = parseURL(text)
case strings.Contains(text, "@"):
el = parseURL("mailto:" + text)
}
if l, ok := el.(models.Link); ok {
l.Label = text
el = l
}
if el == nil {
el = models.Text{Lines: []string{text}}
}
a.Elem = append(a.Elem, el)
}
if a != nil {
authors = append(authors, *a)
}
return authors, nil
}
func parseURL(text string) models.Elem {
u, err := url.Parse(text)
if err != nil {
log.Printf("Parse(%q): %v", text, err)
return nil
}
return models.Link{URL: u}
}
func parseTime(text string) (t time.Time, ok bool) {
t, err := time.Parse("15:04 2 Jan 2006", text)
if err == nil {
return t, true
}
t, err = time.Parse("2 Jan 2006", text)
if err == nil {
// at 11am UTC it is the same date everywhere
t = t.Add(time.Hour * 11)
return t, true
}
return time.Time{}, false
}
func isSpeakerNote(s string) bool {
return strings.HasPrefix(s, ": ")
}