1- using Goodbye . WordPress ;
1+ namespace WPExportApp {
2+ using System . Threading . Tasks ;
3+ using Goodbye . WordPress ;
24
3- var exporter = WordPressExporter . Create (
4- postReader : new MysqlPostReader ( new ConnectionStringBuilder
5+ class WPExportMain {
6+ /// <summary>
7+ /// Entry Point
8+ /// </summary>
9+ /// <param name="args">CLA</param>
10+ static async Task Main ( string [ ] args )
11+ {
12+ var config = new JsonConfig ( ) ;
13+ await config . Load ( ) ;
14+ if ( config . Options == null )
15+ throw new System . NullReferenceException ( ) ;
16+
17+ var exporter = WordPressExporter . Create (
18+ postReader : new MysqlPostReader (
19+ new ConnectionStringBuilder {
20+ Host = config . Options . Host ,
21+ Database = config . Options . Database ,
22+ Username = config . Options . Username ,
23+ Password = config . Options . Password
24+ // , TlsVersion = config.Options.TlsVersion
25+ } ) ,
26+ contentOutputDirectory : config . Options . ContentOutputDirectory ,
27+ archiveOutputFilePath : config . Options . ArchiveOutputFilePath ,
28+ // And now the delegate...
29+ @delegate : new CustomExporterDelegate ( config . Options . Patterns )
30+ ) ;
31+
32+ await exporter . ExportAsync ( ) ;
33+ }
34+ }
35+
36+
37+ sealed class CustomExporterDelegate : WordPressExporterDelegate
538 {
6- Host = "localhost" ,
7- Username = "user" ,
8- Password = "***" ,
9- Database = "wordpressdb"
10- } ) ,
11- contentOutputDirectory : "exported-posts" ,
12- archiveOutputFilePath : "exported-posts/archive.json" ,
13-
14- // And now the delegate...
15- @delegate : new CustomExporterDelegate ( ) ) ;
16-
17- await exporter . ExportAsync ( ) ;
18-
19- sealed class CustomExporterDelegate : WordPressExporterDelegate
20- {
21- /// <summary>Process post contents</summary>
22- public override Post ProcessPost (
23- WordPressExporter exporter ,
24- Post post )
39+ JsonConfig . ReplacePattern [ ] StrPatterns ;
40+
41+ public CustomExporterDelegate ( JsonConfig . ReplacePattern [ ] patterns )
42+ {
43+ StrPatterns = patterns ;
44+ }
45+
46+ // Replace weird unicode chars
47+ private string SubstituteCommon ( string str ) =>
48+ str . Replace ( "‘" , "'" ) . Replace ( "’" , "'" )
49+ . Replace ( "“" , "\" " ) . Replace ( "”" , "\" " ) ;
50+
51+ private string ProcessContent ( string content ) {
52+ content = SubstituteCommon ( content )
53+ . Replace ( "http://" , "https://" ) ;
54+
55+ if ( StrPatterns != null && StrPatterns . Length > 0 )
56+ foreach ( var pattern in StrPatterns )
57+ content = content . Replace ( pattern . Needle , pattern . Substitute ) ;
58+
59+ return content ;
60+ }
61+
62+ /// <summary>Process post contents</summary>
63+ public override Post ProcessPost (
64+ WordPressExporter exporter ,
65+ Post post )
2566 // Perform the default post processing first by calling base
2667 => base . ProcessPost ( exporter , post ) with
2768 {
28- // Then replace '--' with Unicode em dash '—'
29- Content = post . Content . Replace ( "--" , "—" )
69+ Content = ProcessContent ( post . Content )
3070 } ;
3171
32- /// <summary>Add 'CustomMetadata' to each post's YAML front matter</summary>
33- public override void PopulatePostYamlFrontMatter (
34- WordPressExporter exporter ,
35- Post post ,
36- SharpYaml . Serialization . YamlMappingNode rootNode )
37- {
38- base . PopulatePostYamlFrontMatter ( exporter , post , rootNode ) ;
39- rootNode . Add ( "CustomMetadata" , "Some Value" ) ;
72+ /// <summary>Add 'CustomMetadata' to each post's YAML front matter</summary>
73+ public override void PopulatePostYamlFrontMatter (
74+ WordPressExporter exporter ,
75+ Post post ,
76+ SharpYaml . Serialization . YamlMappingNode rootNode )
77+ {
78+ base . PopulatePostYamlFrontMatter ( exporter , post , rootNode ) ;
79+ // rootNode.Add("CustomMetadata", "Some Value");
80+ }
4081 }
41- }
82+ }
0 commit comments