-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
125 lines (110 loc) · 4.28 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace BlogML2Ghost
{
class Program
{
static XNamespace NS = "http://www.blogml.com/2006/09/BlogML";
static readonly DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
static void Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("Usage: blogml2ghost <input.xml> <output.json>");
return;
}
string input = args[0];
string output = args[1];
var data = ConvertData(input);
var metaData = new
{
exported_on = GetJsDate(DateTime.UtcNow),
version = "000"
};
var final = new
{
meta = metaData,
data = data
};
var str = Newtonsoft.Json.JsonConvert.SerializeObject(final, new Newtonsoft.Json.JsonSerializerSettings()
{
NullValueHandling = Newtonsoft.Json.NullValueHandling.Include
});
File.WriteAllText(output, str);
Console.WriteLine("BlogML file converted successfully!");
}
static object ConvertData(string input)
{
var root = XElement.Load(File.OpenRead(input));
var categories = root.Element(NS + "categories")
.Elements(NS + "category")
.Select((x, i) => new { x, i })
.ToDictionary(x => (Guid)x.x.Attribute("id"), v => new
{
id = v.i,
name = v.x.Element(NS + "title").Value,
slug = GetSlugFromTitle(v.x.Element(NS + "title").Value),
description = ""
});
var postsRoot = root.Element(NS + "posts");
var posts = postsRoot.Elements(NS + "post");
var transformedPosts = new List<object>();
var postToTag = new List<object>();
int postId = 0;
foreach (var e in posts)
{
postId++;
transformedPosts.Add(new
{
id = postId,
title = e.Element(NS + "title").Value,
slug = GetSlugFromTitle(e.Element(NS + "title").Value),
html = e.Element(NS + "content").Value,
markdown = e.Element(NS + "content").Value,
image = (object)null,
featured = 0,
page = 0,
language = "en_US",
status = "published",
meta_title = (object)null,
meta_description = (object)null,
author_id = 1,
created_at = GetJsDate((DateTime)e.Attribute("date-created")),
created_by = 1,
updated_at = GetJsDate((DateTime)e.Attribute("date-modified")),
updated_by = 1,
published_at = GetJsDate((DateTime)e.Attribute("date-created")),
published_by = 1
});
var tags = e.Element(NS + "categories");
if (tags != null)
{
var moreTags = tags
.Elements(NS + "category")
.Select(x => new { post_id = postId, tag_id = categories[(Guid)x.Attribute("ref")].id });
postToTag.AddRange(moreTags);
}
}
return new
{
posts = transformedPosts,
tags = categories.Values.ToList(),
posts_tags = postToTag
};
}
private static long GetJsDate(DateTime dateTime)
{
return (long)(dateTime - epoch).TotalMilliseconds;
}
static string GetSlugFromTitle(string url)
{
return Regex.Replace(Regex.Replace(url, "[^A-Za-z0-9-]+", "-"), "-{2,}", "-");
}
}
}