-
Notifications
You must be signed in to change notification settings - Fork 2
/
toTxt.dart
153 lines (121 loc) · 3.8 KB
/
toTxt.dart
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
import 'dart:io';
import 'objects/chapter.dart';
import 'objects/verse.dart';
import 'objects/book.dart';
// Variables
String storedTitle = null;
String libroEnString = '';
List<String> libroEnLineas = List<String>();
List<Verse> listaDeVersos = List<Verse>();
File output = null;
String title = '';
String abbreviation = '';
String description = '';
Book libro = Book();
void main(List<String> args) async {
// Verificar si no se ha enviado argumentos
if(args.length == 0){
print('[Error 001] falta el path del archivo\n');
return null;
}
else if(args.length == 1){
print('[Error 002] falta la abreviatura de la traducción\n');
return null;
}
// Inicializar variables
libroEnString = await File(args[0]).readAsString();
libroEnLineas = libroEnString.split('\n');
// Establecer titulo, abreviacion y descripcion
setHeaders();
// Separar por LINEAS el documento
for(var verse in libroEnLineas)
{
if(verse[0] == '#' || verse.length < 5) continue;
Verse temp = checkLine(data: verse); // Procesar la linea
if(temp != null) // Omitir si la linea es NULL
listaDeVersos.add(temp);
}
//Crear libro
crearLibro(listaDeVersos);
Directory('${args[1]}_TXT').create();
libro.chapters.forEach((chapter)
{
chapter.verses.forEach((verse)
{
output = File('${args[1]}_TXT/${abbreviation.split('_')[0]}_${verse.id.replaceAll(':', '_')}');
output.create();
output.writeAsString('${verse.text}');
});
});
// Show output text
print('Libro $title ha sido creado como $abbreviation.json.');
}
Verse checkLine({String data})
{
if(data.length == 0 || data == '\n') return null;
final words_verse = data.split(' ');
final verso = Verse();
verso.id = words_verse[0];
verso.text = '';
for(var i = 1; i < words_verse.length; i++)
verso.text += '${words_verse[i].trim()} ';
verso.text = verso.text.trim();
return verso;
}
void setHeaders(){
for(int i = 0; i < 3; i++){
List<String> split = libroEnLineas[i].split(' ');
switch (split[0]) {
case '#title':
for(int j = 1; j < split.length; j++)
title += split[j] + ' ';
title = title.trim().substring(0, title.length - 2);
break;
case '#description':
for(int j = 1; j < split.length; j++)
description += split[j] + ' ';
description = description.trim().substring(0, description.length - 2);
break;
case '#abbreviation':
for(int j = 1; j < split.length; j++)
abbreviation += split[j] + ' ';
abbreviation = abbreviation.trim().substring(0, abbreviation.length - 2);
break;
default:
print('[Error 002] verifique parametros:\n\n#title\n#description\n#abbreviation.\n');
}
}
}
void crearLibro(List<Verse> listaDeVersos){
int actualChapter = 0;
libro.title = title;
libro.description = description;
libro.abbreviation = abbreviation;
libro.chapters = List<Chapter>();
libro.chapters.add(Chapter(number: actualChapter + 1));
libro.chapters[actualChapter].verses = List<Verse>();
for(var verse in listaDeVersos){
if(getChapterAndVerse(verse.id)['chapter'] == actualChapter + 1){
libro.chapters[actualChapter].verses.add(verse);
}
else{
actualChapter++;
libro.chapters.add(Chapter(number: actualChapter + 1));
libro.chapters[actualChapter].verses = List<Verse>();
libro.chapters[actualChapter].verses.add(verse);
}
}
}
Map<String, int> getChapterAndVerse(String id){
int chapter = null;
int verse = null;
try {
chapter = int.parse(id.split(':')[0]);
verse = int.parse(id.split(':')[1]);
}
catch (e) { print('[Error 003] el ID: $id esta mal.'); }
return {
"chapter": chapter,
"verse": verse
};
}