예제 #1
0
        public void GetPostTest()
        {
            var weblogInfo = new WeblogInfo
            {
                ApiUrl = STR_JEKYLL_PROJECT_FOLDER,
                Name   = "Jekyll Test Blog"
            };


            var pub = new LocalJekyllPublisher(null, weblogInfo, null);

            var post = pub.GetPost(STR_JEKYLL_POST_ID);

            Assert.IsNotNull(post);
            Assert.IsNotNull(post.Body);
        }
예제 #2
0
        /// <summary>
        /// Returns a Post by Id
        /// </summary>
        /// <param name="postId"></param>
        /// <param name="weblogInfo"></param>
        /// <returns></returns>
        public Post GetPost(string postId, WeblogInfo weblogInfo)
        {
            if (weblogInfo.Type == WeblogTypes.MetaWeblogApi || weblogInfo.Type == WeblogTypes.Wordpress)
            {
                MetaWebLogWordpressApiClient client;
                client = new MetaWebLogWordpressApiClient(weblogInfo);
                return(client.GetPost(postId));
            }

            if (weblogInfo.Type == WeblogTypes.LocalJekyll)
            {
                var pub = new LocalJekyllPublisher(null, weblogInfo, null);
                return(pub.GetPost(postId));
            }

            // Medium doesn't support post retrieval so return null
            return(null);
        }
예제 #3
0
        public void PublishTest()
        {
            var webLogInfo = new WeblogInfo
            {
                ApiUrl = STR_JEKYLL_PROJECT_FOLDER,
                Name   = "Jekyll Test Blog"
            };

            var rawMd = System.IO.File.ReadAllText(STR_MM_POST_FILE_NAME);

            var post = new Post();  // filled from meta data but not used here
            var meta = WeblogPostMetadata.GetPostYamlConfigFromMarkdown(rawMd);


            var pub = new LocalJekyllPublisher(meta, webLogInfo, STR_MM_POST_FILE_NAME);

            pub.PublishPost(false);
        }
        public Task DownloadJekyllPosts()
        {
            var publisher = new LocalJekyllPublisher(Model.ActivePostMetadata, Model.ActiveWeblogInfo, null);

            StatusBar.ShowStatusProgress($"Downloading last {Model.NumberOfPostsToRetrieve} posts...");

            var posts = publisher.GetRecentPosts(Model.NumberOfPostsToRetrieve)?.ToList();

            if (posts == null)
            {
                StatusBar.ShowStatusError($"An error occurred trying to retrieve posts: {publisher.ErrorMessage}");
            }

            Dispatcher.Invoke(() =>
            {
                StatusBar.ShowStatusSuccess($"{posts.Count} posts downloaded.");
                Model.PostList = posts;
            });

            return(Task.CompletedTask);
        }
