Exemplo n.º 1
0
  /** Returns all drafts sorted by title. */
  public Observable<List<Draft>> getAllDrafts() {
    return fileManager
        .getTree()
        .flatMap(
            new Func1<DirNode, Observable<List<Draft>>>() {
              @Override
              public Observable<List<Draft>> call(DirNode dirNode) {
                // check if drafts dir exists
                List<Draft> drafts = new ArrayList<>();
                if (!dirNode.getEntries().containsKey(DIR_DRAFTS)) return Observable.just(drafts);

                // parse titles
                DirNode draftsDir = (DirNode) dirNode.getEntries().get(DIR_DRAFTS);
                for (AbstractNode draftNode : draftsDir.getEntries().values()) {
                  if (!(draftNode instanceof FileNode)) continue;
                  Optional<Draft> draft = parseDraft((FileNode) draftNode);
                  if (draft.isPresent()) drafts.add(draft.get());
                }

                // sort by title
                Collections.sort(drafts);

                return Observable.just(drafts);
              }
            });
  }
Exemplo n.º 2
0
  /** Returns all posts sorted by date with the newest first. */
  public Observable<List<Post>> getAllPosts() {
    return fileManager
        .getTree()
        .flatMap(
            new Func1<DirNode, Observable<List<Post>>>() {
              @Override
              public Observable<List<Post>> call(DirNode dirNode) {
                // check if post dir exists
                List<Post> posts = new ArrayList<>();
                if (!dirNode.getEntries().containsKey(DIR_POSTS)) return Observable.just(posts);

                // parse titles
                DirNode postsDir = (DirNode) dirNode.getEntries().get(DIR_POSTS);
                for (AbstractNode postNode : postsDir.getEntries().values()) {
                  if (!(postNode instanceof FileNode)) continue;
                  Optional<Post> post = parsePost((FileNode) postNode);
                  if (post.isPresent()) posts.add(post.get());
                }

                // sort by date
                Collections.sort(posts);
                Collections.reverse(posts);

                return Observable.just(posts);
              }
            });
  }
Exemplo n.º 3
0
  /** Creates and returns a new draft file (locally). */
  public Observable<Draft> createNewDraft(final String title) {
    return fileManager
        .getTree()
        .flatMap(
            new Func1<DirNode, Observable<Draft>>() {
              @Override
              public Observable<Draft> call(DirNode rootNode) {
                try {
                  // create draft file and setup front matter
                  FileNode draftNode =
                      fileManager.createNewFile(
                          assertDir(rootNode, DIR_DRAFTS), draftTitleToFilename(title));
                  setupDefaultFrontMatter(draftNode, title);
                  return Observable.just(new Draft(title, draftNode));

                } catch (IOException ioe) {
                  return Observable.error(ioe);
                }
              }
            });
  }
Exemplo n.º 4
0
 /**
  * Moves a previously created post to the _drafts folder.
  *
  * @return the newly created draft.
  */
 public Observable<Draft> unpublishPost(final Post post) {
   final String draftTitle = draftTitleToFilename(post.getTitle());
   return fileManager
       .getTree()
       // move draft
       .flatMap(
           new Func1<DirNode, Observable<FileNode>>() {
             @Override
             public Observable<FileNode> call(DirNode rootDir) {
               DirNode draftsDir = assertDir(rootDir, DIR_DRAFTS);
               return fileManager.moveFile(post.getFileNode(), draftsDir, draftTitle);
             }
           })
       // create draft object
       .flatMap(
           new Func1<FileNode, Observable<Draft>>() {
             @Override
             public Observable<Draft> call(FileNode newNode) {
               return Observable.just(new Draft(draftTitle, newNode));
             }
           });
 }
Exemplo n.º 5
0
  /** Creates and returns a new post file (locally). */
  public Observable<Post> createNewPost(final String title) {
    return fileManager
        .getTree()
        .flatMap(
            new Func1<DirNode, Observable<Post>>() {
              @Override
              public Observable<Post> call(DirNode rootNode) {
                try {
                  // create post file and setup front matter
                  FileNode postNode =
                      fileManager.createNewFile(
                          assertDir(rootNode, DIR_POSTS), postTitleToFilename(title));
                  setupDefaultFrontMatter(postNode, title);
                  return Observable.just(
                      new Post(title, Calendar.getInstance().getTime(), postNode));

                } catch (IOException ioe) {
                  return Observable.error(ioe);
                }
              }
            });
  }
Exemplo n.º 6
0
 /**
  * Publishes a previously created draft to the _posts folder.
  *
  * @return the newly created post.
  */
 public Observable<Post> publishDraft(final Draft draft) {
   final String postTitle = postTitleToFilename(draft.getTitle());
   return fileManager
       .getTree()
       // move draft
       .flatMap(
           new Func1<DirNode, Observable<FileNode>>() {
             @Override
             public Observable<FileNode> call(DirNode rootDir) {
               DirNode postsDir = assertDir(rootDir, DIR_POSTS);
               return fileManager.moveFile(draft.getFileNode(), postsDir, postTitle);
             }
           })
       // create post object
       .flatMap(
           new Func1<FileNode, Observable<Post>>() {
             @Override
             public Observable<Post> call(FileNode newNode) {
               return Observable.just(
                   new Post(postTitle, Calendar.getInstance().getTime(), newNode));
             }
           });
 }