Search in sources :

Example 1 with OrgParserSettings

use of com.orgzly.org.parser.OrgParserSettings in project orgzly-android by orgzly.

the class Shelf method writeBookToFile.

/**
 * Writes content of book from database to specified file.
 * TODO: Do in Provider under transaction
 */
public void writeBookToFile(final Book book, BookName.Format format, File file) throws IOException {
    /* Use the same encoding. */
    String encoding = book.getUsedEncoding();
    if (encoding == null) {
        encoding = Charset.defaultCharset().name();
    }
    final PrintWriter out = new PrintWriter(file, encoding);
    try {
        String separateNotesWithNewLine = AppPreferences.separateNotesWithNewLine(mContext);
        String createdAtPropertyName = AppPreferences.createdAtProperty(mContext);
        boolean useCreatedAtProperty = AppPreferences.createdAt(mContext);
        OrgParserSettings parserSettings = OrgParserSettings.getBasic();
        if (mContext.getString(R.string.pref_value_separate_notes_with_new_line_always).equals(separateNotesWithNewLine)) {
            parserSettings.separateNotesWithNewLine = OrgParserSettings.SeparateNotesWithNewLine.ALWAYS;
        } else if (mContext.getString(R.string.pref_value_separate_notes_with_new_line_multi_line_notes_only).equals(separateNotesWithNewLine)) {
            parserSettings.separateNotesWithNewLine = OrgParserSettings.SeparateNotesWithNewLine.MULTI_LINE_NOTES_ONLY;
        } else if (mContext.getString(R.string.pref_value_separate_notes_with_new_line_never).equals(separateNotesWithNewLine)) {
            parserSettings.separateNotesWithNewLine = OrgParserSettings.SeparateNotesWithNewLine.NEVER;
        }
        parserSettings.separateHeaderAndContentWithNewLine = AppPreferences.separateHeaderAndContentWithNewLine(mContext);
        parserSettings.tagsColumn = AppPreferences.tagsColumn(mContext);
        parserSettings.orgIndentMode = AppPreferences.orgIndentMode(mContext);
        parserSettings.orgIndentIndentationPerLevel = AppPreferences.orgIndentIndentationPerLevel(mContext);
        final OrgParserWriter parserWriter = new OrgParserWriter(parserSettings);
        // Write preface
        out.write(parserWriter.whiteSpacedFilePreface(book.getPreface()));
        // Write notes
        NotesClient.forEachBookNote(mContext, book.getName(), note -> {
            // Update note properties with created-at property, if the time exists.
            if (useCreatedAtProperty && createdAtPropertyName != null && note.getCreatedAt() > 0) {
                OrgDateTime time = new OrgDateTime(note.getCreatedAt(), false);
                note.getHead().addProperty(createdAtPropertyName, time.toString());
            }
            out.write(parserWriter.whiteSpacedHead(note.getHead(), note.getPosition().getLevel(), book.getOrgFileSettings().isIndented()));
        });
    } finally {
        out.close();
    }
}
Also used : OrgParserSettings(com.orgzly.org.parser.OrgParserSettings) OrgDateTime(com.orgzly.org.datetime.OrgDateTime) PrintWriter(java.io.PrintWriter) OrgParserWriter(com.orgzly.org.parser.OrgParserWriter)

Aggregations

OrgDateTime (com.orgzly.org.datetime.OrgDateTime)1 OrgParserSettings (com.orgzly.org.parser.OrgParserSettings)1 OrgParserWriter (com.orgzly.org.parser.OrgParserWriter)1 PrintWriter (java.io.PrintWriter)1