Skip to content

Instantly share code, notes, and snippets.

@oyakodon
Created June 9, 2016 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oyakodon/1f2f3af24890cd804ac7b5e5a32800d8 to your computer and use it in GitHub Desktop.
Save oyakodon/1f2f3af24890cd804ac7b5e5a32800d8 to your computer and use it in GitHub Desktop.
ブログ記事作成支援ツール / C#

ブログ記事作成支援ツール

僕のブログのテンプレート部分を自動生成するプログラムです。
スクリーンショット

完全自分仕様で気が向いて作ってみましたが、リンクの挿入部は
ちょっとは使えるのではなかろうか?と思います。
async / awaitとか、EventHandlerとかの復習もできて良かったです。

使い方

  1. 起動した時点でテンプレートが自動生成されて編集できるようになっています
  2. 記事を書きます
      その時、リンクを挿入したくなったらURLを打って「挿入」を クリックすると編集中のカーソルのところに
      マークダウン記法でリンクが挿入されます(「本文の位置に挿入」チェック時・下のボックスからコピペも可)
  3. タイトルを打って「変更」をクリックすると記事の『「」のお話』の部分が置き換わり、上のボックスに
      「ですけど。」の付いたタイトルが表示されます。
  4. はてなブログの記事作成画面にコピペして投稿。

注意

動かす人がいればですが、アイコンを同梱していないので、そこでエラー吐くかもしれません。

作った人

