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);
}
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();
}
}
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);
}
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();
}
Aggregations