Skip to content

Commit 8c383b6

Browse files
authored
Merge pull request #470 from ignatandrei/LiveWriter
2 parents 50081ad + 218a16f commit 8c383b6

103 files changed

Lines changed: 60722 additions & 4 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

all.csv

Lines changed: 268 additions & 0 deletions
Large diffs are not rendered by default.

v2/Generator.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 17
4-
VisualStudioVersion = 17.7.34031.279
3+
# Visual Studio Version 18
4+
VisualStudioVersion = 18.4.11626.88 stable
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Generator", "Generator\Generator.csproj", "{48CA2DDA-C3D0-4D5F-9A05-8CEFEB777E61}"
77
EndProject
@@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GeneratorData", "GeneratorD
1111
EndProject
1212
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GV.Steps", "GV.Steps\GV.Steps.csproj", "{57236574-B2E4-4742-BDD3-0C72E2220676}"
1313
EndProject
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenLiveWriterPost", "OpenLiveWriterPost\OpenLiveWriterPost.csproj", "{DF7E2A72-9D51-4AC9-982D-5EDC36B5DA26}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -33,6 +35,10 @@ Global
3335
{57236574-B2E4-4742-BDD3-0C72E2220676}.Debug|Any CPU.Build.0 = Debug|Any CPU
3436
{57236574-B2E4-4742-BDD3-0C72E2220676}.Release|Any CPU.ActiveCfg = Release|Any CPU
3537
{57236574-B2E4-4742-BDD3-0C72E2220676}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{DF7E2A72-9D51-4AC9-982D-5EDC36B5DA26}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{DF7E2A72-9D51-4AC9-982D-5EDC36B5DA26}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{DF7E2A72-9D51-4AC9-982D-5EDC36B5DA26}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{DF7E2A72-9D51-4AC9-982D-5EDC36B5DA26}.Release|Any CPU.Build.0 = Release|Any CPU
3642
EndGlobalSection
3743
GlobalSection(SolutionProperties) = preSolution
3844
HideSolutionNode = FALSE

v2/Generator/Generator.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFramework>net8.0-windows</TargetFramework>
66
<Nullable>enable</Nullable>
7+
<UseWindowsForms>true</UseWindowsForms>
8+
<PlatformTarget>x86</PlatformTarget>
79
</PropertyGroup>
810

911
<PropertyGroup>
@@ -90,6 +92,7 @@
9092
</ItemGroup>
9193
<ItemGroup>
9294
<ProjectReference Include="..\GeneratorData\GeneratorData.csproj" />
95+
<ProjectReference Include="..\OpenLiveWriterPost\OpenLiveWriterPost.csproj" />
9396
</ItemGroup>
9497

9598

v2/Generator/MultiGeneratorV2.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,8 @@ private async Task<bool> WrotePost(Description it, string pathDocusaurus)
986986
await File.WriteAllTextAsync(file, output);
987987
Process.Start("notepad.exe", file);
988988
//Console.WriteLine(output);
989+
OpenLiveWriterPost.WritePost.GeneratePostFromHtml(it.Generator.Name ?? "", output);
990+
989991
await Task.Delay(100);
990992
return true;
991993
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0-windows</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<UseWindowsForms>true</UseWindowsForms>
8+
<PlatformTarget>x86</PlatformTarget>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="System.Drawing.Common" Version="8.0.0" />
13+
<Reference Include="OpenLiveWriter.CoreServices">
14+
<HintPath>..\dlls\Writer\OpenLiveWriter.CoreServices.dll</HintPath>
15+
</Reference>
16+
<Reference Include="OpenLiveWriter.Extensibility">
17+
<HintPath>..\dlls\Writer\OpenLiveWriter.Extensibility.dll</HintPath>
18+
</Reference>
19+
<Reference Include="OpenLiveWriter.PostEditor">
20+
<HintPath>..\dlls\Writer\OpenLiveWriter.PostEditor.dll</HintPath>
21+
</Reference>
22+
<Reference Include="OpenLiveWriter.Interop">
23+
<HintPath>..\dlls\Writer\OpenLiveWriter.Interop.dll</HintPath>
24+
<Private>true</Private>
25+
</Reference>
26+
27+
</ItemGroup>
28+
29+
</Project>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using OpenLiveWriter.CoreServices;
2+
using OpenLiveWriter.Extensibility.BlogClient;
3+
using OpenLiveWriter.PostEditor;
4+
using System.Reflection;
5+
6+
namespace OpenLiveWriterPost;
7+
8+
internal class OpenLiveWriterPostGenerator
9+
{
10+
11+
12+
public static void SavePost(BlogPost post, string filePath)
13+
{
14+
SavePostAsStructuredStorage(post, filePath);
15+
}
16+
17+
private static void SavePostAsStructuredStorage(BlogPost post, string filePath)
18+
{
19+
ApplicationEnvironment.Initialize(Assembly.GetExecutingAssembly(),
20+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"Windows Live\Writer"));
21+
22+
PostEditorFile.Initialize();
23+
24+
string? directoryPath = Path.GetDirectoryName(filePath);
25+
if (string.IsNullOrEmpty(directoryPath))
26+
{
27+
directoryPath = Directory.GetCurrentDirectory();
28+
}
29+
30+
DirectoryInfo targetDirectory = new DirectoryInfo(directoryPath);
31+
string[] existingDrafts = Directory.GetFiles(targetDirectory.FullName, "*.wpost");
32+
33+
PostEditorFile pef = PostEditorFile.CreateNew(targetDirectory);
34+
var bef = new BlogPostEditingContext(post.Id, post);
35+
pef.SaveBlogPost(bef);
36+
37+
string createdDraftPath = FindCreatedDraftPath(targetDirectory.FullName, existingDrafts);
38+
if (!string.Equals(createdDraftPath, filePath, StringComparison.OrdinalIgnoreCase))
39+
{
40+
if (File.Exists(filePath))
41+
{
42+
File.Delete(filePath);
43+
}
44+
45+
File.Move(createdDraftPath, filePath);
46+
}
47+
}
48+
49+
private static string FindCreatedDraftPath(string directoryPath, string[] existingDrafts)
50+
{
51+
string[] currentDrafts = Directory.GetFiles(directoryPath, "*.wpost");
52+
53+
foreach (string currentDraft in currentDrafts)
54+
{
55+
bool existedBefore = false;
56+
foreach (string existingDraft in existingDrafts)
57+
{
58+
if (string.Equals(currentDraft, existingDraft, StringComparison.OrdinalIgnoreCase))
59+
{
60+
existedBefore = true;
61+
break;
62+
}
63+
}
64+
65+
if (!existedBefore)
66+
{
67+
return currentDraft;
68+
}
69+
}
70+
71+
throw new IOException($"Unable to determine the draft file created in '{directoryPath}'.");
72+
}
73+
74+
75+
76+
}