예제 #5
0
        public void GetPosts()
        {
            var webLogInfo = new WeblogInfo
            {
                ApiUrl = STR_JEKYLL_PROJECT_FOLDER,
                Name   = "Jekyll Test Blog"
            };

            var rawMd = System.IO.File.ReadAllText(STR_MM_POST_FILE_NAME);

            var post = new Post();  // filled from meta data but not used here
            var meta = WeblogPostMetadata.GetPostYamlConfigFromMarkdown(rawMd, post);


            var pub   = new LocalJekyllPublisher(meta, webLogInfo, null);
            var posts = pub.GetRecentPosts(20).ToList();

            Console.WriteLine(posts.Count);

            foreach (var pst in posts)
            {
                Console.WriteLine($"{pst.Title} -  {pst.mt_excerpt}");
            }
        }
        private async Task CreateDownloadedPost()
        {
            var item = ListViewPosts.SelectedItem as Post;

            if (item == null)
            {
                return;
            }

            WeblogInfo weblogInfo = Model.ActiveWeblogInfo;

            StatusBar.ShowStatusProgress("Downloading Weblog post '" + item.Title + "'");


            string postId = item.PostId?.ToString();

            Post post = null;

            if (weblogInfo.Type == WeblogTypes.MetaWeblogApi)
            {
                var wrapper = new MetaWeblogWrapper(weblogInfo.ApiUrl,
                                                    weblogInfo.Username,
                                                    mmApp.DecryptString(weblogInfo.Password),
                                                    weblogInfo.BlogId);

                try
                {
                    post = await Task.Run(() => wrapper.GetPost(postId));
                }
                catch (Exception ex)
                {
                    StatusBar.ShowStatusError("Unable to download post.\r\n\r\n" + ex.Message);
                    return;
                }

                Model.Addin.CreateDownloadedPostOnDisk(post, weblogInfo.Name);
            }
            else if (weblogInfo.Type == WeblogTypes.Wordpress)
            {
                var wrapper = new WordPressWrapper(weblogInfo.ApiUrl,
                                                   weblogInfo.Username,
                                                   mmApp.DecryptString(weblogInfo.Password));

                try
                {
                    post = wrapper.GetPost(postId);
                }
                catch (Exception ex)
                {
                    StatusBar.ShowStatus();
                    StatusBar.ShowStatusError("Unable to download post.\r\n\r\n" + ex.Message);
                    return;
                }

                Model.Addin.CreateDownloadedPostOnDisk(post, weblogInfo.Name);
            }
            else if (weblogInfo.Type == WeblogTypes.LocalJekyll)
            {
                var pub = new LocalJekyllPublisher(null, weblogInfo, null);
                post = pub.GetPost(postId);
                if (post == null)
                {
                    StatusBar.ShowStatusError("Unable to import post from Jekyll.");
                    return;
                }

                string outputFile = pub.CreateDownloadedPostOnDisk(post, weblogInfo.Name);

                mmApp.Model.Window.OpenTab(outputFile);
                mmApp.Model.Window.ShowFolderBrowser(folder: Path.GetDirectoryName(outputFile));
            }


            Close();
            StatusBar.ShowStatusSuccess("Post has been imported into Markdown Monster Web log Posts.");
        }
