-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnextpress.py
306 lines (283 loc) · 10.1 KB
/
nextpress.py
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#======================================================================
#
# nextpress.py -
#
# Created by skywind on 2019/05/05
# Last Modified: 2019/05/05 16:50:15
#
#======================================================================
from __future__ import print_function, unicode_literals
import sys
import os
import time
import config
import ascmini
import utils
#----------------------------------------------------------------------
# markdown render
#----------------------------------------------------------------------
def markpress_render(doc):
html = doc.convert('config')
try:
import render
except ImportError:
return html
hr = render.HtmlRender(html)
hr.process_viz()
return hr.render()
#----------------------------------------------------------------------
# load markdown
#----------------------------------------------------------------------
def markpress_load(filename):
if not os.path.exists(filename):
config.fatal('file not find: ' + filename)
doc = utils.MarkdownDoc(filename)
if not doc._uuid:
config.perror(filename, 1, 'uuid not find in the markdown meta-header')
return None
uuid = doc._uuid.strip()
if not uuid.isdigit():
config.perror(filename, 1, 'invalid uuid %s'%uuid)
return None
return doc
#----------------------------------------------------------------------
# page maker
#----------------------------------------------------------------------
def markpress_page_make(html, title):
output = ''
output += '<html>\n<head>\n'
output += '<meta charset="UTF-8" />\n'
if title:
text = ascmini.web.text2html(title)
output += '<title>%s</title>\n'%text
css = config.template['style.css']
if css:
output += '<style type="text/css">\n'
output += css
output += '\n</style>\n'
header = config.template['header.html']
if header:
output += header
output += '\n'
output += '</head>\n\n<body>\n\n'
output += '<!--markdown start-->\n'
output += html
output += '\n\n'
output += '<!--markdown endup-->\n\n'
footer = config.template['footer.html']
if footer:
output += footer
output += '\n'
output += '</body>\n\n</html>\n\n'
return output
#----------------------------------------------------------------------
# update file
#----------------------------------------------------------------------
def markpress_update(filename):
doc = markpress_load(filename)
if not doc:
return -1
uuid = doc._uuid
post = {}
post['id'] = uuid
post['title'] = doc._title
post['content'] = markpress_render(doc)
status = doc._status
if status not in ('', 'draft', 'private', 'publish'):
config.perror(filename, 1, 'invalid status %s'%status)
return -3
post['status'] = status and status or 'draft'
if doc._cats:
post['category'] = doc._cats
if doc._tags:
post['tag'] = doc._tags
if doc._slug:
post['slug'] = doc._slug
if doc._date:
post['date'] = utils.utc_datetime(doc._date)
wp = config.wp_client()
wp.post_edit(post)
pp = wp.post_get(uuid)
print('post uuid=%s updated: %s'%(uuid, filename))
print('%s'%pp.link)
return 0
#----------------------------------------------------------------------
# fetch info
#----------------------------------------------------------------------
def markpress_info(filename):
doc = markpress_load(filename)
if not doc:
return -1
uuid = doc._uuid
wp = config.wp_client()
pp = wp.post_get(uuid)
print('uuid: %s'%uuid)
print('title: %s'%doc._title)
print('link: %s'%pp.link)
return 0
#----------------------------------------------------------------------
# convert
#----------------------------------------------------------------------
def markpress_compile(filename, outname):
doc = markpress_load(filename)
if not doc:
return -1
doc._html = markpress_render(doc)
content = markpress_page_make(doc._html, doc._title)
if (not outname) or (outname == '-'):
fp = sys.stdout
else:
import codecs
fp = codecs.open(outname, 'w', encoding = 'utf-8')
fp.write(content)
if fp != sys.stdout:
fp.close()
return 0
#----------------------------------------------------------------------
# open in browser
#----------------------------------------------------------------------
def markpress_open(filename, preview):
doc = markpress_load(filename)
if not doc:
return -1
url = doc.link()
if preview:
url = url + '&preview=true'
import subprocess
subprocess.call(['cmd.exe', '/C', 'start', url])
return 0
#----------------------------------------------------------------------
#
#----------------------------------------------------------------------
def markpress_preview(filename):
markpress_open(filename, True)
return 0
#----------------------------------------------------------------------
# main
#----------------------------------------------------------------------
def main(argv = None):
if argv is None:
argv = sys.argv
options, args = ascmini.utils.getopt(argv[1:])
if not config.PRESENT:
config.fatal('missing config: ' + config.ININAME)
if 'site' in options:
site = options['site'].strip()
if site:
if site not in config.cfg.config:
config.fatal('config section mission: ' + site)
try:
config.select(site)
except ValueError as e:
config.fatal(str(e))
if config.options['proxy']:
config.proxy(config.options['proxy'])
if 'h' in options or 'help' in options:
if 'n' in options or 'new' in options:
print('usage: markpress {-n --new} [--site=SITE] <filename>')
print('Create a new post and save it to file. Dump to stdout')
print('if filename is a hyphen (-).')
elif 'u' in options or 'update' in options:
print('usage: markpress {-u --update} [--site=SITE] <filename>')
print('Update file to wordpress server')
elif 'i' in options or 'info' in options:
print('usage: markpress {-i --info} [--site=SITE] <filename>')
print('Get post info')
elif 'c' in options or 'compile' in options:
print('usage: markpress {-c --compile} [--site=SITE] <filename> [outname]')
print('Compile markdown to html')
elif 'o' in options or 'open' in options:
print('usage: markpress {-o --open} <filename>')
print('Open post in browser')
elif 'p' in options or 'preview' in options:
print('usage: markpress {-p --preview} [--site=SITE] <filename>')
print('Preview markdown')
else:
config.fatal('what help do you need ?')
elif 'n' in options or 'new' in options:
if not args:
config.fatal('missing file name')
name = args[0]
if name == '-':
fp = sys.stdout
elif os.path.exists(name):
if 'f' not in options:
config.fatal('file already exists: ' + name)
wp = config.wp_client()
pid = wp.post_new()
pp = wp.post_get(pid)
if name != '-':
import codecs
fp = codecs.open(name, 'w', encoding = 'utf-8')
fp.write('---\n')
fp.write('uuid: ' + str(pid) + '\n')
fp.write('title: \n')
fp.write('status: draft\n')
fp.write('categories: \n')
fp.write('tags: \n')
fp.write('slug: \n')
fp.write('---\n\n')
if name != '-':
print('new post uuid=%s saved in %s'%(pid, name))
print(pp.link)
elif 'u' in options or 'update' in options:
if not args:
config.fatal('missing file name')
markpress_update(args[0])
elif 'i' in options or 'info' in options:
if not args:
config.fatal('missing file name')
markpress_info(args[0])
elif 'c' in options or 'compile' in options:
if not args:
config.fatal('missing file name')
if len(args) >= 2:
outname = args[1]
else:
outname = os.path.splitext(args[0])[0] + '.html'
markpress_compile(args[0], outname)
elif 'o' in options or 'open' in options:
if not args:
config.fatal('missing file name')
markpress_open(args[0], False)
elif 'p' in options or 'preview' in options:
if not args:
config.fatal('missing file name')
markpress_preview(args[0])
else:
print('usage: markpress <operation> [...]')
print('operations:')
print(' markpress {-n --new} <filename>')
print(' markpress {-u --update} <filename>')
print(' markpress {-i --info} <filename>')
print(' markpress {-c --compile} <filename> [outname]')
print(' markpress {-o --open} <filename>')
print(' markpress {-p --preview} <filename>')
print()
print("use 'markpress {-h --help}' with an operation for detail")
return 0
#----------------------------------------------------------------------
# testing suit
#----------------------------------------------------------------------
if __name__ == '__main__':
def test1():
args = ['', '-n', '-f', '../content/1.md']
args = ['', '-u', '../content/1.md']
main(args)
return 0
def test2():
args = ['', '-i', '../content/1.md']
args = ['', '-c', '../content/1.md']
# args = ['', '-c', '../content/1.md', '-']
main(args)
return 0
def test9():
args = ['', '-n', '-']
args = []
args = ['', '-h', '-n']
main(args)
return 0
test1()
# main()