Search in sources :

Example 16 with FormattedText

use of biblemulticonverter.data.FormattedText in project BibleMultiConverter by schierlm.

the class RoundtripHTML method doImport.

@Override
public Bible doImport(File inputDir) throws Exception {
    Bible bible;
    // metadata
    try (BufferedReader br = createReader(inputDir, "metadata.js")) {
        String line = br.readLine();
        br.readLine();
        bible = new Bible(line.substring(13, line.length() - 2).replace("\\\"", "\"").replace("\\\\", "\\"));
        Map<String, Object> fieldMap = new HashMap<String, Object>();
        while ((line = br.readLine()) != null) {
            if (line.startsWith("}")) {
                Book bk = new Book((String) fieldMap.get("abbr"), BookID.fromOsisId((String) fieldMap.get("osis")), (String) fieldMap.get("short"), (String) fieldMap.get("long"));
                for (int i = 0; i < (Integer) fieldMap.get("chapters"); i++) {
                    bk.getChapters().add(new Chapter());
                }
                bible.getBooks().add(bk);
                continue;
            }
            int pos = line.indexOf(":");
            String key = line.substring(0, pos);
            String value = line.substring(pos + 1);
            if (value.endsWith(","))
                value = value.substring(0, value.length() - 1);
            if (value.startsWith("\"") && value.endsWith("\"")) {
                fieldMap.put(key, value.substring(1, value.length() - 1).replace("\\\"", "\"").replace("\\\\", "\\"));
            } else if (value.equals("true") || value.equals("false")) {
                fieldMap.put(key, Boolean.parseBoolean(value));
            } else {
                fieldMap.put(key, Integer.parseInt(value));
            }
        }
    }
    // chapters
    for (Book bk : bible.getBooks()) {
        int cnumber = 0;
        for (Chapter ch : bk.getChapters()) {
            cnumber++;
            try (BufferedReader br = createReader(inputDir, getTypeDir(bk.getId()) + "/" + bk.getAbbr() + "_" + cnumber + ".html")) {
                String line;
                List<FormattedText.Visitor<RuntimeException>> footnotes = new ArrayList<>();
                while ((line = br.readLine()) != null) {
                    if (line.equals("<div class=\"biblehtmlcontent prolog\">")) {
                        line = br.readLine();
                        FormattedText prolog = new FormattedText();
                        int end = parseLine(prolog.getAppendVisitor(), line, 0, footnotes);
                        ch.setProlog(prolog);
                        if (end != line.length())
                            throw new IOException(line.substring(end));
                        line = br.readLine();
                        if (!line.equals("</div>"))
                            throw new IOException(line);
                    } else if (line.equals("<div class=\"biblehtmlcontent verses\" id=\"verses\">")) {
                        while ((line = br.readLine()) != null) {
                            if (line.equals("</div>"))
                                break;
                            if (!line.startsWith("<div class=\"v\" id=\"v") || !line.endsWith("</div>"))
                                throw new IOException(line);
                            line = line.substring(20, line.length() - 6);
                            int pos = line.indexOf("\">");
                            Verse v = new Verse(line.substring(0, pos));
                            int end = parseLine(v.getAppendVisitor(), line, pos + 2, footnotes);
                            if (end != line.length())
                                throw new IOException(line.substring(end));
                            ch.getVerses().add(v);
                        }
                        if (!line.equals("</div>"))
                            throw new IOException(line);
                    } else if (line.equals("<div class=\"biblehtmlcontent footnotes\">")) {
                        for (int i = 0; i < footnotes.size(); i++) {
                            line = br.readLine();
                            String prefix = "<div class=\"fn\"><sup class=\"fnt\"><a name=\"fn" + (i + 1) + "\" href=\"#fnm" + (i + 1) + "\">" + (i + 1) + "</a></sup> ";
                            if (!line.startsWith(prefix) || !line.endsWith("</div>"))
                                throw new IOException(line);
                            line = line.substring(prefix.length(), line.length() - 6);
                            int end = parseLine(footnotes.get(i), line, 0, null);
                            if (end != line.length())
                                throw new IOException(line.substring(end));
                        }
                        line = br.readLine();
                        if (!line.equals("</div>"))
                            throw new IOException(line);
                    }
                }
                if (ch.getProlog() != null)
                    ch.getProlog().finished();
                for (Verse v : ch.getVerses()) v.finished();
            }
        }
    }
    return bible;
}
Also used : Visitor(biblemulticonverter.data.FormattedText.Visitor) HashMap(java.util.HashMap) Bible(biblemulticonverter.data.Bible) Chapter(biblemulticonverter.data.Chapter) ArrayList(java.util.ArrayList) FormattedText(biblemulticonverter.data.FormattedText) IOException(java.io.IOException) Book(biblemulticonverter.data.Book) BufferedReader(java.io.BufferedReader) Verse(biblemulticonverter.data.Verse)

