use of biblemulticonverter.data.Book in project BibleMultiConverter by schierlm.
the class StrippedDiffable method doExport.
@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
String outputFile = exportArgs[0];
if (exportArgs.length == 2 && exportArgs[1].equals("ExtractPrologs")) {
extractPrologs(bible);
System.out.println("Prologs extracted.");
} else if (exportArgs.length == 2 && exportArgs[1].equals("MergeIntroductionPrologs")) {
mergeIntroductionPrologs(bible);
System.out.println("Introduction prologs merged.");
} else if (exportArgs.length == 2 && exportArgs[1].equals("ExtractFootnotes")) {
extractFootnotes(bible);
System.out.println("Footnotes extracted.");
} else if (exportArgs.length == 2 && exportArgs[1].equals("RearrangeChapterBoundaries")) {
rearrangeChapterBoundaries(bible);
System.out.println("Chapter boundaries rearranged.");
} else if (exportArgs.length == 3 && exportArgs[1].equals("SelectVariation")) {
selectVariation(bible, exportArgs[2]);
System.out.println("Variation " + exportArgs[2] + " kept.");
} else if (exportArgs.length == 4 && exportArgs[1].equals("RenameBook")) {
renameBookInXref(bible, exportArgs[2], exportArgs[3], true);
for (int i = 0; i < bible.getBooks().size(); i++) {
Book oldBook = bible.getBooks().get(i);
if (oldBook.getAbbr().equals(exportArgs[2])) {
Book newBook = new Book(exportArgs[3], oldBook.getId(), oldBook.getShortName(), oldBook.getLongName());
newBook.getChapters().addAll(oldBook.getChapters());
bible.getBooks().set(i, newBook);
}
}
System.out.println("Book " + exportArgs[2] + " renamed to " + exportArgs[3]);
} else if (exportArgs.length >= 2 && exportArgs[1].equals("OptimizeFormatting")) {
Map<String, String[]> mappings = new HashMap<>();
for (int i = 2; i < exportArgs.length; i++) {
int pos = exportArgs[i].indexOf("->");
if (pos == -1) {
System.out.println("WARNING: Skipped malformed formatting mapping " + exportArgs[i]);
continue;
}
String key = exportArgs[i].substring(0, pos).trim();
String[] values = exportArgs[i].substring(pos + 2).split("&");
mappings.put(key, values);
}
optimizeFormatting(bible, mappings);
} else {
EnumSet<Feature> chosenFeeatures = EnumSet.noneOf(Feature.class);
for (int i = 1; i < exportArgs.length; i++) {
chosenFeeatures.add(Feature.valueOf(exportArgs[i]));
}
strip(bible, chosenFeeatures);
}
List<Book> booksToRemove = new ArrayList<Book>();
for (Book book : bible.getBooks()) {
while (book.getChapters().size() > 0) {
Chapter lastChapter = book.getChapters().get(book.getChapters().size() - 1);
if (lastChapter.getProlog() != null || lastChapter.getVerses().size() > 0)
break;
book.getChapters().remove(lastChapter);
}
if (book.getChapters().size() == 0)
booksToRemove.add(book);
}
bible.getBooks().removeAll(booksToRemove);
new Diffable().doExport(bible, new String[] { outputFile });
}
use of biblemulticonverter.data.Book in project BibleMultiConverter by schierlm.
the class StrippedDiffable method optimizeFormatting.
private void optimizeFormatting(Bible bible, Map<String, String[]> mappings) {
CountFormattingVisitor oldCount = new CountFormattingVisitor();
CountFormattingVisitor newCount = new CountFormattingVisitor();
for (Book book : bible.getBooks()) {
for (Chapter chap : book.getChapters()) {
if (chap.getProlog() != null) {
chap.getProlog().accept(oldCount);
FormattedText newProlog = new FormattedText();
chap.getProlog().accept(new OptimizeFormattingVisitor(newProlog.getAppendVisitor(), mappings));
newProlog.finished();
newProlog.accept(newCount);
chap.setProlog(newProlog);
}
for (int j = 0; j < chap.getVerses().size(); j++) {
Verse v = chap.getVerses().get(j);
v.accept(oldCount);
Verse nv = new Verse(v.getNumber());
v.accept(new OptimizeFormattingVisitor(nv.getAppendVisitor(), mappings));
nv.finished();
nv.accept(newCount);
chap.getVerses().set(j, nv);
}
}
}
System.out.println("Formatting instructions before optimizing:");
oldCount.printSummary();
System.out.println();
System.out.println("Formatting instructions after optimizing:");
newCount.printSummary();
}
use of biblemulticonverter.data.Book in project BibleMultiConverter by schierlm.
the class TheWord method doImport.
@Override
public Bible doImport(File inputFile) throws Exception {
warningCount = 0;
Bible result = new Bible("Imported From theWord");
boolean hasOT = true, hasNT = true;
if (inputFile.getName().toLowerCase().endsWith(".ot")) {
hasNT = false;
} else if (inputFile.getName().toLowerCase().endsWith(".nt")) {
hasOT = false;
}
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), "UTF-8"))) {
for (BookID bid : BOOK_ORDER) {
if ((bid.isNT() && !hasNT) || (!bid.isNT() && !hasOT))
continue;
Book bk = new Book(bid.getOsisID(), bid, bid.getEnglishName(), bid.getEnglishName());
int[] verseCount = StandardVersification.KJV.getVerseCount(bid);
for (int cnumber = 1; cnumber <= verseCount.length; cnumber++) {
Chapter ch = new Chapter();
boolean hasVerses = false;
int maxVerse = verseCount[cnumber - 1];
for (int vnumber = 1; vnumber <= maxVerse; vnumber++) {
String line = br.readLine();
if (line.startsWith("\uFEFF"))
line = line.substring(1);
if (line.equals("- - -") || line.trim().length() == 0)
continue;
line = line.replaceAll(" +", " ").trim();
hasVerses = true;
Verse v = new Verse("" + vnumber);
if (line.contains("<WH") || line.contains("<WG")) {
Matcher m = Pattern.compile("(<FI>[^<>]*<Fi>|<FO>[^<>]*<Fo>|[^<> ]*)((<W[GH][0-9]+>)+)").matcher(line.replaceFirst("^(<W[GH][0-9]+x>)+", ""));
StringBuffer sb = new StringBuffer();
while (m.find()) {
String word = m.group(1);
String tags = m.group(2);
m.appendReplacement(sb, "");
sb.append("<S%" + tags.substring(3, tags.length() - 1).replaceAll("><W[GH]", "%") + ">");
sb.append(word);
sb.append("<s%>");
}
m.appendTail(sb);
line = sb.toString();
}
int pos = parseLine(v.getAppendVisitor(), line, 0, null);
if (pos != line.length())
v.getAppendVisitor().visitText(line.substring(pos));
v.finished();
ch.getVerses().add(v);
}
if (hasVerses) {
while (bk.getChapters().size() < cnumber - 1) {
bk.getChapters().add(new Chapter());
}
if (bk.getChapters().size() != cnumber - 1)
throw new RuntimeException();
bk.getChapters().add(ch);
}
}
if (bk.getChapters().size() > 0)
result.getBooks().add(bk);
}
}
return result;
}
use of biblemulticonverter.data.Book in project BibleMultiConverter by schierlm.
the class VersificationMappedDiffable method doExport.
@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
String outputFile = exportArgs[0];
VersificationSet vs = new VersificationSet(new File(exportArgs[1]));
VersificationMapping vm = vs.findMapping(exportArgs[2]);
boolean dropUnmapped = false, showNumbers = false, addTags = false;
;
for (int i = 3; i < exportArgs.length; i++) {
if (exportArgs[i].equals("DropUnmapped"))
dropUnmapped = true;
else if (exportArgs[i].equals("ShowNumbers"))
showNumbers = true;
else if (exportArgs[i].equals("AddTags"))
addTags = true;
else
throw new IllegalArgumentException("Unsupported option: " + exportArgs[i]);
}
Map<BookID, String> abbrMap = new EnumMap<>(BookID.class);
for (Book book : bible.getBooks()) {
abbrMap.put(book.getId(), book.getAbbr());
}
Bible newBible = new Bible(bible.getName());
Map<BookID, Book> newBooks = new EnumMap<>(BookID.class);
for (Book book : bible.getBooks()) {
if (book.getId().getZefID() < 1) {
// metadata book, introduction or appendix
newBible.getBooks().add(book);
continue;
}
int cnumber = 0;
for (Chapter chap : book.getChapters()) {
cnumber++;
if (chap.getProlog() != null && chap.getVerses().isEmpty()) {
System.out.println("WARNING: Prolog for " + book.getAbbr() + " " + cnumber + " got lost as chapter contains no verses.");
}
for (int j = 0; j < chap.getVerses().size(); j++) {
Verse oldVerse = chap.getVerses().get(j);
Reference ref = new Reference(book.getId(), cnumber, oldVerse.getNumber()), newRef;
List<Reference> newRefs = vm.getMapping(ref);
if ((newRefs == null || newRefs.isEmpty()) && dropUnmapped) {
if (j == 0 && chap.getProlog() != null) {
System.out.println("WARNING: Prolog for " + book.getAbbr() + " " + cnumber + " got lost as first verse of it is unmapped.");
}
continue;
}
if (newRefs == null || newRefs.contains(ref) || newRefs.isEmpty())
newRef = ref;
else
newRef = newRefs.get(0);
if (!newBooks.containsKey(newRef.getBook())) {
Book newBook = null;
for (Book oldBook : bible.getBooks()) {
if (oldBook.getId() == newRef.getBook()) {
newBook = new Book(oldBook.getAbbr(), newRef.getBook(), oldBook.getShortName(), oldBook.getLongName());
break;
}
}
if (newBook == null)
newBook = new Book(newRef.getBook().getOsisID(), newRef.getBook(), newRef.getBook().getEnglishName(), newRef.getBook().getEnglishName());
newBooks.put(newRef.getBook(), newBook);
newBible.getBooks().add(newBook);
}
Book newBook = newBooks.get(newRef.getBook());
while (newBook.getChapters().size() < newRef.getChapter()) newBook.getChapters().add(new Chapter());
Chapter newChapter = newBook.getChapters().get(newRef.getChapter() - 1);
if (j == 0 && chap.getProlog() != null) {
FormattedText newProlog = new FormattedText();
if (newChapter.getProlog() != null) {
newChapter.getProlog().accept(newProlog.getAppendVisitor());
newProlog.getAppendVisitor().visitLineBreak(LineBreakKind.PARAGRAPH);
}
chap.getProlog().accept(new MapXrefVisitor(newProlog.getAppendVisitor(), vm, dropUnmapped, abbrMap));
newProlog.finished();
newChapter.setProlog(newProlog);
}
Verse newVerse = null;
for (Verse v : newChapter.getVerses()) {
if (v.getNumber().equals(newRef.getVerse())) {
newVerse = v;
break;
}
}
boolean needSpace = true;
if (newVerse == null) {
newVerse = new Verse(newRef.getVerse());
newChapter.getVerses().add(newVerse);
needSpace = false;
}
if (needSpace || !ref.equals(newRef)) {
Visitor<RuntimeException> v = newVerse.getAppendVisitor();
if (addTags)
v = v.visitExtraAttribute(ExtraAttributePriority.KEEP_CONTENT, "v11n", "origverse", ref.getBook().getOsisID() + "--" + ref.getChapter() + "--" + ref.getVerse());
if (needSpace)
v.visitText(" ");
if (showNumbers) {
String verseNumber;
if (!ref.getBook().equals(newRef.getBook())) {
verseNumber = ref.getBook().getOsisID() + " " + ref.getChapter() + ":" + ref.getVerse();
} else if (ref.getChapter() != newRef.getChapter()) {
verseNumber = ref.getChapter() + ":" + ref.getVerse();
} else {
verseNumber = ref.getVerse();
}
v.visitFormattingInstruction(FormattingInstructionKind.BOLD).visitText("(" + verseNumber + ")");
v.visitText(" ");
}
}
oldVerse.accept(new MapXrefVisitor(newVerse.getAppendVisitor(), vm, dropUnmapped, abbrMap));
}
}
}
for (Book bk : newBible.getBooks()) {
for (Chapter ch : bk.getChapters()) {
for (Verse v : ch.getVerses()) {
v.finished();
}
}
}
bible = newBible;
new Diffable().doExport(bible, new String[] { outputFile });
}
use of biblemulticonverter.data.Book in project BibleMultiConverter by schierlm.
the class YCHPalmBible method doExport.
@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
String filename = exportArgs[0];
String description = bible.getName();
MetadataBook metadata = bible.getMetadataBook();
if (metadata != null) {
String metaDescription = bible.getMetadataBook().getValue(MetadataBookKey.description);
if (metaDescription != null)
description = metaDescription;
}
try (final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "windows-1252"))) {
bw.write("<PARSERINFO ENCODE=\"Cp1252\" WORDTYPE=\"SPCSEP\">");
bw.newLine();
bw.write("<BIBLE NAME=\"" + bible.getName() + "\" INFO=\"" + description + "\">");
bw.newLine();
Visitor<IOException> contentVisitor = new FormattedText.VisitorAdapter<IOException>(null) {
@Override
public void visitVerseSeparator() throws IOException {
// strip
}
@Override
public void visitText(String text) throws IOException {
bw.write(text);
}
@Override
public Visitor<IOException> visitFormattingInstruction(FormattingInstructionKind kind) throws IOException {
return this;
}
@Override
public Visitor<IOException> visitCSSFormatting(String css) throws IOException {
return this;
}
@Override
public void visitLineBreak(LineBreakKind kind) throws IOException {
bw.write(" ");
}
};
for (Book bk : bible.getBooks()) {
int zefID = bk.getId().getZefID();
if (zefID < 1 || zefID >= PALM_BOOK_NUMBERS.length || PALM_BOOK_NUMBERS[zefID] == 0) {
System.out.println("WARNING: Skipping unsupported book " + bk.getAbbr() + " (" + bk.getId().getOsisID() + ")");
continue;
}
bw.write("<BOOK NAME=\"" + bk.getShortName() + "\" NUMBER=\"" + PALM_BOOK_NUMBERS[bk.getId().getZefID()] + "\" SHORTCUT=\"" + bk.getAbbr() + "\">");
bw.newLine();
String longtitle = bk.getLongName();
int chapter = 0, verse;
for (Chapter chap : bk.getChapters()) {
chapter++;
if (chap.getProlog() != null)
System.out.println("WARNING: Skipping prolog (prologs not supported)!");
verse = 1;
String chaptext = CHAPTER_NAME + " " + chapter;
bw.write("<CHAPTER>");
bw.newLine();
for (VirtualVerse v : chap.createVirtualVerses()) {
while (v.getNumber() > verse) {
bw.write("<VERSE></VERSE>");
bw.newLine();
verse++;
}
if (v.getNumber() != verse)
throw new RuntimeException("Verse is " + v.getNumber() + ", should be " + verse);
boolean needVersText = false;
bw.write("<VERSE>");
if (longtitle != null) {
bw.write("<BOOKTEXT>" + longtitle);
longtitle = null;
needVersText = true;
}
if (chaptext != null) {
bw.write("<CHAPTEXT>" + chaptext);
chaptext = null;
needVersText = true;
}
for (Headline hl : v.getHeadlines()) {
bw.write("<DESCTEXT>");
hl.accept(contentVisitor);
needVersText = true;
}
if (needVersText)
bw.write("<VERSTEXT>");
for (Verse vv : v.getVerses()) {
if (!vv.getNumber().equals("" + v.getNumber())) {
bw.write("{" + vv.getNumber() + "} ");
}
vv.accept(contentVisitor);
}
bw.write("</VERSE>");
verse++;
bw.newLine();
}
bw.write("</CHAPTER>");
bw.newLine();
}
bw.write("</BOOK>");
bw.newLine();
}
bw.write("</BIBLE>");
bw.newLine();
}
}
Aggregations