Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import sys
- import ssl
- import subprocess
- from xmlrpclib import ServerProxy
- from markdown import markdown
- applescript_prompt_for_title = """
- tell application "BBEdit"
- set theDocument to the active document of the front text window
- end tell
- set theDialogTitle to "Post " & "\\"" & the name of theDocument & "\\""
- set theTitle to the text of (display dialog "Enter the post title" with title theDialogTitle buttons {"Cancel", "Post"} default button "Post" default answer "")
- """
- applescript_get_text = """
- tell application "BBEdit"
- set theDocument to the active document of the front text window
- set theContent to the text of theDocument
- end tell
- """
- applescript_display_dialog = 'display dialog "%s" with title "%s" buttons "Ok default button "Ok""'
- class Server():
- def __init__(self):
- self.blog_id = 1 # arbitrary, unused
- # edit url, username, password before use
- self.url = ''
- self.username = ''
- self.password = ''
- self.proxy = ServerProxy(self.url , context=ssl.SSLContext(ssl.PROTOCOL_TLSv1_2))
- self.filter = {'orderby': 'post_id'}
- self.fields = ['post_title', 'post_id', 'guid', 'link']
- self.content_template = {'post_type'}
- def getPosts(self, filter=None, fields=None):
- wp_filter = filter or self.filter
- wp_fields = fields or self.fields
- return self.proxy.wp.getPosts(self.blog_id, self.username, self.password, wp_filter, wp_fields)
- def newPost(self, post):
- return self.proxy.wp.newPost(self.blog_id, self.username, self.password, post.content())
- class Post():
- def __init__(self, title, body):
- self.title = title
- self.body = body
- def content(self):
- return {'post_title': self.title,
- 'post_content': self.body}
- def render(text):
- extensions = ['markdown.extensions.extra',
- 'markdown.extensions.codehilite',
- 'markdown.extensions.smarty']
- extension_config = {'markdown.extensions.codehilite':
- {'linenums': False, 'guess_lang': False}}
- return markdown(text, extensions=extensions, extension_configs=extension_config,
- outputformat='html5')
- def run_applescript(script):
- process = subprocess.Popen(['osascript', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- try:
- outdata, errdata = process.communicate(script)
- except:
- pass
- if outdata == "":
- return None
- return outdata
- def prompt_for_title():
- result = run_applescript(applescript_prompt_for_title)
- title = ','.join(result.split(',')[1:]).strip()
- return title
- def get_document_text():
- return run_applescript(applescript_get_text)
- def display_dialog(message, title="Post To Blog"):
- script = applescript_display_dialog % (message, title)
- _ = run_applescript('display dialog "%s" with title "%s" buttons "Ok" default button "Ok"' % (message, title))
- def main(args=None):
- try:
- title = prompt_for_title()
- text = get_document_text().decode('utf8')
- html = render(text)
- post = Post(title, html)
- Server().newPost(post)
- display_dialog("Success")
- except Exception as e:
- display_dialog("Failed with error %s" % e)
- return 1
- return 0
- if __name__ == '__main__':
- sys.exit(main())
Add Comment
Please, Sign In to add comment