/
vimrepress.py
962 lines (787 loc) · 33.7 KB
/
vimrepress.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
# -*- coding: utf-8 -*-
import vim
import urllib
import xmlrpclib
import re
import os
import mimetypes
import webbrowser
import tempfile
from ConfigParser import SafeConfigParser
try:
import markdown
except ImportError:
try:
import markdown2 as markdown
except ImportError:
class markdown_stub(object):
def markdown(self, n):
raise VimPressException("The package python-markdown is "
"required and is either not present or not properly "
"installed.")
markdown = markdown_stub()
def exception_check(func):
def __check(*args, **kwargs):
try:
return func(*args, **kwargs)
except (VimPressException, AssertionError), e:
echoerr(str(e))
except (xmlrpclib.Fault, xmlrpclib.ProtocolError), e:
if getattr(e, "faultString", None) is None:
echoerr("xmlrpc error: %s" % e)
else:
echoerr("xmlrpc error: %s" % e.faultString.encode("utf-8"))
except IOError, e:
echoerr("network error: %s" % e)
except Exception, e:
echoerr("something wrong: %s" % e)
raise
return __check
echomsg = lambda s: vim.command('echomsg "%s"' % s)
echoerr = lambda s: vim.command('echoerr "%s"' % s)
################################################
# Helper Classes
#################################################
class VimPressException(Exception):
pass
class DataObject(object):
#CONST
DEFAULT_LIST_COUNT = "15"
IMAGE_TEMPLATE = '<a href="%(url)s">' \
'<img title="%(file)s" alt="%(file)s" src="%(url)s"' \
' class="aligncenter" /></a>'
MARKER = dict(bg="=========== Meta ============",
mid="=============================",
ed="========== Content ==========",
more='"====== Press Here for More ======',
list_title='"====== '
'%(edit_type)s List in %(blog_url)s =========')
LIST_VIEW_KEY_MAP = dict(enter="<enter>", delete="<delete>")
CUSTOM_FIELD_KEY = "mkd_text"
#Temp variables.
__xmlrpc = None
__conf_index = 0
__config = None
view = 'edit'
vimpress_temp_dir = ''
blog_username = property(lambda self: self.xmlrpc.username)
blog_url = property(lambda self: self.xmlrpc.blog_url)
store_markdown = property(lambda self: self.xmlrpc.store_markdown)
conf_index = property(lambda self: self.__conf_index)
current_post_id = property(lambda self: self.xmlrpc.current_post_id,
lambda self, d: setattr(self.xmlrpc, "current_post_id", d))
post_cache = property(lambda self: self.xmlrpc.post_cache)
@property
def current_post(self):
post_id = self.current_post_id
post = self.post_cache.get(post_id)
if post is None and post_id == '':
post = ContentStruct()
if post.post_id != '':
self.current_post_id = post.post_id
self.current_post = post
else:
self.post_cache[''] = post
assert post is not None, "current_post, no way to return None"
return post
@current_post.setter
def current_post(self, data):
post_id = str(data.post_id)
# New post, just post first time
if self.current_post_id == '' and \
post_id != '' and '' in self.post_cache:
self.post_cache.pop('')
self.current_post_id = post_id
if post_id not in self.post_cache:
self.post_cache[post_id] = data
@conf_index.setter
def conf_index(self, index):
try:
index = int(index)
except ValueError:
raise VimPressException("Invalid Index: %s" % index)
#auto increase
if index < 0:
self.__conf_index += 1
if self.__conf_index >= len(self.config):
self.__conf_index = 0
# user enter index
else:
assert index < len(self.config), "Invalid Index: %d" % index
self.__conf_index = index
self.__xmlrpc = None
@property
def xmlrpc(self):
if self.__xmlrpc is None:
conf_index = self.conf_index
config = self.config[conf_index]
if "xmlrpc_obj" not in config:
try:
blog_username = config['username']
blog_password = config.get('password', '')
blog_url = config['blog_url']
store_markdown = config.get('store_markdown', 'y')
except KeyError, e:
raise VimPressException("Configuration error: %s" % e)
echomsg("Connecting to '%s' ... " % blog_url)
if blog_password == '':
blog_password = vim_input(
"Enter password for %s" % blog_url, True)
config["xmlrpc_obj"] = wp_xmlrpc(blog_url,
blog_username, blog_password, store_markdown)
self.__xmlrpc = config["xmlrpc_obj"]
# Setting tags and categories for completefunc
categories = [i["categoryName"].encode("utf-8")
for i in self.xmlrpc.get_categories()]
vim.command('let s:completable = "%s"' % '|'.join(categories))
echomsg("done.")
return self.__xmlrpc
@property
def config(self):
if self.__config is None or len(self.__config) == 0:
confpsr = SafeConfigParser({'store_markdown': 'y'})
confile = os.path.expanduser("~/.vimpressrc")
conf_options = ("blog_url", "username", "password", "store_markdown")
if os.path.exists(confile):
conf_list = []
confpsr.read(confile)
for sec in confpsr.sections():
values = [confpsr.get(sec, i) for i in conf_options]
conf_list.append(dict(zip(conf_options, values)))
if len(conf_list) > 0:
self.__config = conf_list
else:
raise VimPressException(
"You have an empty `~/.vimpressrc', "
"thus no configuration will be read. "
"If you still have your account info in "
"`.vimrc', remove `~/.vimpressrc' and "
"try again.")
else:
try:
self.__config = vim.eval("VIMPRESS")
except vim.error:
pass
else:
# write config to `~/.vimpressrc`,
# coding account in .vimrc is obsolesced.
if not os.path.exists(confile) and \
self.__config is not None:
with open(confile, 'w') as f:
for i, c in enumerate(self.__config):
sec = "Blog%d" % i
confpsr.add_section(sec)
for i in conf_options:
confpsr.set(sec, i, c.get(i, ''))
confpsr.write(f)
echomsg("Your Blog accounts are now copied to "
"`~/.vimpressrc', defining account info "
"in `.vimrc` is now obsolesced, and may "
"lead to secret leak if you share your "
"vim configuration with public. Please "
"REMOVE the `let VIMPRESS =' code in your "
"`.vimrc'. ")
if self.__config is None or len(self.__config) == 0:
raise VimPressException("Could not find vimpress "
"configuration. Please read ':help vimpress' "
"for more information.")
return self.__config
class wp_xmlrpc(object):
def __init__(self, blog_url, username, password, store_markdown):
self.blog_url = blog_url
self.username = username
self.password = password
self.store_markdown = store_markdown
p = xmlrpclib.ServerProxy(os.path.join(blog_url, "xmlrpc.php"))
self.mw_api = p.metaWeblog
self.wp_api = p.wp
self.mt_api = p.mt
self.demo_api = p.demo
assert self.demo_api.sayHello() == "Hello!", \
"XMLRPC Error with communication with '%s'@'%s'" % \
(username, blog_url)
self.cache_reset()
self.post_cache = dict()
self.current_post_id = ''
def cache_reset(self):
self.__cache_post_titles = []
self.__post_title_max = False
def cache_remove_post(self, postid):
for p in self.__cache_post_titles:
if p["postid"] == str(postid):
self.__cache_post_titles.remove(p)
break
is_reached_title_max = property(lambda self: self.__post_title_max)
new_post = lambda self, post_struct: self.mw_api.newPost('',
self.username, self.password, post_struct)
get_post = lambda self, post_id: self.mw_api.getPost(post_id,
self.username, self.password)
edit_post = lambda self, post_id, post_struct: \
self.mw_api.editPost(post_id, self.username,
self.password, post_struct)
delete_post = lambda self, post_id: self.mw_api.deletePost('',
post_id, self.username, self.password, '')
def get_recent_post_titles(self, retrive_count=0):
if retrive_count > len(self.__cache_post_titles) and \
not self.is_reached_title_max:
self.__cache_post_titles = self.mt_api.getRecentPostTitles('',
self.username, self.password, retrive_count)
if len(self.__cache_post_titles) < retrive_count:
self.__post_title_max = True
return self.__cache_post_titles
get_categories = lambda self: self.mw_api.getCategories('',
self.username, self.password)
new_category = lambda self, category: self.wp_api.newCategory('',
self.username, self.password, category)
new_media_object = lambda self, object_struct: \
self.mw_api.newMediaObject('', self.username,
self.password, object_struct)
get_page = lambda self, page_id: self.wp_api.getPage('',
page_id, self.username, self.password)
delete_page = lambda self, page_id: self.wp_api.deletePage('',
self.username, self.password, page_id)
get_page_list = lambda self: self.wp_api.getPageList('',
self.username, self.password)
class ContentStruct(object):
buffer_meta = None
post_struct_meta = None
EDIT_TYPE = ''
@property
def META_TEMPLATE(self):
KEYS_BASIC = ("StrID", "Title", "Slug")
KEYS_EXT = ("Cats", "Tags")
KEYS_BLOG = ("EditType", "EditFormat", "BlogAddr")
pt = ['"{k:<6}: {{{t}}}'.format(k=p, t=p.lower()) for p in KEYS_BASIC]
if self.EDIT_TYPE == "post":
pt.extend(['"{k:<6}: {{{t}}}'.format(k=p, t=p.lower())
for p in KEYS_EXT])
pm = "\n".join(pt)
bm = "\n".join(['"{k:<11}: {{{t}}}'.format(k=p, t=p.lower())
for p in KEYS_BLOG])
return u'"{bg}\n{0}\n"{mid}\n{1}\n"{ed}\n'.format(pm, bm, **G.MARKER)
POST_BEGIN = property(lambda self: len(self.META_TEMPLATE.splitlines()))
raw_text = ''
html_text = ''
def __init__(self, edit_type=None, post_id=None):
self.EDIT_TYPE = edit_type
self.buffer_meta = dict(strid='', edittype=edit_type,
blogaddr=g_data.blog_url)
self.post_struct_meta = dict(title='',
wp_slug='',
post_type=edit_type,
description='',
custom_fields=[],
post_status='draft')
if post_id is not None:
self.refresh_from_wp(post_id)
if self.EDIT_TYPE is None:
self.parse_buffer()
def parse_buffer(self):
start = 0
while not vim.current.buffer[start][1:].startswith(G.MARKER['bg']):
start += 1
end = start + 1
while not vim.current.buffer[end][1:].startswith(G.MARKER['ed']):
if not vim.current.buffer[end].startswith('"===='):
line = vim.current.buffer[end][1:].strip().split(":")
k, v = line[0].strip().lower(), ':'.join(line[1:])
self.buffer_meta[k.strip().lower()] = v.strip().decode('utf-8')
end += 1
if self.EDIT_TYPE != self.buffer_meta["edittype"]:
self.EDIT_TYPE = self.buffer_meta["edittype"]
self.buffer_meta["content"] = '\n'.join(
vim.current.buffer[end + 1:]).decode('utf-8')
def fill_buffer(self):
meta = dict(strid="", title="", slug="",
cats="", tags="", editformat="Markdown", edittype="")
meta.update(self.buffer_meta)
meta_text = self.META_TEMPLATE.format(**meta)\
.encode('utf-8').splitlines()
vim.current.buffer[0] = meta_text[0]
vim.current.buffer.append(meta_text[1:])
content = self.buffer_meta.get("content", ' ')\
.encode('utf-8').splitlines()
vim.current.buffer.append(content)
def update_buffer_meta(self):
"""
Updates the meta data region of a blog editing buffer.
@params **kwargs - keyworded arguments
"""
kw = self.buffer_meta
start = 0
while not vim.current.buffer[start][1:].startswith(G.MARKER['bg']):
start += 1
end = start + 1
while not vim.current.buffer[end][1:].startswith(G.MARKER['ed']):
if not vim.current.buffer[end].startswith('"===='):
line = vim.current.buffer[end][1:].strip().split(":")
k, v = line[0].strip().lower(), ':'.join(line[1:])
if k in kw:
new_line = u"\"%s: %s" % (line[0], kw[k])
vim.current.buffer[end] = new_line.encode('utf-8')
end += 1
def refresh_from_buffer(self):
self.parse_buffer()
meta = self.buffer_meta
struct = self.post_struct_meta
struct.update(title=meta["title"],
wp_slug=meta["slug"], post_type=self.EDIT_TYPE)
if self.EDIT_TYPE == "post":
struct.update(categories=meta["cats"].split(','),
mt_keywords=meta["tags"].split(','))
self.rawtext = rawtext = meta["content"]
#Translate markdown and save in custom fields.
if meta["editformat"].lower() == "markdown":
if g_data.store_markdown == 'y':
# Store markdown in custom field only when enabled
for f in struct["custom_fields"]:
if f["key"] == G.CUSTOM_FIELD_KEY:
f["value"] = rawtext
break
## Not found, add new custom field.
else:
field = dict(key=G.CUSTOM_FIELD_KEY, value=rawtext)
struct["custom_fields"].append(field)
struct["description"] = self.html_text = markdown.markdown(rawtext)
else:
struct["description"] = self.html_text = rawtext
def refresh_from_wp(self, post_id):
# get from wp
self.post_struct_meta = struct = getattr(g_data.xmlrpc,
"get_" + self.EDIT_TYPE)(post_id)
# struct buffer meta
meta = dict(editformat="HTML",
title=struct["title"],
slug=struct["wp_slug"])
if self.EDIT_TYPE == "post":
meta.update(strid=str(struct["postid"]),
cats=", ".join(struct["categories"]),
tags=struct["mt_keywords"])
MORE_KEY = "mt_text_more"
else:
meta.update(strid=str(struct["page_id"]))
MORE_KEY = "text_more"
self.html_text = content = struct["description"]
#detect more text
post_more = struct.get(MORE_KEY, '')
if len(post_more) > 0:
content += u'<!--more-->' + post_more
struct[MORE_KEY] = ''
self.html_text = struct["description"] = content
#Use Markdown text if exists in custom fields
for field in struct["custom_fields"]:
if field["key"] == G.CUSTOM_FIELD_KEY:
meta['editformat'] = "Markdown"
self.raw_text = content = field["value"]
vim.command('setl syntax=md_blogsyntax')
break
else:
self.raw_text = content
vim.command('setl syntax=html_blogsyntax')
meta["content"] = content
self.buffer_meta.update(meta)
def save_post(self):
ps = self.post_struct_meta
if self.EDIT_TYPE == "post":
if ps.get("postid", '') == '' and self.post_id == '':
post_id = g_data.xmlrpc.new_post(ps)
else:
post_id = ps["postid"] if "postid" in ps \
else int(self.post_id)
g_data.xmlrpc.edit_post(post_id, ps)
else:
if ps.get("page_id", '') == '' and self.post_id == '':
post_id = g_data.xmlrpc.new_post(ps)
else:
post_id = ps["page_id"] if "page_id" in ps \
else int(self.post_id)
g_data.xmlrpc.edit_post(post_id, ps)
self.refresh_from_wp(post_id)
post_status = property(lambda self:
self.post_struct_meta[self.EDIT_TYPE + "_status"])
@post_status.setter
def post_status(self, data):
if data is not None:
self.post_struct_meta[self.EDIT_TYPE + "_status"] = data
post_id = property(lambda self: self.buffer_meta["strid"])
def html_preview(self):
"""
Opens a browser with a local preview of the content.
@params text_html - the html content
meta - a dictionary of the meta data
"""
if g_data.vimpress_temp_dir == '':
g_data.vimpress_temp_dir = tempfile.mkdtemp(suffix="vimpress")
html = \
u"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Vimpress Local Preview: %(title)s</title> <style type="text/css"> ul, li { margin: 1em; } :link,:visited { text-decoration:none } h1,h2,h3,h4,h5,h6,pre,code { font-size:1.1em; } h1 {font-size: 1.8em;} h2 {font-size: 1.5em;} h3{font-size: 1.3em;} h4{font-size: 1.2em;} h5 {font-size: 1.1em;} a img,:link img,:visited img { border:none } body { margin:0 auto; width:770px; font-family: Helvetica, Arial, Sans-serif; font-size:12px; color:#444; } </style> </meta> </head> <body> %(content)s </body> </html>
""" % dict(content=self.html_text, title=self.buffer_meta["title"])
with open(os.path.join(
g_data.vimpress_temp_dir, "vimpress_temp.html"), 'w') as f:
f.write(html.encode('utf-8'))
webbrowser.open("file://%s" % f.name)
def remote_preview(self, pub="draft"):
self.post_status = pub
self.save_post()
webbrowser.open("%s?p=%s&preview=true" %
(g_data.blog_url, self.post_id))
#################################################
# Golbal Variables
#################################################
G = DataObject
g_data = DataObject()
#################################################
# Helper Functions
#################################################
def vim_encoding_check(func):
"""
Decorator.
Check vim environment. wordpress via xmlrpc only support unicode data,
setting vim to utf-8 for all data compatible.
"""
def __check(*args, **kw):
orig_enc = vim.eval("&encoding")
if orig_enc is None:
echomsg("Failed to detech current vim encoding. Make sure your"
" content encoded in UTF-8 to have vimpress work "
"correctly.")
elif orig_enc != "utf-8":
modified = vim.eval("&modified")
buf_list = '\n'.join(vim.current.buffer).decode(orig_enc).encode('utf-8').splitlines()
del vim.current.buffer[:]
vim.command("setl encoding=utf-8")
vim.current.buffer[0] = buf_list[0]
if len(buf_list) > 1:
vim.current.buffer.append(buf_list[1:])
if modified == '0':
vim.command('setl nomodified')
return func(*args, **kw)
return __check
def view_switch(view = "", assert_view = "", reset = False):
"""
Decorator.
For commands to switch between edit/list view, data/status need to be configured.
"""
def switch(func):
def __run(*args, **kw):
if assert_view != '':
if g_data.view != assert_view:
raise VimPressException("Command only available at '%s' view." % assert_view)
if func.func_name == "blog_new":
if g_data.view == "list":
kw["currentContent"] = ['']
else:
kw["currentContent"] = vim.current.buffer[:]
elif func.func_name == "blog_config_switch":
if g_data.view == "list":
kw["refresh_list"] = True
if reset:
g_data.xmlrpc.cache_reset()
if view != '':
#Switching view
if g_data.view != view:
#from list view
if g_data.view == "list":
for v in g_data.LIST_VIEW_KEY_MAP.values():
if vim.eval("mapcheck('%s')" % v):
vim.command('unmap <buffer> %s' % v)
g_data.view = view
return func(*args, **kw)
return __run
return switch
def blog_wise_open_view():
"""
Wisely decides whether to wipe out the content of current buffer or open a new splited window.
"""
if vim.current.buffer.name is None and \
(vim.eval('&modified') == '0' or
len(vim.current.buffer) == 1):
vim.command('setl modifiable')
del vim.current.buffer[:]
vim.command('setl nomodified')
else:
vim.command(":new")
vim.command('setl syntax=md_blogsyntax')
vim.command('setl completefunc=vimrepress#CateComplete')
@vim_encoding_check
def vim_input(message = 'input', secret = False):
vim.command('call inputsave()')
vim.command("let s:user_input = %s('%s :')" % (("inputsecret" if secret else "input"), message))
vim.command('call inputrestore()')
return vim.eval('s:user_input')
#################################################
# Command Functions
#################################################
@exception_check
@vim_encoding_check
@view_switch(assert_view = "edit", reset = True)
def blog_save(pub = None):
"""
Saves the current editing buffer.
@params pub - either "draft" or "publish"
"""
if pub not in ("publish", "draft", None):
raise VimPressException(":BlogSave draft|publish")
cp = g_data.current_post
assert cp is not None, "Can't get current post obj."
cp.refresh_from_buffer()
if cp.buffer_meta["blogaddr"] != g_data.blog_url and cp.post_id != '':
confirm = vim_input("Are u sure saving it to \"%s\" ? BlogAddr in current buffer does NOT matched. \nStill saving it ? (may cause data lost) [yes/NO]" % g_data.blog_url)
assert confirm.lower() == 'yes', "Aborted."
cp.post_status = pub
cp.save_post()
cp.update_buffer_meta()
g_data.current_post = cp
notify = "%s ID=%s saved with status '%s'" % (cp.post_status, cp.post_id, cp.post_status)
echomsg(notify)
vim.command('setl nomodified')
@exception_check
@vim_encoding_check
@view_switch(view = "edit")
def blog_new(edit_type = "post", currentContent = None):
"""
Creates a new editing buffer of specified type.
@params edit_type - either "post" or "page"
"""
if edit_type.lower() not in ("post", "page", "category"):
raise VimPressException("Invalid option: %s " % edit_type)
if edit_type.lower() == "category":
category_name = vim_input("New Category name")
category_slug = vim_input("Category slug (optional)")
ret = g_data.xmlrpc.new_category(dict(name = category_name, slug = category_slug))
if type(ret) is int:
echomsg("Category '%s' created with ID %d. Updating local cache ... "
% (category_name, ret))
categories = [i["categoryName"].encode("utf-8")
for i in g_data.xmlrpc.get_categories()]
vim.command('let s:completable = "%s"' % '|'.join(categories))
echomsg("Done.")
else:
echoerr("Category create ERROR: " + str(ret))
else:
blog_wise_open_view()
g_data.current_post = ContentStruct(edit_type = edit_type)
cp = g_data.current_post
cp.fill_buffer()
@view_switch(view = "edit")
def blog_edit(edit_type, post_id):
"""
Opens a new editing buffer with blog content of specified type and id.
@params edit_type - either "post" or "page"
post_id - the id of the post or page
"""
blog_wise_open_view()
if edit_type.lower() not in ("post", "page"):
raise VimPressException("Invalid option: %s " % edit_type)
post_id = str(post_id)
if post_id in g_data.post_cache:
cp = g_data.current_post = g_data.post_cache[post_id]
else:
cp = g_data.current_post = ContentStruct(edit_type = edit_type, post_id = post_id)
cp.fill_buffer()
vim.current.window.cursor = (cp.POST_BEGIN, 0)
vim.command('setl nomodified')
vim.command('setl textwidth=0')
for v in G.LIST_VIEW_KEY_MAP.values():
if vim.eval("mapcheck('%s')" % v):
vim.command('unmap <buffer> %s' % v)
@view_switch(assert_view = "list")
def blog_delete(edit_type, post_id):
"""
Deletes a page or post of specified id.
@params edit_type - either "page" or "post"
post_id - the id of the post or page
"""
if edit_type.lower() not in ("post", "page"):
raise VimPressException("Invalid option: %s " % edit_type)
deleted = getattr(g_data.xmlrpc, "delete_" + edit_type)(post_id)
assert deleted is True, "There was a problem deleting the %s." % edit_type
echomsg("Deleted %s id %s. " % (edit_type, str(post_id)))
g_data.xmlrpc.cache_remove_post(post_id)
blog_list(edit_type)
@exception_check
@view_switch(assert_view = "list")
def blog_list_on_key_press(action, edit_type):
"""
Calls blog open on the current line of a listing buffer.
"""
if action.lower() not in ("open", "delete"):
raise VimPressException("Invalid option: %s" % action)
row = vim.current.window.cursor[0]
line = vim.current.buffer[row - 1]
id = line.split()[0]
title = line[len(id):].strip()
try:
int(id)
except ValueError:
if line.find("More") != -1:
assert g_data.xmlrpc.is_reached_title_max is False, "No more posts."
vim.command("setl modifiable")
del vim.current.buffer[len(vim.current.buffer) - 1:]
append_blog_list(edit_type)
vim.current.buffer.append(G.MARKER['more'])
vim.command("setl nomodified")
vim.command("setl nomodifiable")
return
else:
raise VimPressException("Move cursor to a post/page line and press Enter.")
if len(title) > 30:
title = title[:30] + ' ...'
if action.lower() == "delete":
confirm = vim_input("Confirm Delete [%s]: %s? [yes/NO]" % (id, title))
assert confirm.lower() == 'yes', "Delete Aborted."
vim.command("setl modifiable")
del vim.current.buffer[:]
vim.command("setl nomodified")
if action == "open":
blog_edit(edit_type, int(id))
elif action == "delete":
blog_delete(edit_type, int(id))
def append_blog_list(edit_type, count = G.DEFAULT_LIST_COUNT):
if edit_type.lower() == "post":
current_posts = len(vim.current.buffer) - 1
retrive_count = int(count) + current_posts
posts_titles = g_data.xmlrpc.get_recent_post_titles(retrive_count)
vim.current.buffer.append(
[(u"%(postid)s\t%(title)s" % p).encode('utf-8')
for p in posts_titles[current_posts:]])
else:
pages = g_data.xmlrpc.get_page_list()
vim.current.buffer.append(
[(u"%(page_id)s\t%(page_title)s" % p).encode('utf-8') for p in pages])
@exception_check
@vim_encoding_check
@view_switch(view = "list")
def blog_list(edit_type = "post", keep_type = False):
"""
Creates a listing buffer of specified type.
@params edit_type - either "post(s)" or "page(s)"
"""
if keep_type:
first_line = vim.current.buffer[0]
assert first_line.find("List") != -1, "Failed to detect current list type."
edit_type = first_line.split()[1].lower()
blog_wise_open_view()
vim.current.buffer[0] = G.MARKER["list_title"] % \
dict(edit_type = edit_type.capitalize(), blog_url = G.blog_url)
if edit_type.lower() not in ("post", "page"):
raise VimPressException("Invalid option: %s " % edit_type)
append_blog_list(edit_type, G.DEFAULT_LIST_COUNT)
if edit_type == "post":
vim.current.buffer.append(G.MARKER['more'])
vim.command("setl nomodified")
vim.command("setl nomodifiable")
vim.current.window.cursor = (2, 0)
vim.command("map <silent> <buffer> %(enter)s :py blog_list_on_key_press('open', '%%s')<cr>"
% G.LIST_VIEW_KEY_MAP % edit_type)
vim.command("map <silent> <buffer> %(delete)s :py blog_list_on_key_press('delete', '%%s')<cr>"
% G.LIST_VIEW_KEY_MAP % edit_type)
echomsg("Press <Enter> to edit. <Delete> to move to trash.")
@exception_check
@vim_encoding_check
@view_switch(assert_view = "edit")
def blog_upload_media(file_path):
"""
Uploads a file to the blog.
@params file_path - the file's path
"""
if not os.path.exists(file_path):
raise VimPressException("File does not exist: %s" % file_path)
name = os.path.basename(file_path)
filetype = mimetypes.guess_type(file_path)[0]
with open(file_path) as f:
bits = xmlrpclib.Binary(f.read())
result = g_data.xmlrpc.new_media_object(dict(name = name, type = filetype, bits = bits))
ran = vim.current.range
if filetype.startswith("image"):
img = G.IMAGE_TEMPLATE % result
ran.append(img)
else:
ran.append(result["url"])
ran.append('')
@exception_check
@vim_encoding_check
@view_switch(assert_view = "edit")
def blog_append_code(code_type = ""):
html = \
"""<pre lang="%s"%s>
</pre>"""
if code_type == "":
code_type = ("text", "")
else:
code_type = (code_type, ' line="1"')
html = html % code_type
row, col = vim.current.window.cursor
code_block = html.splitlines()
vim.current.range.append(code_block)
vim.current.window.cursor = (row + len(code_block), 0)
@exception_check
@vim_encoding_check
@view_switch(assert_view = "edit")
def blog_preview(pub = "local"):
"""
Opens a browser window displaying the content.
@params pub - If "local", the content is shown in a browser locally.
If "draft", the content is saved as a draft and previewed remotely.
If "publish", the content is published and displayed remotely.
"""
cp = g_data.current_post
cp.refresh_from_buffer()
if pub == "local":
cp.html_preview()
elif pub in ("publish", "draft"):
cp.remote_preview(pub)
if pub == "draft":
echomsg("You have to login in the browser to preview the post when save as draft.")
else:
raise VimPressException("Invalid option: %s " % pub)
@exception_check
def blog_guess_open(what):
"""
Tries several methods to get the post id from different user inputs, such as args, url, postid etc.
"""
post_id = ''
blog_index = -1
if type(what) is str:
if what.startswith(g_data.blog_url):
blog_index = 1
# User input a url contained in the profiles
if blog_index != -1:
guess_id = re.search(r"\S+?p=(\d+)$", what)
# permalinks
if guess_id is None:
# try again for /archives/%post_id%
guess_id = re.search(r"\S+/archives/(\d+)", what)
# fail, try get full link from headers
if guess_id is None:
headers = urllib.urlopen(what).headers.headers
for link in headers:
if link.startswith("Link:"):
post_id = re.search(r"<\S+?p=(\d+)>", link).group(1)
else:
post_id = guess_id.group(1)
# full link with ID (http://blog.url/?p=ID)
else:
post_id = guess_id.group(1)
# detected something ?
assert post_id != '', "Failed to get post/page id from '%s'." % what
#switch view if needed.
if blog_index != -1 and blog_index != g_data.conf_index:
blog_config_switch(blog_index)
# User input not a url, try numeric
else:
try:
post_id = str(int(what))
except ValueError:
raise VimPressException("Failed to get post/page id from '%s'." % what)
blog_edit("post", post_id)
@exception_check
@vim_encoding_check
@view_switch()
def blog_config_switch(index = -1, refresh_list = False):
"""
Switches the blog to the 'index' of the configuration array.
"""
g_data.conf_index = index
if refresh_list:
blog_list(keep_type = True)
echomsg("Vimpress switched to '%s'@'%s'" % (g_data.blog_username, g_data.blog_url))