tDiaryのデータをはてな形式に変換する
今回の移転に際して、tDiaryのデータをはてなのエクスポート形式に変換する簡単なプログラムを書いた。せっかくなので載せておく。しかもなぜかVB.NET。
最初は編集ツールとしてちゃんとGUIつけて編集できるようにしようと思ったけど途中でめんどくなってやめた。だからForm.Newに直書きしてそのままでっちあげたのでかなり邪道。
' tDiaryFile.vb Imports System.IO Imports System.Text Public Class tDiaryFile Public Shared Function Open(ByVal fileName As String) As Dictionary(Of Date, tDiaryEntry) Dim items As New List(Of String) Dim returnValue As New Dictionary(Of Date, tDiaryEntry) Using fStream As FileStream = New FileStream(fileName, FileMode.Open, FileAccess.Read) Dim reader As New StreamReader(fStream, Encoding.GetEncoding("EUC-JP")) Dim line As String = String.Empty Dim item As String = String.Empty ' 一行目はバージョン宣言 line = reader.ReadLine() Do Until reader.EndOfStream line = reader.ReadLine() If line = "." Then items.Add(item) item = String.Empty Else item = item & line & vbCrLf End If Loop End Using For Each item As String In items Dim entry As New tDiaryEntry Dim header As String = item.Substring(0, item.IndexOf(vbCrLf & vbCrLf)) Dim body As String = item.Substring(item.IndexOf(vbCrLf & vbCrLf) + 4) For Each line As String In header.Split(vbCrLf) Dim key As String = line.Substring(0, line.IndexOf(":")) Dim value As String = line.Substring(line.IndexOf(":") + 2) Select Case key Case "Date" entry.Date = New Date(Integer.Parse(value.Substring(0, 4)), _ Integer.Parse(value.Substring(4, 2)), _ Integer.Parse(value.Substring(6))) Case "Title" entry.Title = value Case "Visible" entry.Visible = (value = "true") End Select Next entry.Body = body returnValue.Add(entry.Date, entry) Next Return returnValue End Function End Class ' tDiaryEntry.vb Public Class tDiaryEntry Public Sub New() Me.New(Now.Date, String.Empty, String.Empty, True) End Sub Public Sub New(ByVal d As Date, ByVal title As String, _ ByVal body As String, ByVal visible As Boolean) propDate = d propTitle = title propBody = body propVisible = visible End Sub Private propTitle As String Public Property Title() As String Get Return propTitle End Get Set(ByVal value As String) propTitle = value End Set End Property Private propDate As Date Public Property [Date]() As Date Get Return propDate End Get Set(ByVal value As Date) propDate = value End Set End Property Private propVisible As Boolean Public Property Visible() As Boolean Get Return propVisible End Get Set(ByVal value As Boolean) propVisible = value End Set End Property Private propBody As String Public Property Body() As String Get Return propBody End Get Set(ByVal value As String) propBody = value End Set End Property End Class ' Form1.vb Public Class Form1 Public Sub New() ' この呼び出しは、Windows フォーム デザイナで必要です。 InitializeComponent() ' InitializeComponent() 呼び出しの後で初期化を追加します。 Dim allData As New Dictionary(Of Date, tDiaryEntry) For Each file As String In My.Computer.FileSystem.GetFiles("(your data directory)", _ FileIO.SearchOption.SearchAllSubDirectories, "*.td2") Dim monthData As Dictionary(Of Date, tDiaryEntry) = tDiaryFile.Open(file) For Each Data As tDiaryEntry In monthData.Values allData.Add(Data.Date, Data) Next Next Using writer As System.IO.StreamWriter = _ My.Computer.FileSystem.OpenTextFileWriter( _ My.Computer.FileSystem.CombinePath( _ My.Computer.FileSystem.SpecialDirectories.Desktop, "export.xml"), False) writer.WriteLine("<?xml version=""1.0"" encoding=""UTF-8""?>") writer.WriteLine("<diary>") For Each Data As tDiaryEntry In allData.Values If Data.Visible Then writer.WriteLine(String.Format( _ "<day date=""{0:D4}-{1:D2}-{2:D2}"" title=""{3}"">", _ Data.Date.Year, Data.Date.Month, Data.Date.Day, Data.Title)) writer.WriteLine("<body>") writer.WriteLine(Data.Body) writer.WriteLine("</body>") writer.WriteLine("</day>") End If Next writer.WriteLine("</diary>") End Using End Sub End Class