예제 #7
0
        /// <summary>
        /// High level method that sends posts to the Weblog
        ///
        /// </summary>
        /// <returns></returns>
        public async Task <bool> SendPost(WeblogInfo weblogInfo, bool sendAsDraft = false)
        {
            var editor = Model.ActiveEditor;

            if (editor == null)
            {
                return(false);
            }

            var doc = editor.MarkdownDocument;

            WeblogModel.ActivePost = new Post()
            {
                DateCreated = DateTime.Now
            };

            // start by retrieving the current Markdown from the editor
            string markdown = editor.MarkdownDocument.CurrentText;

            // Retrieve Meta data from post and clean up the raw markdown
            // so we render without the config data
            var meta = WeblogPostMetadata.GetPostConfigFromMarkdown(markdown, WeblogModel.ActivePost, weblogInfo);

            string html = doc.RenderHtml(meta.MarkdownBody, usePragmaLines: false);

            WeblogModel.ActivePost.Body       = html;
            WeblogModel.ActivePost.PostId     = meta.PostId;
            WeblogModel.ActivePost.PostStatus = meta.PostStatus;
            WeblogModel.ActivePost.Permalink  = meta.Permalink;

            // Custom Field Processing:
            // Add custom fields from existing post
            // then add or update our custom fields
            var customFields = new Dictionary <string, CustomField>();

            // load existing custom fields from post online if possible
            if (!string.IsNullOrEmpty(meta.PostId))
            {
                var existingPost = GetPost(meta.PostId, weblogInfo);
                if (existingPost != null && meta.CustomFields != null && existingPost.CustomFields != null)
                {
                    foreach (var kvp in existingPost.CustomFields)
                    {
                        if (!customFields.ContainsKey(kvp.Key))
                        {
                            AddOrUpdateCustomField(customFields, kvp.Key, kvp.Value);
                        }
                    }
                }
            }
            // add custom fields from Weblog configuration
            if (weblogInfo.CustomFields != null)
            {
                foreach (var kvp in weblogInfo.CustomFields)
                {
                    AddOrUpdateCustomField(customFields, kvp.Key, kvp.Value);
                }
            }
            // add custom fields from Meta data
            if (meta.CustomFields != null)
            {
                foreach (var kvp in meta.CustomFields)
                {
                    AddOrUpdateCustomField(customFields, kvp.Key, kvp.Value.Value);
                }
            }

            if (!string.IsNullOrEmpty(markdown))
            {
                AddOrUpdateCustomField(customFields, "mt_markdown", markdown);
            }

            WeblogModel.ActivePost.CustomFields = customFields.Values.ToArray();

            var config = WeblogAddinConfiguration.Current;

            var kv = config.Weblogs.FirstOrDefault(kvl => kvl.Value.Name == meta.WeblogName);

            if (kv.Equals(default(KeyValuePair <string, WeblogInfo>)))
            {
                MessageBox.Show("Invalid Weblog configuration selected.",
                                "Weblog Posting Failed",
                                MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return(false);
            }
            weblogInfo = kv.Value;

            var type = weblogInfo.Type;

            if (type == WeblogTypes.Unknown)
            {
                type = weblogInfo.Type;
            }


            string previewUrl = weblogInfo.PreviewUrl;
            string basePath   = Path.GetDirectoryName(doc.Filename);
            string postUrl    = null;

            if (type == WeblogTypes.MetaWeblogApi || type == WeblogTypes.Wordpress)
            {
                MetaWebLogWordpressApiClient client;
                client = new MetaWebLogWordpressApiClient(weblogInfo);

                // if values are already configured don't overwrite them again
                client.DontInferFeaturedImage = meta.DontInferFeaturedImage;
                client.FeaturedImageUrl       = meta.FeaturedImageUrl;
                client.FeatureImageId         = meta.FeaturedImageId;

                var result = await Task.Run <bool>(() => client.PublishCompletePost(WeblogModel.ActivePost, basePath,
                                                                                    sendAsDraft, markdown));

                //if (!client.PublishCompletePost(WeblogModel.ActivePost, basePath,
                //    sendAsDraft, markdown))
                if (!result)
                {
                    mmApp.Log($"Error sending post to Weblog at {weblogInfo.ApiUrl}: " + client.ErrorMessage);
                    MessageBox.Show("Error sending post to Weblog: " + client.ErrorMessage,
                                    mmApp.ApplicationName,
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Exclamation);
                    return(false);
                }

                var post = client.GetPost(WeblogModel.ActivePost.PostId);
                if (post != null)
                {
                    postUrl        = post.Url;
                    meta.Permalink = post.Permalink;
                }
            }
            if (type == WeblogTypes.Medium)
            {
                var client = new MediumApiClient(weblogInfo);
                var post   = client.PublishCompletePost(WeblogModel.ActivePost, basePath, sendAsDraft);
                if (post == null)
                {
                    mmApp.Log($"Error sending post to Weblog at {weblogInfo.ApiUrl}: " + client.ErrorMessage);
                    MessageBox.Show($"Error sending post to Weblog: " + client.ErrorMessage,
                                    mmApp.ApplicationName,
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Exclamation);
                    return(false);
                }

                // this is null
                postUrl = client.PostUrl;
            }

            if (type == WeblogTypes.LocalJekyll)
            {
                var pub = new LocalJekyllPublisher(meta, weblogInfo, Model.ActiveDocument.Filename);
                pub.PublishPost(false);

                if (!string.IsNullOrEmpty(weblogInfo.LaunchCommand))
                {
                    if (!pub.BuildAndLaunchSite())
                    {
                        ShowStatusError(pub.ErrorMessage);
                        return(false);
                    }
                    previewUrl = null;
                    postUrl    = pub.GetPostUrl(weblogInfo.PreviewUrl ?? "http://localhost:4000/{0}");
                }
            }

            meta.PostId = WeblogModel.ActivePost.PostId?.ToString();

            // retrieve the raw editor markdown
            markdown             = editor.MarkdownDocument.CurrentText;
            meta.RawMarkdownBody = markdown;

            // add the meta configuration to it
            markdown = meta.SetPostYamlFromMetaData();

            // write it back out to editor
            editor.SetMarkdown(markdown, updateDirtyFlag: true, keepUndoBuffer: true);

            try
            {
                if (!string.IsNullOrEmpty(postUrl))
                {
                    ShellUtils.GoUrl(postUrl);
                }
                else if (!string.IsNullOrEmpty(previewUrl))
                {
                    var url = string.Format(previewUrl, WeblogModel.ActivePost.PostId);
                    ShellUtils.GoUrl(url);
                }
                else
                {
                    ShellUtils.GoUrl(new Uri(weblogInfo.ApiUrl).GetLeftPart(UriPartial.Authority));
                }
            }
            catch
            {
                mmApp.Log("Failed to display Weblog Url after posting: " +
                          weblogInfo.PreviewUrl ?? postUrl ?? weblogInfo.ApiUrl);
            }

            return(true);
        }