v2/OpenLiveWriterPost/WritePost.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using OpenLiveWriter.Extensibility.BlogClient;
2+
using OpenLiveWriter.Extensibility.ImageEditing;
3+
4+
namespace OpenLiveWriterPost;
5+
public static class WritePost
6+
{
7+
public static void GeneratePostFromHtml(string name, string html)
8+
{
9+
var draftsFolder1 = GetOpenLiveWriterDraftsFolder();
10+
var post1 = new BlogPost
11+
{
12+
Id = Guid.NewGuid().ToString("D"),
13+
Title = name,
14+
DatePublished = DateTime.Now.AddDays(10),
15+
DatePublishedOverride = DateTime.Now.AddDays(10),
16+
Contents = html,
17+
//Categories = [ "Programming", "C#", "Blogging" ],
18+
};
19+
// Save the post
20+
var safeTitle = SanitizeFileName(post1.Title);
21+
var fileName1 = $"{safeTitle}.wpost";
22+
var filePath1 = Path.Combine(draftsFolder1, fileName1);
23+
24+
if(File.Exists(filePath1)) File.Delete(filePath1);
25+
26+
Console.WriteLine("Creating hardcoded blog post...");
27+
28+
OpenLiveWriterPostGenerator.SavePost(post1, filePath1);
29+
30+
}
31+
static string SanitizeFileName(string? name)
32+
{
33+
var sanitizedName = (name ?? string.Empty).Trim();
34+
foreach (var invalidChar in Path.GetInvalidFileNameChars())
35+
{
36+
sanitizedName = sanitizedName.Replace(invalidChar, '_');
37+
}
38+
39+
return string.IsNullOrWhiteSpace(sanitizedName) ? "untitled" : sanitizedName;
40+
}
41+
static string GetOpenLiveWriterDraftsFolder()
42+
{
43+
// OpenLive Writer drafts are typically stored in:
44+
// %USERPROFILE%\Documents\My Weblog Posts\Drafts
45+
// or
46+
// %LOCALAPPDATA%\OpenLiveWriter\Drafts
47+
48+
var userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
49+
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
50+
var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
51+
52+
// Try different possible locations for OpenLive Writer drafts
53+
var possiblePaths = new[]
54+
{
55+
Path.Combine(documentsPath, "My Weblog Posts", "Drafts"),
56+
Path.Combine(localAppData, "OpenLiveWriter", "Drafts"),
57+
Path.Combine(userProfile, "Documents", "My Weblog Posts", "Drafts"),
58+
Path.Combine(localAppData, "Open Live Writer", "Drafts")
59+
};
60+
61+
foreach (var path in possiblePaths)
62+
{
63+
if (Directory.Exists(path))
64+
{
65+
Console.WriteLine($"Using existing OpenLive Writer drafts folder: {path}");
66+
return path;
67+
}
68+
}
69+
70+
// If none exist, create the default one
71+
var defaultPath = Path.Combine(documentsPath, "My Weblog Posts", "Drafts");
72+
Console.WriteLine($"Creating OpenLive Writer drafts folder: {defaultPath}");
73+
Directory.CreateDirectory(defaultPath);
74+
return defaultPath;
75+
}
76+
}

v2/dlls/Writer/ApprovalTests.dll

135 KB
Binary file not shown.

v2/dlls/Writer/ApprovalTests.xml

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
5.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)