親子丼
Twitter : @alltekito
Blog : http://oykdn.hatenablog.com/

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace BlogTemplate
{
public partial class Form1 : Form
{
private bool saved = false;
public Form1()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.FixedSingle;
setContent();
EventHandler handler = null;
handler = (s, e) => getUrlTitle(text_Url.Text);
btn_getLink.Click += handler;
EventHandler exithandler = null;
exithandler = (s, e) => this.Close();
MenuItem_Exit.Click += exithandler;
EventHandler savehandler = null;
savehandler = (s, e) => Save();
MenuItem_Save.Click += savehandler;
}
private async void getUrlTitle(string url)
{
text_Md.Enabled = false;
var client = new WebClient();
client.Encoding = Encoding.UTF8;
try
{
var t = client.DownloadStringTaskAsync(url);
var s = await t;
var regmch = Regex.Match(s, @"<title>(?<title>.*?)</title>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
var title = regmch.Groups["title"].Value;
text_Md.Enabled = true;
text_Md.Text = string.Format("[{0}]({1})", title, url);
if (check_Insert.Checked)
{
if (text_Content.SelectedText.Length == 0)
text_Content.SelectedText = text_Md.Text + " " + Environment.NewLine;
}
}
catch (Exception)
{
text_Md.Enabled = false;
text_Md.Text = "Failed to get title of webpage.";
}
}
private void button1_Click(object sender, EventArgs e)
{
var title = text_Title.Text;
text_Desukedo.Enabled = true;
text_Desukedo.Text = title + "、ですけど。";
text_Content.Text = Regex.Replace(text_Content.Text, @"今日は「.*」のお話でした", "今日は「" + title + "」のお話でした");
}
private void setContent()
{
var content = @"おやこどぅぉーーん。
どうも、親子丼です。
<DATE>
ということで、今日は「」のお話でした、では今日はこの辺で。
それでは、また。
親子丼でした。
---
※今日の一言
 
";
var today = DateTime.Now.Date;
var ci = new System.Globalization.CultureInfo("en-US");
var date = "## " + today.ToString("yyyy/M/d (ddd)", ci);
text_Content.Text = content.Replace("<DATE>", date);
}
private void MenuItem_AcoPane_Click(object sender, EventArgs e)
{
var acopane_source = @"<div class=""menu"">
<label for=""Panel1"">[+] コードを表示</label>
<input type=""checkbox"" id=""Panel1"" class=""on-off""/>
<ul>
<li>
<!-- コンテンツ -->
</li>
</ul>
</div> ";
if (text_Content.SelectedText.Length == 0)
text_Content.SelectedText = acopane_source + Environment.NewLine;
}
private void Save()
{
var sfd = new SaveFileDialog();
sfd.FileName = DateTime.Now.ToString("yyyy-M-d") + ".txt";
sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
sfd.Filter = "テキストファイル(*.txt)|*.txt|Markdownファイル(*.md)|*.md|すべてのファイル(*.*)|*.*";
sfd.Title = "保存先のファイルを選択してください";
if (sfd.ShowDialog() == DialogResult.OK)
{
try
{
var sw = new StreamWriter(sfd.FileName, false, Encoding.GetEncoding("UTF-8"));
sw.Write(text_Content.Text);
sw.Close();
MessageBox.Show("保存が完了しました。", "完了", MessageBoxButtons.OK, MessageBoxIcon.Information);
saved = true;
}
catch (Exception)
{
MessageBox.Show("保存中にエラーが発生しました。", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (!saved)
{
var dialog = MessageBox.Show("編集中の本文があります。\n保存しますか?", "確認", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
if (dialog == DialogResult.Yes)
{
Save();
} else
{
var dialog2 = MessageBox.Show("本当に終了しますか?", "確認", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (dialog2 == DialogResult.No)
{
e.Cancel = true;
}
}
}
}
}
}
namespace BlogTemplate
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.text_Content = new System.Windows.Forms.TextBox();
this.text_Title = new System.Windows.Forms.TextBox();
this.btn_ChContent = new System.Windows.Forms.Button();
this.text_Desukedo = new System.Windows.Forms.TextBox();
this.text_Url = new System.Windows.Forms.TextBox();
this.btn_getLink = new System.Windows.Forms.Button();
this.text_Md = new System.Windows.Forms.TextBox();
this.check_Insert = new System.Windows.Forms.CheckBox();
this.lab_Honbun = new System.Windows.Forms.Label();
this.lab_ChTitle = new System.Windows.Forms.Label();
this.lab_chtitle_Title = new System.Windows.Forms.Label();
this.lab_chtitle_Desukedo = new System.Windows.Forms.Label();
this.lab_mkLink = new System.Windows.Forms.Label();
this.lab_mklink_Url = new System.Windows.Forms.Label();
this.lab_mklick_Md = new System.Windows.Forms.Label();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.MenuItem_File = new System.Windows.Forms.ToolStripMenuItem();
this.MenuItem_Save = new System.Windows.Forms.ToolStripMenuItem();
this.MenuItem_Exit = new System.Windows.Forms.ToolStripMenuItem();
this.MenuItem_Template = new System.Windows.Forms.ToolStripMenuItem();
this.MenuItem_AcoPane = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// text_Content
//
this.text_Content.Location = new System.Drawing.Point(17, 56);
this.text_Content.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.text_Content.Multiline = true;
this.text_Content.Name = "text_Content";
this.text_Content.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.text_Content.Size = new System.Drawing.Size(548, 302);
this.text_Content.TabIndex = 0;
//
// text_Title
//
this.text_Title.Location = new System.Drawing.Point(676, 81);
this.text_Title.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.text_Title.Name = "text_Title";
this.text_Title.Size = new System.Drawing.Size(132, 24);
this.text_Title.TabIndex = 1;
//
// btn_ChContent
//
this.btn_ChContent.Location = new System.Drawing.Point(823, 81);
this.btn_ChContent.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.btn_ChContent.Name = "btn_ChContent";
this.btn_ChContent.Size = new System.Drawing.Size(83, 28);
this.btn_ChContent.TabIndex = 2;
this.btn_ChContent.Text = "変更";
this.btn_ChContent.UseVisualStyleBackColor = true;
this.btn_ChContent.Click += new System.EventHandler(this.button1_Click);
//
// text_Desukedo
//
this.text_Desukedo.Enabled = false;
this.text_Desukedo.Location = new System.Drawing.Point(716, 112);
this.text_Desukedo.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.text_Desukedo.Name = "text_Desukedo";
this.text_Desukedo.Size = new System.Drawing.Size(190, 24);
this.text_Desukedo.TabIndex = 3;
//
// text_Url
//
this.text_Url.Location = new System.Drawing.Point(656, 197);
this.text_Url.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.text_Url.Name = "text_Url";
this.text_Url.Size = new System.Drawing.Size(250, 24);
this.text_Url.TabIndex = 4;
//
// btn_getLink
//
this.btn_getLink.Location = new System.Drawing.Point(823, 233);
this.btn_getLink.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.btn_getLink.Name = "btn_getLink";
this.btn_getLink.Size = new System.Drawing.Size(83, 30);
this.btn_getLink.TabIndex = 5;
this.btn_getLink.Text = "取得";
this.btn_getLink.UseVisualStyleBackColor = true;
//
// text_Md
//
this.text_Md.Enabled = false;
this.text_Md.Location = new System.Drawing.Point(590, 299);
this.text_Md.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.text_Md.Name = "text_Md";
this.text_Md.Size = new System.Drawing.Size(316, 24);
this.text_Md.TabIndex = 6;
//
// check_Insert
//
this.check_Insert.AutoSize = true;
this.check_Insert.Font = new System.Drawing.Font("Meiryo UI", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.check_Insert.Location = new System.Drawing.Point(590, 238);
this.check_Insert.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.check_Insert.Name = "check_Insert";
this.check_Insert.Size = new System.Drawing.Size(161, 22);
this.check_Insert.TabIndex = 7;
this.check_Insert.Text = "本文の選択位置に挿入";
this.check_Insert.UseVisualStyleBackColor = true;
//
// lab_Honbun
//
this.lab_Honbun.AutoSize = true;
this.lab_Honbun.Font = new System.Drawing.Font("Meiryo UI", 10F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.lab_Honbun.Location = new System.Drawing.Point(12, 26);
this.lab_Honbun.Name = "lab_Honbun";
this.lab_Honbun.Size = new System.Drawing.Size(50, 18);
this.lab_Honbun.TabIndex = 8;
this.lab_Honbun.Text = "本文:";
//
// lab_ChTitle
//
this.lab_ChTitle.AutoSize = true;
this.lab_ChTitle.Font = new System.Drawing.Font("Meiryo UI", 10F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.lab_ChTitle.Location = new System.Drawing.Point(586, 59);
this.lab_ChTitle.Name = "lab_ChTitle";
this.lab_ChTitle.Size = new System.Drawing.Size(102, 18);
this.lab_ChTitle.TabIndex = 9;
this.lab_ChTitle.Text = "タイトルの変更:";
//
// lab_chtitle_Title
//
this.lab_chtitle_Title.AutoSize = true;
this.lab_chtitle_Title.Font = new System.Drawing.Font("Meiryo UI", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.lab_chtitle_Title.Location = new System.Drawing.Point(586, 84);
this.lab_chtitle_Title.Name = "lab_chtitle_Title";
this.lab_chtitle_Title.Size = new System.Drawing.Size(63, 18);
this.lab_chtitle_Title.TabIndex = 10;
this.lab_chtitle_Title.Text = "タイトル:";
//
// lab_chtitle_Desukedo
//
this.lab_chtitle_Desukedo.AutoSize = true;
this.lab_chtitle_Desukedo.Font = new System.Drawing.Font("Meiryo UI", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.lab_chtitle_Desukedo.Location = new System.Drawing.Point(586, 118);
this.lab_chtitle_Desukedo.Name = "lab_chtitle_Desukedo";
this.lab_chtitle_Desukedo.Size = new System.Drawing.Size(106, 18);
this.lab_chtitle_Desukedo.TabIndex = 11;
this.lab_chtitle_Desukedo.Text = "ですけどタイトル:";
//
// lab_mkLink
//
this.lab_mkLink.AutoSize = true;
this.lab_mkLink.Font = new System.Drawing.Font("Meiryo UI", 10F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.lab_mkLink.Location = new System.Drawing.Point(586, 176);
this.lab_mkLink.Name = "lab_mkLink";
this.lab_mkLink.Size = new System.Drawing.Size(196, 18);
this.lab_mkLink.TabIndex = 12;
this.lab_mkLink.Text = "リンクの挿入・Markdown生成:";
//
// lab_mklink_Url
//
this.lab_mklink_Url.AutoSize = true;
this.lab_mklink_Url.Font = new System.Drawing.Font("Meiryo UI", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.lab_mklink_Url.Location = new System.Drawing.Point(586, 207);
this.lab_mklink_Url.Name = "lab_mklink_Url";
this.lab_mklink_Url.Size = new System.Drawing.Size(50, 18);
this.lab_mklink_Url.TabIndex = 13;
this.lab_mklink_Url.Text = "URL:";
//
// lab_mklick_Md
//
this.lab_mklick_Md.AutoSize = true;
this.lab_mklick_Md.Font = new System.Drawing.Font("Meiryo UI", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.lab_mklick_Md.Location = new System.Drawing.Point(586, 276);
this.lab_mklick_Md.Name = "lab_mklick_Md";
this.lab_mklick_Md.Size = new System.Drawing.Size(94, 18);
this.lab_mklick_Md.TabIndex = 14;
this.lab_mklick_Md.Text = "Markdown:";
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.MenuItem_File,
this.MenuItem_Template});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(924, 24);
this.menuStrip1.TabIndex = 15;
this.menuStrip1.Text = "menuStrip1";
//
// MenuItem_File
//
this.MenuItem_File.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.MenuItem_Save,
this.MenuItem_Exit});
this.MenuItem_File.Name = "MenuItem_File";
this.MenuItem_File.Size = new System.Drawing.Size(70, 20);
this.MenuItem_File.Text = "ファイル(&F)";
//
// MenuItem_Save
//
this.MenuItem_Save.Name = "MenuItem_Save";
this.MenuItem_Save.Size = new System.Drawing.Size(152, 22);
this.MenuItem_Save.Text = "保存(&S)";
//
// MenuItem_Exit
//
this.MenuItem_Exit.Name = "MenuItem_Exit";
this.MenuItem_Exit.Size = new System.Drawing.Size(152, 22);
this.MenuItem_Exit.Text = "終了(&Q)";
//
// MenuItem_Template
//
this.MenuItem_Template.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.MenuItem_AcoPane});
this.MenuItem_Template.Name = "MenuItem_Template";
this.MenuItem_Template.Size = new System.Drawing.Size(94, 20);
this.MenuItem_Template.Text = "テンプレート(&T)";
//
// MenuItem_AcoPane
//
this.MenuItem_AcoPane.Name = "MenuItem_AcoPane";
this.MenuItem_AcoPane.Size = new System.Drawing.Size(175, 22);
this.MenuItem_AcoPane.Text = "アコーディオンパネル";
this.MenuItem_AcoPane.Click += new System.EventHandler(this.MenuItem_AcoPane_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 17F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(924, 372);
this.Controls.Add(this.lab_mklick_Md);
this.Controls.Add(this.lab_mklink_Url);
this.Controls.Add(this.lab_mkLink);
this.Controls.Add(this.lab_chtitle_Desukedo);
this.Controls.Add(this.lab_chtitle_Title);
this.Controls.Add(this.lab_ChTitle);
this.Controls.Add(this.lab_Honbun);
this.Controls.Add(this.check_Insert);
this.Controls.Add(this.text_Md);
this.Controls.Add(this.btn_getLink);
this.Controls.Add(this.text_Url);
this.Controls.Add(this.text_Desukedo);
this.Controls.Add(this.btn_ChContent);
this.Controls.Add(this.text_Title);
this.Controls.Add(this.text_Content);
this.Controls.Add(this.menuStrip1);
this.Font = new System.Drawing.Font("Meiryo UI", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MainMenuStrip = this.menuStrip1;
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "はてなブログ 記事作成支援ツール";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox text_Content;
private System.Windows.Forms.TextBox text_Title;
private System.Windows.Forms.Button btn_ChContent;
private System.Windows.Forms.TextBox text_Desukedo;
private System.Windows.Forms.TextBox text_Url;
private System.Windows.Forms.Button btn_getLink;
private System.Windows.Forms.TextBox text_Md;
private System.Windows.Forms.CheckBox check_Insert;
private System.Windows.Forms.Label lab_Honbun;
private System.Windows.Forms.Label lab_ChTitle;
private System.Windows.Forms.Label lab_chtitle_Title;
private System.Windows.Forms.Label lab_chtitle_Desukedo;
private System.Windows.Forms.Label lab_mkLink;
private System.Windows.Forms.Label lab_mklink_Url;
private System.Windows.Forms.Label lab_mklick_Md;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem MenuItem_File;
private System.Windows.Forms.ToolStripMenuItem MenuItem_Save;
private System.Windows.Forms.ToolStripMenuItem MenuItem_Exit;
private System.Windows.Forms.ToolStripMenuItem MenuItem_Template;
private System.Windows.Forms.ToolStripMenuItem MenuItem_AcoPane;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment