@@ -30,6 +30,10 @@ def get_parser():
30
30
default = None , dest = "output" ,
31
31
help = "The output folder to write posts." )
32
32
33
+ parser .add_argument ("--test" ,
34
+ default = False , dest = "test" ,
35
+ action = 'store_true' ,
36
+ help = "Test generation only (show files to be created)" )
33
37
return parser
34
38
35
39
@@ -61,14 +65,15 @@ def validate_authors(authors):
61
65
return valid
62
66
63
67
64
- def parse_feeds (authors , output_dir ):
68
+ def parse_feeds (authors , output_dir , test = False ):
65
69
'''read in the list of authors, parse feeds and save results to a
66
70
specified output directory.
67
71
68
72
Parameters
69
73
==========
70
74
authors: a list of authors, read in from an authors.yml
71
75
output_dir: the output directory to write markdown posts to.
76
+ test: don't write any files, only test generate
72
77
'''
73
78
if output_dir == None :
74
79
print ("Output directory must be defined." )
@@ -84,8 +89,11 @@ def parse_feeds(authors, output_dir):
84
89
# Create output folder
85
90
author_folder = os .path .join (output_dir , author ['tag' ])
86
91
if not os .path .exists (author_folder ):
87
- print ("Creating new author folder %s" % author_folder )
88
- os .mkdir (author_folder )
92
+ if test is False :
93
+ print ("Creating new author folder %s" % author_folder )
94
+ os .mkdir (author_folder )
95
+ else :
96
+ print ("[TEST] new author folder %s" % author_folder )
89
97
90
98
# Parse the feed, each entry is written to file based on title
91
99
feed = feedparser .parse (author ['feed' ])
@@ -95,12 +103,15 @@ def parse_feeds(authors, output_dir):
95
103
96
104
# Write the file if it doesn't exist
97
105
if not os .path .exists (markdown ):
106
+
98
107
print ('Preparing new post: %s' % markdown )
99
108
post = generate_post (entry , author , feed )
100
109
101
- # Write to file
102
- with open (markdown , 'w' ) as filey :
103
- filey .write (frontmatter .dumps (post ))
110
+ if test is False :
111
+
112
+ # Write to file
113
+ with open (markdown , 'w' ) as filey :
114
+ filey .write (frontmatter .dumps (post ))
104
115
105
116
106
117
def generate_post (entry , author , feed ):
@@ -145,6 +156,9 @@ def get_markdown_file(author_folder, entry):
145
156
146
157
# The id is the last part of the url, lowercase
147
158
title = [x for x in entry ['id' ].split ('/' ) if x ][- 1 ].lower ()
159
+
160
+ # Replace any variable names (? in wordpress) with -
161
+ title = title .replace ('?' , '' )
148
162
filename = '%s-%s-%s-%s.md' % (year , month , day , title )
149
163
150
164
# The output markdown name is consistent
@@ -206,7 +220,7 @@ def help(return_code=0):
206
220
print ("Authors file %s is not valid." % authors )
207
221
208
222
# Generate outputs based on authors
209
- parse_feeds (authors , args .output )
223
+ parse_feeds (authors , args .output , args . test )
210
224
211
225
212
226
if __name__ == '__main__' :
0 commit comments