-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcatechism.go
More file actions
158 lines (130 loc) · 4.31 KB
/
catechism.go
File metadata and controls
158 lines (130 loc) · 4.31 KB
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
package romanus
import (
"errors"
"github.com/derekparker/trie"
)
// Catechism contains a list of parts
type Catechism struct {
Parts []Part `json:"-"`
PartSummary []PartSummary `json:"parts"`
searchTree *trie.Trie
}
// Part contains a per-part introduction and a list of articles
type Part struct {
Title string `json:"title"`
PartNumber uint8 `json:"partNumber"`
Introduction Introduction `json:"introduction"`
Articles []Article `json:"articles"`
}
// Introduction contains preface sections for a part
type Introduction struct {
Sections []Section `json:"sections"`
}
// Article contains a list of sections. Heading holds the formal all-caps
// heading from the original Roman Catechism (e.g. the quoted commandment or
// petition text); Title is the navigable short form.
type Article struct {
Title string `json:"title"`
Heading string `json:"heading"`
ArticleNumber uint8 `json:"articleNumber"`
Sections []Section `json:"sections"`
}
// Section contains a list of paragraphs
type Section struct {
Title string `json:"title"`
SectionNumber uint8 `json:"sectionNumber"`
Paragraphs []Paragraph `json:"paragraphs"`
}
// Paragraph contains text
type Paragraph struct {
ParagraphNumber uint8 `json:"paragraphNumber"`
Text string `json:"text"`
}
// PartSummary contains metadata for a part
type PartSummary struct {
Title string `json:"title"`
PartNumber uint8 `json:"partNumber"`
Articles []ArticleSummary `json:"articles"`
}
// ArticleSummary contains metadata for an article
type ArticleSummary struct {
Title string `json:"title"`
Heading string `json:"heading"`
ArticleNumber uint8 `json:"articleNumber"`
}
// GetPart obtains a part within the catechism by its number
func (c *Catechism) GetPart(partNumber int) (*Part, error) {
idx := partNumber - 1
if idx < 0 || idx >= len(c.Parts) {
return nil, errors.New("invalid part number")
}
return &c.Parts[idx], nil
}
// GetIntroduction obtains the introduction for a given part
func (c *Catechism) GetIntroduction(partNumber int) (*Introduction, error) {
p, err := c.GetPart(partNumber)
if err != nil {
return nil, err
}
return &p.Introduction, nil
}
// GetIntroductionSection obtains a section within a part's introduction
func (c *Catechism) GetIntroductionSection(partNumber, sectionNumber int) (*Section, error) {
intro, err := c.GetIntroduction(partNumber)
if err != nil {
return nil, err
}
idx := sectionNumber - 1
if idx < 0 || idx >= len(intro.Sections) {
return nil, errors.New("invalid section number")
}
return &intro.Sections[idx], nil
}
// GetIntroductionParagraph obtains a paragraph within a part's introduction
func (c *Catechism) GetIntroductionParagraph(partNumber, sectionNumber, paragraphNumber int) (*Paragraph, error) {
s, err := c.GetIntroductionSection(partNumber, sectionNumber)
if err != nil {
return nil, err
}
idx := paragraphNumber - 1
if idx < 0 || idx >= len(s.Paragraphs) {
return nil, errors.New("invalid paragraph number")
}
return &s.Paragraphs[idx], nil
}
// GetArticle obtains an article within the catechism by its number
func (c *Catechism) GetArticle(partNumber, articleNumber int) (*Article, error) {
p, err := c.GetPart(partNumber)
if err != nil {
return nil, err
}
idx := articleNumber - 1
if idx < 0 || idx >= len(p.Articles) {
return nil, errors.New("invalid article number")
}
return &p.Articles[idx], nil
}
// GetSection obtains a section within the catechism by its number
func (c *Catechism) GetSection(partNumber, articleNumber, sectionNumber int) (*Section, error) {
a, err := c.GetArticle(partNumber, articleNumber)
if err != nil {
return nil, err
}
idx := sectionNumber - 1
if idx < 0 || idx >= len(a.Sections) {
return nil, errors.New("invalid section number")
}
return &a.Sections[idx], nil
}
// GetParagraph obtains a paragraph within the catechism by its number
func (c *Catechism) GetParagraph(partNumber, articleNumber, sectionNumber, paragraphNumber int) (*Paragraph, error) {
s, err := c.GetSection(partNumber, articleNumber, sectionNumber)
if err != nil {
return nil, err
}
idx := paragraphNumber - 1
if idx < 0 || idx >= len(s.Paragraphs) {
return nil, errors.New("invalid paragraph number")
}
return &s.Paragraphs[idx], nil
}