This repository was archived by the owner on May 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_document.go
More file actions
168 lines (140 loc) · 3.38 KB
/
Copy pathtext_document.go
File metadata and controls
168 lines (140 loc) · 3.38 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
159
160
161
162
163
164
165
166
167
168
package divido
import (
"bufio"
"fmt"
"io"
"path/filepath"
"strings"
)
const (
significantBreaksInRow = 3
notesSuffix = "_notes"
)
type TextDocument []TextElement
func (td TextDocument) ChapterTitles() []string {
chapters := make([]string, 0, len(td)/2)
for _, tc := range td {
if tc.Type == ChapterTitle {
for _, p := range tc.Content {
chapters = append(chapters, p.String())
}
}
}
return chapters
}
func (td TextDocument) ChapterParagraphs(chapterTitle string) []TextParagraph {
paragraphs := make([]TextParagraph, 0)
accumulate := false
for _, tc := range td {
if tc.Type == Break {
continue
}
if tc.Type == ChapterTitle {
if accumulate == true {
break
} else {
if len(tc.Content) > 0 && tc.Content[0].String() == chapterTitle {
accumulate = true
}
}
}
if tc.Type == Paragraph && accumulate {
paragraphs = append(paragraphs, tc.Content...)
}
}
return paragraphs
}
func (td TextDocument) ReplaceFrom(start int, old, new string) int {
if start >= len(td) {
return -1
}
for ii := start; ii < len(td); ii++ {
n := td[ii]
if n.Type != Paragraph {
continue
}
for pi, p := range n.Content {
if strings.Contains(p.String(), old) {
n.Content[pi] = TextParagraph(strings.Replace(p.String(), old, new, 1))
return ii
}
}
}
return -1
}
func NewTextDocument(reader io.Reader) TextDocument {
//divido includes the following steps to process plain text into structured text:
//1) determine significant breaks (>1 \n character in a row), paragraphs
//2) mark chapter titles and in-chapter dividers
scanner := bufio.NewScanner(reader)
td := make(TextDocument, 0)
breaksInRow := 0
// 1)
for scanner.Scan() {
paragraphCandidate := scanner.Text()
if len(paragraphCandidate) == 0 {
breaksInRow++
} else {
if breaksInRow > significantBreaksInRow {
td = append(td, TextElement{
Type: Break,
})
}
if len(td) > 0 &&
td[len(td)-1].Type == Paragraph {
td[len(td)-1].Content = append(td[len(td)-1].Content, TextParagraph(paragraphCandidate))
} else {
td = append(td, TextElement{
Content: NewParagraphs(paragraphCandidate),
Type: Paragraph,
})
}
breaksInRow = 0
}
}
//2)
for ii, tc := range td {
if tc.Type == Paragraph && len(tc.Content) == 1 {
if ii < len(td)-1 && td[ii+1].Type == Break {
td[ii].Type = ChapterTitle
}
}
}
return td
}
func NewTextDocumentWithNotes(document io.Reader, notes io.Reader) TextDocument {
td := NewTextDocument(document)
nt := NewTextDocument(notes)
previousType := Break
// reformat nodes to have ChapterTitle, then Paragraph pattern
for ii, tc := range nt {
if tc.Type == ChapterTitle {
if previousType == ChapterTitle {
nt[ii].Type = Paragraph
previousType = Paragraph
} else {
previousType = ChapterTitle
}
}
}
noteTitles := nt.ChapterTitles()
if len(noteTitles) == 0 {
return td
}
lastIndex := 0
for _, ni := range nt.ChapterTitles() {
noteIndex := fmt.Sprintf("[%s]", ni)
for _, nc := range nt.ChapterParagraphs(ni) {
noteText := fmt.Sprintf(" (%s)", nc.String())
if lastIndex = td.ReplaceFrom(lastIndex, noteIndex, noteText); lastIndex < 0 {
break
}
}
}
return td
}
func DefaultNotesFilename(filename string) string {
ext := filepath.Ext(filename)
filenameSansExt := strings.TrimSuffix(filename, ext)
return filenameSansExt + notesSuffix + ext
}