Example 17 with FormattedText

use of biblemulticonverter.data.FormattedText in project BibleMultiConverter by schierlm.

the class RoundtripXML method parseBible.

protected Bible parseBible(JAXBElement<BibleType> sBible) throws Exception {
    Bible dBible = new Bible(sBible.getValue().getName());
    for (BibleType.Book sBook : sBible.getValue().getBook()) {
        Book dBook = new Book(sBook.getAbbr(), BookID.fromOsisId(sBook.getId().replaceAll("-[0-9]+$", "")), sBook.getShortName(), sBook.getLongName());
        dBible.getBooks().add(dBook);
        for (BibleType.Book.Chapter sChapter : sBook.getChapter()) {
            Chapter dChapter = new Chapter();
            dBook.getChapters().add(dChapter);
            if (sChapter.getProlog() != null) {
                dChapter.setProlog(new FormattedText());
                parseContent(dChapter.getProlog().getAppendVisitor(), sChapter.getProlog().getContent());
                dChapter.getProlog().finished();
            }
            for (BibleType.Book.Chapter.Verse sVerse : sChapter.getVerse()) {
                Verse dVerse = new Verse(sVerse.getNumber());
                dChapter.getVerses().add(dVerse);
                parseContent(dVerse.getAppendVisitor(), sVerse.getContent());
                dVerse.finished();
            }
        }
    }
    return dBible;
}
Also used : Book(biblemulticonverter.data.Book) Bible(biblemulticonverter.data.Bible) Chapter(biblemulticonverter.data.Chapter) BibleType(biblemulticonverter.schema.roundtripxml.BibleType) FormattedText(biblemulticonverter.data.FormattedText) Verse(biblemulticonverter.data.Verse)

Example 18 with FormattedText

use of biblemulticonverter.data.FormattedText in project BibleMultiConverter by schierlm.

the class ScrambledDiffable method doExport.

@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
    String outputFile = exportArgs[0];
    Scrambler scrambler = new Scrambler(exportArgs.length == 1 ? null : exportArgs[1]);
    for (Book book : bible.getBooks()) {
        if (book.getId() == BookID.METADATA)
            continue;
        for (Chapter chapter : book.getChapters()) {
            if (chapter.getProlog() != null) {
                FormattedText newProlog = new FormattedText();
                scrambleText(scrambler, chapter.getProlog(), newProlog);
                chapter.setProlog(newProlog);
            }
            List<Verse> verses = chapter.getVerses();
            for (int i = 0; i < verses.size(); i++) {
                Verse oldVerse = verses.get(i);
                Verse newVerse = new Verse(oldVerse.getNumber());
                scrambleText(scrambler, oldVerse, newVerse);
                verses.set(i, newVerse);
            }
        }
    }
    new Diffable().doExport(bible, new String[] { outputFile });
}
Also used : Book(biblemulticonverter.data.Book) Chapter(biblemulticonverter.data.Chapter) FormattedText(biblemulticonverter.data.FormattedText) Verse(biblemulticonverter.data.Verse)

