-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWP Conversion.linq
174 lines (137 loc) · 7.99 KB
/
WP Conversion.linq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<Query Kind="Program">
<Reference><RuntimeDirectory>\System.Web.dll</Reference>
<Reference><RuntimeDirectory>\System.Configuration.dll</Reference>
<Reference><RuntimeDirectory>\Microsoft.Build.Framework.dll</Reference>
<Reference><RuntimeDirectory>\System.ComponentModel.DataAnnotations.dll</Reference>
<Reference><RuntimeDirectory>\System.Runtime.Caching.dll</Reference>
<Reference><RuntimeDirectory>\System.Web.ApplicationServices.dll</Reference>
<Reference><RuntimeDirectory>\System.Web.Services.dll</Reference>
<Reference><RuntimeDirectory>\Microsoft.Build.Utilities.v4.0.dll</Reference>
<Reference><RuntimeDirectory>\System.Security.dll</Reference>
<Reference><RuntimeDirectory>\System.DirectoryServices.dll</Reference>
<Reference><RuntimeDirectory>\System.DirectoryServices.Protocols.dll</Reference>
<Reference><RuntimeDirectory>\System.EnterpriseServices.dll</Reference>
<Reference><RuntimeDirectory>\System.Design.dll</Reference>
<Reference><RuntimeDirectory>\Microsoft.Build.Tasks.v4.0.dll</Reference>
<Reference><RuntimeDirectory>\System.ServiceProcess.dll</Reference>
<Reference><RuntimeDirectory>\System.Windows.Forms.dll</Reference>
<Reference><RuntimeDirectory>\System.Web.RegularExpressions.dll</Reference>
<Namespace>System.Linq</Namespace>
<Namespace>System.Web</Namespace>
</Query>
void Main()
{
var outputPath = @"D:\Projects\improve.dk (Hexo, GIT)\source\_posts";
var xmlPath = @"D:\Projects\improve.dk (Hexo, GIT)\marksrasmussen-blog.wordpress.2014-03-08.xml";
var testPath = @"D:\Projects\improve.dk (Hexo, GIT)\test.html";
var xml = File.ReadAllText(xmlPath);
var xd = new XmlDocument();
xd.LoadXml(xml);
var nsmgr = new XmlNamespaceManager(xd.NameTable);
nsmgr.AddNamespace("content", "http://purl.org/rss/1.0/modules/content/");
nsmgr.AddNamespace("wp", "http://wordpress.org/export/1.2/");
var testHtml = new StringBuilder();
int count = 1;
foreach (XmlNode item in xd.SelectNodes("//item"))
{
var title = item.SelectSingleNode("title").InnerText;
var date = Convert.ToDateTime(item.SelectSingleNode("pubDate").InnerText);
var content = item.SelectSingleNode("content:encoded", nsmgr).InnerText;
var slug = item.SelectSingleNode("wp:post_name", nsmgr).InnerText;
var categories = getCategories(item);
string postOutputDir = Path.Combine(outputPath, slug);
Directory.CreateDirectory(postOutputDir);
string postFilePath = Path.Combine(postOutputDir, slug + ".md");
var sb = new StringBuilder();
sb.AppendLine("permalink: " + slug);
sb.AppendLine("title: " + title);
sb.AppendLine("date: " + date.ToString("yyyy-MM-dd"));
sb.AppendLine("tags: [" + string.Join(", ", categories) + "]");
sb.AppendLine("---");
sb.AppendLine(formatContent(content));
File.WriteAllText(postFilePath, sb.ToString());
testHtml.AppendLine((count++).ToString().PadLeft(3, '0') + " <a href='http://127.0.0.1:4000/" + slug + "/'>Hexo</a> <a href='http://improve.dk/" + slug + "/'>WP</a> " + slug + "<br />");
Console.WriteLine(date + ": " + slug);
}
File.WriteAllText(testPath, testHtml.ToString());
}
string formatContent(string content)
{
var options = RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.Compiled;
// H2
content = Regex.Replace(content, "^<h2>(.+?)</h2>(\n|\r|\r\n)", "## $1", options);
// H3
content = Regex.Replace(content, "^<h3>(.+?)</h3>(\n|\r|\r\n)", "### $1", options);
// H4
content = Regex.Replace(content, "^<h4>(.+?)</h4>(\n|\r|\r\n)", "### $1", options);
// Image
content = Regex.Replace(content, "^<div class=('|\")imgwrapper('|\")>.*?/\\d+/\\d+/(.*?)\"><img .*?</a></div></div>", "$3", options);
// tsql
content = Regex.Replace(content, "<pre lang=\"tsql\">(.*?)</pre>", m => "```sql" + Environment.NewLine + HttpUtility.HtmlDecode(m.Groups[1].Value) + Environment.NewLine + "```", RegexOptions.Singleline);
content = Regex.Replace(content, "<pre lang=\"tsql\" escaped=\"true\">(.*?)</pre>", m => "```sql" + Environment.NewLine + HttpUtility.HtmlDecode(m.Groups[1].Value) + Environment.NewLine + "```", RegexOptions.Singleline);
content = Regex.Replace(content, "<pre escaped=\"true\" lang=\"tsql\">(.*?)</pre>", m => "```sql" + Environment.NewLine + HttpUtility.HtmlDecode(m.Groups[1].Value) + Environment.NewLine + "```", RegexOptions.Singleline);
// csharp
content = Regex.Replace(content, "<pre lang=\"csharp\"( escaped=\"true\")?>(.*?)</pre>", m => "```csharp" + Environment.NewLine + HttpUtility.HtmlDecode(m.Groups[2].Value) + Environment.NewLine + "```", RegexOptions.Singleline);
content = Regex.Replace(content, "\\[csharp\\](.+?)\\[/csharp\\]", m => "```csharp" + Environment.NewLine + HttpUtility.HtmlDecode(m.Groups[1].Value) + Environment.NewLine + "```", RegexOptions.Singleline);
// plain
content = Regex.Replace(content, "<pre class=\"plain\">(.*?)</pre>", m => "```" + Environment.NewLine + HttpUtility.HtmlDecode(m.Groups[1].Value) + Environment.NewLine + "```", RegexOptions.Singleline);
content = Regex.Replace(content, "<pre lang=\"text\">(.*?)</pre>", m => "```" + Environment.NewLine + HttpUtility.HtmlDecode(m.Groups[1].Value) + Environment.NewLine + "```", RegexOptions.Singleline);
// xml
content = Regex.Replace(content, "<pre lang=\"xml\" escaped=\"true\">(.+?)</pre>", m => "```xml" + Environment.NewLine + HttpUtility.HtmlDecode(m.Groups[1].Value) + Environment.NewLine + "```", RegexOptions.Singleline);
content = Regex.Replace(content, "<pre lang=\"mxml\" escaped=\"true\">(.+?)</pre>", m => "```mxml" + Environment.NewLine + HttpUtility.HtmlDecode(m.Groups[1].Value) + Environment.NewLine + "```", RegexOptions.Singleline);
// bash
content = Regex.Replace(content, "<pre lang=\"bash\">(.+?)</pre>", m => "```bash" + Environment.NewLine + m.Groups[1].Value + Environment.NewLine + "```", RegexOptions.Singleline);
// a
content = Regex.Replace(content, "<a href=\"(.+?)\">(.+?)</a>", "[$2]($1)", RegexOptions.Singleline);
// b
content = Regex.Replace(content, "<b>(.+?)</b>", "**$1**", options);
// strong
content = Regex.Replace(content, "<strong>(.+?)</strong>", "**$1**", options);
// p
content = Regex.Replace(content, "<p>(.+?)</p>", "$1", RegexOptions.Singleline);
// Entities
content = content.Replace("&", "&");
content = content.Replace(">", ">");
content = content.Replace("<", "<");
// br
content = Regex.Replace(content, "<br ?/>", " ", options);
// em
content = Regex.Replace(content, "<em>(.+?)</em>", "*$1*", options);
content = Regex.Replace(content, "<i>(.+?)</i>", "*$1*", options);
// Exercerpt
content = replaceFirst(content, Environment.NewLine + Environment.NewLine, Environment.NewLine + Environment.NewLine + "<!-- more -->" + Environment.NewLine + Environment.NewLine);
// li
content = Regex.Replace(content, "\t?<li>(.+?)</li>", "* $1", options);
// ul
content = Regex.Replace(content, "<ul>(.+?)</ul>", "$1", RegexOptions.Singleline);
return content;
}
string replaceFirst(string content, string search, string replace)
{
int pos = content.IndexOf(search);
if (pos < 0)
return content;
return content.Substring(0, pos) + replace + content.Substring(pos + search.Length);
}
string[] getCategories(XmlNode item)
{
return item.SelectNodes("category[@domain='category']")
.Cast<XmlNode>()
.Select(x => translateCategory(HttpUtility.HtmlDecode(x.InnerText)))
.Distinct()
.ToArray();
}
string translateCategory(string cat)
{
switch (cat)
{
case "Community": return "SQL Server - Community";
case "Data Types": return "SQL Server - Data Types";
case "Internals": return "SQL Server - Internals";
case "Misc": return "SQL Server";
case "Optimization": return "SQL Server - Optimization";
case "OrcaMDF": return "SQL Server - OrcaMDF";
case "Tricks": return "SQL Server - Tricks";
default: return cat;
}
}