001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.boot;
019
020import java.io.File;
021import java.util.List;
022import java.util.Map;
023
024import static io.stallion.utils.Literals.*;
025
026import com.sun.prism.Texture;
027import io.stallion.dataAccess.DataAccessRegistry;
028import io.stallion.dataAccess.file.TextFilePersister;
029import io.stallion.exceptions.CommandException;
030import io.stallion.exceptions.UsageException;
031import io.stallion.services.Log;
032import io.stallion.settings.ContentFolder;
033import io.stallion.settings.Settings;
034import io.stallion.utils.DateUtils;
035import io.stallion.utils.GeneralUtils;
036import io.stallion.utils.Prompter;
037import io.stallion.utils.SimpleTemplate;
038import org.apache.commons.io.FileUtils;
039import org.apache.commons.lang3.StringUtils;
040
041
042public class NewDraftPageAction implements StallionRunAction<CommandOptionsBase>  {
043    @Override
044    public String getActionName() {
045        return "new-draft-page";
046    }
047
048    @Override
049    public String getHelp() {
050        return "Create a new draft, markdown page or blog post.";
051    }
052
053    @Override
054    public void loadApp(CommandOptionsBase options) {
055        AppContextLoader.loadCompletely(options);
056    }
057
058    public void execute(CommandOptionsBase options) throws Exception {
059        int x = 1;
060        ContentFolder cf = null;
061        if (Settings.instance().getFolders().size() == 0) {
062            throw new UsageException("Your site does not have any content folders defined.");
063        } else if (Settings.instance().getFolders().size() == 1) {
064            cf = Settings.instance().getFolders().get(0);
065        }
066        if (cf == null) {
067            for (ContentFolder folder : Settings.instance().getFolders()) {
068                System.out.println(x + ") " + folder.getBucket());
069                x++;
070            }
071            String folder = Prompter.prompt("Choose a folder from the list above: ").trim();
072
073            if (StringUtils.isNumeric(folder)) {
074                Integer num = Integer.parseInt(folder) - 1;
075                if (num >= 0 && num < Settings.instance().getFolders().size()) {
076                    cf = Settings.instance().getFolders().get(num);
077                }
078            }
079            if (cf == null) {
080                for (ContentFolder afolder : Settings.instance().getFolders()) {
081                    if (folder.equals(afolder.getBucket())) {
082                        cf = afolder;
083                    }
084                }
085            }
086            if (cf == null) {
087                throw new CommandException("Could not find folder: " + folder);
088            }
089        }
090
091
092        String title = Prompter.prompt("Choose a post title: ");
093        String slug = "";
094        if (!empty(cf.getListingRootUrl())) {
095            slug = cf.getListingRootUrl();
096        }
097        if (!slug.endsWith("/")) {
098            slug += "/";
099        }
100        slug += GeneralUtils.slugify(title);
101
102        String postContent = new SimpleTemplate(postTemplate)
103                .put("publishDate", "2099-01-01 11:15:00 America/New_York")
104                .put("id", DataAccessRegistry.instance().getTickets().nextId())
105                .put("slug", slug)
106                .put("title", title)
107                .put("siteUrl", Settings.instance().getSiteUrl())
108                .put("previewKey", GeneralUtils.randomToken(8))
109                .render();
110        String blogFolderPath = ((TextFilePersister)DataAccessRegistry.instance().get(cf.getBucket()).getPersister()).getBucketFolderPath();
111        String fileName =  blogFolderPath + "/" + DateUtils.formatNow("yyyy-MM-dd") + "-" + GeneralUtils.slugify(title) + ".txt";
112        File file = new File(fileName);
113        FileUtils.write(file, postContent, "UTF-8");
114        System.out.println("Successfully wrote new blog post to file: " + file.getAbsolutePath());
115    }
116
117    private static final String postTemplate = "{ title }\n=====================================\n" +
118            "publishDate: { publishDate }\n" +
119            "slug: { slug }\n" +
120            "id: { id }\n" +
121            "author: \n" +
122            "previewKey: {previewKey}\n" +
123            "metaDescription: \n" +
124            "\n" +
125            "Hello, I am a brand new blog post. I can be previewed at [{siteUrl}{slug}?stPreview={previewKey}]({siteUrl}{slug}?stPreview={previewKey})\n\n" +
126            "When you are ready to publish me, change the publishDate to a near future or past date.";
127
128
129}