Search in sources :

Example 1 with OrgParserWriter

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

the class Shelf method reParseNotesStateAndTitles.

/**
 * Using current states configuration, update states and titles for all notes.
 * Keywords that were part of the title can become states and vice versa.
 */
public void reParseNotesStateAndTitles() throws IOException {
    ArrayList<ContentProviderOperation> ops = new ArrayList<>();
    /* Get all notes. */
    Cursor cursor = mContext.getContentResolver().query(ProviderContract.Notes.ContentUri.notes(), null, null, null, null);
    try {
        OrgParser.Builder parserBuilder = new OrgParser.Builder().setTodoKeywords(AppPreferences.todoKeywordsSet(mContext)).setDoneKeywords(AppPreferences.doneKeywordsSet(mContext));
        OrgParserWriter parserWriter = new OrgParserWriter();
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            /* Get current heading string. */
            Note note = NotesClient.fromCursor(cursor);
            /* Skip root node. */
            if (note.getPosition().getLevel() == 0) {
                continue;
            }
            OrgHead head = note.getHead();
            String headString = parserWriter.whiteSpacedHead(head, note.getPosition().getLevel(), false);
            /* Re-parse heading using current setting of keywords. */
            OrgParsedFile file = parserBuilder.setInput(headString).build().parse();
            if (BuildConfig.LOG_DEBUG)
                LogUtils.d(TAG, "Note " + note + " parsed has " + file.getHeadsInList().size() + " notes");
            if (file.getHeadsInList().size() != 1) {
                throw new IOException("Got " + file.getHeadsInList().size() + " notes after parsing \"" + headString + "\"");
            }
            OrgHead newHead = file.getHeadsInList().get(0).getHead();
            ContentValues values = new ContentValues();
            /* Update if state, title or priority are different. */
            if (!TextUtils.equals(newHead.getState(), head.getState()) || !TextUtils.equals(newHead.getTitle(), head.getTitle()) || !TextUtils.equals(newHead.getPriority(), head.getPriority())) {
                values.put(ProviderContract.Notes.UpdateParam.TITLE, newHead.getTitle());
                values.put(ProviderContract.Notes.UpdateParam.STATE, newHead.getState());
                values.put(ProviderContract.Notes.UpdateParam.PRIORITY, newHead.getPriority());
            }
            if (values.size() > 0) {
                ops.add(ContentProviderOperation.newUpdate(ContentUris.withAppendedId(ProviderContract.Notes.ContentUri.notes(), cursor.getLong(0))).withValues(values).build());
            }
        }
    } finally {
        cursor.close();
    }
    /*
         * Apply batch.
         */
    try {
        mContext.getContentResolver().applyBatch(ProviderContract.AUTHORITY, ops);
    } catch (RemoteException | OperationApplicationException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    notifyDataChanged(mContext);
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) OrgHead(com.orgzly.org.OrgHead) CircularArrayList(com.orgzly.android.util.CircularArrayList) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Cursor(android.database.Cursor) OrgParserWriter(com.orgzly.org.parser.OrgParserWriter) OrgParsedFile(com.orgzly.org.parser.OrgParsedFile) OrgParser(com.orgzly.org.parser.OrgParser) DbNote(com.orgzly.android.provider.models.DbNote) RemoteException(android.os.RemoteException) OperationApplicationException(android.content.OperationApplicationException)

Example 2 with OrgParserWriter

use of com.orgzly.org.parser.OrgParserWriter 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)

Example 3 with OrgParserWriter

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

the class NoteFragment method noteHash.

/**
 * Hash used to detect note modifications.
 */
private long noteHash(Note note) {
    OrgParserWriter parserWriter = new OrgParserWriter();
    String head = parserWriter.whiteSpacedHead(note.getHead(), note.getPosition().getLevel(), false);
    return MiscUtils.sha1(head);
}
Also used : OrgParserWriter(com.orgzly.org.parser.OrgParserWriter)

Example 4 with OrgParserWriter

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

the class LipsumBookGenerator method generateOrgString.

public static String generateOrgString(int content, int[] notesAndContent) {
    StringBuilder result = new StringBuilder();
    LoremIpsum loremIpsum = new LoremIpsum();
    OrgParserWriter parserWriter = new OrgParserWriter();
    result.append(parserWriter.whiteSpacedFilePreface(loremIpsum.getWords(content / CHARS_PER_WORD)));
    if (notesAndContent != null) {
        for (int i = 0; i < notesAndContent.length; i += 2) {
            OrgHead head = new OrgHead();
            head.setTitle(loremIpsum.getWords(notesAndContent[i] / CHARS_PER_WORD));
            head.setContent(loremIpsum.getWords(notesAndContent[i + 1] / CHARS_PER_WORD));
            result.append(parserWriter.whiteSpacedHead(head, 1, false));
        }
    }
    return result.toString();
}
Also used : LoremIpsum(de.svenjacobs.loremipsum.LoremIpsum) OrgHead(com.orgzly.org.OrgHead) OrgParserWriter(com.orgzly.org.parser.OrgParserWriter)

Aggregations

OrgParserWriter (com.orgzly.org.parser.OrgParserWriter)4 OrgHead (com.orgzly.org.OrgHead)2 ContentProviderOperation (android.content.ContentProviderOperation)1 ContentValues (android.content.ContentValues)1 OperationApplicationException (android.content.OperationApplicationException)1 Cursor (android.database.Cursor)1 RemoteException (android.os.RemoteException)1 DbNote (com.orgzly.android.provider.models.DbNote)1 CircularArrayList (com.orgzly.android.util.CircularArrayList)1 OrgDateTime (com.orgzly.org.datetime.OrgDateTime)1 OrgParsedFile (com.orgzly.org.parser.OrgParsedFile)1 OrgParser (com.orgzly.org.parser.OrgParser)1 OrgParserSettings (com.orgzly.org.parser.OrgParserSettings)1 LoremIpsum (de.svenjacobs.loremipsum.LoremIpsum)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1