~shulhan/asciidoctor-go

ref: v0.1.0 asciidoctor-go/revision.go -rw-r--r-- 1.4 KiB
4c1ded16Shulhan Release asciidoctor-go v0.1.0 (2021-03-06) 4 months ago
                                                                                
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
// Copyright 2020, Shulhan <ms@kilabit.info>. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package asciidoctor

import (
	"strings"
)

//
// Revision contains the document version, date, and remark.
//
type Revision struct {
	Number string
	Date   string
	Remark string
}

//
// parseRevision parse document revision in the following format,
//
//	DOC_REVISION     = DOC_REV_VERSION [ "," DOC_REV_DATE ]
//
//	DOC_REV_VERSION  = "v" 1*DIGIT "." 1*DIGIT "." 1*DIGIT
//
//	DOC_REV_DATE     = 1*2DIGIT WSP 3*ALPHA WSP 4*DIGIT
//
func parseRevision(raw string) Revision {
	var (
		rev   Revision
		x     int
		lastc byte
	)
	if len(raw) == 0 {
		return rev
	}
	if raw[0] == 'v' {
		for x = 0; x < len(raw); x++ {
			if raw[x] == ',' || raw[x] == ':' {
				lastc = raw[x]
				break
			}
		}
		rev.Number = strings.TrimSpace(raw[1:x])
		if x == len(raw) {
			return rev
		}
		raw = raw[x:]
		x = 0
	}
	if len(raw) > 0 && raw[0] != ':' {
		// Parse date, check for option remark that start with ':'.
		if lastc != 0 {
			raw = raw[1:]
		}
		for x = 0; x < len(raw); x++ {
			if raw[x] == ':' {
				break
			}
		}
		rev.Date = strings.TrimSpace(raw[:x])
		if x == len(raw) {
			return rev
		}
		raw = raw[x:]
		x = 0
	}
	if len(raw) > 0 {
		// We have left over as remark.
		rev.Remark = strings.TrimSpace(raw[x+1:])
	}
	return rev
}