Example 19 with FormattedText

use of biblemulticonverter.data.FormattedText in project BibleMultiConverter by schierlm.

the class StrippedDiffable method strip.

protected void strip(Bible bible, EnumSet<Feature> chosenFeatures) {
    EnumSet<Feature> foundFeatures = EnumSet.noneOf(Feature.class);
    for (int i = 0; i < bible.getBooks().size(); i++) {
        Book book = bible.getBooks().get(i);
        BookID bid = book.getId();
        boolean stripBook;
        switch(bid) {
            case METADATA:
                stripBook = isEnabled(Feature.StripMetadataBook, chosenFeatures, foundFeatures);
                break;
            case INTRODUCTION:
                stripBook = isEnabled(Feature.StripIntroductionBooks, chosenFeatures, foundFeatures);
                break;
            case INTRODUCTION_OT:
                // no "shortcut" or here due to side effects of isEnabled!
                stripBook = isEnabled(Feature.StripIntroductionBooks, chosenFeatures, foundFeatures) | isEnabled(Feature.StripOldTestament, chosenFeatures, foundFeatures);
                break;
            case INTRODUCTION_NT:
                // no "shortcut" or here due to side effects of isEnabled!
                stripBook = isEnabled(Feature.StripIntroductionBooks, chosenFeatures, foundFeatures) | isEnabled(Feature.StripNewTestament, chosenFeatures, foundFeatures);
                break;
            case APPENDIX:
                stripBook = isEnabled(Feature.StripAppendixBook, chosenFeatures, foundFeatures);
                break;
            case DICTIONARY_ENTRY:
                stripBook = isEnabled(Feature.StripDictionaryEntries, chosenFeatures, foundFeatures);
                break;
            default:
                stripBook = isEnabled(bid.isNT() ? Feature.StripNewTestament : Feature.StripOldTestament, chosenFeatures, foundFeatures);
                if (bid.isDeuterocanonical())
                    stripBook = stripBook | isEnabled(Feature.StripDeuterocanonicalBooks, chosenFeatures, foundFeatures);
                break;
        }
        if (stripBook) {
            bible.getBooks().remove(i);
            i--;
            continue;
        }
        for (Chapter chap : book.getChapters()) {
            if (chap.getProlog() != null) {
                if (isEnabled(Feature.StripPrologs, chosenFeatures, foundFeatures)) {
                    chap.setProlog(null);
                } else {
                    FormattedText newProlog = new FormattedText();
                    chap.getProlog().accept(new StripVisitor(newProlog.getAppendVisitor(), chosenFeatures, foundFeatures));
                    newProlog.finished();
                    chap.setProlog(newProlog);
                }
            }
            for (int j = 0; j < chap.getVerses().size(); j++) {
                Verse v = chap.getVerses().get(j);
                Verse nv = new Verse(v.getNumber());
                v.accept(new StripVisitor(nv.getAppendVisitor(), chosenFeatures, foundFeatures));
                nv.finished();
                chap.getVerses().set(j, nv);
            }
        }
    }
    EnumSet<Feature> stripped = EnumSet.copyOf(chosenFeatures);
    stripped.retainAll(foundFeatures);
    chosenFeatures.removeAll(stripped);
    foundFeatures.removeAll(stripped);
    printFeatureSet("Features stripped:", stripped);
    printFeatureSet("Features to strip not found:", chosenFeatures);
    printFeatureSet("Features still present after stripping:", foundFeatures);
}
Also used : BookID(biblemulticonverter.data.BookID) Book(biblemulticonverter.data.Book) Chapter(biblemulticonverter.data.Chapter) FormattedText(biblemulticonverter.data.FormattedText) Verse(biblemulticonverter.data.Verse)

Example 20 with FormattedText

