Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/Users/yuki/.rbenv/versions/1.9.3-p194/bin/ruby
- # coding: utf-8
- # %%%{CotEditorXInput=AllText}%%%
- require 'xmlrpc/client'
- require 'redcarpet'
- require 'base64'
- class MyHTMLRender < Redcarpet::Render::HTML
- def block_code(code, language)
- lang = " lang-#{language}" if language
- "<pre class='prettyprint#{lang}'><code>#{code}</code></pre>"
- end
- def image(link, title, alt_text)
- open(link, "rb") do |file|
- if title
- title = title + File.extname(link)
- else
- title = File.basename(link) unless title
- end
- extension = File.extname(link)
- type = ""
- if /jpg|jpeg/ =~ extension
- type = "image/jpeg"
- elsif /gif/ =~ extension
- type = "image/gif"
- elsif /png/ =~ extension
- type = "image/png"
- end
- struct = {
- :name => title,
- :type => type,
- :bits => XMLRPC::Base64.new(file.read)
- }
- img = $server.call("wp.uploadFile",1,$user,$pass,struct)
- '<img src="' +img['url']+ '" />'
- end
- end
- end
- def read_header
- while line = STDIN.gets.force_encoding('utf-8')
- if /#\s*(.+?)$/ =~ line
- @title = $1
- elsif /(http.+?)$/ =~ line
- custom_field = {'key' => "linked_list_url", 'value' => $1}
- @fields << custom_field
- @categories << "Links"
- elsif /^-+$/ =~ line.chomp()
- break
- else
- @tags = @tags + line.split(/\s*,\s*/)
- end
- end
- end
- $user = USERNAME
- $pass = PASSWORD
- $server = XMLRPC::Client.new(DOMAIN, '/xmlrpc.php')
- @title = ""
- @fields = []
- @tags = []
- @categories = []
- read_header
- options = {:fenced_code_blocks => true,
- :space_after_headers => true,
- :no_intra_emphasis => true,
- :autolink => true}
- markdown = Redcarpet::Markdown.new(MyHTMLRender, options)
- content = markdown.render(STDIN.read.force_encoding('utf-8'))
- struct = {
- 'title' => @title,
- 'categories' => @categories,
- 'mt_keywords' => @tags,
- 'description' => content,
- 'custom_fields' => @fields
- }
- id = $server.call("metaWeblog.newPost",1,$user,$pass,struct,1)
Add Comment
Please, Sign In to add comment