use of biblemulticonverter.data.FormattedText in project BibleMultiConverter by schierlm.

the class StrippedDiffable method mergeIntroductionPrologs.

protected void mergeIntroductionPrologs(Bible bible) {
    List<FormattedText> prologBuffer = new ArrayList<FormattedText>();
    for (int i = 0; i < bible.getBooks().size(); i++) {
        Book book = bible.getBooks().get(i);
        if (book.getId().getZefID() < 0) {
            if (book.getChapters().size() == 1) {
                Chapter ch = book.getChapters().get(0);
                if (ch.getVerses().size() > 0)
                    System.out.println("WARNING: Book " + book.getAbbr() + " has verses; not merged.");
                if (ch.getProlog() != null)
                    prologBuffer.add(ch.getProlog());
                else
                    System.out.println("WARNING: Book " + book.getAbbr() + " does not have a prolog; not merged.");
            } else {
                System.out.println("WARNING: Book " + book.getAbbr() + " has " + book.getChapters().size() + " chapters; not merged.");
            }
            bible.getBooks().remove(i);
            i--;
        } else if (prologBuffer.size() > 0 && book.getChapters().size() > 0) {
            Chapter ch = book.getChapters().get(0);
            if (ch.getProlog() != null)
                prologBuffer.add(ch.getProlog());
            FormattedText newProlog = new FormattedText();
            Visitor<RuntimeException> v = newProlog.getAppendVisitor();
            ch.setProlog(newProlog);
            boolean first = true;
            for (FormattedText oldProlog : prologBuffer) {
                if (!first)
                    v.visitLineBreak(LineBreakKind.PARAGRAPH);
                first = false;
                oldProlog.accept(v);
            }
            prologBuffer.clear();
        }
    }
    if (prologBuffer.size() > 0) {
        System.out.println("WARNING: " + prologBuffer.size() + " introduction prologs after last bible book were merged to first bible book!");
        for (int i = 0; i < bible.getBooks().size(); i++) {
            Book book = bible.getBooks().get(i);
            if (book.getId().getZefID() > 0 && prologBuffer.size() > 0 && book.getChapters().size() > 0) {
                Chapter ch = book.getChapters().get(0);
                Visitor<RuntimeException> v = ch.getProlog().getAppendVisitor();
                for (FormattedText oldProlog : prologBuffer) {
                    v.visitLineBreak(LineBreakKind.PARAGRAPH);
                    oldProlog.accept(v);
                }
                break;
            }
        }
    }
}
Also used : Visitor(biblemulticonverter.data.FormattedText.Visitor) Book(biblemulticonverter.data.Book) ArrayList(java.util.ArrayList) Chapter(biblemulticonverter.data.Chapter) FormattedText(biblemulticonverter.data.FormattedText)

Aggregations

FormattedText (biblemulticonverter.data.FormattedText)31 Chapter (biblemulticonverter.data.Chapter)25 Book (biblemulticonverter.data.Book)24 Verse (biblemulticonverter.data.Verse)22 Bible (biblemulticonverter.data.Bible)14 BookID (biblemulticonverter.data.BookID)10 Headline (biblemulticonverter.data.FormattedText.Headline)9 ArrayList (java.util.ArrayList)9 MetadataBook (biblemulticonverter.data.MetadataBook)8 IOException (java.io.IOException)8 Visitor (biblemulticonverter.data.FormattedText.Visitor)7 VirtualVerse (biblemulticonverter.data.VirtualVerse)7 File (java.io.File)6 EnumMap (java.util.EnumMap)5 HashMap (java.util.HashMap)4 Matcher (java.util.regex.Matcher)4 Element (org.w3c.dom.Element)3 Node (org.w3c.dom.Node)3 ExtraAttributePriority (biblemulticonverter.data.FormattedText.ExtraAttributePriority)2 FormattingInstructionKind (biblemulticonverter.data.FormattedText.FormattingInstructionKind)2